package com.rongwei.bsserver.controller;


import com.rongwei.bscommon.sys.service.ApsProcessOperationService;
import com.rongwei.bsentity.domain.ApsProcessOperationOutMaterDo;
import com.rongwei.rwcommon.base.R;
import lombok.extern.slf4j.Slf4j;
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.List;
import java.util.Map;

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

    @Autowired
    private ApsProcessOperationService apsProcessOperationService;

    /*
    选择工艺路线自动生成工序作业
     */
    @PostMapping("/chooseCraftRoute")
    public R chooseCraftRoute(@RequestBody Map<String, Object> params) {
        try {
            log.info("选择工艺路线;参数为：{}", params);
            R resData = apsProcessOperationService.resetCraftRoute(params);
            return resData;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("选择工艺路线异常:" + e.getMessage());
            return R.error("选择工艺路线异常:" + e.getMessage());
        }
    }

    /*
    选择历史工艺路线自动生成工序作业
     */
    @PostMapping("/chooseHistoryRoute")
    public R chooseHistoryRoute(@RequestBody Map<String, Object> params) {
        try {
            log.info("选择历史工艺路线;参数为：{}", params);
            R resData = apsProcessOperationService.chooseHistoryRoute(params);
            return resData;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("选择历史工艺路线异常:" + e.getMessage());
            return R.error("选择历史工艺路线异常:" + e.getMessage());
        }
    }

    /*
    清除工艺路线
     */
    @PostMapping("/clearCraftRoute")
    public void clearCraftRoute(@RequestBody Map<String, Object> params) {
        try {
            log.info("清除工艺路线;参数为：{}", params);
            apsProcessOperationService.clearCraftRoute(params);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("清除工艺路线异常:" + e.getMessage());
        }
    }

    /**
     * 校验工序作业
     * 检查该工序作业所有输出物料，如果(计划输出卷数-计划备料卷数-计划成品卷数)不等于所有后道工序作业计划加工卷数合计；错误提示：{工序}{工序显示名}的{输出物料} 计划输出卷数-计划备料卷数-计划成品卷数必须等于所有后道工序作业计划加工卷数合计；
     */
    @PostMapping("/checkOperation")
    public R checkOperation(@RequestBody Map<String, Object> params) {
        try {
            log.info("校验工序作业;参数为：{}", params);
            return apsProcessOperationService.checkOperation(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("请检查工序作业详情");
        }
    }

    /*
    更新后面所有工序信息
     */
    @PostMapping("/resetProcessOperation")
    public R resetProcessOperation(@RequestBody List<ApsProcessOperationOutMaterDo> apsProcessOperationOutMaterDoList) {
        try {
            log.info("更新后面所有工序信息;参数为：{}", apsProcessOperationOutMaterDoList);
            apsProcessOperationService.resetProcessOperation(apsProcessOperationOutMaterDoList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("更新后面所有工序信息异常:" + e.getMessage());
            return R.error("更新后面所有工序信息异常:" + e.getMessage());
        }
    }

    /**
     * 坯料计划修改坯料需求后，更新工序作业相关信息
     */
    @PostMapping("/updateProcess")
    public R updateProcess(@RequestBody Map<String, Object> params) {
        try {
            log.info("更新坯料计划的工序;参数为：{}", params);
            return apsProcessOperationService.updateProcess(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("更新坯料计划的工序失败:" + e.getMessage());
        }
    }

    /**
     * 坯料计划使用备料保存后，更新被借调工序的所有后续工序
     */
    @PostMapping("/updateBorrowProcess")
    public R updateBorrowProcess(@RequestBody Map<String, Object> params) {
        try {
            log.info("更新被借调工序;参数为：{}", params);
            return apsProcessOperationService.updateBorrowProcess(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("更新被借调工序失败");
        }
    }

    /**
     * 工序作业备份
     *
     * @return
     */
    @PostMapping("/backup")
    public R backupProcessOperation() {
        log.info("开始备份工序作业");
        return apsProcessOperationService.backupProcessOperation();
    }

    /**
     * 工序作业备份恢复
     *
     * @return
     */
    @PostMapping("/backup/restore")
    public R backupRestoreProcessOperation() {
        log.info("开始恢复工序作业");
        return apsProcessOperationService.backupRestoreProcessOperation();
    }

    /**
     * 取消工序作业的冲突
     *
     * @return
     */
    @PostMapping("/cancel/conflict")
    public R cancelConflict(@RequestBody List<String> ids) {
        log.info("开始根据ID：{}忽略作业冲突信息", ids);
        return apsProcessOperationService.cancelConflict(ids);
    }

}

