package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;
import com.iformall.common.ResultData;
import com.iformall.domain.po.*;
import com.iformall.domain.vo.WxBillSettleVo;
import com.iformall.enums.*;
import com.iformall.mapper.*;
import com.iformall.service.ExcelService;
import com.iformall.service.WxBillSettleService;
import com.iformall.service.WxFlowService;
import com.iformall.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author luozukai
 * @date 2019/8/26 17:37
 */
@Service
public class WxBillSettleServiceImpl implements WxBillSettleService {
    @Autowired
    private WxBillSettleMapper wxBillSettleMapper;
    @Autowired
    private WxMerchantMapper wxMerchantMapper;
    @Autowired
    private WxFlowService wxFlowService;
    @Autowired
    private WxBillSettleBillMapper wxBillSettleBillMapper;
    @Autowired
    private WxBillRentMapper wxBillRentMapper;
    @Autowired
    private WxBillDepositMapper wxBillDepositMapper;
    @Autowired
    private WxBillPropertyMapper wxBillPropertyMapper;
    @Autowired
    private WxBillDailyMapper wxBillDailyMapper;
    @Autowired
    private WxBillOtherMapper wxBillOtherMapper;
    @Autowired
    private WxBillPropertyDepositMapper wxBillPropertyDepositMapper;
    @Autowired
    private WxBillOtherDepositMapper wxBillOtherDepositMapper;
    @Autowired
    private WxBillSettleRecordMapper wxBillSettleRecordMapper;
    @Autowired
    private WxMerchantSubsidyMapper wxMerchantSubsidyMapper;
    @Autowired
    private ExcelService excelService;
    @Autowired
    private WxBillActionMapper wxBillActionMapper;
    @Autowired
    private WxBillAllMapper wxBillAllMapper;

    @Override
    public PageInfo<WxBillSettle> listAsPage(WxBillSettle record, Integer pageIndex, Integer pageSize) {
        record.setLimitStart(0);
        record.setLimitEnd(Integer.MAX_VALUE);
        List totalList = wxBillSettleMapper.findList(record);
        if(CollectionUtils.isEmpty(totalList)){
            PageHelper.startPage(pageIndex, pageSize);
            PageInfo<WxBillSettle> pageInfo = new PageInfo(new ArrayList());
            pageInfo.setPages(0);
            pageInfo.setTotal(0);
            PageHelper.clearPage();
            return pageInfo;
        }

        int total = totalList.size();
        int pages;
        if(total % pageSize == 0){
            pages = total / pageSize;
        }else{
            pages = total / pageSize +1;
        }
        record.setLimitStart((pageIndex -1) * pageSize);
        record.setLimitEnd(pageSize);
        List<WxBillSettle> list = wxBillSettleMapper.findList(record);

        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<WxBillSettle> pageInfo = new PageInfo(list);
        pageInfo.setPages(pages);
        pageInfo.setTotal(total);
        PageHelper.clearPage();
        return pageInfo;

    }

    @Override
    public WxBillSettle getById(Long id,Integer filterSettleRecord){
        WxBillSettle billSettle = wxBillSettleMapper.selectById(id);

        if(billSettle != null) {
            WxMerchant merchant = wxMerchantMapper.selectById(billSettle.getMerchantId());
            if(merchant != null){
                billSettle.setMerchantName(merchant.getName());
            }

            //加载账单列表
            WxBillSettleBill wxBillSettleBill = new WxBillSettleBill();
            wxBillSettleBill.setTenantId(billSettle.getTenantId());
            wxBillSettleBill.setSettleId(billSettle.getId());
            wxBillSettleBill.setType(EnumSettleBillType.R.getCode());
            billSettle.setReceiveBillIds(wxBillSettleBillMapper.findBySettleId(wxBillSettleBill));
            wxBillSettleBill.setType(EnumSettleBillType.P.getCode());
            billSettle.setPayBillIds(wxBillSettleBillMapper.findBySettleId(wxBillSettleBill));

            //加载清算历史
            WxBillSettleRecord wxBillSettleRecord = new WxBillSettleRecord();
            wxBillSettleRecord.setTenantId(billSettle.getTenantId());
            wxBillSettleRecord.setSettleId(billSettle.getId());
            billSettle.setSettleRecordList(wxBillSettleRecordMapper.findList(wxBillSettleRecord));

            //加载应收、承付、结余
            WxBillSettle query = new WxBillSettle();
            query.setId(id);
            query.setFilterSettleRecord(filterSettleRecord);
            query = wxBillSettleMapper.findList(query).get(0);
            billSettle.setReceiveMoney(query.getReceiveMoney());
            billSettle.setPayMoney(query.getPayMoney());
            billSettle.setBalance(query.getBalance());

            //清算方向
            if(billSettle.getReceiveMoney() > billSettle.getPayMoney()){
                billSettle.setSettleWay(EnumSettleWay.B.getCode());
            }else{
                billSettle.setSettleWay(EnumSettleWay.M.getCode());
            }
        }
        return billSettle;
    }

