package com.fjhx.sale.flow;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fjhx.area.utils.CustomizeAreaUtil;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.enums.FlowStatusEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.sale.entity.contract.dto.ContractDto;
import com.fjhx.sale.entity.contract.po.Contract;
import com.fjhx.sale.entity.contract.po.ContractProduct;
import com.fjhx.sale.entity.contract.po.ContractProject;
import com.fjhx.sale.entity.contract.po.ContractShipment;
import com.fjhx.sale.entity.serviceContract.dto.ServiceContractDto;
import com.fjhx.sale.service.contract.ContractProductService;
import com.fjhx.sale.service.contract.ContractProjectService;
import com.fjhx.sale.service.contract.ContractService;
import com.fjhx.sale.service.contract.ContractShipmentService;
import com.fjhx.sale.util.code.CodeEnum;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

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

    @Autowired
    private CodingRuleService codingRuleService;

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



    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 采购付款数据
     * @return
     */
    @Override
    @DSTransactional
    public Long start(Long flowId, JSONObject submitData) {
        ContractService contractService = SpringUtil.getBean(ContractService.class);
        ContractProductService contractProductService = SpringUtil.getBean(ContractProductService.class);
        ContractProjectService contractProjectService = SpringUtil.getBean(ContractProjectService.class);
        ContractShipmentService contractShipmentService = SpringUtil.getBean(ContractShipmentService.class);
        ContractDto contract = submitData.toJavaObject(ContractDto.class);
        //赋值城市省份信息
        CustomizeAreaUtil.setAreaId(contract);
        contract.setCode(codingRuleService.createCode(CodingRuleEnum.CONTRACT.getKey(),contract.getBuyCorporationId()));
        contract.setUserName(SecurityUtils.getUsername());
        contract.setStatus(FlowStatusEnum.UNDER_REVIEW.getKey());
        contract.setBuyCityId(contract.getCityId());
        contract.setBuyCountryId(contract.getCountryId());
        contract.setBuyProvinceId(contract.getProvinceId());

        contractService.save(contract);
        List<ContractProduct> contractProductList = contract.getContractProductList();
        if(CollectionUtils.isNotEmpty(contractProductList)){//保存合同产品
            for(ContractProduct c : contractProductList){
                c.setId(IdWorker.getId());
                c.setContractId(contract.getId());
                c.setExpendQuantity(c.getQuantity());
                ObsFileUtil.saveFile(c.getFileList(),c.getId());
            }
            contractProductService.saveBatch(contractProductList);
        }
        List<ContractProject> contractProjectList = contract.getContractProjectList();
        if(CollectionUtils.isNotEmpty(contractProjectList)){//保存收费项目
            for(ContractProject c : contractProjectList){
                c.setContractId(contract.getId());
            }
            contractProjectService.saveBatch(contractProjectList);
        }
        List<ContractShipment> contractShipmentList = contract.getContractShipmentList();
        if(CollectionUtils.isNotEmpty(contractShipmentList)){//保存自定义出货
            for(ContractShipment c : contractShipmentList){
                c.setContractId(contract.getId());
            }
            contractShipmentService.saveBatch(contractShipmentList);
        }
        //交接单附件列表
        ObsFileUtil.saveFile(contract.getFileList(),contract.getId(),1);
        //包装指示附件列表
        ObsFileUtil.saveFile(contract.getPackageFileList(),contract.getId(),2);
        return contract.getId();
    }

    /**
     * 结束流程
     * @param flowId 流程ID
     * @param businessId 业务ID
     * @param submitData 数据
     */
    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        ContractService contractService = SpringUtil.getBean(ContractService.class);
        //通过业务ID查询合同数据
        Contract contract = contractService.getById(businessId);
        if(ObjectUtils.isEmpty(contract)){
            throw new ServiceException("合同不存在");
        }
        //修改采购状态为审批通过
        contract.setStatus(FlowStatusEnum.PASS.getKey());
        contract.setApprovedDate(new Date());
        contractService.updateById(contract);
    }
}
