package com.sw.mes.mcs.controller;

import com.sw.mes.core.dto.CooperateDataInfoDto;
import com.sw.mes.core.dto.UploadFbMatchComposeInfoDto;
import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.fb.checkout.entity.SquareRodNumber;
import com.sw.mes.fb.checkout.service.FbCheckOutDataService;
import com.sw.mes.fb.checkout.service.SquareRodNumberService;
import com.sw.mes.fb.utils.StringUtils;
import com.sw.mes.mcs.dto.FbAppearanceCheckResultDto;
import com.sw.mes.mcs.dto.FbRankResultDto;
import com.sw.mes.mcs.dto.FbSizeInfoDto;
import com.sw.mes.mcs.dto.FbWeightDto;
import com.sw.mes.mcs.dto.GradeResultParamDto;
import com.sw.mes.mcs.dto.SquareRankDto;
import com.sw.mes.mcs.service.McsToMesService;
import com.sw.mes.mcs.service.MesToMcsService;
import com.sw.mes.mcs.utils.ResultDto;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author: GaoXinJian
 * @Date: 2022-01-13 13:23
 */
@RestController
@RequestMapping("/mcsToMesController")
@Slf4j
@Api(description = "mcs与mes通信接口")
public class McsToMesController {


    @Autowired
    McsToMesService mcsToMesService;

    @Autowired
    private SquareRodNumberService squareRodNumberService;

    @Autowired
    private FbCheckOutDataService fbCheckOutDataService;

    @Autowired
    MesToMcsService mesToMcsService;


    @ApiOperation(value = "方棒尺寸数据上传接口")
    @PostMapping("/fbCheckResult")
    public Result fbSizeInfo(@RequestBody FbSizeInfoDto fbSizeInfoDto){
        return mcsToMesService.fbSizeInfo(fbSizeInfoDto);
    }

    @ApiOperation(value = "方棒外观检测数据上传接口")
    @PostMapping("/fbAppearanceCheckResult")
    public Result fbAppearanceCheckResult(@RequestBody FbAppearanceCheckResultDto fbAppearanceCheckResultDto){
        return mcsToMesService.fbAppearanceCheckResult(fbAppearanceCheckResultDto);
    }


    @ApiOperation(value = "方棒称重数据上传接口")
    @PostMapping("/fbWeightResult")
    public Result fbWeightResult(@RequestBody FbWeightDto fbWeightDto){
//        try {
//            if(!StringUtils.isBlank(fbWeightDto.getFbSectionNo())){
//                List<SquareRodNumber> squareRodNumbersBySectionNo = squareRodNumberService.getSquareRodNumbersBySectionNo(fbWeightDto.getFbSectionNo());
//                if (null != squareRodNumbersBySectionNo && squareRodNumbersBySectionNo.size() > 0) {
//                    for (SquareRodNumber squareRodNumber : squareRodNumbersBySectionNo) {
//                        squareRodNumberService.delete(squareRodNumber.getId());
//                    }
//                }
//            }        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return mcsToMesService.fbWeightResult(fbWeightDto);
    }


    @ApiOperation(value = "方棒配棒信息上传接口")
    @PostMapping("/cooperateDataInfo")
    public Result cooperateDataInfo(@RequestBody CooperateDataInfoDto cooperateDataInfoDto) {
        return mcsToMesService.cooperateDataInfo(cooperateDataInfoDto);
    }


    /**
     * MCS上报配棒完成接口
     * @param uploadFbMatchComposeInfoDto
     * @return
     */
    @ApiOperation(value = "MCS上报配棒完成接口")
    @PostMapping("/uploadFbMatchComposeInfo")
    public Result uploadFbMatchComposeInfo(@RequestBody UploadFbMatchComposeInfoDto uploadFbMatchComposeInfoDto) {

        //mcs配棒信息上报
        return mcsToMesService.uploadFbSpellComposeInfo(uploadFbMatchComposeInfoDto);
    }


    @ApiOperation(value = "通过配棒编号查询母棒信息接口")
    @PostMapping("/findByCooperateNo")
    public Result findByCooperateNo(@RequestBody CooperateDataInfoDto cooperateDataInfoDto) {
        return mcsToMesService.findByCooperateNo(cooperateDataInfoDto);
    }



    @ApiOperation(value = "方棒评级结果获取")
    @PostMapping("/squareRankGain")
    public Result squareRank(@RequestBody GradeResultParamDto gradeResultDto) {

        String fbSectionNo = gradeResultDto.getSectionNo();
        if (null == fbSectionNo || fbSectionNo.isEmpty()){
            return ResultGenerator.fail("方棒编号不能为空！");
        }
        FbCheckOutData fbCheckOutData = fbCheckOutDataService.findDataByFbSectionNo(fbSectionNo);
        if (null != fbCheckOutData){
//            FbRankResultDto fbRankResultDto= new FbRankResultDto();
//            fbRankResultDto.setSectionNo(fbSectionNo);
//            fbRankResultDto.setFbCheckOutData(fbCheckOutData);

//            SquareRankDto squareRankDto = new SquareRankDto();
//            squareRankDto.setSectionNo(fbCheckOutData.getFbSectionNo());
//            squareRankDto.setProductRank(fbCheckOutData.getProductLevel());
//            squareRankDto.setTestRank(fbCheckOutData.getFbExperiLevel());
//            squareRankDto.setReCheck(StringUtils.isBlank(fbCheckOutData.getReCheckType()) || "0".equals(fbCheckOutData.getReCheckType()) ? "0" : "1");
//            squareRankDto.setIsFbSizeCheck(fbCheckOutData.getIsFbSizeCheck());
//            squareRankDto.setOrderNo(fbCheckOutData.getOrderNo().isEmpty() ? "MKFB0000000001" : fbCheckOutData.getOrderNo());
//            squareRankDto.setConductiveType(fbCheckOutData.getPlanConductiveType());
//            squareRankDto.setProductGrade(fbCheckOutData.getProductLevel());
//            squareRankDto.setProductType(fbCheckOutData.getProductType());
//            squareRankDto.setGradeType(fbCheckOutData.getGradeType());
//            squareRankDto.setLength(fbCheckOutData.getLength());
//            squareRankDto.setSpecification(fbCheckOutData.getSpecification());
//            squareRankDto.setSideLengthRange(fbCheckOutData.getSideLengthRange());
//
//            ResultDto resultDto = mesToMcsService.squareRank(squareRankDto);
//            if (resultDto.getStatus() == 100) {
//                return ResultGenerator.success(resultDto.getMessage(), resultDto.getData());
//            } else {
//                return ResultGenerator.message(Integer.valueOf(resultDto.getStatus()), resultDto.getMessage(), resultDto.getData());
//            }
            return ResultGenerator.success(fbCheckOutData);
        }
        return ResultGenerator.success();
    }

}
