package com.yuchen.saas.park.service.service.property.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyBillFeeReductionDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillFeeReduction;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillFeeReductionVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyBillFeeReductionMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyBillFeeReductionService;
import com.yuchen.saas.parking.api.vo.black.VehicleBlacklistVO;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 物业-收费-前台收费-收银台-费用减免 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-09-26
 */
@Slf4j
@Service
public class SaasParkPropertyBillFeeReductionServiceImpl extends BaseServiceImpl<SaasParkPropertyBillFeeReductionMapper, SaasParkPropertyBillFeeReduction> implements ISaasParkPropertyBillFeeReductionService {




    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyBillFeeReductionDto saasParkPropertyBillFeeReductionDto) {
        SaasParkPropertyBillFeeReduction saasParkPropertyBillFeeReduction = new SaasParkPropertyBillFeeReduction();
        BeanUtil.copyProperties(saasParkPropertyBillFeeReductionDto, saasParkPropertyBillFeeReduction);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyBillFeeReduction);
        return result;
    }



    @Override
    public void export(SaasParkPropertyBillFeeReductionDto saasParkPropertyBillFeeReductionDto, HttpServletResponse response) {

        LambdaQueryWrapper<SaasParkPropertyBillFeeReduction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasParkPropertyBillFeeReduction::getParkId, saasParkPropertyBillFeeReductionDto.getParkId())
                //账单起止日期
                .ge(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getStartDate()), SaasParkPropertyBillFeeReduction::getStartDate, saasParkPropertyBillFeeReductionDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getEndDate()), SaasParkPropertyBillFeeReduction::getStartDate, saasParkPropertyBillFeeReductionDto.getEndDate())
                //减免方式id
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getEductionTypeId()), SaasParkPropertyBillFeeReduction::getEductionTypeId, saasParkPropertyBillFeeReductionDto.getEductionTypeId())
                //减免方式
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getEductionType()), SaasParkPropertyBillFeeReduction::getEductionType, saasParkPropertyBillFeeReductionDto.getEductionType())
                .orderByDesc(SaasParkPropertyBillFeeReduction::getCreateTime);
        List<SaasParkPropertyBillFeeReduction> list = baseMapper.selectList(queryWrapper);
        List<SaasParkPropertyBillFeeReductionVo> saasParkPropertyBillFeeReductionVos =new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            saasParkPropertyBillFeeReductionVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", saasParkPropertyBillFeeReductionVos.size());
        try {
            String fileName = URLEncoder.encode("收费历史表信息", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), VehicleBlacklistVO.class)
                    .sheet("收费历史表信息")
                    .doWrite(saasParkPropertyBillFeeReductionVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("收费历史表导出异常");
            e.printStackTrace();
        }
    }



    @Override
    public R<IPage<SaasParkPropertyBillFeeReductionVo>> getList(SaasParkPropertyBillFeeReductionDto saasParkPropertyBillFeeReductionDto, Query query) {


        Page<SaasParkPropertyBillFeeReduction> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyBillFeeReduction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getParkId()), SaasParkPropertyBillFeeReduction::getParkId, saasParkPropertyBillFeeReductionDto.getParkId())
                //账单起止日期
                .ge(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getStartDate()), SaasParkPropertyBillFeeReduction::getStartDate, saasParkPropertyBillFeeReductionDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getStartDate()), SaasParkPropertyBillFeeReduction::getStartDate, saasParkPropertyBillFeeReductionDto.getStartDate())
                //减免方式id
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getEductionTypeId()), SaasParkPropertyBillFeeReduction::getEductionTypeId, saasParkPropertyBillFeeReductionDto.getEductionTypeId())
                //减免方式
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeReductionDto.getEductionType()), SaasParkPropertyBillFeeReduction::getEductionType, saasParkPropertyBillFeeReductionDto.getEductionType())
                .orderByDesc(SaasParkPropertyBillFeeReduction::getCreateTime);
        IPage<SaasParkPropertyBillFeeReduction> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyBillFeeReduction> saasParkPropertyBillFeeReductionList = pages.getRecords();

        if (saasParkPropertyBillFeeReductionList.isEmpty()) {
            return R.data(Condition.getPage(query));
        }
        //组装返回response
        IPage<SaasParkPropertyBillFeeReductionVo> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
        List<SaasParkPropertyBillFeeReductionVo> records = this.listVO(pages.getRecords());

        pageVo.setRecords(records);
        return R.data(pageVo);
    }

    private List<SaasParkPropertyBillFeeReductionVo> listVO(List<SaasParkPropertyBillFeeReduction> listRecords) {
        return (List) listRecords.stream().map(entity -> {
            SaasParkPropertyBillFeeReductionVo saasParkPropertyBillFeeReductionVo = detail(entity.getId(), entity).getData();
            return saasParkPropertyBillFeeReductionVo;
        }).collect(Collectors.toList());
    }

    public R<SaasParkPropertyBillFeeReductionVo> detail(Long id, SaasParkPropertyBillFeeReduction saasParkPropertyBillFeeReduction) {
        SaasParkPropertyBillFeeReductionVo saasParkPropertyBillFeeReductionVo = new SaasParkPropertyBillFeeReductionVo();
        BeanUtils.copyProperties(saasParkPropertyBillFeeReduction, saasParkPropertyBillFeeReductionVo);
        return R.data(saasParkPropertyBillFeeReductionVo);
    }















}
