package com.jwipc.contract.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jwipc.contract.entity.ContractPaymentApplyDetail;
import com.jwipc.contract.mapper.ContractPaymentApplyDetailMapper;
import com.jwipc.contract.model.po.AttachmentManualCheckPO;
import com.jwipc.contract.model.po.CreateContractPayDetailPO;
import com.jwipc.contract.service.IContractPaymentApplyDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资产合同付款申请明细表 服务实现类
 * </p>
 *
 * @author 邓京术
 * @since 2025-12-23
 */
@Service
public class ContractPaymentApplyDetailServiceImpl extends ServiceImpl<ContractPaymentApplyDetailMapper, ContractPaymentApplyDetail> implements IContractPaymentApplyDetailService {

    @Override
    public List<CreateContractPayDetailPO> queryCodApplyInvoiceById(Long id) {
        return baseMapper.queryCodApplyInvoiceById(id);
    }

    @Override
    public List<CreateContractPayDetailPO> queryInvoiceByContractDetailId(Long id) {
        return baseMapper.queryInvoiceByContractDetailId(id);
    }

    @Override
    public List<CreateContractPayDetailPO> findByAccountCodeAndItem1(List<CreateContractPayDetailPO> list, Long id, String contractNo) {
        return baseMapper.findByAccountCodeAndItem1(list, id, contractNo);
    }

    @Override
    public List<CreateContractPayDetailPO> findByAccountByStageId(Long id) {
        return baseMapper.findByAccountByStageId(id);
    }

    @Override
    public List<CreateContractPayDetailPO> findItemDateById(Long applyId) {
        return baseMapper.queryCodApplyInvoiceById(applyId);
    }

