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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxBadnessClassifyService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBadnessClassify;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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:不良分类控制层
 * @author: xinwang.yi
 * @date: 2022/04/26
 */
@RestController
@RequestMapping(value = MesExtConstWords.BASE_URL_MES_JX + "/badness-classify")
@Api(tags = "不良分类")
public class JxBadnessClassifyController {

    @Autowired
    private IJxBadnessClassifyService badnessClassifyService;
    
    @GetMapping(value = "/query")
    @ApiOperation(value = "分页查询不良分类")
    public ResultBean queryBadnessClassifyPager(MesBadnessClassify badnessClassify, Pager pager) {
        try {
            badnessClassify.setOrganizeCode(AuthUtil.getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(badnessClassifyService.queryBadnessClassifyPager(badnessClassify, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增不良分类")
    public ResultBean insertBadnessClassify(MesBadnessClassify badnessClassify) {
        try {
            ValidatorBean.beginValid(badnessClassify)
                    .notNull(MesExtConstWords.BADNESS_TYPE_CODE, badnessClassify.getBadnessTypeCode())
                    .notNull(MesExtConstWords.BADNESS_TYPE_NAME, badnessClassify.getBadnessTypeName())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_CODE, badnessClassify.getBadnessClassifyCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_NAME, badnessClassify.getBadnessClassifyName());
            badnessClassify.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            badnessClassifyService.insertBadnessClassify(badnessClassify, 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);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "修改不良分类")
    public ResultBean updateBadnessClassify(MesBadnessClassify sxQmsBuBadnessClassify) {
        try {
            ValidatorBean.beginValid(sxQmsBuBadnessClassify)
                    .notNull(MesExtConstWords.ID, sxQmsBuBadnessClassify.getId())
                    .notNull(MesExtConstWords.BADNESS_TYPE_CODE, sxQmsBuBadnessClassify.getBadnessTypeCode())
                    .notNull(MesExtConstWords.BADNESS_TYPE_NAME, sxQmsBuBadnessClassify.getBadnessTypeName())
                    .notNull(MesExtConstWords.BADNESS_TYPE_NAME, sxQmsBuBadnessClassify.getBadnessClassifyCode())
                    .notNull(MesExtConstWords.BADNESS_TYPE_NAME, sxQmsBuBadnessClassify.getBadnessClassifyName());
            badnessClassifyService.updateBadnessClassify(sxQmsBuBadnessClassify, 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-weakly")
    @ApiOperation(value = "删除不良分类")
    public ResultBean updateBadnessClassifyValidById(Long ids[]) {
        try {
            badnessClassifyService.deleteBadnessClassify(ids, 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);
        }
    }
}
