/**
 * @filename:!BillingDisbursementServiceImpl 2024-10-28
 * @project KJT v1.1.0 Copyright(c) 2024 wanglang Co. Ltd. All right reserved.
 */
package com.trade.service.finance.disbursement.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.common.core.page.GridPage;
import com.trade.common.utils.DateUtils;
import com.trade.common.utils.LoggerUtil;
import com.trade.service.enterprise.customer.domain.Enterprise;
import com.trade.service.enterprise.customer.service.EnterpriseBaseService;
import com.trade.service.finance.constant.BillingConstant;
import com.trade.service.finance.disbursement.domain.BillingDisbursement;
import com.trade.service.finance.disbursement.dto.DisbursementSearchDto;
import com.trade.service.finance.disbursement.dto.ExportTaxRefundSelfDto;
import com.trade.service.finance.disbursement.dto.ReceivePaymentSearchDto;
import com.trade.service.finance.disbursement.mapper.BillingDisbursementMapper;
import com.trade.service.finance.disbursement.service.BillingDisbursementService;
import com.trade.service.finance.disbursement.vo.BillingDisbursementPageVo;
import com.trade.service.finance.disbursement.vo.ReceivePaymentPageVo;
import com.trade.service.finance.paymentcollection.domain.OrderIncomeAssociation;
import com.trade.service.trade.salescontract.dto.SalesContractDto;
import com.trade.service.trade.salescontract.service.SalesContractService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:出账表服务实现
 * @version: v1.1.0
 * @author: wanglang
 */
@Service
public class BillingDisbursementServiceImpl extends ServiceImpl<BillingDisbursementMapper, BillingDisbursement> implements BillingDisbursementService {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);;
    @Resource
    private SalesContractService salesContractService;
    @Resource
    private EnterpriseBaseService enterpriseBaseService;

    @Override
    public GridPage<BillingDisbursementPageVo> queryListByPage(DisbursementSearchDto searchDto) {
        Page<BillingDisbursementPageVo> page = PageHelper.startPage(searchDto.getPage(), searchDto.getRows());
        List<BillingDisbursementPageVo> voList = this.baseMapper.selectByPage(searchDto);
        return new GridPage<>(searchDto.getPage(), searchDto.getRows(), page.getTotal(), voList);
    }

    @Override
    public List<ReceivePaymentPageVo> queryPageForCustomer(ReceivePaymentSearchDto searchDto) {
        return this.baseMapper.selectByPageForCustomer(searchDto);
    }

    @Override
    public boolean saveSelfTaxRefund(List<ExportTaxRefundSelfDto> selfDtos) {
        if (CollectionUtil.isEmpty(selfDtos)) {
            return false;
        }
        for (ExportTaxRefundSelfDto selfDto : selfDtos) {
            SalesContractDto salesContractDto = salesContractService.getSalesContractById(selfDto.getSaleContractId());
            if (salesContractDto == null) {
                log.error("销售合同id={}合同不存在", selfDto.getSaleContractId());
                continue;
            }
            BillingDisbursement billingDisbursement = new BillingDisbursement();
            billingDisbursement.setSalesContractNo(salesContractDto.getContractNo());
            billingDisbursement.setCustomerUserId(salesContractDto.getCustomerUserId());
            billingDisbursement.setPaymentAmount(selfDto.getRefundAmount());
            billingDisbursement.setPaymentCurrency("CNY");
            billingDisbursement.setAgencyCertNo(salesContractDto.getAgencyCertNo());
            billingDisbursement.setAgencyEnterpriseName(salesContractDto.getAgencyEnterpriseName());
            // 自营的是没有代理公司信息的 根据需求页面 设置这里为"税务局"
            billingDisbursement.setPayerName("税务局");
            billingDisbursement.setImporterCertNo(salesContractDto.getImporterCertNo());
            billingDisbursement.setImporterEnterpriseName(salesContractDto.getImporterEnterpriseName());
            billingDisbursement.setPaymentType(BillingConstant.PAYMENT_TYPE_TAX_REFUND);
            billingDisbursement.setStatus(BillingConstant.DISBURSEMENT_STATUS_CONFIRMED);
            billingDisbursement.setSource(2);
            billingDisbursement.setPayeeType(BillingConstant.PAYEE_TYPE_CUSTOMER);
            Enterprise enterprise = enterpriseBaseService
                    .getEnterpriseByCustomerId(salesContractDto.getCustomerUserId());
            if (enterprise != null) {
                billingDisbursement.setTradeParticipantsIdentifier(enterprise.getName());
            }
            billingDisbursement.setPayerType(BillingConstant.PAYERTYPE_TAXATION_BUREAU);
            billingDisbursement.setPaymentDate(DateUtils.parseDate2LocalDate(selfDto.getRzrq()));
            billingDisbursement.setBusinessNumber(selfDto.getCustomsDeclarationNo());
            this.save(billingDisbursement);
        }
        return true;
    }

    @Override
    public Long saveOrderDisbursement(OrderIncomeAssociation orderIncomeAssociation) {
        BillingDisbursement billingDisbursement = new BillingDisbursement();
        SalesContractDto salesContractDto = salesContractService
                .getSalesContractById(orderIncomeAssociation.getSaleContractId());
        billingDisbursement.setSalesContractNo(salesContractDto.getContractNo());
        billingDisbursement.setCustomerUserId(salesContractDto.getCustomerUserId());
        billingDisbursement.setPaymentAmount(orderIncomeAssociation.getReceivePayment());
        billingDisbursement.setPaymentCurrency(orderIncomeAssociation.getCurrency());
        billingDisbursement.setAgencyCertNo(salesContractDto.getAgencyCertNo());
        billingDisbursement.setAgencyEnterpriseName(salesContractDto.getAgencyEnterpriseName());
        billingDisbursement.setPayerName(salesContractDto.getImporterEnterpriseName());
        billingDisbursement.setImporterCertNo(salesContractDto.getImporterCertNo());
        billingDisbursement.setImporterEnterpriseName(salesContractDto.getImporterEnterpriseName());
        billingDisbursement.setPaymentType(orderIncomeAssociation.getPaymentType());
        billingDisbursement.setStatus(BillingConstant.DISBURSEMENT_STATUS_CONFIRMED);
        billingDisbursement.setSource(orderIncomeAssociation.getSource());
        billingDisbursement.setPayeeType(BillingConstant.PAYEE_TYPE_CUSTOMER);
        Enterprise enterprise = enterpriseBaseService
                .getEnterpriseByCustomerId(salesContractDto.getCustomerUserId());
        if (enterprise != null) {
            billingDisbursement.setTradeParticipantsIdentifier(enterprise.getName());
        }
        billingDisbursement.setPayerType(BillingConstant.PAYERTYPE_IMPORTER);
        billingDisbursement.setPaymentDate(orderIncomeAssociation.getExpectedPaymentDueDate());
        billingDisbursement.setBusinessNumber(orderIncomeAssociation.getVoucherNo());
        this.save(billingDisbursement);
        return billingDisbursement.getId();
    }
}
