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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxBadnessClassifyDetailService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBadnessClassifyDetail;
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:BU不良分类明细控制层
 * @author: xinwang.yi
 * @date: 2022/06/07
 */
@RestController
@RequestMapping(value = MesExtConstWords.BASE_URL_MES_JX + "/badness-classify-detail")
@Api(tags = "不良分类明细")
public class JxBadnessClassifyDetailController {

    @Autowired
    private IJxBadnessClassifyDetailService badnessClassifyDetailService;
    
    @GetMapping(value = "/query")
    @ApiOperation(value = "分页查询不良分类明细")
    public ResultBean queryBadnessClassifyDetailPager(MesBadnessClassifyDetail badnessClassifyDetail, Pager pager) {
        try {
            badnessClassifyDetail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(badnessClassifyDetailService.queryBadnessClassifyDetailPager(badnessClassifyDetail, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增不良分类明细")
    public ResultBean insertBadnessClassifyDetail(MesBadnessClassifyDetail badnessClassifyDetail) {
        try {
            ValidatorBean.beginValid(badnessClassifyDetail)
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_CODE, badnessClassifyDetail.getBadnessClassifyCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_NAME, badnessClassifyDetail.getBadnessClassifyName())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_DETAIL_CODE, badnessClassifyDetail.getBadnessClassifyDetailCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_DETAIL_NAME, badnessClassifyDetail.getBadnessClassifyDetailName());
            badnessClassifyDetail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            badnessClassifyDetailService.insertBadnessClassifyDetail(badnessClassifyDetail, 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 updateBadnessClassifyDetail(MesBadnessClassifyDetail badnessClassifyDetail) {
        try {
            ValidatorBean.beginValid(badnessClassifyDetail)
                    .notNull(MesExtConstWords.ID, badnessClassifyDetail.getId())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_CODE, badnessClassifyDetail.getBadnessClassifyCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_NAME, badnessClassifyDetail.getBadnessClassifyName())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_DETAIL_CODE, badnessClassifyDetail.getBadnessClassifyDetailCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_DETAIL_NAME, badnessClassifyDetail.getBadnessClassifyDetailName());
            badnessClassifyDetailService.updateBadnessClassifyDetail(badnessClassifyDetail, 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 deleteBadnessClassifyDetail(Long ids[]) {
        try {
            badnessClassifyDetailService.deleteBadnessClassifyDetail(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);
        }
    }
}
