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

import cn.estsh.i3plus.ext.mes.api.base.IModuleExcelManageService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesModuleExcelManage;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "模块EXCEL模板管理")
public class ModuleExcelManageController extends MesBaseController {

    @Autowired
    private IModuleExcelManageService moduleExcelManageService;

    @GetMapping(value = "/module-excel-manage/query")
    @ApiOperation(value = "按条件分页查询模块EXCEL模板信息")
    public ResultBean queryModuleExcelManageByPager(MesModuleExcelManage moduleExcelManage, Pager pager) {
        try {
            moduleExcelManage.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(moduleExcelManageService.queryModuleExcelManageByPager(moduleExcelManage, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/module-excel-manage/insert")
    @ApiOperation(value = "新增模块EXCEL模板信息")
    public ResultBean insertModuleExcelManage(MultipartFile file, MesModuleExcelManage moduleExcelManage) {
        try {
            if (file == null) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("上传的文件为null，后台未能接收到")
                        .build();
            }

            ValidatorBean.beginValid(moduleExcelManage)
                    .notNull(MesExtConstWords.MODULE_CODE, moduleExcelManage.getModuleCode())
                    .notNull(MesExtConstWords.MODULE_NAME, moduleExcelManage.getModuleName());

            moduleExcelManage.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            moduleExcelManage.setCreateUser(AuthUtil.getSessionUser().getUserName());
            moduleExcelManageService.insertModuleExcelManage(file, moduleExcelManage);

            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/module-excel-manage/update")
    @ApiOperation(value = "更新模块EXCEL模板信息")
    public ResultBean updateModuleExcelManage(MultipartFile file, MesModuleExcelManage moduleExcelManage) {
        try {
            ValidatorBean.beginValid(moduleExcelManage)
                    .notNull(MesExtConstWords.ID, moduleExcelManage.getId())
                    .notNull(MesExtConstWords.MODULE_CODE, moduleExcelManage.getModuleCode())
                    .notNull(MesExtConstWords.MODULE_NAME, moduleExcelManage.getModuleName());

            moduleExcelManage.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            moduleExcelManage.setModifyUser(AuthUtil.getSessionUser().getUserName());
            moduleExcelManageService.updateModuleExcelManage(file, moduleExcelManage);
            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 = "/module-excel-manage/status-update")
    @ApiOperation(value = "根据id启用或禁用模块EXCEL模板信息")
    public ResultBean updateModuleExcelManageValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            moduleExcelManageService.updateModuleExcelManageValidById(id, status, 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);
        }
    }

    @PostMapping(value = "/module-excel-manage/down-load")
    @ApiOperation(value = "下载模块EXCEL模板")
    public ResultBean doDownLoadModuleExcelManage(HttpServletResponse resp, String fileUrl) {
        try {
            if (StringUtils.isEmpty(fileUrl)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            moduleExcelManageService.doDownLoadModuleExcelManage(resp, fileUrl);
            return ResultBean.success("下载成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
