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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxBadnessTypeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBadnessType;
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/06/06
 */
@RestController
@RequestMapping(value = MesExtConstWords.BASE_URL_MES_JX + "/badness-type")
@Api(tags = "不良类型")
public class JxBadnessTypeController {

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

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增不良类型")
    public ResultBean insertBadnessType(MesBadnessType badnessType) {
        try {
            ValidatorBean.beginValid(badnessType)
                    .notNull(MesExtConstWords.BADNESS_TYPE_CODE, badnessType.getBadnessTypeCode())
                    .notNull(MesExtConstWords.BADNESS_TYPE_NAME, badnessType.getBadnessTypeName())
                    .notNull(MesExtConstWords.COLOR, badnessType.getColor());
            badnessType.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            badnessTypeService.insertBadnessType(badnessType, 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 updateBadnessType(MesBadnessType badnessType) {
        try {
            ValidatorBean.beginValid(badnessType)
                    .notNull(MesExtConstWords.ID, badnessType.getId())
                    .notNull(MesExtConstWords.BADNESS_TYPE_CODE, badnessType.getBadnessTypeCode())
                    .notNull(MesExtConstWords.BADNESS_TYPE_NAME, badnessType.getBadnessTypeName())
                    .notNull(MesExtConstWords.COLOR, badnessType.getColor());
            badnessTypeService.updateBadnessType(badnessType, 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 deleteBadnessType(Long ids[]) {
        try {
            badnessTypeService.deleteBadnessType(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);
        }
    }
}
