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

import cn.estsh.i3plus.ext.mes.api.base.IFourMManageService;
import cn.estsh.i3plus.ext.mes.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/2 15:51
 **/
@RestController
@Api(tags = "4M管理")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/four-m-manage")
public class FourMManageController {

    @Autowired
    private IFourMManageService fourMManageService;


    /**
     * 插入
     *
     * @param fourMManage mes自动
     * @return {@link ResultBean}
     */
    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增")
    public ResultBean insert(@RequestBody MesFourMManage fourMManage) {
        try {
            return ResultBean.success("新增成功").setResultObject(fourMManageService.insert(fourMManage))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改单据规则
     *
     * @param fourMManage mes部分自动降落
     * @return {@link ResultBean}
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改")
    public ResultBean update(@RequestBody MesFourMManage fourMManage) {
        try {
            return ResultBean.success("修改成功").setResultObject(fourMManageService.update(fourMManage))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 根据ID启用或禁用单据规则
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status-update")
    @ApiOperation(value = "根据ID启用或禁用")
    public ResultBean updateOrderIsValidById(long id, Integer status) {
        try {
            return ResultBean.success("根据ID启用或禁用").setResultObject(fourMManageService.updateIsValidById(id, status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询分页
     *
     * @param fourMManage mes自动
     * @param pager       寻呼机
     * @return {@link ResultBean}
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "查询分页")
    public ResultBean queryOrderRule(MesFourMManage fourMManage, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(fourMManageService.queryByPager(fourMManage, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
