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

import cn.estsh.i3plus.ext.mes.api.base.IDefectExtService;
import cn.estsh.i3plus.ext.mes.api.base.IProjectExtService;
import cn.estsh.i3plus.ext.mes.api.base.IShiftExtService;
import cn.estsh.i3plus.ext.mes.api.base.bu.IPartDataExtService;
import cn.estsh.i3plus.ext.mes.api.base.bu.ISxBuBaseDataService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOnSiteOrderDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcedureProject;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProjectSort;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.PartCategoryModel;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
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.i3plus.pojo.mes.bean.MesDefect;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCategory;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 13:33 下午
 * @Description:
 **/
@RestController
@Api(tags = "基础数据")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/base-data-ext")
public class BaseDataExtController {
    public static final Logger LOGGER = LoggerFactory.getLogger(BaseDataExtController.class);

    @Autowired
    private IDefectExtService defectExtService;

    @Autowired
    private IProjectExtService projectExtService;

    @Autowired
    private IPartDataExtService partDataService;

    @Autowired
    private ISxBuBaseDataService buBaseDataService;

    @Autowired
    private IShiftExtService shiftExtService;

    @GetMapping(value = "mes-defect/query")
    @ApiOperation("按条件分页查询MES不良信息资料数据")
    public ResultBean queryMesDefectByPager(MesDefect mesDefect, Pager pager) {
        try {
            mesDefect.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager listPager = defectExtService.queryMesDefectByPager(mesDefect, pager);
            return ResultBean.success("操作成功").setListPager(listPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException var4) {
            return ResultBean.fail(var4);
        } catch (Exception var5) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(var5);
        }
    }