    public void addBillAction(Long billId,Long money){
        final IdWorker idWorker = IdWorker.get();
        WxBillAction billAction = new WxBillAction();
        billAction.setId(idWorker.nextId());
        billAction.setCreatetime(new Date());
        billAction.setUserName("系统端");
        billAction.setAction(EnumBillAction.UPDATE_SETTLE_MONEY.getCode());
        billAction.setBillId(billId);
        BigDecimal newmoney = new BigDecimal(money).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP);
        billAction.setDetails("通过结算单清算"+newmoney.toString()+"元");
        billAction.setUpdatetime(new Date());
        billAction.setPayDate(new Date());
        wxBillActionMapper.insert(billAction);
    }

    @Override
    public void updateFreezeOrStatus(List<WxBillSettleBill> billList,Integer freezeStatus,Integer status){
        //1租金2押金3物业4日常费用6其他8补贴
        for (WxBillSettleBill bill:billList) {
            if(EnumBillType.RENT.getCode().equals(bill.getBillType())) {
                WxBillRent wxBillRent = new WxBillRent();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillRent dbBill = wxBillRentMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getOwe() == null) dbBill.setOwe(0l);
                        wxBillRent.setPay(dbBill.getPay()+ dbBill.getOwe());
                        wxBillRent.setPayDate(new Date());
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillRentMapper.updateById(wxBillRent);
            }else if(EnumBillType.DEPOSIT.getCode().equals(bill.getBillType())) {
                WxBillDeposit wxBillRent = new WxBillDeposit();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillDeposit dbBill = wxBillDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getReceivePay() == null) dbBill.setReceivePay(0l);
                        wxBillRent.setPay(dbBill.getPay()+dbBill.getReceivePay() ); //押金是ReceivePay
                        wxBillRent.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            wxBillRent.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            wxBillRent.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillDepositMapper.updateById(wxBillRent);

                WxBillOtherDeposit wxBillOtherDeposit = new WxBillOtherDeposit();
                wxBillOtherDeposit.setId(bill.getBillId());
                if(freezeStatus != null) wxBillOtherDeposit.setFreeze(freezeStatus);
                if(status != null){
                    wxBillOtherDeposit.setStatus(status);
                    wxBillOtherDeposit.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillOtherDeposit dbBill = wxBillOtherDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getReceivePay() == null) dbBill.setReceivePay(0l);
                        wxBillOtherDeposit.setPay(dbBill.getPay()+ dbBill.getReceivePay());
                        wxBillOtherDeposit.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            wxBillOtherDeposit.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            wxBillOtherDeposit.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillOtherDepositMapper.updateById(wxBillOtherDeposit);

                WxBillPropertyDeposit propertyDeposit = new WxBillPropertyDeposit();
                propertyDeposit.setId(bill.getBillId());
                if(freezeStatus != null) propertyDeposit.setFreeze(freezeStatus);
                if(status != null){
                    propertyDeposit.setStatus(status);
                    propertyDeposit.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillPropertyDeposit dbBill = wxBillPropertyDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getReceivePay() == null) dbBill.setReceivePay(0l);
                        propertyDeposit.setPay(dbBill.getPay()+ dbBill.getReceivePay());
                        propertyDeposit.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            propertyDeposit.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            propertyDeposit.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillPropertyDepositMapper.updateById(propertyDeposit);

            }else if(EnumBillType.PROPERTY.getCode().equals(bill.getBillType())) {
                WxBillProperty wxBillRent = new WxBillProperty();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillProperty dbBill = wxBillPropertyMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getOwe() == null) dbBill.setOwe(0l);
                        wxBillRent.setPay(dbBill.getPay()+dbBill.getOwe());
                        wxBillRent.setPayDate(new Date());
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillPropertyMapper.updateById(wxBillRent);

                WxBillPropertyDeposit propertyDeposit = new WxBillPropertyDeposit();
                propertyDeposit.setId(bill.getBillId());
                if(freezeStatus != null) propertyDeposit.setFreeze(freezeStatus);
                if(status != null){
                    propertyDeposit.setStatus(status);
                    propertyDeposit.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillPropertyDeposit dbBill = wxBillPropertyDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getReceivePay() == null) dbBill.setReceivePay(0l);
                        propertyDeposit.setPay(dbBill.getPay()+dbBill.getReceivePay());
                        propertyDeposit.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            propertyDeposit.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            propertyDeposit.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillPropertyDepositMapper.updateById(propertyDeposit);
            }else if(EnumBillType.ROUTINE.getCode().equals(bill.getBillType())) {
                WxBillOther wxBillRent = new WxBillOther();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillOther dbBill = wxBillOtherMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getOwe() == null) dbBill.setOwe(0l);
                        wxBillRent.setPay(dbBill.getPay()+dbBill.getOwe());
                        wxBillRent.setPayDate(new Date());
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillOtherMapper.updateById(wxBillRent);
            }else if(EnumBillType.OTHER_DEPOSIT.getCode().equals(bill.getBillType())) {
                WxBillOtherDeposit wxBillRent = new WxBillOtherDeposit();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillOtherDeposit dbBill = wxBillOtherDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getReceivePay() == null) dbBill.setReceivePay(0l);
                        wxBillRent.setPay(dbBill.getPay()+dbBill.getReceivePay());
                        wxBillRent.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            wxBillRent.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            wxBillRent.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillOtherDepositMapper.updateById(wxBillRent);

                WxMerchantSubsidy wxMerchantSubsidy = new WxMerchantSubsidy();
                wxMerchantSubsidy.setId(bill.getBillId());
                if(freezeStatus != null) wxMerchantSubsidy.setFreeze(freezeStatus);
                if(status != null){
                    wxMerchantSubsidy.setStatus(status);
                }
                wxMerchantSubsidyMapper.updateById(wxMerchantSubsidy);
            }else if(EnumBillType.OTHER_DEPOSIT.getCode().equals(bill.getBillType())) {
                WxBillOtherDeposit wxBillRent = new WxBillOtherDeposit();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillOtherDeposit dbBill = wxBillOtherDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getReceivePay() == null) dbBill.setReceivePay(0l);
                        wxBillRent.setPay(dbBill.getPay()+dbBill.getReceivePay());
                        wxBillRent.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            wxBillRent.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            wxBillRent.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillOtherDepositMapper.updateById(wxBillRent);
            }else{
                WxBillDaily wxBillDaily = new WxBillDaily();
                wxBillDaily.setId(bill.getBillId());
                if(freezeStatus != null) wxBillDaily.setFreeze(freezeStatus);
                if(status != null){
                    wxBillDaily.setStatus(status);
                    wxBillDaily.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillDaily dbBill = wxBillDailyMapper.selectById(wxBillDaily.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getOwe() == null) dbBill.setOwe(0l);
                        wxBillDaily.setPay(dbBill.getPay()+dbBill.getOwe());
                        wxBillDaily.setPayDate(new Date());
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillDailyMapper.updateById(wxBillDaily);

                WxBillOther wxBillRent = new WxBillOther();
                wxBillRent.setId(bill.getBillId());
                if(freezeStatus != null) wxBillRent.setFreeze(freezeStatus);
                if(status != null){
                    wxBillRent.setStatus(status);
                    wxBillRent.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillOther dbBill = wxBillOtherMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        if(dbBill.getPay() == null) dbBill.setPay(0l);
                        if(dbBill.getOwe() == null) dbBill.setOwe(0l);
                        wxBillRent.setPay(dbBill.getPay()+dbBill.getOwe());
                        wxBillRent.setPayDate(new Date());
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillOtherMapper.updateById(wxBillRent);

                WxBillPropertyDeposit propertyDeposit = new WxBillPropertyDeposit();
                propertyDeposit.setId(bill.getBillId());
                if(freezeStatus != null) propertyDeposit.setFreeze(freezeStatus);
                if(status != null){
                    propertyDeposit.setStatus(status);
                    propertyDeposit.setPayDate(new Date());
                }
                if(EnumBillRentStatus.PAID.getCode().equals(status)){
                    WxBillPropertyDeposit dbBill = wxBillPropertyDepositMapper.selectById(wxBillRent.getId());
                    if(dbBill!=null) {
                        propertyDeposit.setPay(dbBill.getPay()+dbBill.getReceivePay());
                        propertyDeposit.setPayDate(new Date());
                        if(EnumSettleBillType.R.getCode().equals(bill.getType())){
                            propertyDeposit.setStatus(EnumBillRentStatus.PAID.getCode());
                        }else{
                            propertyDeposit.setStatus(EnumBillRentStatus.RETURN.getCode());
                        }
                        addBillAction(bill.getBillId(), dbBill.getOwe());
                    }
                }
                wxBillPropertyDepositMapper.updateById(propertyDeposit);
            }
        }
    }

    public Integer getType(Long billId, WxBillSettle record){
        for (WxBillSettleBill bill:record.getReceiveBillIds()) {
            if(bill.getBillId().equals(billId)){
                return 0;
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public ResultData saveOrUpdate(WxBillSettle record, MallUserInfo userInfo) {
        final IdWorker idWorker = IdWorker.get();

        //检查账单是否存在其他结算单中
//        WxBillSettle query = new WxBillSettle();
//        query.setId(record.getId());
//        List<WxBillSettleBill> list = new ArrayList<>();
//        list.addAll(record.getReceiveBillIds());
//        list.addAll(record.getPayBillIds());
//        query.setReceiveBillIds(list);
//        List<WxBillSettleBill> existList = wxBillSettleBillMapper.findExist(query);
//        Set<Long> idSet = new HashSet();
//        if(CollectionUtils.isNotEmpty(existList)){
//            for (WxBillSettleBill b:existList) {
//                idSet.add(b.getBillId());
//            }
//            String ids = "";
//            for (Long id:idSet) {
//                WxBillSettleBill queryBill = new WxBillSettleBill();
//                queryBill.setBillId(id);
//                queryBill.setType(getType(id,record));
//                WxBillSettleBill db = wxBillSettleBillMapper.findByBillId(queryBill).get(0);
//                BigDecimal r = new BigDecimal(db.getReceivePay()).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP);
//                ids += db.getBillName()+r.toString()+"元,";
//            }
//
//            return new ResultData(ErrorCode.DB_FAIL.getCode(), ids.substring(0,ids.length()-1)+"已经在其他结算单中，请移除！");
//        }

        if (record.getFlowParams() == null || record.getFlowParams().size() == 0) {
            record.setStatus(EnumSettleStatus.NOT_FINISH.getCode());
            //选择的账单改为已结清
            updateFreezeOrStatus(record.getReceiveBillIds(), null,EnumBillRentStatus.PAID.getCode());
            updateFreezeOrStatus(record.getPayBillIds(), null,EnumBillRentStatus.PAID.getCode());

            //刚好结清
            if(record.getReceiveMoney().equals(record.getPayMoney())){
                record.setStatus(EnumSettleStatus.FINISH.getCode());
            }
        }
        if (record.getId() == null) {
            record.setId(idWorker.nextId());
            wxBillSettleMapper.insert(record);
        }else{
            wxBillSettleMapper.updateById(record);
        }

        wxBillSettleBillMapper.delBySettleId(record);
        //保存中间表
        for (WxBillSettleBill bill:record.getReceiveBillIds()) {
            bill.setId(idWorker.nextId());
            bill.setSettleId(record.getId());
            bill.setTenantId(record.getTenantId());
            bill.setType(EnumSettleBillType.R.getCode());
            wxBillSettleBillMapper.insert(bill);
        }
        for (WxBillSettleBill bill:record.getPayBillIds()) {
            bill.setId(idWorker.nextId());
            bill.setSettleId(record.getId());
            bill.setTenantId(record.getTenantId());
            bill.setType(EnumSettleBillType.P.getCode());
            wxBillSettleBillMapper.insert(bill);
        }

        //改为冻结状态
        if (record.getFlowParams() != null && record.getFlowParams().size() > 0) {
            updateFreezeOrStatus(record.getReceiveBillIds(), EnumFreezeType.YES.getCode(),null);
            updateFreezeOrStatus(record.getPayBillIds(),EnumFreezeType.YES.getCode(),null);

            //启动审批流
            record.getFlowParams().put("businessId",record.getId());
            if (record.getFlowParams() != null && record.getFlowParams().size() > 0) {
                wxFlowService.start(record.getFlowParams(), userInfo.getId(), userInfo.getName(), record.getTenantId());
            }
        }
        return new ResultData();
    }

    @Override
    public void exportBill(HttpServletRequest request, HttpServletResponse response, WxBillSettle wxBillSettle) {
        List<WxBillSettle> list = wxBillSettleMapper.findList(wxBillSettle);
        List<WxBillSettleVo> voList = new ArrayList<>();

        for (WxBillSettle s:list) {
            WxBillSettleVo vo = new WxBillSettleVo();
            vo.setSettleNumber(s.getSettleNumber());
            vo.setMerchantName(s.getMerchantName());
            vo.setCreatetime(DateUtils.formatDateTime(s.getCreatetime()));
            vo.setReceiveMoney(new BigDecimal(s.getReceiveMoney()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).toString());
            vo.setPayMoney(new BigDecimal(s.getPayMoney()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).toString());
            vo.setBalance(new BigDecimal(s.getReceiveMoney()).subtract(new BigDecimal(s.getPayMoney())).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).toString());
            vo.setSettletime(DateUtils.formatDateTime(s.getCreatetime()));
            vo.setStatus(EnumSettleStatus.getEnum(s.getStatus()).getMessage());
            vo.setApplyStatus(EnumRentContractAppStatus.getEnum(s.getApplyStatus()).getMessage());
            voList.add(vo);
        }
        excelService.exportExcel(voList, null, "结算单", WxBillSettleVo.class, "结算单.xlsx", response, false);
    }

    @Override
    public void downloadBill(HttpServletRequest request, HttpServletResponse response, WxBillSettle wxBillSettle) {

    }
}
