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

import cn.estsh.i3plus.ext.mes.api.base.IExteriorScratchProofCellService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesExteriorScratchProofCell;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
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.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;

/**
 * @Description : MES外观防划伤工位
 * @Reference :
 * @Author : xiangming.liao
 * @CreateDate : 2020-03-12
 * @Modify:
 **/
@RestController
@Api(tags = "外观防划伤工位")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/mes-exterior-scartch-proof-cell-data")
public class ExteriorScratchProofCellController {
    
    @Autowired
    private IExteriorScratchProofCellService pxteriorScratchProofCellService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询外观防划伤工位")
    public ResultBean queryMesExteriorScratchProofCellByPager(MesExteriorScratchProofCell part, Pager pager) {
        try {
            ListPager listPager = pxteriorScratchProofCellService.queryMesExteriorScratchProofCellByPager(part, pager);
            return ResultBean.success("操作成功").setListPager(listPager)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增外观防划伤工位")
    public ResultBean insertMesExteriorScratchProofCell(MultipartFile file,MesExteriorScratchProofCell exteriorScratchProofCell) {
        try {
            exteriorScratchProofCell.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ConvertBean.serviceModelInitialize(exteriorScratchProofCell, AuthUtil.getSessionUser().getUserName());
            pxteriorScratchProofCellService.insertMesExteriorScratchProofCell(file, exteriorScratchProofCell);
            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 = "/update")
    @ApiOperation(value = "更新外观防划伤工位")
    public ResultBean updateMesExteriorScratchProofCell(MultipartFile file,MesExteriorScratchProofCell exteriorScratchProofCell) {
        try {
            ValidatorBean.beginValid(exteriorScratchProofCell)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, exteriorScratchProofCell.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CELL_CODE, exteriorScratchProofCell.getWorkCellCode());
            ConvertBean.serviceModelUpdate(exteriorScratchProofCell, AuthUtil.getSessionUser().getUserName());
            pxteriorScratchProofCellService.updateMesExteriorScratchProofCell(file, exteriorScratchProofCell);
            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 = "/status-update")
    @ApiOperation(value = "根据id启用或禁用外观防划伤工位")
    public ResultBean updateMesExteriorScratchProofCellStatusById(Long id, int status) {
        try {
            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            pxteriorScratchProofCellService.updateMesExteriorScratchProofCellStatusById(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);
        }
    }

    @PutMapping(value = "/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateMesExteriorScratchProofCellValidByIds(String[] idsStr, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(idsStr, true);
            pxteriorScratchProofCellService.updateMesExteriorScratchProofCellValidByIds(idsStr,status);
            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 = "/file-batch/upload")
    @ApiOperation(value = "批量上传外观防划伤工位文件")
    public ResultBean doBatchUploadExteriorScratchProofCellFile(MultipartFile[] files, String lotNo) {
        try {
            if (files == null || StringUtils.isEmpty(lotNo)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("缺少入参,先选择导入批次号后选择文件上传")
                        .build();
            }
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(pxteriorScratchProofCellService.doBatchUploadExteriorScratchProofCellFile(
                            files, lotNo, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
