package com.jwipc.finance.logic.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.finance.convert.AttachmentManualCheckConvert;
import com.jwipc.finance.dto.AttachmentManualCheckDTO;
import com.jwipc.finance.entity.AttachmentManualCheck;
import com.jwipc.finance.logic.AttachmentManualCheckLogic;
import com.jwipc.finance.model.po.AttachmentManualCheckPO;
import com.jwipc.finance.model.po.PaymentNoticeDetailPO;
import com.jwipc.finance.model.query.AttachmentManualCheckQuery;
import com.jwipc.finance.service.IAttachmentManualCheckService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.dto.DictDTO;
import com.jwipc.sys.dto.AttachmentTypeConfigDTO;
import com.jwipc.sys.logic.AttachmentTypeConfigLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2025/12/11 11:52
 * @desc
 */
@Service
@RequiredArgsConstructor
public class AttachmentManualCheckLogicImpl implements AttachmentManualCheckLogic {
    private final IAttachmentManualCheckService attachmentManualCheckService;
    private final AttachmentTypeConfigLogic attachmentTypeConfigLogic;
    private final DictClient dictClient;
    private final AttachmentManualCheckConvert attachmentManualCheckConvert;

    @Override
    public PageView findByPage(AttachmentManualCheckQuery query) {
        List<AttachmentManualCheckPO> list = new ArrayList<>();
        if (ObjectUtil.isNotNull(query.getBusinessId())){
           list = attachmentManualCheckService.getByBusinessIdAndTypeList(query.getBusinessId(),
                   query.getBusinessType()==1?ListUtil.toList(0,1):ListUtil.toList(query.getBusinessType()));
        }
        boolean listNull = CollUtil.isEmpty(list);
        //如果是对账
        if (query.getBusinessType()==0||(query.getBusinessType()==1&&CollUtil.isEmpty(list))){
            if (CollUtil.isEmpty(list)){
                if ("9999".equals(query.getSupplierCode())){
                    List<AttachmentTypeConfigDTO> transferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                            "correlation_account_statement_manual_check",
                            "correlation_account_statement_check");
                    if (CollUtil.isNotEmpty(transferAttachmentTypeCheckConfigs)){
                        for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : transferAttachmentTypeCheckConfigs) {
                            AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                            attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                            attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                            attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                            attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                            list.add(attachmentManualCheckPO);
                        }
                    }
                }else {
                    if (("CNY").equals(query.getCurrencyCode())&&(("B").equals(query.getPaymentWay())||("D").equals(query.getPaymentWay()))){
                        //对账单银行转账或外来对冲(人工检查)校验
                        List<AttachmentTypeConfigDTO> transferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                                "transfer_account_statement_check",
                                "account_statement_check");
                        if (CollUtil.isNotEmpty(transferAttachmentTypeCheckConfigs)){
                            for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : transferAttachmentTypeCheckConfigs) {
                                AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                                attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                                attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                                attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                                attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                                list.add(attachmentManualCheckPO);
                            }
                        }
                    }
                    if (("CNY").equals(query.getCurrencyCode())&&("C").equals(query.getPaymentWay())){
                        //对账单承兑汇票(人工检查)校验
                        List<AttachmentTypeConfigDTO> acceptanceAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                                "acceptance_account_statement_check",
                                "account_statement_check");
                        if (CollUtil.isNotEmpty(acceptanceAttachmentTypeCheckConfigs)){
                            for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : acceptanceAttachmentTypeCheckConfigs) {
                                AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                                attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                                attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                                attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                                attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                                list.add(attachmentManualCheckPO);
                            }
                        }
                    }
                    if (!("CNY").equals(query.getCurrencyCode())&&("B").equals(query.getPaymentWay())){
                        //对账单银行转账(人工检查)校验
                        List<AttachmentTypeConfigDTO> overseasTransferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                                "overseas_transfer_account_statement_check",
                                "overseas_account_statement_check");
                        if (CollUtil.isNotEmpty(overseasTransferAttachmentTypeCheckConfigs)){
                            for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : overseasTransferAttachmentTypeCheckConfigs) {
                                AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                                attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                                attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                                attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                                attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                                list.add(attachmentManualCheckPO);
                            }
                        }
                    }
                }


            }
        }
        if (query.getBusinessType()==1){
            boolean flag = true;
            if (listNull){
                flag = false;
            }else {
                //判断list中是否存在type=1的数据
                flag = list.stream().anyMatch(attachmentManualCheckPO -> attachmentManualCheckPO.getBusinessType()==1);
            }

            if (!flag){
                if (("CNY").equals(query.getCurrencyCode())&&(("B").equals(query.getPaymentWay())||("D").equals(query.getPaymentWay()))){
                    //对账单银行转账或外来对冲(人工检查)校验
                    List<AttachmentTypeConfigDTO> transferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                            "transfer_invoiced_apply_check",
                            "invoiced_apply_check");
                    if (CollUtil.isNotEmpty(transferAttachmentTypeCheckConfigs)){
                        for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : transferAttachmentTypeCheckConfigs) {
                            AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                            attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                            attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                            attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                            attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                            list.add(attachmentManualCheckPO);
                        }
                    }
                }
                if (("CNY").equals(query.getCurrencyCode())&&("C").equals(query.getPaymentWay())){
                    //对账单承兑汇票(人工检查)校验
                    List<AttachmentTypeConfigDTO> acceptanceAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                            "acceptance_invoiced_apply_check",
                            "invoiced_apply_check");
                    if (CollUtil.isNotEmpty(acceptanceAttachmentTypeCheckConfigs)){
                        for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : acceptanceAttachmentTypeCheckConfigs) {
                            AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                            attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                            attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                            attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                            attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                            list.add(attachmentManualCheckPO);
                        }
                    }
                }
                if (!("CNY").equals(query.getCurrencyCode())&&("B").equals(query.getPaymentWay())){
                    //对账单银行转账(人工检查)校验
                    List<AttachmentTypeConfigDTO> overseasTransferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listNeedCheckByBusinessTypeCode(
                            "overseas_transfer_invoiced_apply_check",
                            "overseas_invoiced_apply_check");
                    if (CollUtil.isNotEmpty(overseasTransferAttachmentTypeCheckConfigs)){
                        for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : overseasTransferAttachmentTypeCheckConfigs) {
                            AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                            attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                            attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                            attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                            attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                            list.add(attachmentManualCheckPO);
                        }
                    }
                }
            }

        }
        if (query.getBusinessType()==2){
            if (listNull){
                //对账单银行转账(人工检查)校验
                List<AttachmentTypeConfigDTO> overseasTransferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("cost_invoiced_apply_check");
                if (CollUtil.isNotEmpty(overseasTransferAttachmentTypeCheckConfigs)){
                    for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : overseasTransferAttachmentTypeCheckConfigs) {
                        AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                        attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                        attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                        attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                        attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                        list.add(attachmentManualCheckPO);
                    }
                }
            }

        }
        if (query.getBusinessType()==3){
            if (listNull){
                //对账单银行转账(人工检查)校验
                List<AttachmentTypeConfigDTO> overseasTransferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode("company_invoiced_apply_check");
                if (CollUtil.isNotEmpty(overseasTransferAttachmentTypeCheckConfigs)){
                    for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : overseasTransferAttachmentTypeCheckConfigs) {
                        AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                        attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                        attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                        attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                        attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
                        list.add(attachmentManualCheckPO);
                    }
                }
            }

        }
        //资产合同付款申请
        if (query.getBusinessType()==4){
            list = attachmentManualCheckService.getByBusinessIdAndTypeList(query.getBusinessId(),
                    query.getBusinessType()==1?ListUtil.toList(0,1):ListUtil.toList(query.getBusinessType()));
            listNull = CollUtil.isEmpty(list);
            if (listNull) {
                //对账单银行转账(人工检查)校验
                List<AttachmentTypeConfigDTO> overseasTransferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode(query.getPaymentWay());
                if (CollUtil.isNotEmpty(overseasTransferAttachmentTypeCheckConfigs)) {
                    for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : overseasTransferAttachmentTypeCheckConfigs) {
                        AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                        attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                        attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                        attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                        attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
//                        attachmentManualCheckPO.setManualCheck(0);
                        list.add(attachmentManualCheckPO);
                    }
                }
            } else {
                ////资产合同付款申请(人工检查)校验
                List<AttachmentTypeConfigDTO> overseasTransferAttachmentTypeCheckConfigs = attachmentTypeConfigLogic.listByBusinessTypeCode(query.getPaymentWay());
                if (CollUtil.isNotEmpty(overseasTransferAttachmentTypeCheckConfigs)) {
                    for (AttachmentTypeConfigDTO transferAttachmentTypeCheckConfig : overseasTransferAttachmentTypeCheckConfigs) {
                        boolean isExist = false;
                        for (AttachmentManualCheckPO attachmentManualCheckPO : list) {
                            if (attachmentManualCheckPO.getType().equals(transferAttachmentTypeCheckConfig.getCode())) {
                                isExist = true;
                                break;
                            }
                        }
                        if (!isExist) {
                            AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
                            attachmentManualCheckPO.setBusinessId(query.getBusinessId());
                            attachmentManualCheckPO.setBusinessCode(query.getBusinessCode());
                            attachmentManualCheckPO.setBusinessType(query.getBusinessType());
                            attachmentManualCheckPO.setType(transferAttachmentTypeCheckConfig.getCode());
//                            attachmentManualCheckPO.setManualCheck(0);
                            list.add(attachmentManualCheckPO);
                        }
                    }
                }
            }
        }
        return PageUtil.getPV(list, list.size());
    }

    @Override
    public PageView findByPaymentByPage(AttachmentManualCheckQuery query) {
        List<AttachmentManualCheckDTO> listPage = this.findByPaymentList(query);
        return PageUtil.getPV(listPage, listPage.size());
    }

    @Override
    public List<AttachmentManualCheckDTO> findByPaymentList(AttachmentManualCheckQuery query) {
        List<AttachmentTypeConfigDTO> allConfigList = new ArrayList<>();
        if (query.getBusinessType()==1){
            if (query.getCurrencyCode().equals("CNY")&&(query.getPaymentWay().equals("B")||query.getPaymentWay().equals("D"))){
                //对账单银行转账或外来对冲(人工检查)校验
                allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("transfer_account_statement_check"));
                allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("transfer_invoiced_apply_check"));
            }
            if (query.getCurrencyCode().equals("CNY")&&query.getPaymentWay().equals("C")){
                //对账单承兑汇票(人工检查)校验
                allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("acceptance_account_statement_check"));
                allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("acceptance_invoiced_apply_check"));
            }
            if (!query.getCurrencyCode().equals("CNY")&&query.getPaymentWay().equals("B")){
                //对账单承兑汇票(人工检查)校验
                allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("overseas_transfer_account_statement_check"));
                allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("overseas_transfer_invoiced_apply_check"));
            }
        }
        if (query.getBusinessType()==2){
            allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("cost_invoiced_apply_check"));
        }
        if (query.getBusinessType()==3){
            allConfigList.addAll(attachmentTypeConfigLogic.listByBusinessTypeCode("company_invoiced_apply_check"));
        }

        final List<String> businessCodeList = Arrays.asList(query.getBusinessCodes().split(","));
        List<AttachmentManualCheck> list = attachmentManualCheckService.list(new LambdaQueryWrapper<AttachmentManualCheck>().in(AttachmentManualCheck::getBusinessCode,businessCodeList));

        List<AttachmentManualCheckPO> listPage = new ArrayList<>();
        for (AttachmentTypeConfigDTO attachmentTypeConfigDTO : allConfigList){
            int manualCheck = 1;
            String remark = "";
            if (CollUtil.isNotEmpty(list)){
                for (AttachmentManualCheck attachmentManualCheck : list){
                    if (attachmentManualCheck.getType().equals(attachmentTypeConfigDTO.getCode())){
                        remark = attachmentManualCheck.getRemark();
                    }
                    if (attachmentManualCheck.getType().equals(attachmentTypeConfigDTO.getCode())&&attachmentManualCheck.getManualCheck()==0){
                        manualCheck = 0;
                        break;
                    }
                }
            }else {
                manualCheck = 0;
            }
            AttachmentManualCheckPO attachmentManualCheckPO = new AttachmentManualCheckPO();
            attachmentManualCheckPO.setType(attachmentTypeConfigDTO.getCode());
            DictDTO dictDTO = dictClient.getDictByTypeAndCode("businessFileType", attachmentTypeConfigDTO.getCode());
            attachmentManualCheckPO.setTypeName(ObjectUtil.isNotNull(dictDTO)?dictDTO.getName():"");
            attachmentManualCheckPO.setManualCheck(manualCheck);
            attachmentManualCheckPO.setRemark(remark);
            listPage.add(attachmentManualCheckPO);
        }
        return attachmentManualCheckConvert.po2DTOList(listPage);
    }
}
