package com.sckw.report.service;

import com.sckw.contract.api.RemoteContractService;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.enums.CooperateTypeEnum;
import com.sckw.core.model.enums.EntTypeEnum;
import com.sckw.core.model.enums.SystemTypeEnum;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.order.api.dubbo.TradeOrderStatisticsService;
import com.sckw.order.api.model.TradeOrderCountStatisticsDTO;
import com.sckw.payment.api.dubbo.PaymentDubboService;
import com.sckw.payment.api.model.dto.LedgerCount;
import com.sckw.report.service.vo.QueryBusinessStatisticsResVo;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.EntCacheResDto;
import com.sckw.transport.api.dubbo.TransportStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author czh
 * @desc 工作台
 * @date 2023/9/11
 */
@Slf4j
@Service
public class KwWorkService {

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private TransportStatisticsService transportStatisticsService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private PaymentDubboService paymentDubboService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteContractService remoteContractService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private TradeOrderStatisticsService tradeOrderStatisticsService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;


    /**
     * @return QueryBusinessStatisticsResVo
     * @desc: 业务数据数量统计
     * @author: czh
     * @date: 2023/9/11
     */
    public List<QueryBusinessStatisticsResVo> queryBusinessStatistics() {
        boolean isSystem = LoginUserHolder.getSystemType().equals(SystemTypeEnum.MANAGE.getCode());
        Long entId = null;
        String entTypes = "";

        if (!isSystem) {
            EntCacheResDto entCacheResDto = remoteSystemService.queryEntCacheById(LoginUserHolder.getEntId());
            if (Objects.isNull(entCacheResDto)) {
                return Collections.emptyList();
            }
            entId = entCacheResDto.getId();
            entTypes = entCacheResDto.getEntTypes();
        }
        //承运
        boolean hasCarrier = entTypes.contains(String.valueOf(EntTypeEnum.LOGISTICS4.getCode())) || entTypes.contains(String.valueOf(EntTypeEnum.LOGISTICS3.getCode()));
        //托运
        boolean hasCheck = entTypes.contains(String.valueOf(EntTypeEnum.LOGISTICS3.getCode()));
        //采购
        boolean hasPurchase = entTypes.contains(String.valueOf(EntTypeEnum.PURCHASER.getCode()));
        //供应
        boolean hasSupplier = entTypes.contains(String.valueOf(EntTypeEnum.SUPPLIER.getCode()));

        List<QueryBusinessStatisticsResVo> list = new ArrayList<>();
        //合同部分
        Map<Integer, Integer> contractMap = remoteContractService.queryContractValidCount(entId);
        if (hasPurchase || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("采购合同");
            queryBusinessStatisticsResVo.setNum(contractMap.getOrDefault(CooperateTypeEnum.PURCHASER.getCode(), Global.NUMERICAL_ZERO));
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasSupplier || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("销售合同");
            queryBusinessStatisticsResVo.setNum(contractMap.getOrDefault(CooperateTypeEnum.SUPPLIER.getCode(), Global.NUMERICAL_ZERO));
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasCarrier || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("承运合同");
            queryBusinessStatisticsResVo.setNum(contractMap.getOrDefault(CooperateTypeEnum.CARRIAGE.getCode(), Global.NUMERICAL_ZERO));
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasCheck || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("托运合同");
            queryBusinessStatisticsResVo.setNum(contractMap.getOrDefault(CooperateTypeEnum.CONSIGN.getCode(), Global.NUMERICAL_ZERO));
            list.add(queryBusinessStatisticsResVo);
        }

        //订单部分
        Map<Integer, Integer> orderNumByTopEntId = tradeOrderStatisticsService.getOrderNumByTopEntId(entId);
        if (hasSupplier || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("销售订单");
            queryBusinessStatisticsResVo.setNum(orderNumByTopEntId.get(Global.NUMERICAL_TWO));
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasPurchase || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("采购订单");
            queryBusinessStatisticsResVo.setNum(orderNumByTopEntId.get(Global.NUMERICAL_ONE));
            list.add(queryBusinessStatisticsResVo);
        }

        //承运订单
        Integer integer = transportStatisticsService.statisticsLogistics(entId);
        if (hasCarrier || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("承运订单");
            queryBusinessStatisticsResVo.setNum(Objects.isNull(integer) ? Global.NUMERICAL_ZERO : integer);
            list.add(queryBusinessStatisticsResVo);
        }

        //托运订单
        Integer integer1 = transportStatisticsService.statisticsLogisticsByConsign(entId);
        if (hasCheck || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("托运订单");
            queryBusinessStatisticsResVo.setNum(Objects.isNull(integer1) ? Global.NUMERICAL_ZERO : integer1);
            list.add(queryBusinessStatisticsResVo);
        }

        LedgerCount ledgerCount = paymentDubboService.countLedger(entId);
        if (hasSupplier || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("销售对账");
            queryBusinessStatisticsResVo.setNum(Objects.isNull(ledgerCount.getSell()) ? Global.NUMERICAL_ZERO : ledgerCount.getSell());
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasPurchase || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("采购对账");
            queryBusinessStatisticsResVo.setNum(Objects.isNull(ledgerCount.getPurchase()) ? Global.NUMERICAL_ZERO : ledgerCount.getPurchase());
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasCarrier || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("承运对账");
            queryBusinessStatisticsResVo.setNum(Objects.isNull(ledgerCount.getCarrier()) ? Global.NUMERICAL_ZERO : ledgerCount.getCarrier());
            list.add(queryBusinessStatisticsResVo);
        }

        if (hasCheck || isSystem) {
            QueryBusinessStatisticsResVo queryBusinessStatisticsResVo = new QueryBusinessStatisticsResVo();
            queryBusinessStatisticsResVo.setType("托运对账");
            queryBusinessStatisticsResVo.setNum(Objects.isNull(ledgerCount.getShipper()) ? Global.NUMERICAL_ZERO : ledgerCount.getShipper());
            list.add(queryBusinessStatisticsResVo);
        }
        return list;
    }

}
