package com.fjhx.jxst.flow;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.enums.PushBusinessTypeEnum;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.jxst.entity.sales.dto.SalesContractDto;
import com.fjhx.jxst.entity.sales.po.SalesContractDetails;
import com.fjhx.jxst.service.sales.SalesContractDetailsService;
import com.fjhx.jxst.service.sales.SalesContractService;
import com.fjhx.mes.entity.work.dto.WorkOrderDto;
import com.fjhx.mes.service.work.WorkOrderService;
import com.fjhx.socket.core.PushTypeEnum;
import com.fjhx.socket.core.WebSocketPush;
import com.fjhx.wms.service.stock.StockService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
public class SalesContractFlow extends FlowDelegate {

    @Autowired
    private SalesContractService salesContractService;
    @Autowired
    private SalesContractDetailsService salesContractDetailsService;
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private StockService stockService;

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

    @Override
    public Long start(Long flowId, JSONObject submitData) {
        SalesContractDto salesContractDto = submitData.toJavaObject(SalesContractDto.class);
        salesContractDto.setFlowId(flowId);
        salesContractDto.setFlowKey(getFlowKey());
        salesContractService.add(salesContractDto);
        return salesContractDto.getId();
    }

    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        //按产品往“工单管理”中插入对应的工单数据
        List<SalesContractDetails> salesContractDetailsList = salesContractDetailsService.list(q -> q.eq(SalesContractDetails::getSalesContractId, businessId));
        for (SalesContractDetails salesContractDetails : salesContractDetailsList) {
            WorkOrderDto workOrder = new WorkOrderDto();
            workOrder.setSourceId(businessId);
            workOrder.setSource("sales_contract");//销售合同
            workOrder.setProductId(salesContractDetails.getProductId());
            workOrder.setQuantity(salesContractDetails.getQuantity());
            workOrder.setIsCustomized(salesContractDetails.getIsCustomized());
            workOrder.setProductionQuantity(workOrder.getQuantity());
            workOrder.setContractDetailsId(salesContractDetails.getId());

            //非定制工单查询库存如果存在库存需要人工干预是否需要生产
            BigDecimal availableStockQuantity = stockService.getAvailableStockQuantity(salesContractDetails.getProductId());
            if ("0".equals(salesContractDetails.getIsCustomized()) && availableStockQuantity.compareTo(BigDecimal.ZERO) > 0) {
                workOrder.setProductionQuantity(null);
            }

            //添加工单
            workOrderService.add(workOrder);
        }


        //给角色为研发的人发消息
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        List<Long> userIds = userService.getUserIdsByRoleKey("dev");
        DynamicDataSourceContextHolder.poll();
        WebSocketPush.byUsers(PushTypeEnum.MESSAGE, userIds, "您有新的工单待处理", PushBusinessTypeEnum.NEW_CONTRACT.getType());
    }

}
