package org.jeecg.modules.orderplatform.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.orderplatform.entity.PlatformOrder;
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 static org.jeecg.modules.orderplatform.util.CodeBuilder.generateCode;

@Slf4j
@RestController
@RequestMapping("/platform/operator")
public class OperatorApprovalProcessCtrl {

    @Autowired
    private IPlatformOrderService platformOrderService;

    @Autowired
    private OperateRecordHandler operateRecordHandler;

    @Autowired
    private ApprovalNodeNoticeHandler approvalNodeNoticeHandler;

    /**
     * 运营下单
     */
    @PostMapping(value = "/payout")
    public Result<PlatformOrder> payout(@RequestBody PlatformOrder platformOrder) {
        Result<PlatformOrder> result = new Result<>();
        try {
            //订单状态->订单开启
            platformOrder.setOrderStatus(0);
            platformOrder.setOrderCode(generateCode());
            boolean save = platformOrderService.save(platformOrder);
            if (save) {
                operateRecordHandler.Submit(platformOrder.getOrderCode(), "PAYOUT");
                approvalNodeNoticeHandler.NOTICE_WE_CHAT_ORDER("PAYOUT",platformOrder.getOrderCode());
                result.setResult(platformOrderService.queryByCode(platformOrder.getOrderCode()));
                result.success("下单成功！");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("下单失败");
        }
        return result;
    }

    /**
     * 驳回修改后重新发起订单
     */
    @PostMapping(value = "/rePayout")
    public Result<PlatformOrder> rePayout(@RequestBody PlatformOrder platformOrder) {
        Result<PlatformOrder> result = new Result<PlatformOrder>();
        platformOrder.setOrderStatus(0);
        boolean ok = platformOrderService.updateById(platformOrder);
        if (ok) {
            operateRecordHandler.Submit(platformOrder.getOrderCode(), "PAYOUT");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_ORDER("PAYOUT",platformOrder.getOrderCode());
            result.success("订单重新提交成功!");
        }else {
            result.error500("订单重新提交异常，请联系管理员");
        }

        return result;
    }

    /**
     * 订单发起人可以将订单作废
     */
    @PostMapping(value = "/destroy")
    public Result<Object> destroy(@RequestBody PlatformOrder platformOrder) {
        Result<Object> result = new Result<>();
        //订单状态->订单作废
        platformOrder.setOrderStatus(-1);
        boolean ok = platformOrderService.updateById(platformOrder);
        if (ok) {
            operateRecordHandler.Approval(platformOrder.getOrderCode(), "DESTROY_ORDER");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_ORDER("DESTROY_ORDER",platformOrder.getOrderCode());
            result.success("接单已销毁!");
        } else {
            result.error500("订单销毁失败，请联系管理员!");
        }
        return result;
    }
}
