package com.sd.business.flow;

import com.alibaba.fastjson.JSONObject;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.flow.enums.FlowStatusEnum;
import com.ruoyi.common.core.domain.BaseIdPo;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.common.utils.SecurityUtils;
import com.sd.business.entity.order.dto.OrderFlowExampleDto;
import com.sd.business.entity.order.dto.OrderInfoDto;
import com.sd.business.entity.order.enums.OrderStatusEnum;
import com.sd.business.entity.order.po.OrderFlowExample;
import com.sd.business.entity.order.po.OrderInfo;
import com.sd.business.service.order.OrderFlowExampleService;
import com.sd.business.service.order.OrderService;
import com.sd.business.service.production.ProductionTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 发起采购流程
 */
@Service
public class OrderFlow extends FlowDelegate {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderFlowExampleService orderFlowExampleService;

    @Autowired
    private ProductionTaskService productionTaskService;

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

    @Override
    public Long start(Long flowId, JSONObject submitData) {

        OrderInfoDto dto = submitData.toJavaObject(OrderInfoDto.class);
        dto.setFlowId(flowId);
        dto.setFlowStatus(FlowStatusEnum.IN_PROGRESS.getKey());

        if (dto.getId() == null) {
            orderService.add(dto);
        } else {
            orderService.edit(dto);
        }

        OrderFlowExampleDto orderFlowExample = new OrderFlowExampleDto();
        orderFlowExample.setOrderId(dto.getId());
        orderFlowExample.setFlowId(flowId);
        orderFlowExample.setFlowStatus(FlowStatusEnum.IN_PROGRESS.getKey());
        orderFlowExampleService.add(orderFlowExample);
        return dto.getId();
    }

    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        // 查询委外订单是否存在包材
        Boolean isExist = orderService.isExistOrderSkuBom(businessId);

        orderService.update(q -> q
                .eq(BaseIdPo::getId, businessId)
                .set(OrderInfo::getFlowStatus, FlowStatusEnum.PASS.getKey())
                // 委外订单不存在包材则直接修改为生产中
                .set(OrderInfo::getStatus, !isExist ? OrderStatusEnum.IN_PRODUCTION.getKey() : OrderStatusEnum.STOCK_PREPARATION.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
        );
        if (!isExist) {
            // 生成生产任务和工单
            productionTaskService.addTaskAndWorkOrder(businessId);
        }

        orderFlowExampleService.update(q -> q.eq(OrderFlowExample::getOrderId, businessId)
                .eq(OrderFlowExample::getFlowId, flowId)
                .set(OrderFlowExample::getFlowStatus, FlowStatusEnum.PASS.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId()));

    }

    @Override
    public void returnToOriginator(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
        orderService.update(q -> q
                .eq(BaseIdPo::getId, businessId)
                .set(OrderInfo::getFlowStatus, flowStatus.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
        );
        orderFlowExampleService.update(q -> q.eq(OrderFlowExample::getOrderId, businessId)
                .eq(OrderFlowExample::getFlowId, flowId)
                .set(OrderFlowExample::getFlowStatus, flowStatus.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId()));
    }

    @Override
    public void relaunch(Long flowId, Long businessId, FlowStatusEnum flowStatus, JSONObject submitData) {
        OrderInfoDto dto = submitData.toJavaObject(OrderInfoDto.class);
        dto.setFlowStatus(FlowStatusEnum.IN_PROGRESS.getKey());
        orderService.edit(dto);
        orderFlowExampleService.update(q -> q.eq(OrderFlowExample::getOrderId, businessId)
                .eq(OrderFlowExample::getFlowId, flowId)
                .set(OrderFlowExample::getFlowStatus, FlowStatusEnum.IN_PROGRESS.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId()));
    }

    @Override
    public void reject(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
        returnToOriginator(flowId, businessId, flowStatus);
    }

    @Override
    public void cancellation(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
        returnToOriginator(flowId, businessId, flowStatus);
    }

}