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.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fjhx.area.service.SetCustomizeAreaId;
import com.fjhx.area.utils.CustomizeAreaUtil;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.sale.entity.serviceContract.dto.ServiceContractDto;
import com.fjhx.sale.entity.serviceContract.po.ServiceContract;
import com.fjhx.sale.entity.serviceContract.po.ServiceContractPay;
import com.fjhx.sale.entity.serviceContract.po.ServiceContractProduct;
import com.fjhx.sale.enums.ServiceContractEnum;
import com.fjhx.sale.service.serviceContract.ServiceContractPayService;
import com.fjhx.sale.service.serviceContract.ServiceContractProductService;
import com.fjhx.sale.service.serviceContract.ServiceContractService;
import com.fjhx.sale.util.code.CodeEnum;
import com.obs.services.internal.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 服务合同流程
 */
@DS(SourceConstant.SALE)
@Component
public class ServiceContractFlow extends FlowDelegate {

    @Autowired
    private CodingRuleService codingRuleService;

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

    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 采购付款数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {


        ServiceContractService serviceContractService = SpringUtil.getBean(ServiceContractService.class);
        ServiceContractPayService serviceContractPayService = SpringUtil.getBean(ServiceContractPayService.class);
        ServiceContractProductService serviceContractProductService = SpringUtil.getBean(ServiceContractProductService.class);
        ServiceContractDto contract = submitData.toJavaObject(ServiceContractDto.class);

        //赋值城市省份信息
        CustomizeAreaUtil.setAreaId(contract);

        contract.setBuyCityId(contract.getCityId());
        contract.setBuyCountryId(contract.getCountryId());
        contract.setBuyProvinceId(contract.getProvinceId());

        //添加服务合同编码
        contract.setCode(codingRuleService.createCode(CodingRuleEnum.SERVICE_CONTRACT.getKey(),contract.getBuyCorporationId()));
//        contract.setCode(CodeEnum.SALE_QUOTATION.getCode());
        //添加服务合同状态
        contract.setStatus(ServiceContractEnum.UNDER_REVIEW.getKey());
        //添加服务合同信息
        serviceContractService.save(contract);
        List<ServiceContractProduct> serviceContractProductList = contract.getServiceContractProductList();
        if(CollectionUtils.isNotEmpty(serviceContractProductList)){//保存报价产品信息
            serviceContractProductList.forEach(serviceContractProduct -> serviceContractProduct.setServiceContractId(contract.getId()));
            serviceContractProductService.saveBatch(serviceContractProductList);
        }
        List<ServiceContractPay> serviceContractPayList = contract.getServiceContractPayList();
        if(CollectionUtils.isNotEmpty(serviceContractPayList)){//保存报价项目信息
            serviceContractPayList.forEach(contractPay -> contractPay.setServiceContractId(contract.getId()));
            serviceContractPayService.saveBatch(serviceContractPayList);
        }

        return contract.getId();
    }

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