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.SaasParkPropertyBillDepositBillDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillDepositBill;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillDepositBillVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyBillDepositBillMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyBillDepositBillService;
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 SaasParkPropertyBillDepositBillServiceImpl extends BaseServiceImpl<SaasParkPropertyBillDepositBillMapper, SaasParkPropertyBillDepositBill> implements ISaasParkPropertyBillDepositBillService {



    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyBillDepositBillDto saasParkPropertyBillDepositBillDto) {
        SaasParkPropertyBillDepositBill saasParkPropertyBillDepositBill = new SaasParkPropertyBillDepositBill();
        BeanUtil.copyProperties(saasParkPropertyBillDepositBillDto, saasParkPropertyBillDepositBill);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyBillDepositBill);
        return result;
    }

    @Override
    public void export(SaasParkPropertyBillDepositBillDto saasParkPropertyBillDepositBillDto, HttpServletResponse response) {
        LambdaQueryWrapper<SaasParkPropertyBillDepositBill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasParkPropertyBillDepositBill::getParkId, saasParkPropertyBillDepositBillDto.getParkId())
                //关联的账单id
                .eq(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getRelativeReceivableManageId()), SaasParkPropertyBillDepositBill::getRelativeReceivableManageId, saasParkPropertyBillDepositBillDto.getRelativeReceivableManageId())
                //单号
                .eq(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getRoomId()), SaasParkPropertyBillDepositBill::getRoomId, saasParkPropertyBillDepositBillDto.getRoomId())
                .orderByDesc(SaasParkPropertyBillDepositBill::getCreateTime);
        List<SaasParkPropertyBillDepositBill> list = baseMapper.selectList(queryWrapper);
        List<SaasParkPropertyBillDepositBillVo> saasParkPropertyBillDepositBillVos =new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            saasParkPropertyBillDepositBillVos = listVO(list);
        }
        log.info("需要导出的数量条数： {}", saasParkPropertyBillDepositBillVos.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(saasParkPropertyBillDepositBillVos); //最终处理好的数据结果集list
        } catch (Exception e) {
            log.error("押金账单表导出异常");
            e.printStackTrace();
        }
    }


    @Override
    public R<IPage<SaasParkPropertyBillDepositBillVo>> getList(SaasParkPropertyBillDepositBillDto saasParkPropertyBillDepositBillDto, Query query) {
        Page<SaasParkPropertyBillDepositBill> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyBillDepositBill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getParkId()), SaasParkPropertyBillDepositBill::getParkId, saasParkPropertyBillDepositBillDto.getParkId())
                //楼栋id
                .eq(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getBuildingId()), SaasParkPropertyBillDepositBill::getBuildingId, saasParkPropertyBillDepositBillDto.getBuildingId())
                //应收起止日期
                .ge(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getStartDate()), SaasParkPropertyBillDepositBill::getStartDate, saasParkPropertyBillDepositBillDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getEndDate()), SaasParkPropertyBillDepositBill::getStartDate, saasParkPropertyBillDepositBillDto.getEndDate())
                //押金名称
                .eq(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getDepositName()), SaasParkPropertyBillDepositBill::getDepositName, saasParkPropertyBillDepositBillDto.getDepositName())
                .eq(Func.isNotEmpty(saasParkPropertyBillDepositBillDto.getPersonName()), SaasParkPropertyBillDepositBill::getPersonName, saasParkPropertyBillDepositBillDto.getPersonName())

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

        List<SaasParkPropertyBillDepositBill> saasParkPropertyBillDepositBillList = pages.getRecords();

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

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

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

    public R<SaasParkPropertyBillDepositBillVo> detail(Long id, SaasParkPropertyBillDepositBill saasParkPropertyBillDepositBill) {
        SaasParkPropertyBillDepositBillVo saasParkPropertyBillDepositBillVo = new SaasParkPropertyBillDepositBillVo();
        BeanUtils.copyProperties(saasParkPropertyBillDepositBill, saasParkPropertyBillDepositBillVo);
        return R.data(saasParkPropertyBillDepositBillVo);
    }















}


