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


import cn.estsh.i3plus.ext.mes.api.base.ep.IEpPartExtDataService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartExt;
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.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;

/**
 * @Description : MES物料服务
 * @Reference :
 * @Author : xiangming.liao
 * @CreateDate : 2020-02-21
 * @Modify:
 **/
@RestController
@Api(tags = "物料信息")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ep-ext/part")
public class EpMesPartExtController extends MesBaseController {
    
    @Autowired
    private IEpPartExtDataService partExtDataService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询物料信息")
    public ResultBean queryMesPartExtByPager(MesPartExt part, Pager pager) {
        try {
            ListPager listPager = partExtDataService.queryMesPartExtByPager(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 insertMesPartExt(MultipartFile file,MesPartExt part) {
        try {
            ValidatorBean.beginValid(part)
                    .notNull(MesExtConstWords.PART_NO, part.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, part.getPartName())
                    .notNull(MesExtConstWords.CATEGORY_CODE1, part.getCategoryCode1())
                    .notNull(MesExtConstWords.PROCESS_MATCH_TYPE, part.getProcessMatchType())
                    .notNull(MesExtConstWords.PACKAGE_MATCH_TYPE, part.getPackageMatchType())
                    .notNull(MesExtConstWords.IS_OQC, part.getIsOqc())
                    .notNull(MesExtConstWords.IS_CREATE_SN, part.getIsCreateSn())
                    .notNull(MesExtConstWords.IS_PS_PARTS, part.getIsPSParts());

            part.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ConvertBean.serviceModelInitialize(part, AuthUtil.getSessionUser().getUserName());

            partExtDataService.insertMesPartExt(file,part);

            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 updateMesPartExt(MultipartFile file,MesPartExt part) {
        try {

            ValidatorBean.beginValid(part)
                    .notNull(MesExtConstWords.ID, part.getId())
                    .notNull(MesExtConstWords.PART_NO, part.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, part.getPartName())
                    .notNull(MesExtConstWords.CATEGORY_CODE1, part.getCategoryCode1())
                    .notNull(MesExtConstWords.PROCESS_MATCH_TYPE, part.getProcessMatchType())
                    .notNull(MesExtConstWords.PACKAGE_MATCH_TYPE, part.getPackageMatchType())
                    .notNull(MesExtConstWords.IS_OQC, part.getIsOqc())
                    .notNull(MesExtConstWords.IS_CREATE_SN, part.getIsCreateSn())
                    .notNull(MesExtConstWords.IS_PS_PARTS, part.getIsPSParts());
            ConvertBean.serviceModelUpdate(part, AuthUtil.getSessionUser().getUserName());
            partExtDataService.updateMesPartExt(file,part);
            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 updateMesPartExtStatusById(Long id, int status) {
        try {

            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            partExtDataService.updateMesPartExtStatusById(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 = "/part-ext/file-batch/upload")
    @ApiOperation(value = "批量上传物料文件")
    public ResultBean doBatchUploadPartFile(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(partExtDataService.doBatchUploadPartFile(
                            files, lotNo, AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
