package com.rongwei.bsserver.controller;


import com.rongwei.bscommon.sys.service.ApsBlankOrderService;
import com.rongwei.bsentity.domain.ApsBlankOrderDo;
import com.rongwei.bsentity.domain.ApsProductionTechnicalRequirementDo;
import com.rongwei.bsentity.vo.ApsPlanVo;
import com.rongwei.bsentity.vo.CheckAndSaveOrUpdateBlankReq;
import com.rongwei.bsentity.vo.OrderLockVo;
import com.rongwei.rwcommon.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 生产订单_坯料计划 前端控制器
 * </p>
 *
 * @author fpy
 * @since 2024-03-01
 */
@Slf4j
@RestController
@RequestMapping("/apsBlankOrder")
public class ApsBlankOrderController {

    @Autowired
    private ApsBlankOrderService apsBlankOrderService;

    /**
     * 订单批量排程
     *
     * @param apsPlanVo
     * @return
     */
    @PostMapping("/apsScheduling")
    public R apsScheduling(@RequestBody ApsPlanVo apsPlanVo) throws Exception {
        apsBlankOrderService.apsScheduling(apsPlanVo);
        return R.ok();
    }

    /**
     * 重新排程
     *
     * @return
     */
    @GetMapping("/apsReScheduling")
    public R apsReScheduling() throws Exception {
        apsBlankOrderService.apsReScheduling();
        return R.ok();
    }

    /**
     * 坯料计划锁定解锁
     *
     * @param
     * @return
     */
    @PostMapping("/orderLock")
    public R orderLock(@RequestBody OrderLockVo orderLockVo) throws Exception {
        return apsBlankOrderService.orderLock(orderLockVo);
    }

    /**
     * 订单排程取消
     *
     * @param
     * @return
     */
    @PostMapping("/orderApsCancel")
    public R orderApsCancel(@RequestBody List<String> apsProductionOrderIds) throws Exception {
        apsBlankOrderService.orderApsCancel(apsProductionOrderIds);
        return R.ok();
    }

    /**
     * 坯料计划排程取消
     *
     * @return
     */
    @PostMapping("/blankApsCancel")
    public R blankApsCancel(@RequestBody List<String> apsBlankIds) {
        apsBlankOrderService.blankApsCancel(apsBlankIds);
        return R.ok();
    }


    @PostMapping("/test")
    public R test() {
        apsBlankOrderService.test();
        return R.ok();
    }

    /**
     * 坯料计划发布
     *
     * @param blankIds
     * @return
     */
    @PostMapping("/publish")
    public R publish(@RequestBody List<String> blankIds) {
        log.info("发布id为: {}的坯料计划", blankIds);
        return apsBlankOrderService.publish(blankIds);
    }

    /**
     * 坯料计划取消发布
     *
     * @param blankIds
     * @return
     */
    @PostMapping("/publish/cancel")
    public R publishCancel(@RequestBody List<String> blankIds) {
        log.info("取消发布id为: {}的坯料计划", blankIds);
        return apsBlankOrderService.publishCancel(blankIds);
    }

    /*
    插单
     */
    @PostMapping("/insertOrder")
    public R insertOrder(@RequestBody Map<String, Object> params) {
        try {
            log.info("插单;参数为：{}", params);
            R resData = apsBlankOrderService.insertOrder(params);
            return resData;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("插单异常:" + e.getMessage());
            return R.error("插单异常:" + e.getMessage());
        }
    }

    /**
     * 校验和保存坯料计划
     */
    @PostMapping("/checkAndSaveOrUpdateBlank")
    public R checkAndSaveOrUpdateBlank(@RequestBody CheckAndSaveOrUpdateBlankReq req) {
        log.info("校验并保存坯料计划,入参为：{}", req);
        return apsBlankOrderService.checkAndSaveOrUpdateBlank(req);
    }

    /**
     * 取消坯料计划
     */
    @PostMapping("/removeBlankOrder")
    public R removeBlankOrder(@RequestBody ApsBlankOrderDo req) {
        log.info("removeBlankOrder 取消ID为：{}的坯料计划", req.getId());
        return apsBlankOrderService.removeBlankOrder(req);
    }

    /**
     * 保存技术要求之后，是否更新坯料计划的输出成品
     */
    @PostMapping("/checkAndUpdateBlankAndOutput")
    public R checkAndUpdateBlankAndOutput(@RequestBody ApsProductionTechnicalRequirementDo req) {
        try {
            log.info("checkAndUpdateBlankAndOutput 保存技术要求后更新，技术要求id为：{}相关的坯料计划和输出成品", req.getId());
            return apsBlankOrderService.checkAndUpdateBlankAndOutput(req);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("更新输出成品失败");
        }

    }

    /**
     * 根据坯料计划ID查询排程相关提醒
     */
    @PostMapping("/checkSchedule")
    public R checkSchedule(@RequestBody List<String> blankIdList) {
        log.info("checkSchedule 检查排程相关提醒，入参为：{}", blankIdList);
        return apsBlankOrderService.checkSchedule(blankIdList);
    }
}

