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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpEquipmentService;
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.i3plus.pojo.mes.bean.MesEquipment;
import cn.estsh.impp.framework.base.controller.BaseController;
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.*;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-13 9:37
 */
@RestController
@Api(tags = "设备信息配置")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ep-ext/equipment")
public class EpEquipmentController extends BaseController {

    @Autowired
    private IEpEquipmentService equipmentService;

    @GetMapping("/query")
    @ApiOperation(value = "查询")
    public ResultBean queryEquipmentByPager(MesEquipment equipment, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(equipmentService.queryByPager(equipment, pager))
                    .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 insertEquipment(MesEquipment equipment){
        try {
            //条件验证
            ValidatorBean.beginValid(equipment)
                    .notNull(MesExtConstWords.EQUIPMENT_CODE, equipment.getEquipmentCode());
            return ResultBean.success("新增成功").setResultObject(equipmentService.insert(equipment))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public ResultBean updateEquipment(MesEquipment equipment){
        try {
            //条件验证
            ValidatorBean.beginValid(equipment)
                    .notNull(MesExtConstWords.EQUIPMENT_CODE, equipment.getEquipmentCode())
            ;
            return ResultBean.success("修改成功").setResultObject(equipmentService.update(equipment))
                    .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 updateEquipmentCheckIsValidById(long id, Integer status) {
        try {
            return ResultBean.success("根据ID启用或禁用").setResultObject(equipmentService.updateIsValidById(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
