/**
 * @filename:PaymentBasicServiceImpl 2024-10-24
 * @project KJT v1.1.0 Copyright(c) 2024 wanglang Co. Ltd. All right reserved.
 */
package com.trade.admin.business.payment.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.admin.business.payment.domain.PaymentBasic;
import com.trade.admin.business.payment.domain.PaymentBasicInfo;
import com.trade.admin.business.payment.dto.PaymentApproveDto;
import com.trade.admin.business.payment.dto.PaymentExtendDto;
import com.trade.admin.business.payment.dto.PaymentSearchDto;
import com.trade.admin.business.payment.mapper.PaymentBasicMapper;
import com.trade.admin.business.payment.service.PaymentBasicInfoService;
import com.trade.admin.business.payment.service.PaymentBasicService;
import com.trade.admin.business.payment.vo.PaymentApproveVo;
import com.trade.admin.business.payment.vo.PaymentDetailPageVo;
import com.trade.admin.business.payment.vo.PaymentOperatePageVo;
import com.trade.admin.business.payment.vo.PaymentPageVo;
import com.trade.admin.finance.disbursement.service.impl.BillingDisbursementExternal;
import com.trade.admin.system.security.utils.SecurityUtils;
import com.trade.common.core.page.GridPage;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.StringUtils;
import com.trade.common.utils.bean.BeanCopyUtils;
import com.trade.service.business.separateaccounts.domain.SeparateAccounts;
import com.trade.service.business.separateaccounts.domain.SeparateAccountsInfo;
import com.trade.service.filestorage.entity.BusinessRelateFile;
import com.trade.service.filestorage.enums.FileTypeEnum;
import com.trade.service.filestorage.service.BusinessRelateFileBaseService;
import com.trade.service.finance.constant.BillingConstant;
import com.trade.service.finance.paymentcollection.domain.BillingIncomeInfo;
import com.trade.service.finance.paymentcollection.domain.PaymentCollectionOperate;
import com.trade.service.finance.paymentcollection.service.BillingIncomeInfoService;
import com.trade.service.finance.paymentcollection.service.PaymentCollectionOperateService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description:支付基础表服务实现
 * @version: v1.1.0
 * @author: wanglang
 */
@Service
public class PaymentBasicServiceImpl extends ServiceImpl<PaymentBasicMapper, PaymentBasic> implements PaymentBasicService {

    @Resource
    private PaymentCollectionOperateService paymentCollectionOperateService;
    @Resource
    private PaymentBasicInfoService paymentBasicInfoService;
    @Resource
    private BillingIncomeInfoService billingIncomeInfoService;
    @Resource
    private BusinessRelateFileBaseService businessRelateFileBaseService;
    @Resource
    private BillingDisbursementExternal billingDisbursementExternal;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean savePaymentBasic(SeparateAccounts separateAccounts, SeparateAccountsInfo separateAccountsInfo,
            String operator) {
        PaymentBasic paymentBasic = BeanCopyUtils.copy(separateAccounts, PaymentBasic.class);
        paymentBasic.setApplyDate(LocalDateTime.now());
        paymentBasic.setId(null);
        paymentBasic.setBusinessNumber(String.valueOf(separateAccounts.getBillingIncomeId()));
        paymentBasic.setSeparateAccountsInfoId(separateAccountsInfo.getId());
        paymentBasic.setStatus(BillingConstant.PAYMENT_STATUS_UNAPPROVED);
        paymentBasic.setPaymentAmount(separateAccountsInfo.getLedgerAmount());
        paymentBasic.setPaymentCurrency(separateAccountsInfo.getLedgerCurrency());
        this.save(paymentBasic);
        PaymentBasicInfo paymentBasicInfo = new PaymentBasicInfo();
        paymentBasicInfo.setPaymentBasicId(paymentBasic.getId());
        paymentBasicInfo.setPaymentDate(separateAccountsInfo.getPlanEntryDate());
        paymentBasicInfo.setPaymentAmount(separateAccountsInfo.getLedgerAmount());
        paymentBasicInfo.setPaymentCurrency(separateAccountsInfo.getLedgerCurrency());
        paymentBasicInfo.setTradeParticipantsIdentifier(separateAccountsInfo.getTradeParticipantsIdentifier());
        paymentBasicInfo.setTradeParticipantsName(separateAccountsInfo.getTradeParticipantsName());
        BillingIncomeInfo billingIncomeInfo = billingIncomeInfoService
                .getBillingIncomeById(separateAccounts.getBillingIncomeId());
        paymentBasicInfo.setAccountName(billingIncomeInfo.getAccountName());
        paymentBasicInfo.setAccountNumber(billingIncomeInfo.getAccountNumber());
        paymentBasicInfo.setBankName(billingIncomeInfo.getBankName());
        paymentBasicInfo.setOpeningBank(billingIncomeInfo.getOpeningBank());
        paymentBasicInfoService.save(paymentBasicInfo);
        PaymentCollectionOperate paymentCollectionOperate = new PaymentCollectionOperate(paymentBasic.getId(), 4);
        paymentCollectionOperate.setApplicant(operator);
        paymentCollectionOperate.setApplyDate(LocalDateTime.now());
        paymentCollectionOperateService.save(paymentCollectionOperate);
        return true;
    }

