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

import cn.estsh.i3plus.ext.mes.api.base.ep.IHappenStationService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesHappenStation;
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.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-16 10:18
 */
@RestController
@Api(tags = "发生岗位")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ep-ext/happen-station")
public class HappenStationController {
    @Autowired
    private IHappenStationService happenStationService;

    @PostMapping("/insert")
    public ResultBean insertHappenStation(MesHappenStation happenStation){
        try{
            ValidatorBean.beginValid(happenStation)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, happenStation.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, happenStation.getWorkCellCode())
                    .notNull(MesExtConstWords.HAPPEN_STATION, happenStation.getHappenStation());
            happenStationService.insert(happenStation);
            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")
    @ApiOperation(value = "查询")
    public ResultBean queryEquipmentByPager(MesHappenStation happenStation, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(happenStationService.queryByPager(happenStation, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public ResultBean updateEquipment(MesHappenStation happenStation){
        try {
            //条件验证
            ValidatorBean.beginValid(happenStation)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, happenStation.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, happenStation.getWorkCellCode())
                    .notNull(MesExtConstWords.HAPPEN_STATION, happenStation.getHappenStation());

            return ResultBean.success("修改成功").setResultObject(happenStationService.update(happenStation))
                    .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(happenStationService.updateIsValidById(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
