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.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.quotation.po.QuotationPay;
import com.fjhx.sale.entity.quotation.po.QuotationProduct;
import com.fjhx.sale.entity.sale.dto.SaleQuotationDto;
import com.fjhx.sale.entity.sale.po.SaleQuotation;
import com.fjhx.sale.enums.SaleQuotationEnum;
import com.fjhx.sale.service.quotation.QuotationPayService;
import com.fjhx.sale.service.quotation.QuotationProductService;
import com.fjhx.sale.service.sale.SaleQuotationService;
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 SaleQuotationFlow extends FlowDelegate {
    @Autowired
    private CodingRuleService codingRuleService;

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

    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 采购付款数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        SaleQuotationService saleQuotationService = SpringUtil.getBean(SaleQuotationService.class);
        QuotationPayService quotationPayService = SpringUtil.getBean(QuotationPayService.class);
        QuotationProductService quotationProductService = SpringUtil.getBean(QuotationProductService.class);
        SaleQuotationDto saleQuotation = submitData.toJavaObject(SaleQuotationDto.class);
        //赋值城市省份信息
        CustomizeAreaUtil.setAreaId(saleQuotation);
        saleQuotation.setBuyCityId(saleQuotation.getCityId());
        saleQuotation.setBuyCountryId(saleQuotation.getCountryId());
        saleQuotation.setBuyProvinceId(saleQuotation.getProvinceId());
        //添加报价编码
        saleQuotation.setCode(codingRuleService.createCode(CodingRuleEnum.SALE_QUOTATION.getKey(),saleQuotation.getBuyCorporationId()));
//        saleQuotation.setCode(CodeEnum.SALE_QUOTATION.getCode());
        //添加报价状态
        saleQuotation.setStatus(SaleQuotationEnum.UNDER_REVIEW.getKey());
        //添加报价单信息
        saleQuotationService.save(saleQuotation);
        List<QuotationProduct> quotationProductList = saleQuotation.getQuotationProductList();
        if(CollectionUtils.isNotEmpty(quotationProductList)){//保存报价产品信息
            quotationProductList.forEach(quotationProduct -> quotationProduct.setSaleQuotationId(saleQuotation.getId()));
            quotationProductService.saveBatch(quotationProductList);
        }
        List<QuotationPay> quotationPayList = saleQuotation.getQuotationPayList();
        if(CollectionUtils.isNotEmpty(quotationPayList)){//保存报价项目信息
           quotationPayList.forEach(quotationPay -> quotationPay.setSaleQuotationId(saleQuotation.getId()));
            quotationPayService.saveBatch(quotationPayList);
        }

        return saleQuotation.getId();
    }

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