    @Override
    public GridPage<PaymentPageVo> queryListByPage(PaymentSearchDto searchDto) {
        Page<PaymentPageVo> page = PageHelper.startPage(searchDto.getPage(), searchDto.getRows());
        List<PaymentPageVo> voList = this.baseMapper.selectByPage(searchDto);
        for (PaymentPageVo paymentPageVo : voList) {
            PaymentBasicInfo paymentBasicInfo = paymentBasicInfoService.getByCodePaymentBasicId(paymentPageVo.getId());
            paymentPageVo.setPaymentAmount(paymentBasicInfo.getPaymentAmount());
            paymentPageVo.setPaymentCurrency(paymentBasicInfo.getPaymentCurrency());
        }
        return new GridPage<>(searchDto.getPage(), searchDto.getRows(), page.getTotal(), voList);
    }

    @Override
    public PaymentOperatePageVo getPaymentOperatePage(Long id) {
        PaymentBasic paymentBasic = this.baseMapper.selectById(id);
        if (paymentBasic == null) {
            throw new ServiceException("未找到该出支付记录");
        }
        PaymentBasicInfo paymentBasicInfo = paymentBasicInfoService.getByCodePaymentBasicId(paymentBasic.getId());
        PaymentOperatePageVo paymentOperatePageVo = new PaymentOperatePageVo();
        paymentOperatePageVo.setPaymentBasic(paymentBasic);
        paymentOperatePageVo.setPaymentBasicInfo(paymentBasicInfo);
        BillingIncomeInfo billingIncomeInfo = billingIncomeInfoService.getById(paymentBasic.getTransactionNumber());
        PaymentCollectionOperate paymentCollectionOperate = paymentCollectionOperateService
                .getBusinessInfo(billingIncomeInfo.getId(), 1);
        PaymentExtendDto paymentExtendDto = new PaymentExtendDto();
        paymentExtendDto.setApplicant(paymentCollectionOperate.getReviewer());
        paymentExtendDto.setRemark(paymentCollectionOperate.getReviewRemark());
        paymentExtendDto.setBusinessNumber(paymentBasic.getBusinessNumber());
        BusinessRelateFile businessRelateFile = businessRelateFileBaseService.getByCondition(paymentBasicInfo.getId(),
                BillingConstant.ERECEIVE_PAYMENT_VOUCHER, FileTypeEnum.DISBURSEMENT_ENTRY.getCode());
        if (businessRelateFile == null) {
            businessRelateFileBaseService.fillBizObjFiles(paymentExtendDto, billingIncomeInfo.getId(),
                    BillingConstant.ERECEIVE_PAYMENT_VOUCHER, FileTypeEnum.DISBURSEMENT_ENTRY.getCode());
        } else {
            businessRelateFileBaseService.fillBizObjFiles(paymentExtendDto, paymentBasicInfo.getId(),
                    BillingConstant.ERECEIVE_PAYMENT_VOUCHER, FileTypeEnum.DISBURSEMENT_ENTRY.getCode());
        }
        paymentOperatePageVo.setPaymentExtendDto(paymentExtendDto);
        return paymentOperatePageVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean approvePayment(PaymentApproveDto approveDto) {
        PaymentBasic paymentBasic = this.baseMapper.selectById(approveDto.getId());
        if (paymentBasic == null) {
            throw new ServiceException("未找到该出支付记录");
        }
        paymentBasic.setStatus(approveDto.getStatus());
        this.baseMapper.updateById(paymentBasic);
        String userNickName = SecurityUtils.getUserNickName();
        PaymentCollectionOperate paymentCollectionOperate = paymentCollectionOperateService
                .getBusinessInfo(paymentBasic.getId(), 4);
        if (paymentCollectionOperate != null) {
            if (StringUtils.isEmpty(paymentCollectionOperate.getReviewer())) {
                paymentCollectionOperate.setReviewer(userNickName);
                paymentCollectionOperate.setReviewDate(LocalDateTime.now());
                paymentCollectionOperate.setReviewRemark(approveDto.getRemark());
                paymentCollectionOperateService.updateById(paymentCollectionOperate);
            } else {
                paymentCollectionOperate.setId(null);
                paymentCollectionOperate.setReviewer(userNickName);
                paymentCollectionOperate.setReviewDate(LocalDateTime.now());
                paymentCollectionOperate.setReviewRemark(approveDto.getRemark());
                paymentCollectionOperateService.save(paymentCollectionOperate);
            }
        } else {
            PaymentCollectionOperate paymentCollectionOperateSave = new PaymentCollectionOperate(paymentBasic.getId(),
                    4);
            paymentCollectionOperateSave.setReviewer(userNickName);
            paymentCollectionOperateSave.setReviewDate(LocalDateTime.now());
            paymentCollectionOperateSave.setReviewRemark(approveDto.getRemark());
            paymentCollectionOperateService.save(paymentCollectionOperate);
        }
        if (BillingConstant.PAYMENT_STATUS_CONFIRMED.equals(approveDto.getStatus())) {
            PaymentBasicInfo paymentBasicInfo = paymentBasicInfoService.getByCodePaymentBasicId(paymentBasic.getId());
            billingDisbursementExternal.saveDisbursement(paymentBasic, paymentBasicInfo, userNickName);
        }
        return true;
    }

    @Override
    public PaymentDetailPageVo getPaymentDetailPage(Long id) {
        PaymentOperatePageVo paymentOperatePageVo = getPaymentOperatePage(id);
        PaymentDetailPageVo paymentDetailPageVo = new PaymentDetailPageVo();
        paymentDetailPageVo.setPaymentBasic(paymentOperatePageVo.getPaymentBasic());
        paymentDetailPageVo.setPaymentBasicInfo(paymentOperatePageVo.getPaymentBasicInfo());
        paymentDetailPageVo.setPaymentExtendDto(paymentOperatePageVo.getPaymentExtendDto());
        PaymentCollectionOperate paymentCollectionOperate = paymentCollectionOperateService.getBusinessInfo(id, 4);
        if (paymentCollectionOperate != null) {
            PaymentApproveVo approveVo = new PaymentApproveVo();
            approveVo.setReviewer(paymentCollectionOperate.getReviewer());
            approveVo.setReviewDate(paymentCollectionOperate.getReviewDate());
            approveVo.setRemark(paymentCollectionOperate.getReviewRemark());
            approveVo.setStatus(paymentDetailPageVo.getPaymentBasic().getStatus());
            paymentDetailPageVo.setApproveVo(approveVo);
        }
        return paymentDetailPageVo;
    }
}
