package cn.estsh.i3plus.ext.mes.apiservice.controller.base;

import cn.estsh.i3plus.ext.mes.api.base.ISpeParamService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesSpeParam;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/4 18:05
 **/
@RestController
@Api(tags = "特殊工艺参数")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/spe-param")
public class SpeParamController {
    @Autowired
    private ISpeParamService speParamService;

    @GetMapping("query")
    @ApiOperation(value = "条件分页")
    public ResultBean queryPager(MesSpeParam detail, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(this.speParamService.queryByPager(detail, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query-cell")
    @ApiOperation(value = "查询工序代码下拉框")
    public ResultBean queryCell(String workCenterCode, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultList(this.speParamService.queryCell(workCenterCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 物料点检作业
     *
     * @param detail
     * @return
     */
    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultBean update(@RequestBody MesSpeParam detail) {
        try {
            this.speParamService.update(detail);
            return ResultBean.success("扫描成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("insert")
    @ApiOperation(value = "新增")
    public ResultBean insert(@RequestBody MesSpeParam speParam) {

        try {
            speParamService.insert(speParam);
            return ResultBean.success("扫描成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query-param")
    @ApiOperation(value = "查询参数")
    public ResultBean queryParamNo(String organizeCode, String workCenterCode, String workCellCode) {

        try {

            return ResultBean.success("扫描成功")
                    .setResultList(speParamService.queryParamNo(workCenterCode, organizeCode, workCellCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query-work-order")
    @ApiOperation(value = "查询工单列表")
    public ResultBean queryWorkOrder(String organizeCode, String workCenterCode) {

        try {

            return ResultBean.success("扫描成功")
                    .setResultList(speParamService.queryWorkOrder(workCenterCode, organizeCode)).
                            setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }



    /**
     * 班组确认
     *
     * @param details
     * @return
     */
    @PutMapping("/shift-group-check/update")
    @ApiOperation(value = "班组确认")
    public ResultBean shiftGroupCheckUpdate(@RequestBody List<MesSpeParam> details) {
        try {
            this.speParamService.updateShiftGroupCheck(details);
            return ResultBean.success("班组确认成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 品质确认
     *
     * @param details
     * @return
     */
    @PutMapping("/quality-check/update")
    @ApiOperation(value = "品质确认")
    public ResultBean qualityCheckUpdate(@RequestBody List<MesSpeParam> details) {
        try {
            this.speParamService.updateQualityCheck(details);
            return ResultBean.success("品质确认成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
