package cn.estsh.i3plus.ext.mes.apiservice.controller.busi.bu;

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesSkillKanbanProcessInformationService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesSkillKanbanProcessInformation;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
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.*;

@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES+"/skill-kanban-process-information")
@Api(tags = "技能看板工序信息")
public class MesSkillKanbanProcessInformationController extends MesBaseController {

    @Autowired
    private IMesSkillKanbanProcessInformationService skillKanbanProcessInformationService;

    @GetMapping(value = "/query-list-page")
    @ApiOperation(value = "技能看板工序信息分页查询")
    public ResultBean getSkillKanbanProcessInformationPage(MesSkillKanbanProcessInformation skillKanbanProcessInformation, Pager pager) {
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            //请求参数非空
            if (null == skillKanbanProcessInformation || null == pager) {
                return ResultBean.fail("请求参数不完整");
            }

            return ResultBean.success("技能看板工序信息分页查询")
                    .setListPager(skillKanbanProcessInformationService.getSkillKanbanProcessInformationPage(skillKanbanProcessInformation, pager, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增技能看板工序信息")
    public ResultBean insertSkillKanbanProcessInformation(MesSkillKanbanProcessInformation skillKanbanProcessInformation) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            /*ValidatorBean.beginValid(mesWorkOrder)
                    .notNull("id", mesWorkOrder.getId());*/
            skillKanbanProcessInformationService.insertSkillKanbanProcessInformation(skillKanbanProcessInformation, organizeCode, userName);
            return new ResultBean(true).setMsg("新增成功");
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "更新技能看板工序信息")
    public ResultBean updateSkillKanbanProcessInformation(MesSkillKanbanProcessInformation skillKanbanProcessInformation) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            ValidatorBean.beginValid(skillKanbanProcessInformation)
                    .notNull(MesExtConstWords.ID, skillKanbanProcessInformation.getId());
            skillKanbanProcessInformationService.updateSkillKanbanProcessInformation(skillKanbanProcessInformation, organizeCode, userName);
            return new ResultBean(true).setMsg("更新成功");
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
