package org.jeecg.modules.orderplatform.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.ApprovalNodeNoticeHandler;
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;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private IPlatformOrderService platformOrderService;

    @Autowired
    private IPlatformMaterialService platformMaterialService;

    @Autowired
    private OperateRecordHandler operateRecordHandler;

    @Autowired
    private ApprovalNodeNoticeHandler approvalNodeNoticeHandler;

    /**
     * 设计组长接单，根据订单中的物料数量按类型发起物料审批流程
     */
    @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");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_ORDER("ACCEPT_ORDER",platformOrder.getOrderCode());
            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");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_ORDER("REJECT_ORDER",platformOrder.getOrderCode());
            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;
    }

    /**
     *   设计组长批量派单
     */
    @PostMapping("/batchAssign")
    public Result<Map<String,Object>> batchAssign(@RequestBody JSONObject params) {
        Result<Map<String,Object>> result = new Result<>();
        Map<String,Object> assignResult= new HashMap<>();
        JSONArray ids=params.getJSONArray("id");
        PlatformMaterial platformMaterial= new PlatformMaterial(params.getString("plan"),params.getString("shot"),params.getString("clip"));
        ids.forEach(id->{
            //物料状态->脚本待上传
            platformMaterial.setId(Long.valueOf(id.toString()));
            platformMaterial.setMaterialStatus(1);
            platformMaterial.setScriptStatus(0);
            boolean ok = platformMaterialService.updateById(platformMaterial);
            if (ok) {
                assignResult.put(platformMaterial.getId().toString(),"物料派单成功");
            }else {
                assignResult.put(platformMaterial.getId().toString(),"物料派单失败");
            }
        });
        result.setResult(assignResult);
        result.setSuccess(true);
        return result;
    }

    /**
     *  设计组长修改指派人
     */
    @PostMapping("/assignAgain")
    public Result<Object> assignAgain(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        boolean ok = platformMaterialService.updateById(platformMaterial);
        if (ok) {
            result.success("物料重新指派成功!");
        }else {
            result.error500("物料重新指派失败，请联系管理员!");
        }
        return result;
    }
}
