package com.fjhx.account.flow;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fjhx.account.entity.account.dto.AccountPaymentDto;
import com.fjhx.account.entity.account.dto.AccountRequestFundsDto;
import com.fjhx.account.entity.account.enums.AccountRequestFundsStatusEnum;
import com.fjhx.account.entity.account.enums.PaymentStatusEnum;
import com.fjhx.account.entity.account.enums.PaymentTypeEnum;
import com.fjhx.account.entity.account.po.AccountRequestFunds;
import com.fjhx.account.entity.account.po.AccountRequestFundsDetail;
import com.fjhx.account.service.account.AccountPaymentService;
import com.fjhx.account.service.account.AccountRequestFundsDetailService;
import com.fjhx.account.service.account.AccountRequestFundsService;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.flow.core.FlowDelegate;
import com.obs.services.internal.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  请款流程
 * @Author:caozj
 * @DATE:2023/4/3 17:38
 */
@DS(SourceConstant.ACCOUNT)
@Component
public class AccountRequestFundsFlow extends FlowDelegate {
    @Autowired
    private AccountPaymentService accountPaymentService;

    @Override
    public String getFlowKey() {
        return "account_request_funds_flow";
    }


    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 请款表信息
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
//        DynamicDataSourceContextHolder.push(SourceConstant.ACCOUNT);
        AccountRequestFundsService accountRequestFundsService = SpringUtil.getBean(AccountRequestFundsService.class);
        AccountRequestFundsDetailService accountRequestFundsDetailService = SpringUtil.getBean(AccountRequestFundsDetailService.class);
        AccountRequestFundsDto accountRequestFundsDto = submitData.toJavaObject( AccountRequestFundsDto.class);
        accountRequestFundsDto.setStatus(AccountRequestFundsStatusEnum.UNDER_REVIEW.getKey());
        //添加请款表的信息
        accountRequestFundsService.save(accountRequestFundsDto);

        //获取请款详情表的详细信息
        List<AccountRequestFundsDetail> accountRequestFundsDetailList = accountRequestFundsDto.getAccountRequestFundsDetailList();
        if(CollectionUtils.isNotEmpty(accountRequestFundsDetailList)){
            accountRequestFundsDetailList.forEach(accountRequestFundsDetail -> accountRequestFundsDetail
                    .setAccountRequestFundsId(accountRequestFundsDto.getId()));
            //添加请款详情表的信息
            accountRequestFundsDetailService.saveBatch(accountRequestFundsDetailList);
        }
        //添加附件信息
        ObsFileUtil.saveFile(accountRequestFundsDto.getFileList(),accountRequestFundsDto.getId());
        return accountRequestFundsDto.getId();
    }

    /**
     * 结束流程
     * @param flowId 流程ID
     * @param businessId 业务ID
     * @param submitData  请款表信息
     */
    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        AccountRequestFundsService accountRequestFundsService = SpringUtil.getBean(AccountRequestFundsService.class);
        //通过业务ID查询申购数据
        AccountRequestFunds accountRequestFunds = accountRequestFundsService.getById(businessId);
        AccountRequestFundsDto accountRequestFundsDto = submitData.toJavaObject( AccountRequestFundsDto.class);
        if(ObjectUtils.isEmpty(accountRequestFunds)){
            throw new ServiceException("请款表的数据不存在");
        }
        //修改申购状态为审批通过
        accountRequestFunds.setStatus(AccountRequestFundsStatusEnum.PASS.getKey());
        accountRequestFundsService.updateById(accountRequestFunds);
        //添加打款的数据
        addPayment(accountRequestFundsDto);
    }

    /**
     * 添加打款表的数据
     */
    private void addPayment(AccountRequestFundsDto accountRequestFundsDto){
        AccountPaymentDto accountPayment = new AccountPaymentDto();
        accountPayment.setBusinessId(accountRequestFundsDto.getId());
        accountPayment.setType(PaymentTypeEnum.UNDER_REVIEW.getKey());
        accountPayment.setStatus(PaymentStatusEnum.UNDER_REVIEW.getKey());
        accountPayment.setPaymentTime(accountPayment.getPaymentTime());
        accountPayment.setPaymentRemark(accountRequestFundsDto.getPaymentRemarks());

        accountPayment.setDepartmentId(accountRequestFundsDto.getDepartmentId());
        accountPayment.setCorporationId(accountRequestFundsDto.getCorporationId());
        accountPayment.setBusinessId(accountPayment.getId());
        accountPayment.setPaymentMethod(accountRequestFundsDto.getPaymentMethod());
        accountPayment.setIncomeAmount(accountRequestFundsDto.getTotal());
        accountPayment.setAmount(accountRequestFundsDto.getTotal());
        accountPayment.setName(accountRequestFundsDto.getName());
        accountPayment.setBusinessManagementId(accountRequestFundsDto.getAccountManagementId());
        accountPayment.setOpeningBank(accountRequestFundsDto.getOpeningBank());
        accountPayment.setCurrency(accountPayment.getCurrency());
        accountPayment.setAccountOpening(accountRequestFundsDto.getAccountOpening());
        accountPayment.setInterbankNumber(accountPayment.getInterbankNumber());
        accountPaymentService.save(accountPayment);
    }
}
