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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxOfficialLossDetailRecordService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOfficialLossDetailRecordModel;
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.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "仕损明细记录控制层")
public class SxOfficialLossDetailRecordController extends MesBaseController {

    @Autowired
    private ISxOfficialLossDetailRecordService officialLossDetailRecordService;

    @GetMapping(value = "/sx-ext/official-loss-detail-record/item-cfg/get")
    @ApiOperation(value = "按条件分页查询仕损明细记录信息")
    public ResultBean getOfficialLossItemCfgMap() {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultMap(officialLossDetailRecordService.getOfficialLossItemCfgMap(AuthUtil.getOrganize().getOrganizeCode()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-ext/official-loss-detail-record/query")
    @ApiOperation(value = "按条件分页查询仕损明细记录信息")
    public ResultBean queryOfficialLossDetailRecordByPager(SxOfficialLossDetailRecordModel officialLossDetailRecord, Pager pager) {
        try {
            officialLossDetailRecord.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(officialLossDetailRecordService.queryOfficialLossDetailRecordByPager(officialLossDetailRecord, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-ext/official-loss-detail-record/insert")
    @ApiOperation(value = "新增仕损明细记录信息")
    public ResultBean insertOfficialLossDetailRecord(SxOfficialLossDetailRecordModel officialLossDetailRecord) {
        try {
            ValidatorBean.beginValid(officialLossDetailRecord)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, officialLossDetailRecord.getWorkCenterCode())
                    .notNull(MesExtConstWords.LOSS_DATE, officialLossDetailRecord.getLossDate())
                    .notNull(MesExtConstWords.LOSS_TYPE, officialLossDetailRecord.getLossType())
                    .notNull(MesExtConstWords.LOSS_ITEM, officialLossDetailRecord.getLossItem())
                    .notNull(MesExtConstWords.PART_NO, officialLossDetailRecord.getLossDate())
                    .notNull(MesExtConstWords.PART_NAME_RDD, officialLossDetailRecord.getPartNameRdd())
                    .notNull(MesExtConstWords.QTY, officialLossDetailRecord.getQty());

            officialLossDetailRecord.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            officialLossDetailRecord.setCreateUser(AuthUtil.getSessionUser().getUserName());
            officialLossDetailRecordService.insertOfficialLossDetailRecord(officialLossDetailRecord);

            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 = "/sx-ext/official-loss-detail-record/update")
    @ApiOperation(value = "更新仕损明细记录信息")
    public ResultBean updateOfficialLossDetailRecord(SxOfficialLossDetailRecordModel officialLossDetailRecord) {
        try {
            ValidatorBean.beginValid(officialLossDetailRecord)
                    .notNull(MesExtConstWords.ID, officialLossDetailRecord.getIdStr())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, officialLossDetailRecord.getWorkCenterCode())
                    .notNull(MesExtConstWords.LOSS_DATE, officialLossDetailRecord.getLossDate())
                    .notNull(MesExtConstWords.LOSS_TYPE, officialLossDetailRecord.getLossType())
                    .notNull(MesExtConstWords.LOSS_ITEM, officialLossDetailRecord.getLossItem())
                    .notNull(MesExtConstWords.PART_NO, officialLossDetailRecord.getLossDate())
                    .notNull(MesExtConstWords.PART_NAME_RDD, officialLossDetailRecord.getPartNameRdd())
                    .notNull(MesExtConstWords.QTY, officialLossDetailRecord.getQty());

            officialLossDetailRecord.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            officialLossDetailRecord.setModifyUser(AuthUtil.getSessionUser().getUserName());
            officialLossDetailRecordService.updateOfficialLossDetailRecord(officialLossDetailRecord);
            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 = "/sx-ext/official-loss-detail-record/status-update")
    @ApiOperation(value = "根据id启用或禁用仕损明细记录信息")
    public ResultBean updateOfficialLossDetailRecordValidById(String[] idStrArr, Integer status) {
        try {
            if (null == idStrArr) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            officialLossDetailRecordService.updateOfficialLossDetailRecordValidById(idStrArr, status, 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 = "/sx-ext/official-loss-detail-record/delete")
    @ApiOperation(value = "根据id删除仕损明细记录信息")
    public ResultBean deleteOfficialLossDetailRecordById(String[] idStrArr) {
        try {
            if (null == idStrArr) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            officialLossDetailRecordService.deleteOfficialLossDetailRecordById(idStrArr, 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);
        }
    }

}
