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

import cn.estsh.i3plus.ext.mes.api.base.IPcnHttpRequestService;
import cn.estsh.i3plus.ext.mes.pojo.model.WmsPsCheckModel;
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.bean.BaseResultBean;
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.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@Slf4j
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "PCN-HTTP请求接口控制层")
public class PcnHttpRequestController extends MesBaseController {

    @Autowired
    private IPcnHttpRequestService httpRequestService;

    @PostMapping(value = "/work-order-ext-max-seq/query")
    @ApiOperation(value = "PCN获取工单最大SEQ")
    public BaseResultBean getWorkOrderExtMaxSeq(@RequestBody Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get(MesExtConstWords.ORGANIZE_CODE))) {
            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(httpRequestService.getWorkOrderExtMaxSeq(params));
    }

    @PostMapping(value = "/pcn-to-mes/upload-file")
    @ApiOperation(value = "PCN上传文件")
    public BaseResultBean uploadFile(@RequestBody Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get(MesExtConstWords.FILE_BYTE))) {
            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(httpRequestService.uploadFile(params));
    }

    @GetMapping(value = "/find-part-no")
    @ApiOperation(value = "获取WMS箱信息")
    public BaseResultBean getPartNoBySn(String sn, String organizeCode) {
        if (StringUtils.isEmpty(sn)) {
            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(httpRequestService.getPartNoBySn(sn, organizeCode));
    }

    @PostMapping("/find-by-partNo")
    @ApiOperation(value = "通过父物料号查询WmsBomTotal信息")
    public ResultBean getBomTotalByPartNo(@RequestBody Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get(MesExtConstWords.PART_NO))) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("找不到父级物料号")
                    .build();
        }
        return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                .setResultList(httpRequestService.getBomTotalByPartNo((String) params.get(MesExtConstWords.PART_NO), (String) params.get(MesExtConstWords.ORGANIZE_CODE)));
    }

    @PostMapping("/ep/find-by-partNo")
    @ApiOperation(value = "通过父物料号查询WmsBomTotal信息")
    public BaseResultBean getEpBomTotalByPartNo(@RequestBody Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get(MesExtConstWords.PART_NO))) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("找不到父级物料号")
                    .build();
        }
        return httpRequestService.getEpBomTotalByPartNo((String) params.get(MesExtConstWords.PART_NO), (String) params.get(MesExtConstWords.ORGANIZE_CODE));
        //return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
        //        .setResultList();
    }

    /*@PostMapping("/effect")
    @ApiOperation(value = "BOM生效")
    public ResultBean doEffect(@RequestBody Map<String, Object> params) {
        httpRequestService.doEffect((String) params.get("wmsBomTotalModels"));
        return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }*/

    @PostMapping("/effect")
    @ApiOperation(value = "BOM生效")
    public ResultBean doEffect(@RequestBody Map<String, Object> params) {
        log.info("HTTP调用MES BOM生效MES接收参数params:{}", params);

        String partNo = (String) params.get("partNo");
        String organizeCode = (String) params.get("organizeCode");
        log.info("HTTP调用MES BOM生效MES接收参数partNo:{}，organizeCode：{}", partNo, organizeCode);

        httpRequestService.doEffect(partNo, organizeCode);
        return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
    }


    @PostMapping(value = "/query-ps-part")
    @ApiOperation(value = "获取WMS-PS部件点检信息")
    public BaseResultBean queryPsPart(@RequestBody WmsPsCheckModel wmsPsCheckModel) {
        wmsPsCheckModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                .setListPager(httpRequestService.psPartQuery(wmsPsCheckModel));
    }

    @PostMapping(value = "/get-produce-sn-num")
    @ApiOperation(value = "获取工单下条码数量")
    public BaseResultBean getProduceSnNumByOrderNo(@RequestBody Map<String, Object> params) {

        log.info("HTTP调用MES 获取工单下条码数量MES接收参数workOrderNo:{}，organizeCode：{}", params.get(MesExtConstWords.WORK_ORDER_NO), params.get(MesExtConstWords.ORGANIZE_CODE));

        return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                .setResultObject(httpRequestService.getProduceSnNumByOrderNo((String) params.get(MesExtConstWords.WORK_ORDER_NO), (String)params.get(MesExtConstWords.ORGANIZE_CODE)));
    }
}
