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

import cn.estsh.i3plus.ext.mes.api.base.IOrderRuleService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOrderRule;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.base.controller.MesBaseController;
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 cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/15 11:30 上午
 * @Description:
 **/
@RestController
@Api(tags = "单据规则接口")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/order-rule")
public class OrderRuleController extends MesBaseController {
    @Autowired
    private IOrderRuleService orderRuleService;

    /**
     * 新增单据规则
     *
     * @param orderRule
     * @return
     */
    @PostMapping(value = "/mes-order-rule/insert")
    @ApiOperation(value = "新增单据规则")
    public ResultBean insertOrderRule(MesOrderRule orderRule) {
        try {
            //条件验证
            ValidatorBean.beginValid(orderRule)
                    .notNull(MesExtConstWords.TASK_TYPE,orderRule.getTaskType())
                    .notNull(MesExtConstWords.ORDER_TYPE, orderRule.getOrderType())
                    .notNull(MesExtConstWords.CREATE_RULE, orderRule.getCreateRule())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE,orderRule.getWorkCenterCode());
            return ResultBean.success("新增单据规则成功").setResultObject(orderRuleService.insertOrderRule(orderRule))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改单据规则
     *
     * @param orderRule
     * @return
     */
    @PutMapping(value = "/mes-order-rule/update")
    @ApiOperation(value = "修改单据规则")
    public ResultBean updateOrderRule(MesOrderRule orderRule) {
        try {
            //条件验证
            ValidatorBean.beginValid(orderRule)
                    .notNull(MesExtConstWords.TASK_TYPE,orderRule.getTaskType())
                    .notNull(MesExtConstWords.ORDER_TYPE, orderRule.getOrderType())
                    .notNull(MesExtConstWords.CREATE_RULE, orderRule.getCreateRule())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE,orderRule.getWorkCenterCode());
            return ResultBean.success("修改成功").setResultObject(orderRuleService.updateOrderRule(orderRule))
                    .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 = "/mes-order-rule/status-update")
    @ApiOperation(value = "根据ID启用或禁用单据规则")
    public ResultBean updateOrderIsValidById(long id, Integer status) {
        try {
            return ResultBean.success("启用/禁用成功").setResultObject(orderRuleService.updateOrderRuleIsValidById(id, status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询单据规则分页
     *
     * @param orderRule
     * @param pager
     * @return
     */
    @GetMapping(value = "/mes-order-rule/query")
    @ApiOperation(value = "查询单据规则分页")
    public ResultBean queryOrderRule(MesOrderRule orderRule, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(orderRuleService.queryOrderRuleByPager(orderRule, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
