package com.fjhx.sale.flow;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.service.account.AccountRunningWaterService;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.flow.enums.FlowStatusEnum;
import com.fjhx.sale.entity.claim.po.Claim;
import com.fjhx.sale.entity.claim.po.ClaimContract;
import com.fjhx.sale.service.claim.ClaimContractService;
import com.fjhx.sale.service.claim.ClaimService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 外销合同流程
 *
 * @Author:caozj
 * @DATE:2023/4/3 17:38
 */
@Component
//@DS(SourceConstant.SALE)
public class ClaimDelFlow extends FlowDelegate {

    @Autowired
    private ClaimService claimService;

    @Autowired
    private AccountRunningWaterService accountRunningWaterService;

    @Autowired
    private ClaimContractService claimContractService;

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

    /**
     * 发起流程
     *
     * @param flowId     流程ID
     * @param submitData
     * @return 业务id
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        AccountRunningWater water = submitData.toJavaObject(AccountRunningWater.class);
        return water.getId();
    }

    /**
     * 结束流程
     *
     * @param flowId     流程ID
     * @param businessId 业务ID
     * @param submitData 数据
     */
    @Override
    @DSTransactional
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        //通过流水ID查找出到账认领的数据
        List<Claim> claimList = claimService.list(Wrappers.<Claim>query().lambda().eq(Claim::getBusinessId, businessId));
        if (CollectionUtils.isEmpty(claimList)) {
            throw new ServiceException("当前流水暂无认领合同,不能取消认领");
        }
        List<Long> claimIds = claimList.stream().map(Claim::getId).collect(Collectors.toList());
        claimService.removeBatchByIds(claimIds);
        claimContractService.remove(Wrappers.<ClaimContract>query().lambda().in(ClaimContract::getClaimId, claimIds));
        //更新流水数据为认领状态---已认领金额还原
        AccountRunningWater water = new AccountRunningWater();
        water.setCompanyId(SecurityUtils.getCompanyId());

        water.setId(businessId);
        water.setIsClaim(0);//未认领状态
        water.setClaimAmount(BigDecimal.ZERO);
        accountRunningWaterService.updateById(water);
    }

    /**
     * 重新发起
     * @param flowId
     * @param businessId
     * @param flowStatus
     * @param submitData
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void relaunch(Long flowId, Long businessId, FlowStatusEnum flowStatus, JSONObject submitData) {

    }

    /**
     * 驳回
     * @param flowId
     * @param businessId
     * @param flowStatus
     */
    @Override
    public void reject(Long flowId, Long businessId, FlowStatusEnum flowStatus) {

    }

    /**
     * 退回发起人
     * @param flowId
     * @param businessId
     * @param flowStatus
     */
    @Override
    public void cancellation(Long flowId, Long businessId, FlowStatusEnum flowStatus) {

    }
}
