package com.sckw.payment.service.operate;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.model.vo.TableTop;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.payment.dao.KwpSettlementTradeMapper;
import com.sckw.payment.model.constant.SettlementEnum;
import com.sckw.payment.model.dto.ISettlement;
import com.sckw.payment.model.vo.SettlementTrade;
import com.sckw.payment.model.vo.req.SettlementTradeReq;
import com.sckw.payment.model.vo.res.SettlementSumVo;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.SysDictResDto;
import com.sckw.system.api.model.dto.res.UserCacheResDto;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 贸易结算
 *
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */
@Service
@RequiredArgsConstructor
public class SettlementTradeService {
    private final KwpSettlementTradeMapper settlementTradeMapper;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    /**
     * 字典转换
     *
     * @param list 结算单列表数据
     */
    private void changeDict(List<? extends ISettlement> list) {
        //字典等数据转换
        Map<String, SysDictResDto> dictResDtoMap = remoteSystemService.queryDictMapByType(DictTypeEnum.TRADE_TYPE.getType());
        for (ISettlement iSettlement : list) {
            String trading = iSettlement.getTrading();
            SysDictResDto sysDictResDto = dictResDtoMap.get(trading);
            if (Objects.nonNull(sysDictResDto)) {
                iSettlement.setTradingLabel(sysDictResDto.getLabel());
            }
            iSettlement.setStatusLabel(SettlementEnum.getStatusDesc(iSettlement.getStatus()));
        }
    }

    /**
     * 分页查询结算单数据
     *
     * @param settlementReq 结算单查询参数
     * @return
     */
    public PageRes<SettlementTrade> list(SettlementTradeReq settlementReq) {
        PageHelper.startPage(settlementReq.getPage(), settlementReq.getPageSize());
        List<SettlementTrade> kwpLedgerLogisticsList = settlementTradeMapper.list(settlementReq);
        PageInfo<SettlementTrade> settlementPageInfo = new PageInfo<>(kwpLedgerLogisticsList);
        if (!CollectionUtils.isEmpty(kwpLedgerLogisticsList)) {
            //根据企业类型转换对应的数据
            for (SettlementTrade settlementTradeDto : kwpLedgerLogisticsList) {
                UserCacheResDto userCacheResDto = remoteSystemService.queryUserCacheById(settlementTradeDto.getUpdateBy());
                settlementTradeDto.setUpdateByLabel(Objects.nonNull(userCacheResDto) ? userCacheResDto.getName() : "");
                UserCacheResDto userCacheResDto2 = remoteSystemService.queryUserCacheById(settlementTradeDto.getCreateBy());
                settlementTradeDto.setUpdateByLabel(Objects.nonNull(userCacheResDto2) ? userCacheResDto2.getName() : "");
            }
            changeDict(kwpLedgerLogisticsList);
            return new PageRes<>(settlementPageInfo, kwpLedgerLogisticsList);
        }
        return new PageRes<>(settlementPageInfo);
    }

    /**
     * 统计列表的表头数据
     *
     * @param settlementReq 结算单查询参数
     * @return
     */
    public List<TableTop> getCount(SettlementTradeReq settlementReq) {
        Map<String, Long> stringIntegerMap = settlementTradeMapper.count(settlementReq);
        List<TableTop> res = new ArrayList<>(4);
        for (SettlementEnum value : SettlementEnum.values()) {
            TableTop tableTop = new TableTop();
            tableTop.setName(value.getDesc());
            tableTop.setValue(value.getStatus());
            tableTop.setTotal(Math.toIntExact(stringIntegerMap.get(value.getLabel())));
            res.add(tableTop);
        }
        return res;
    }

    /**
     * 表尾统计数据
     *
     * @param settlementReq 结算单查询参数
     * @return
     */
    public SettlementSumVo sumMoney(SettlementTradeReq settlementReq) {
        return settlementTradeMapper.sumMoney2(settlementReq);
    }


    public List<SettlementTrade> exportList(SettlementTradeReq settlementReq, List<Long> idList) {
        List<SettlementTrade> settlementTradeDtos = settlementTradeMapper.operateExportList(settlementReq, idList);
        changeDict(settlementTradeDtos);
        for (SettlementTrade settlementTradeDto : settlementTradeDtos) {
            UserCacheResDto userCacheResDto = remoteSystemService.queryUserCacheById(settlementTradeDto.getCreateBy());
            settlementTradeDto.setCreateByLabel(Objects.nonNull(userCacheResDto) ? userCacheResDto.getName() : "");
        }
        return settlementTradeDtos;
    }
}
