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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesTeamManagementIntroduceService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesTeamManagementIntroduce;
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.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES+"/team-management-introduce")
@Api(tags = "团队管理介绍")
public class MesTeamManagementIntroduceController extends MesBaseController {

    @Autowired
    private IMesTeamManagementIntroduceService teamManagementIntroduceService;

    @GetMapping(value = "/query-list-page")
    @ApiOperation(value = "团队管理介绍分页查询")
    public ResultBean getTeamManagementIntroducePage(MesTeamManagementIntroduce teamManagementIntroduce, Pager pager) {
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            //请求参数非空
            if (null == teamManagementIntroduce || null == pager) {
                return ResultBean.fail("请求参数不完整");
            }

            ResultBean resultBean = new ResultBean(true);
            return ResultBean.success("团队管理介绍分页查询")
                    .setListPager(teamManagementIntroduceService.getTeamManagementIntroducePage(teamManagementIntroduce, 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 insertTeamManagementIntroduce(MesTeamManagementIntroduce teamManagementIntroduce, MultipartFile file1, MultipartFile file2) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            /*ValidatorBean.beginValid(mesWorkOrder)
                    .notNull("id", mesWorkOrder.getId());*/
            teamManagementIntroduceService.insertTeamManagementIntroduce(teamManagementIntroduce, file1, file2, organizeCode, userName);
            return new ResultBean(true).setMsg("新增成功");
        } catch (ImppBusiException imppException) {

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

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新团队管理介绍")
    public ResultBean updateTeamManagementIntroduce(MesTeamManagementIntroduce teamManagementIntroduce, MultipartFile file1, MultipartFile file2) {
        //teamManagementIntroduce.setWorkCenterCode(workCenterCode);
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            /*ValidatorBean.beginValid(teamManagementIntroduce)
                    .notNull("id", teamManagementIntroduce.getId());*/
            teamManagementIntroduceService.updateTeamManagementIntroduce(teamManagementIntroduce, file1, file2, 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-valid")
    @ApiOperation(value = "启用禁用团队管理介绍")
    public ResultBean updateTeamManagementIntroduceValid(MesTeamManagementIntroduce teamManagementIntroduce) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            ValidatorBean.beginValid(teamManagementIntroduce)
                    .notNull("id", teamManagementIntroduce.getId());
            teamManagementIntroduceService.updateTeamManagementIntroduceValid(teamManagementIntroduce, organizeCode, userName);
            return new ResultBean(true).setMsg("更新成功");
        } catch (ImppBusiException imppException) {

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

}
