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

import cn.estsh.i3plus.ext.mes.api.base.IMesEpAssistOutAndEnterService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesAssistOutAndEnter;
import cn.estsh.i3plus.ext.mes.pojo.model.MesAssistOutAndEnterDateSumModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ep/assist-out-and-enter")
@Api(tags = "援入援出明细记录内容控制层")
public class MesEpAssistOutAndEnterController extends MesBaseController {


    @Autowired
    private IMesEpAssistOutAndEnterService assistOutAndEnterService;


    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询援入援出明细记录内容信息")
    public ResultBean queryAssistOutAndEnterByPager(MesAssistOutAndEnter assistOutAndEnter, Pager pager) {
        try {
            assistOutAndEnter.setOrganizeCode(!StringUtils.isEmpty(assistOutAndEnter.getOrganizeCode())
                    ? assistOutAndEnter.getOrganizeCode() : AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(assistOutAndEnterService.queryAssistOutAndEnterPager(assistOutAndEnter, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增援入援出明细记录内容信息")
    public ResultBean insertAssistOutAndEnter(@RequestBody MesAssistOutAndEnter assistOutAndEnter) {
        try {
            ValidatorBean.beginValid(assistOutAndEnter)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, assistOutAndEnter.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CENTER_NAME, assistOutAndEnter.getWorkCenterName())
                    .notNull(MesExtConstWords.START_TIME, assistOutAndEnter.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, assistOutAndEnter.getEndTime())
                    .notNull("assistOutAndEnterDetail", assistOutAndEnter.getAssistOutAndEnterDetail())
                    .notNull("influenceNumber", assistOutAndEnter.getInfluenceNumber())
                    .notNull("influenceDate", assistOutAndEnter.getInfluenceDate());

            assistOutAndEnter.setOrganizeCode(!StringUtils.isEmpty(assistOutAndEnter.getOrganizeCode())
                    ? assistOutAndEnter.getOrganizeCode() : AuthUtil.getOrganize().getOrganizeCode());
            assistOutAndEnter.setCreateUser(!StringUtils.isEmpty(assistOutAndEnter.getCreateUser())
                    ? assistOutAndEnter.getCreateUser() :AuthUtil.getSessionUser().getUserName());
            assistOutAndEnterService.insertAssistOutAndEnter(assistOutAndEnter);

            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新援入援出明细记录内容信息")
    public ResultBean updateAssistOutAndEnter(@RequestBody MesAssistOutAndEnter assistOutAndEnter) {
        try {
            ValidatorBean.beginValid(assistOutAndEnter)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, assistOutAndEnter.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CENTER_NAME, assistOutAndEnter.getWorkCenterName())
                    .notNull(MesExtConstWords.START_TIME, assistOutAndEnter.getStartTime())
                    .notNull(MesExtConstWords.END_TIME, assistOutAndEnter.getEndTime())
                    .notNull("assistOutAndEnterDetail", assistOutAndEnter.getAssistOutAndEnterDetail())
                    .notNull("influenceNumber", assistOutAndEnter.getInfluenceNumber())
                    .notNull("influenceDate", assistOutAndEnter.getInfluenceDate());

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

    @PutMapping(value = "/status-update")
    @ApiOperation(value = "根据id启用或禁用援入援出明细记录内容信息")
    public ResultBean updateAssistOutAndEnterValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            assistOutAndEnterService.updateAssistOutAndEnterValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "根据id删除援入援出明细记录内容信息")
    public ResultBean deleteAssistOutAndEnterById(Long id,String organizeCode,String userName) {
        try {
            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            organizeCode = StringUtils.isEmpty(organizeCode)?AuthUtil.getOrganize().getOrganizeCode():organizeCode;
            userName = StringUtils.isEmpty(userName)?AuthUtil.getSessionUser().getUserName():userName;
            assistOutAndEnterService.deleteAssistOutAndEnterId(id, organizeCode, userName);
            return ResultBean.success("删除成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping(value = "/query-assist-out-and-enter-date-sum")
    @ApiOperation(value = "援入援出时间汇总报表")
    public ResultBean queryAssistOutAndEnterDateSum(MesAssistOutAndEnterDateSumModel assistOutAndEnter) {
        try {
            assistOutAndEnter.setOrganizeCode(!StringUtils.isEmpty(assistOutAndEnter.getOrganizeCode())
                    ? assistOutAndEnter.getOrganizeCode() : AuthUtil.getOrganize().getOrganizeCode());
            List<MesAssistOutAndEnterDateSumModel> modelList =
                    assistOutAndEnterService.queryAssistOutAndEnterDateSum(assistOutAndEnter);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(modelList);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
