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.SaasParkPropertyBillArrearsDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillArrears;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillArrearsVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyBillArrearsMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyBillArrearsService;
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 SaasParkPropertyBillArrearsServiceImpl extends BaseServiceImpl<SaasParkPropertyBillArrearsMapper, SaasParkPropertyBillArrears> implements ISaasParkPropertyBillArrearsService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyBillArrearsDto saasParkPropertyBillArrearsDto) {
        SaasParkPropertyBillArrears saasParkPropertyBillArrears = new SaasParkPropertyBillArrears();
        BeanUtil.copyProperties(saasParkPropertyBillArrearsDto, saasParkPropertyBillArrears);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyBillArrears);
        return result;
    }

    /**
     * TODO 催缴
     * @param id
     * @return
     */
    @Override
    public boolean call(Long id) {
        return false;
    }

    @Override
    public void export(SaasParkPropertyBillArrearsDto reqDTO, HttpServletResponse response) {

        LambdaQueryWrapper<SaasParkPropertyBillArrears> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasParkPropertyBillArrears::getParkId, reqDTO.getParkId())
                .ge(Func.isNotEmpty(reqDTO.getReceivableDate()), SaasParkPropertyBillArrears::getReceivableDate, reqDTO.getReceivableDate())
                .le(Func.isNotEmpty(reqDTO.getReceivableDate()), SaasParkPropertyBillArrears::getReceivableDate, reqDTO.getReceivableDate())
                .ge(Func.isNotEmpty(reqDTO.getStartDate()), SaasParkPropertyBillArrears::getStartDate, reqDTO.getStartDate())
                .le(Func.isNotEmpty(reqDTO.getEndDate()), SaasParkPropertyBillArrears::getStartDate, reqDTO.getEndDate());
        List<SaasParkPropertyBillArrears> list = baseMapper.selectList(queryWrapper);
        List<SaasParkPropertyBillArrearsVo> saasParkPropertyBillArrearsVos =new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            saasParkPropertyBillArrearsVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", saasParkPropertyBillArrearsVos.size());
        try {
            String fileName = URLEncoder.encode("欠费表信息", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), SaasParkPropertyBillArrearsVo.class)
                    .sheet("欠费表信息")
                    .doWrite(saasParkPropertyBillArrearsVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("欠费表导出异常");
            e.printStackTrace();
        }
    }



    @Override
    public R<IPage<SaasParkPropertyBillArrearsVo>> getList(SaasParkPropertyBillArrearsDto saasParkPropertyBillArrearsDto, Query query) {


        Page<SaasParkPropertyBillArrears> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyBillArrears> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyBillArrearsDto.getParkId()), SaasParkPropertyBillArrears::getParkId, saasParkPropertyBillArrearsDto.getParkId())
                .ge(Func.isNotEmpty(saasParkPropertyBillArrearsDto.getReceivableDate()), SaasParkPropertyBillArrears::getReceivableDate, saasParkPropertyBillArrearsDto.getReceivableDate())
                .le(Func.isNotEmpty(saasParkPropertyBillArrearsDto.getReceivableDate()), SaasParkPropertyBillArrears::getReceivableDate, saasParkPropertyBillArrearsDto.getReceivableDate())

                .ge(Func.isNotEmpty(saasParkPropertyBillArrearsDto.getStartDate()), SaasParkPropertyBillArrears::getStartDate, saasParkPropertyBillArrearsDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillArrearsDto.getEndDate()), SaasParkPropertyBillArrears::getStartDate, saasParkPropertyBillArrearsDto.getEndDate())

                .orderByDesc(SaasParkPropertyBillArrears::getCreateTime);
        IPage<SaasParkPropertyBillArrears> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyBillArrears> saasParkPropertyBillArrearsList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyBillArrearsVo> detail(Long id, SaasParkPropertyBillArrears saasParkPropertyBillArrears) {
        SaasParkPropertyBillArrearsVo saasParkPropertyBillArrearsVo = new SaasParkPropertyBillArrearsVo();
        BeanUtils.copyProperties(saasParkPropertyBillArrears, saasParkPropertyBillArrearsVo);
        return R.data(saasParkPropertyBillArrearsVo);
    }


}
