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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxTimeSegmentStatisticsService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesTimeSegmentStatistics;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.BaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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.*;

/**
 * @Description :嘉兴生产线班次分时统计配置
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2023/02/06 09:59
 * @Modify:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "嘉兴生产线班次分时统计配置")
public class JxTimeSegmentStatisticsController extends BaseController {

    @Autowired
    private IJxTimeSegmentStatisticsService timeSegmentStatisticsService;

    @GetMapping(value = "/ext/jx/time-segment-statistics/query-by-pager")
    @ApiOperation(value = "查询")
    public ResultBean queryTimeSegmentStatisticsByPager(MesTimeSegmentStatistics timeSegmentStatistics, Pager pager) {
        try {
            timeSegmentStatistics.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager<MesTimeSegmentStatistics> result = timeSegmentStatisticsService.queryTimeSegmentStatisticsByPager(timeSegmentStatistics, pager);
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setListPager(result);
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/ext/jx/time-segment-statistics/insert")
    @ApiOperation(value = "新增")
    public ResultBean insertTimeSegmentStatistics(MesTimeSegmentStatistics timeSegmentStatistics) {
        try {
            ValidatorBean.beginValid(timeSegmentStatistics)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, timeSegmentStatistics.getWorkCenterCode())
                    .notNull(MesExtConstWords.SHIFT_CODE, timeSegmentStatistics.getShiftCode())
                    .notNull(MesExtConstWords.START_TIME, timeSegmentStatistics.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, timeSegmentStatistics.getEndTime());

            timeSegmentStatistics.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            timeSegmentStatistics.setCreateUser(AuthUtil.getSessionUser().getUserName());

            timeSegmentStatisticsService.insertTimeSegmentStatistics(timeSegmentStatistics);
            return ResultBean.success("新增成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/ext/jx/time-segment-statistics/update")
    @ApiOperation(value = "修改")
    public ResultBean updateTimeSegmentStatistics(MesTimeSegmentStatistics timeSegmentStatistics) {
        try {
            ValidatorBean.beginValid(timeSegmentStatistics)
                    .notNull(MesExtConstWords.ID, timeSegmentStatistics.getId())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, timeSegmentStatistics.getWorkCenterCode())
                    .notNull(MesExtConstWords.SHIFT_CODE, timeSegmentStatistics.getShiftCode())
                    .notNull(MesExtConstWords.START_TIME, timeSegmentStatistics.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, timeSegmentStatistics.getEndTime());

            timeSegmentStatistics.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            timeSegmentStatistics.setModifyUser(AuthUtil.getSessionUser().getUserName());
            timeSegmentStatisticsService.updateTimeSegmentStatistics(timeSegmentStatistics);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/ext/jx/time-segment-statistics/do-disable")
    @ApiOperation(value = "禁用")
    public ResultBean doDisable(Long[] ids) {
        try {
            ValidatorBean.checkNotNull(ids,"id不能为空");
            timeSegmentStatisticsService.doDisable(ids, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("禁用成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/ext/jx/time-segment-statistics/do-enable")
    @ApiOperation(value = "启用")
    public ResultBean doEnable(Long[] ids) {
        try {
            ValidatorBean.checkNotNull(ids,"id不能为空");
            timeSegmentStatisticsService.doEnable(ids, AuthUtil.getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("启用成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/ext/jx/time-segment-statistics/delete-by-ids")
    @ApiOperation(value = "删除")
    public ResultBean deleteByIds(Long[] ids) {
        try {
            ValidatorBean.checkNotNull(ids,"id不能为空");
            timeSegmentStatisticsService.deleteByIds(ids, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("删除成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