    @Override
    public void saveManualCheckList(List<AttachmentManualCheckPO> applyId) {
        for (AttachmentManualCheckPO attachmentManualCheckPO : applyId) {
            baseMapper.saveManualCheckList(attachmentManualCheckPO);
        }
    }
//
//    @Override
//    public List<CreatePcaDetailPO> findByApplyCodeAndItem(List<CreatePcaDetailPO> list, Long applyId) {
//        return baseMapper.findByApplyCodeAndItem(list,applyId);
//    }
//
//    @Override
//    public List<CreatePcaDetailPO> findByAccountCodeAndItem(List<AccountStatementPO> list) {
//        return baseMapper.findByAccountCodeAndItem(list);
//    }
//
//    @Override
//    public List<CreatePcaDetailPO> findByCostAccountCodeAndItem(List<CostAccountStatementPO> list) {
//        return baseMapper.findByCostAccountCodeAndItem(list);
//    }
//
//    @Override
//    public List<CreatePcaDetailPO> findByCompanyAccountCodeAndItem(List<CompanyAccountStatementPO> list) {
//        return baseMapper.findByCompanyAccountCodeAndItem(list);
//    }
//
//    @Override
//    public List<CreatePcaDetailPO> findByAccountCodeAndItem1(List<CreatePcaDetailPO> list, Long id) {
//        return baseMapper.findByAccountCodeAndItem1(list,id);
//    }
//
//    @Override
//    public List<CreatePcaDetailPO> findPaymentDetailByPoCodeList(List<String> poCodeList) {
//        return baseMapper.findPaymentDetailByPoCodeList(poCodeList);
//    }
//
//    @Override
//    public List<PaymentRecordPO> findDetailByPoCode(String poCode) {
//        return baseMapper.findDetailByPoCode(poCode);
//    }
//
//    @Override
//    public List<CreatePaaDetailPO> readAdvancePaymentPoData(PaymentApplyItemQuery query) {
//        return baseMapper.readAdvancePaymentPoData(query);
//    }
//
//    @Override
//    public void findPoAdvanceApplySituation(List<CreatePaaDetailPO> params, List<Long> existedItemIdList, Long applyId) {
//        List<String> poKeyList = Lists.newArrayList();
//        for(CreatePaaDetailPO dto: params){
//            poKeyList.add(dto.getPoKey());
//        }
//
//        // 新增时检查采购订单行在SRM中使用的情况
//        if(0L == applyId){
//            // 获取SRM中预付款申请单明细及金额
//            final List<PaymentAdvanceApplyDetailPO> paaDetailList = baseMapper.createGetPoSituationByPoKey(poKeyList);
//            for(PaymentAdvanceApplyDetailPO detail: paaDetailList){
//                for(CreatePaaDetailPO dto: params){
//                    if(dto.getPoKey().equals(detail.getDataKey())){
//                        dto.setAlreadyApplyAmount(dto.getAlreadyApplyAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
////                        dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount()));
//                        dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount())
//                                .subtract(dto.getAlreadyAccountAmount()==null?BigDecimal.ZERO:dto.getAlreadyAccountAmount())
//                                .add(dto.getAlreadyOutAmount()==null?BigDecimal.ZERO:dto.getAlreadyOutAmount())
//                                .subtract(dto.getAlreadyIntoAmount()==null?BigDecimal.ZERO:dto.getAlreadyIntoAmount())
//                        );
//                        dto.setBudInnerMoney(dto.getBudInnerMoney());
//                        dto.setBudOutMoney(dto.getBudOutMoney());
//                        //已付款金额
//                        if(detail.getStatus() == 5){
//                            dto.setAlreadyAdvanceAmount(dto.getAlreadyAdvanceAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
//                        }
//
//                    }
//                }
//            }
//            //已经票到的金额
//            List<PaymentAdvanceApplyDetailPO> codDetailList = baseMapper.findCodApplyByPoKey(poKeyList);
//            if(CollectionUtils.isNotEmpty(codDetailList)){
//                for(PaymentAdvanceApplyDetailPO detail:codDetailList){
//                    for(CreatePaaDetailPO dto: params){
//                        if(dto.getPoKey().equals(detail.getDataKey())){
//                            dto.setAlreadyApplyAmount(dto.getAlreadyApplyAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
////                            dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount()));
//                            dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount())
//                                    .subtract(dto.getAlreadyAccountAmount()==null?BigDecimal.ZERO:dto.getAlreadyAccountAmount())
//                                    .add(dto.getAlreadyOutAmount()==null?BigDecimal.ZERO:dto.getAlreadyOutAmount())
//                                    .subtract(dto.getAlreadyIntoAmount()==null?BigDecimal.ZERO:dto.getAlreadyIntoAmount())
//                            );
//                            dto.setBudInnerMoney(dto.getBudInnerMoney());
//                            dto.setBudOutMoney(dto.getBudOutMoney());
//                            //已付款金额
//                            if(detail.getStatus() == 5){
//                                dto.setAlreadyAdvanceAmount(dto.getAlreadyAdvanceAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
//                            }
//
//                        }
//                    }
//                }
//            }
//
//            // 临时存放所有的预付款申请明细
//            List<CreatePaaDetailPO> tempList = Lists.newArrayList();
//            tempList.addAll(params);
//
//            for(CreatePaaDetailPO dto: tempList){
//                // 本次可申请金额小于0的明细行需要删除
//                if(dto.getApplyAmount().compareTo(BigDecimal.ZERO) == 0){
//                    params.remove(dto);
//                }
//            }
//        }
//
//        // 编辑是检查采购订单行在SRM中使用的情况
//        // 1.考虑编辑不需要查本事
//        // 2.临时删除的明细ID需要排除
//        if(applyId > 0){
//            final List<PaymentAdvanceApplyDetailPO> paaDetailList = baseMapper.editGetPoSituationByApplyId(existedItemIdList, poKeyList);
//            for(CreatePaaDetailPO dto: params){
//                for(PaymentAdvanceApplyDetailPO detail: paaDetailList){
//                    if(dto.getPoKey().equals(detail.getDataKey())){
//                        //已申请金额
//                        dto.setAlreadyApplyAmount(dto.getAlreadyApplyAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
//                        //可申请金额
////                        dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount()));
//                        dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount())
//                                .subtract(dto.getAlreadyAccountAmount()==null?BigDecimal.ZERO:dto.getAlreadyAccountAmount())
//                                .add(dto.getAlreadyOutAmount()==null?BigDecimal.ZERO:dto.getAlreadyOutAmount())
//                                .subtract(dto.getAlreadyIntoAmount()==null?BigDecimal.ZERO:dto.getAlreadyIntoAmount())
//                        );
//                        //已付款金额
//                        if(detail.getStatus() == 5){
//                            dto.setAlreadyAdvanceAmount(dto.getAlreadyAdvanceAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
//                        }
//                    }
//                }
//            }
//            //已经票到的金额
//            List<PaymentAdvanceApplyDetailPO> codDetailList = baseMapper.findCodApplyByPoKey(poKeyList);
//            if(CollectionUtils.isNotEmpty(codDetailList)){
//                for(PaymentAdvanceApplyDetailPO detail:codDetailList){
//                    for(CreatePaaDetailPO dto: params){
//                        if(dto.getPoKey().equals(detail.getDataKey())){
//                            dto.setAlreadyApplyAmount(dto.getAlreadyApplyAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
//                            dto.setApplyAmount(dto.getItAmount().subtract(dto.getAlreadyApplyAmount()));
//                            //已付款金额
//                            if(detail.getStatus() == 5){
//                                dto.setAlreadyAdvanceAmount(dto.getAlreadyAdvanceAmount().add(detail.getBudInnerMoney()).add(detail.getBudOutMoney()));
//                            }
//
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//    @Override
//    public List<PaymentAdvanceApplyDetailPO> findCodApplyByPoKey(List<String> poKeyList) {
//        return baseMapper.findCodApplyByPoKey(poKeyList);
//    }
//
//    /**
//     * 查下货到中已经预付的数据
//     *
//     * @param applyData
//     * @return
//     */
//    @Override
//    public List<CreatePcaDetailPO> findByAlreadyData(List<CreatePcaDetailPO> applyData) {
//        return baseMapper.findByAlreadyData(applyData);
//    }
//
    /**
     * 更新明细行号问题
     *
     * @param applyId 申请ID
     */
    @Override
    public void updateDetailItemNo(Long applyId) {
        final List<ContractPaymentApplyDetail> detailList = baseMapper.findByCppIdOrCppNo(applyId, "");
        List<ContractPaymentApplyDetail> updateList = Lists.newArrayList();
        int i = 1;
        for(ContractPaymentApplyDetail detail: detailList){
            ContractPaymentApplyDetail updateAsd = new ContractPaymentApplyDetail();
            updateAsd.setId(detail.getId());
            updateAsd.setItemNo(StringUtils.leftPad(String.valueOf(i), 5, "0"));
            updateList.add(updateAsd);
            i++;
        }
        this.updateBatchById(updateList);
    }
//
//
//    @Override
//    public List<PaymentCodApplyDetail> findByPaymentDetail(List<Long> appIdList) {
//        return baseMapper.findByPaymentDetail(appIdList);
//    }

    /**
     * 根据MRO付款申请ID删除货到付款明细
     *
     * @param applyIdList 付款申请ID集合
     */
    @Override
    public void delDetailByApplyId(List<Long> applyIdList) {
        baseMapper.delDetailByApplyId(applyIdList);
    }


    public List<String> getCostCenterCodeByContractNo(String contractNo) {
        return baseMapper.getCostCenterCodeByContractNo(contractNo);
    }

//    @Override
//    public List<CreatePcaDetailPO> findAdvanceApplyDataByDataKey(List<String> dataKeys) {
//        return baseMapper.findAdvanceApplyDataByDataKey(dataKeys);
//    }
}
