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.SaasParkPropertyBillFeeRefundRecordDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillFeeRefundRecord;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillFeeRefundRecordVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyBillFeeRefundRecordMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyBillFeeRefundRecordService;
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 SaasParkPropertyBillFeeRefundRecordServiceImpl extends BaseServiceImpl<SaasParkPropertyBillFeeRefundRecordMapper, SaasParkPropertyBillFeeRefundRecord> implements ISaasParkPropertyBillFeeRefundRecordService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyBillFeeRefundRecordDto saasParkPropertyBillFeeRefundRecordDto) {
        SaasParkPropertyBillFeeRefundRecord saasParkPropertyBillFeeRefundRecord = new SaasParkPropertyBillFeeRefundRecord();
        BeanUtil.copyProperties(saasParkPropertyBillFeeRefundRecordDto, saasParkPropertyBillFeeRefundRecord);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyBillFeeRefundRecord);
        return result;
    }



    @Override
    public void export(SaasParkPropertyBillFeeRefundRecordDto saasParkPropertyBillFeeRefundRecordDto, HttpServletResponse response) {

        LambdaQueryWrapper<SaasParkPropertyBillFeeRefundRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasParkPropertyBillFeeRefundRecord::getParkId, saasParkPropertyBillFeeRefundRecordDto.getParkId())
                //账单起止日期
                .ge(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getStartDate()), SaasParkPropertyBillFeeRefundRecord::getStartDate, saasParkPropertyBillFeeRefundRecordDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getEndDate()), SaasParkPropertyBillFeeRefundRecord::getStartDate, saasParkPropertyBillFeeRefundRecordDto.getEndDate())
                //账单类型id
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getBillTypeId()), SaasParkPropertyBillFeeRefundRecord::getBillTypeId, saasParkPropertyBillFeeRefundRecordDto.getBillTypeId())
                //账单类型
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getBillType()), SaasParkPropertyBillFeeRefundRecord::getBillType, saasParkPropertyBillFeeRefundRecordDto.getBillType())
                .orderByDesc(SaasParkPropertyBillFeeRefundRecord::getCreateTime);
        List<SaasParkPropertyBillFeeRefundRecord> list = baseMapper.selectList(queryWrapper);
        List<SaasParkPropertyBillFeeRefundRecordVo> saasParkPropertyBillFeeRefundRecordVos =new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            saasParkPropertyBillFeeRefundRecordVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", saasParkPropertyBillFeeRefundRecordVos.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(saasParkPropertyBillFeeRefundRecordVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("收费历史表导出异常");
            e.printStackTrace();
        }
    }



    @Override
    public R<IPage<SaasParkPropertyBillFeeRefundRecordVo>> getList(SaasParkPropertyBillFeeRefundRecordDto saasParkPropertyBillFeeRefundRecordDto, Query query) {


        Page<SaasParkPropertyBillFeeRefundRecord> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyBillFeeRefundRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getParkId()), SaasParkPropertyBillFeeRefundRecord::getParkId, saasParkPropertyBillFeeRefundRecordDto.getParkId())
                //账单起止日期
                .ge(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getStartDate()), SaasParkPropertyBillFeeRefundRecord::getStartDate, saasParkPropertyBillFeeRefundRecordDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getEndDate()), SaasParkPropertyBillFeeRefundRecord::getStartDate, saasParkPropertyBillFeeRefundRecordDto.getEndDate())
                //账单类型id
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getBillTypeId()), SaasParkPropertyBillFeeRefundRecord::getBillTypeId, saasParkPropertyBillFeeRefundRecordDto.getBillTypeId())
                //账单类型
                .eq(Func.isNotEmpty(saasParkPropertyBillFeeRefundRecordDto.getBillType()), SaasParkPropertyBillFeeRefundRecord::getBillType, saasParkPropertyBillFeeRefundRecordDto.getBillType())
                .orderByDesc(SaasParkPropertyBillFeeRefundRecord::getCreateTime);
        IPage<SaasParkPropertyBillFeeRefundRecord> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyBillFeeRefundRecord> saasParkPropertyBillFeeRefundRecordList = pages.getRecords();

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

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

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

    public R<SaasParkPropertyBillFeeRefundRecordVo> detail(Long id, SaasParkPropertyBillFeeRefundRecord saasParkPropertyBillFeeRefundRecord) {
        SaasParkPropertyBillFeeRefundRecordVo saasParkPropertyBillFeeRefundRecordVo = new SaasParkPropertyBillFeeRefundRecordVo();
        BeanUtils.copyProperties(saasParkPropertyBillFeeRefundRecord, saasParkPropertyBillFeeRefundRecordVo);
        return R.data(saasParkPropertyBillFeeRefundRecordVo);
    }















}

