package com.sw.mes.grade.controller;

import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.StringUtils;
import com.sw.mes.grade.dto.TestGradeVersion;
import com.sw.mes.grade.service.GradeService;
import com.sw.mes.gread.common.EnumValuesGrade;
import com.sw.mes.gread.entity.GradeVersion;
import com.sw.mes.gread.service.GradeVersionService;
import com.sw.mes.inspect.dao.YbInspectDataDao;
import com.sw.mes.inspect.entity.YbInspectData;
import com.sw.mes.yb.pad.entity.RecutDetail;
import com.sw.mes.yb.pad.service.RecutDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/grade")
@Slf4j
@Api(description = "评级测试接口")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    private GradeVersionService gradeVersionService;

    @Autowired
    private RecutDetailService recutDetailService;

    @ApiOperation(value = "发布评级规则版本")
    @PostMapping("/pushGradeVersion")
    public Result pushGradeVersion(@ApiParam(value = "id", type = "query") Long id, @ApiParam(value = "remarks", type = "query") String remarks) {
        if (StringUtils.isBlank(remarks)) {
            return ResultGenerator.fail("备注不能为空");
        }
        if (remarks.length() > 200) {
            return ResultGenerator.fail("备注最多只能输入200字符");
        }
        GradeVersion gradeVersion = gradeVersionService.get(id);
        if (gradeVersion == null) {
            return ResultGenerator.fail("当前版本不存在");
        }
        gradeService.pushGradeVersion(gradeVersion, remarks);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "测试评级规则版本")
    @PostMapping("/testGradeVersion")
    public Result testGradeVersion(@RequestBody TestGradeVersion testGradeVersion) {
        if (testGradeVersion.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
        if (testGradeVersion.getSectionNos() == null) {
            return ResultGenerator.fail("测试的晶段编号不能为空");
        }
        if (testGradeVersion.getSectionNos().size() == 0) {
            return ResultGenerator.fail("测试的晶段编号不能为空");
        }
        if (testGradeVersion.getSectionNos().size() > 200) {
            return ResultGenerator.fail("每次最多只能测试200个编号");
        }
        GradeVersion gradeVersion = gradeVersionService.get(testGradeVersion.getId());
        if (gradeVersion == null) {
            return ResultGenerator.fail("当前版本不存在");
        }
        if (testGradeVersion.getIsTest() == null) {
            testGradeVersion.setIsTest(true);
        }
        Map<String, YbInspectData> sectionNoDataMap = new HashMap<>();
        testGradeVersion.getSectionNos().forEach(sectionNo -> sectionNoDataMap.put(sectionNo, null));
        return gradeService.testGradeVersion(gradeVersion, sectionNoDataMap, testGradeVersion.getIsTest(), null, null, EnumValues.ExecType.圆棒触发.getKey(), (testGradeVersion.getIsTest() ? "评级测试" : "批量评级") + "-人工触发");
    }

    @Autowired
    private YbInspectDataDao inspectDataBaseDao;

    /**
     * 方棒触发评级，再次评级
     *
     * @param sectionNo      圆棒编号
     * @param specifications 规格
     * @param length         长度
     * @return
     */
    @ApiOperation(value = "方棒触发评级，再次评级")
    @PostMapping("/fbExceGrade")
    public Result<Object> fbExceGrade(@RequestParam("sectionNo") String sectionNo,
                                      @RequestParam(value = "specifications", required = false) String specifications,
                                      @RequestParam(value = "length", required = false) BigDecimal length) {
        if (StringUtils.isBlank(sectionNo)) {
            return ResultGenerator.fail("晶段编号，不能为空");
        }
        RecutDetail recutDetail = recutDetailService.findBySectionNoFb(sectionNo);
        if (specifications == null) {
            return ResultGenerator.fail("方棒规格，不能为空");
        }
        specifications = specifications.split("\\*")[0];
        List<YbInspectData> inspectDataBases = inspectDataBaseDao.findBySectionNo(recutDetail.getSectionNo());
        YbInspectData inspectDataBase = null;
        if (inspectDataBases.size() > 0) {
            inspectDataBase = inspectDataBases.get(0);
        }
        if (inspectDataBase == null) {
            return ResultGenerator.fail("圆棒检验数据不存在，不在触发评级");
        }
//        if (specifications.equals(inspectDataBase.getProductTypeSecond())) {
//            return ResultGenerator.fail("方棒规格与圆棒规格一致，不在触发评级");
//        }
        Map<String, YbInspectData> sectionNoDataMap = new HashMap<>(1);
        sectionNoDataMap.put(sectionNo, inspectDataBase);
        GradeVersion gradeVersion = gradeVersionService.findTopByStatusAndRegion(EnumValuesGrade.GradeVersionStatus.已生效.getKey(), EnumValuesGrade.Region.圆棒.getKey());
        if (gradeVersion == null) {
            return ResultGenerator.fail("现在没有生效的圆棒评级规则");
        }
        specifications = inspectDataBase.getProductTypeSecond();
        //指定晶段评级
        return gradeService.testGradeVersion(gradeVersion, sectionNoDataMap, true, specifications, length, EnumValues.ExecType.方棒触发.getKey(), "方棒评级-方棒触发" + specifications);
    }


//    @ApiOperation(value = "发送EC消息")
//    @PostMapping("/sendMessage")
//    public Result sendMessage(String content) {
//        String token = ecMessageService.geToken();
//        if (token != null) {
//            EcMessageContentDto ecMessageContentDto = new EcMessageContentDto();
//            ecMessageContentDto.setContext(content);
//            ecMessageContentDto.setTitle("CIGC2952205D4812D61V01 --- 电阻率异常");
//            ecMessageContentDto.setLinkUrl("");
//            ecMessageContentDto.setLinkMobileUrl("");
//            ecMessageContentDto.setLoginIdList("31180065");
//            boolean isStatus = ecMessageService.sendEcMessage(token, ecMessageContentDto);
//            if (isStatus) {
//                return ResultGenerator.success();
//            } else {
//                return ResultGenerator.fail("发送失败");
//            }
//        } else {
//            return ResultGenerator.fail("toekn获取失败");
//        }
//    }
}