    @PostMapping(value = "/mes-project-sort/insert")
    @ApiOperation(value = "添加MES_项目分类信息")
    public ResultBean insertMesProjectSort(MesProjectSort mesProjectSort) {
        try {
            mesProjectSort.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            projectExtService.insertMesProjectSort(mesProjectSort);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-project-sort/update")
    @ApiOperation(value = "更新MES_项目分类信息")
    public ResultBean updateMesProjectSort(MesProjectSort mesProjectSort) {
        try {

            if (mesProjectSort.getId() == null || mesProjectSort.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            projectExtService.updateMesProjectSort(mesProjectSort, 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);
        }
    }

    @GetMapping(value = "/mes-project-sort/query")
    @ApiOperation(value = "按条件分页查询MES_项目分类信息", notes = "按条件分页查询MES_项目分类信息")
    public ResultBean queryMesProjectSortByPager(MesProjectSort mesProjectSort, Pager pager) {
        try {
            mesProjectSort.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setListPager(projectExtService.queryMesProjectSortByPager(mesProjectSort, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-project-sort/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateMesProjectSortStatus(String[] idsStr, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(idsStr, true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            projectExtService.updateMesProjectSortStatus(idsStr, status, user.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 = "/mes-procedure-project/insert")
    @ApiOperation(value = "添加MES_过程项目信息")
    public ResultBean insertMesProcedureProject(MesProcedureProject mesProcedureProject) {
        try {
            mesProcedureProject.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            projectExtService.insertMesProcedureProject(mesProcedureProject);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-procedure-project/update")
    @ApiOperation(value = "更新MES_过程项目信息")
    public ResultBean updateMesProcedureProject(MesProcedureProject mesProcedureProject) {
        try {

            if (mesProcedureProject.getId() == null || mesProcedureProject.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            projectExtService.updateMesProcedureProject(mesProcedureProject, 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);
        }
    }

    @GetMapping(value = "/mes-procedure-project/query")
    @ApiOperation(value = "按条件分页查询MES_过程项目信息", notes = "按条件分页查询MES_过程项目信息")
    public ResultBean queryMesProcedureProjectByPager(MesProcedureProject mesProcedureProject, Pager pager) {
        try {
            mesProcedureProject.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setListPager(projectExtService.queryMesProcedureProjectByPager(mesProcedureProject, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-procedure-project/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateMesProcedureProjectStatus(String[] idsStr, int status) {
        try {
            ConvertBean.modelSafeArrayNumber(idsStr, true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            projectExtService.updateMesProcedureProjectStatus(idsStr, status, user.getUserName());
            return ResultBean.success("操作成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/mes-on-site-order-detail/report-query")
    @ApiOperation(value = "巡检单明细数据查询报表", notes = "巡检单明细数据查询报表")
    public ResultBean queryMesOnSiteOrderDetailReportByPager(MesOnSiteOrderDetail mesOnSiteOrderDetail, Pager pager) {
        try {
            mesOnSiteOrderDetail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setListPager(projectExtService.queryMesOnSiteOrderDetailReportByPager(mesOnSiteOrderDetail, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-on-site-order-detail/update")
    @ApiOperation(value = "更新巡检单明细数据")
    public ResultBean updateMesOnSiteOrderDetail(MesOnSiteOrderDetail mesOnSiteOrderDetail) {
        try {
            projectExtService.updateMesOnSiteOrderDetail(mesOnSiteOrderDetail, 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);
        }
    }

    @GetMapping(value = "/mes-category-property/query")
    @ApiOperation(value = "按条件查询物料类型1、2、3信息")
    public ResultBean queryMesPartCategoryByProperties(MesPartCategory partCategory) {
        try {
            List<PartCategoryModel> partCategoryList = partDataService.queryMesPartCategoryByProperties(partCategory);
            return ResultBean.success("操作成功").setResultList(partCategoryList)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/mes-category-all/query")
    @ApiOperation(value = "按条件查询物料类型信息")
    public ResultBean queryAllMesPartCategory(String organizeCode) {
        try {
            List<PartCategoryModel> partCategoryList = partDataService.queryMesProduceCategoryAll(organizeCode);
            return ResultBean.success("操作成功").setResultList(partCategoryList)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/mes-work-center-ext/query")
    @ApiOperation(value = "查询所有产线")
    public ResultBean queryMesWorkCenterExtByListPager(String organizeCode, String workCenterCode, String workCenterName, String careerCode, String categoryCode, @RequestBody Pager pager) {
        try {
            return ResultBean.success("操作成功").setListPager(buBaseDataService.queryWorkCenterByListPager(organizeCode, workCenterCode, workCenterName, careerCode, categoryCode, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/mes-work-center/query")
    @ApiOperation(value = "条件查询产线")
    public ResultBean queryMesWorkCenterExt(String organizeCode, @RequestParam(value = "lineType", required = false) String lineType) {
        try {
            return ResultBean.success("操作成功").setResultList(buBaseDataService.queryWorkCenters(organizeCode, lineType))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/mes-work-order/by-plan-time")
    @ApiOperation(value = "根据计划时间范围查询工单")
    public ResultBean queryMesWorkOrdersByPlanTime(String organizeCode, String startTime, String endTime, @RequestParam(value = "workOrderNoList", required = false) List<String> workOrderNoList ) {
        try {
            return ResultBean.success("操作成功").setResultList(buBaseDataService.queryWorkOrdersByPlanTime(organizeCode, startTime, endTime, workOrderNoList))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/mes-shift")
    @ApiOperation(value = "根据产线代码、班次代码查班次信息")
    public ResultBean queryMesShift(@RequestParam("organizeCode") String organizeCode, @RequestParam("workCenterCode") String workCenterCode, @RequestParam("shiftCode") String shiftCode) {
        try {
            return ResultBean.success("操作成功").setResultObject(buBaseDataService.queryMesShift( organizeCode, workCenterCode, shiftCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/mes-sn-rule")
    @ApiOperation(value = "根据物料编码、匹配类型、编码类型查询生产编码规则")
    public ResultBean queryMesSnRule(@RequestParam("organizeCode") String organizeCode, @RequestParam("partNo") String partNo, @RequestParam("codeType") Integer codeType, @RequestParam("matchType") Integer matchType) {
        try {
            return ResultBean.success("操作成功").setResultObject(buBaseDataService.queryMesSnRule( organizeCode, partNo, codeType, matchType))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/all-mes-shift-map")
    @ApiOperation(value = "根据产线代码、班次代码查班次信息")
    public ResultBean queryMesShift(@RequestParam("organizeCode") String organizeCode) {
        try {
            return ResultBean.success("操作成功").setResultObject(shiftExtService.queryShiftMap(organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
