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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IFourMDetailService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesFourMChange;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesFourMConfirm;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesFourMDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesFourMManage;
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.*;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/4 18:05
 **/
@RestController
@Api(tags = "4M变更")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/four-m-detail")
public class FourMDetailController {
    @Autowired
    private IFourMDetailService fourMDetailService;

    @GetMapping("query")
    @ApiOperation(value = "条件分页")
    public ResultBean queryPager(MesFourMDetail detail, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(this.fourMDetailService.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.fourMDetailService.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 MesFourMDetail detail) {
        try {
            this.fourMDetailService.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 MesFourMDetail speParam) {

        try {
            fourMDetailService.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-manage")
    @ApiOperation(value = "查询参数")
    public ResultBean queryManage(MesFourMManage fourMManage, Pager pager) {

        try {

            return ResultBean.success("扫描成功")
                    .setListPager(fourMDetailService.queryByPager(fourMManage, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query-change")
    @ApiOperation(value = "查询参数")
    public ResultBean queryManage(MesFourMChange fourMChange, Pager pager) {

        try {

            return ResultBean.success("扫描成功")
                    .setListPager(fourMDetailService.queryByPager(fourMChange, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query-confirm")
    @ApiOperation(value = "查询参数")
    public ResultBean queryManage(MesFourMConfirm fourMConfirm, Pager pager) {

        try {

            return ResultBean.success("扫描成功")
                    .setListPager(fourMDetailService.queryByPager(fourMConfirm, pager))
                    .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(fourMDetailService.queryWorkOrder(workCenterCode, organizeCode)).
                            setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
