package org.jeecg.modules.orderplatform.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.orderplatform.entity.PlatformMaterial;
import org.jeecg.modules.orderplatform.entity.PlatformOrder;
import org.jeecg.modules.orderplatform.service.IPlatformMaterialService;
import org.jeecg.modules.orderplatform.service.IPlatformOrderService;
import org.jeecg.modules.orderplatform.util.OperateRecordHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/platform/designer")
public class DesignerCommitProcessCtrl {

    @Autowired
    private IPlatformOrderService platformOrderService;

    @Autowired
    private IPlatformMaterialService platformMaterialService;

    @Autowired
    private OperateRecordHandler operateRecordHandler;

    /**
     * 设计组长接单，根据订单中的物料数量按类型发起物料审批流程
     */
    @PostMapping(value = "/accept")
    public Result<Object> accept(@RequestBody PlatformOrder platformOrder) {
        Result<Object> result = new Result<>();
        //订单状态->制作中
        platformOrder.setOrderStatus(1);
        boolean ok = platformOrderService.updateById(platformOrder);
        if (ok) {
            //自动分发订单，生成物料审批流程
            platformOrderService.generateMaterials(platformOrder);
            operateRecordHandler.Approval(platformOrder.getOrderCode(),"ACCEPT_ORDER");
            result.success("接单成功!");
        }else {
            result.error500("订单接单失败，请联系管理员!");
        }

        return result;
    }

    /**
     *  设计组长驳回订单，说明驳回理由
     */
    @PostMapping("/reject")
    public Result<Object> reject(@RequestBody PlatformOrder platformOrder) {
        Result<Object> result = new Result<>();
        //订单状态->驳回，可重新提交
        platformOrder.setOrderStatus(2);
        boolean ok = platformOrderService.updateById(platformOrder);
        if (ok) {
            operateRecordHandler.Approval(platformOrder.getOrderCode(),"REJECT_ORDER");
            result.success("订单已被驳回!");
        }else {
            result.error500("订单驳回失败，请联系管理员!");
        }
        return result;
    }

    /**
     *   设计组长派单,指定编导、拍摄、剪辑
     */
    @PostMapping("/assign")
    public Result<Object> assign(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        //物料状态->脚本待上传
        platformMaterial.setMaterialStatus(1);
        platformMaterial.setScriptStatus(0);
        boolean ok = platformMaterialService.updateById(platformMaterial);
        if (ok) {
            result.success("物料派单成功!");
        }else {
            result.error500("物料派单失败，请联系管理员!");
        }
        return result;
    }
}
