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.sale.entity.contract.dto.ContractDto;
import com.fjhx.sale.entity.sample.dto.SampleDto;
import com.fjhx.sale.entity.sample.po.Sample;
import com.fjhx.sale.entity.sample.po.SampleProduct;
import com.fjhx.sale.entity.sample.po.SampleProject;
import com.fjhx.sale.entity.sample.po.SampleShipment;
import com.fjhx.sale.service.sample.SampleProductService;
import com.fjhx.sale.service.sample.SampleProjectService;
import com.fjhx.sale.service.sample.SampleService;
import com.fjhx.sale.service.sample.SampleShipmentService;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.sale.util.code.CodeEnum;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.enums.BusinessType;
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
 */
@Component
@DS(SourceConstant.SALE)
public class SampleFlow extends FlowDelegate {

    @Autowired
    private CodingRuleService codingRuleService;

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



    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 采购付款数据
     * @return
     */
    @Override
    @DSTransactional
    public Long start(Long flowId, JSONObject submitData) {
        SampleService sampleService = SpringUtil.getBean(SampleService.class);
        SampleProductService sampleProductService = SpringUtil.getBean(SampleProductService.class);
        SampleProjectService sampleProjectService = SpringUtil.getBean(SampleProjectService.class);
        SampleShipmentService sampleShipmentService = SpringUtil.getBean(SampleShipmentService.class);
//        Sample sample = submitData.toJavaObject(Sample.class);

        SampleDto sample = submitData.toJavaObject(SampleDto.class);
        //赋值城市省份信息
        CustomizeAreaUtil.setAreaId(sample);
        sample.setBuyCityId(sample.getCityId());
        sample.setBuyCountryId(sample.getCountryId());
        sample.setBuyProvinceId(sample.getProvinceId());
//        sample.setCode(CodeEnum.SAMPLE_CODE.getCode());
        sample.setCode(codingRuleService.createCode(CodingRuleEnum.SAMPLE.getKey(),sample.getBuyCorporationId()));
        sample.setStatus(FlowStatusEnum.UNDER_REVIEW.getKey());
        sample.setUserName(SecurityUtils.getUsername());

        sampleService.save(sample);
        List<SampleProduct> sampleProductList = sample.getSampleProductList();
        if(CollectionUtils.isNotEmpty(sampleProductList)){//保存样品单产品
            for(SampleProduct c : sampleProductList){
                c.setId(IdWorker.getId());
                c.setSampleId(sample.getId());
                c.setExpendQuantity(c.getQuantity());
                ObsFileUtil.saveFile(c.getFileList(),c.getId());
            }
            sampleProductService.saveBatch(sampleProductList);
        }
        List<SampleProject> sampleProjectList = sample.getSampleProjectList();
        if(CollectionUtils.isNotEmpty(sampleProjectList)){//保存收费项目
            for(SampleProject c : sampleProjectList){
                c.setSampleId(sample.getId());
            }
            sampleProjectService.saveBatch(sampleProjectList);
        }
        List<SampleShipment> sampleShipmentList = sample.getSampleShipmentList();
        if(CollectionUtils.isNotEmpty(sampleShipmentList)){//保存自定义出货
            for(SampleShipment c : sampleShipmentList){
                c.setSampleId(sample.getId());
            }
            sampleShipmentService.saveBatch(sampleShipmentList);
        }
        //交接单附件
        ObsFileUtil.saveFile(sample.getFileList(),sample.getId(),1);
        //包装附件
        ObsFileUtil.saveFile(sample.getPackageFileList(),sample.getId(),2);
        return sample.getId();

    }

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