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

import cn.estsh.i3plus.ext.mes.api.base.IWorkCenterNotificationBarService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterNotificationBar;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/17 18:06 下午
 * @Description:
 **/
@RestController
@Api(tags = "产线公告接口")
@Slf4j
@RequestMapping(CommonConstWords.BASE_URL_MES + "/notification-bar")
public class WorkCenterNotificationBarController extends MesBaseController {
    @Autowired
    private IWorkCenterNotificationBarService workCenterNotificationBarService;

    /**
     * 新增产线公告
     *
     * @param workCenterNotificationBar
     * @return
     */
    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增产线公告")
    public ResultBean insertWorkCenterNotificationBar(MesWorkCenterNotificationBar workCenterNotificationBar) {
        try {
            log.info("新增产线公告workCenterNotificationBar:{}", workCenterNotificationBar);
            //条件验证
            ValidatorBean.beginValid(workCenterNotificationBar)
                    .notNull(MesExtConstWords.AREA_CODE, workCenterNotificationBar.getAreaCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, workCenterNotificationBar.getWorkCenterCode())
                    .notNull(MesExtConstWords.CONTENT, workCenterNotificationBar.getContent());
            return ResultBean.success("新增产线公告成功").setResultObject(workCenterNotificationBarService.insertWorkCenterNotificationBar(workCenterNotificationBar))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改产线公告
     *
     * @param workCenterNotificationBar
     * @return
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改产线公告")
    public ResultBean updateWorkCenterNotificationBar(MesWorkCenterNotificationBar workCenterNotificationBar) {
        try {
            log.info("修改产线公告workCenterNotificationBar:{}", workCenterNotificationBar);
            //条件验证
            ValidatorBean.beginValid(workCenterNotificationBar)
                    .notNull(MesExtConstWords.AREA_CODE, workCenterNotificationBar.getAreaCode())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, workCenterNotificationBar.getWorkCenterCode())
                    .notNull(MesExtConstWords.CONTENT, workCenterNotificationBar.getContent());
            return ResultBean.success("修改产线公告成功").setResultObject(workCenterNotificationBarService.updateWorkCenterNotificationBar(workCenterNotificationBar))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询产线公告分页
     *
     * @param workCenterNotificationBar
     * @param pager
     * @return
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "查询产线公告分页")
    public ResultBean queryWorkCenterNotificationBar(MesWorkCenterNotificationBar workCenterNotificationBar, Pager pager) {
        try {
            return ResultBean.success("查询产线公告成功").setListPager(workCenterNotificationBarService.queryWorkCenterNotificationBarByPager(workCenterNotificationBar, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 启用禁用产线公告
     *
     * @param id
     * @param isValid
     * @return
     */
    @PutMapping(value = "/status-update")
    @ApiOperation(value = "启用禁用产线公告")
    public ResultBean updateStatusworkCenterNotificationBar(long id, Integer isValid) {
        try {
            return ResultBean.success("启用/禁用产线公告成功").setResultObject(workCenterNotificationBarService.updateStatusWorkCenterNotificationBar(id, isValid))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
