//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.enums.FlowStatusEnum1;
//import com.fjhx.common.service.coding.CodingRuleService;
//import com.fjhx.flow.core.FlowDelegate;
//import com.fjhx.flow.core.FlowThreadLocalUtil;
//import com.fjhx.flow.enums.FlowStatusEnum;
//import com.fjhx.flow.enums.HandleTypeEnum;
//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.ruoyi.common.annotation.LogicIgnore;
//import com.ruoyi.common.core.domain.BasePo;
//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.util.Date;
//import java.util.List;
//
///**
// * 报价单流程
// */
////@DS(SourceConstant.SALE)
//@Component
//public class SaleQuotationFlow extends FlowDelegate {
//    @Autowired
//    private CodingRuleService codingRuleService;
//
//    @Autowired
//    private SaleQuotationService saleQuotationService;
//
//    @Autowired
//    private QuotationPayService quotationPayService;
//
//    @Autowired
//    private QuotationProductService quotationProductService;
//
//    @Override
//    public String getFlowKey() {
//        return "sale_quotation_flow";
//    }
//
//    /**
//     * 发起流程
//     * @param flowId 流程ID
//     * @param submitData 采购付款数据
//     * @return
//     */
//    @Override
//    public Long start(Long flowId, JSONObject submitData) {
//        SaleQuotationDto saleQuotation = submitData.toJavaObject(SaleQuotationDto.class);
//
//        //添加报价编码
//        saleQuotation.setCode(codingRuleService.createCode(CodingRuleEnum.SALE_QUOTATION.getKey(),saleQuotation.getBuyCorporationId()));
////        saleQuotation.setCode(CodeEnum.SALE_QUOTATION.getCode());
//        return connStart(saleQuotation);
//    }
//
//    /**
//     * 添加/修改报价单   公众代码
//     * @param saleQuotation
//     */
//    public Long connStart(SaleQuotationDto saleQuotation){
//        //赋值城市省份信息
//        CustomizeAreaUtil.setAreaId(saleQuotation);
//        saleQuotation.setBuyCityId(saleQuotation.getCityId());
//        saleQuotation.setBuyCountryId(saleQuotation.getCountryId());
//        saleQuotation.setBuyProvinceId(saleQuotation.getProvinceId());
//        //添加报价状态
//        saleQuotation.setStatus(SaleQuotationEnum.UNDER_REVIEW.getKey());
//        //添加报价单信息
//        saleQuotationService.saveOrUpdate(saleQuotation);
//        List<QuotationProduct> quotationProductList = saleQuotation.getQuotationProductList();
//        if(CollectionUtils.isNotEmpty(quotationProductList)){//保存报价产品信息
//            quotationProductList.forEach(quotationProduct -> quotationProduct.setSaleQuotationId(saleQuotation.getId()));
//            quotationProductService.saveOrUpdateBatch(quotationProductList);
//        }
//        List<QuotationPay> quotationPayList = saleQuotation.getQuotationPayList();
//        if(CollectionUtils.isNotEmpty(quotationPayList)){//保存报价项目信息
//            quotationPayList.forEach(quotationPay -> quotationPay.setSaleQuotationId(saleQuotation.getId()));
//            quotationPayService.saveOrUpdateBatch(quotationPayList);
//        }
//        return saleQuotation.getId();
//    }
//    /**
//     * 重新发起
//     * @param flowId
//     * @param businessId
//     * @param flowStatus
//     * @param submitData
//     */
//    @Override
//    @LogicIgnore
//    @Transactional(rollbackFor = Exception.class)
//    public void relaunch(Long flowId, Long businessId, FlowStatusEnum flowStatus, JSONObject submitData) {
//        //删除采购合同
//        SaleQuotationDto saleQuotationDto = submitData.toJavaObject(SaleQuotationDto.class);
//        if(ObjectUtils.isEmpty(saleQuotationDto)){
//            throw new ServiceException("报价单数据不能为空");
//        }
//        connStart(saleQuotationDto);
//    }
//
//    /**
//     * 驳回
//     * @param flowId
//     * @param businessId
//     * @param flowStatus
//     */
//    @Override
//    public void reject(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
//            saleQuotationService.update(q -> q
//                    .eq(SaleQuotation::getId, businessId)
//                    .set(SaleQuotation::getStatus, 20)//20为驳回
//                    .set(SaleQuotation::getUpdateTime, new Date())
//                    .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
//            );
//
//    }
//    /**
//     * 结束流程
//     * @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);
//    }
//
//    /**
//     * 驳回方法
//     */
//    public void reject() {
//        if (HandleTypeEnum.REJECT.equals(FlowThreadLocalUtil.getHandleTypeEnum())) {
//            saleQuotationService.update(q -> q
//                    .eq(SaleQuotation::getId, FlowThreadLocalUtil.getBusinessId())
//                    .set(SaleQuotation::getStatus, 20)//20为驳回
//                    .set(SaleQuotation::getUpdateTime, new Date())
//                    .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
//            );
//        }
//    }
//
//    /**
//     * 作废
//     */
//    @Override
//    public void cancellation(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
//        super.cancellation(flowId, businessId, flowStatus);
//
//        saleQuotationService.update(q -> q
//                .eq(SaleQuotation::getId, businessId)
//                .set(SaleQuotation::getStatus, FlowStatusEnum1.CANCELLATION.getKey())
//                .set(BasePo::getUpdateTime, new Date())
//                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
//        );
//    }
//}
