package com.sw.mes.grade.controller;

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.core.utils.StringUtils;
import com.sw.mes.grade.dto.TestGradeVersion;
import com.sw.mes.grade.service.GradeService;
import com.sw.mes.gread.entity.GradeVersion;
import com.sw.mes.gread.service.GradeVersionService;
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.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.HashMap;
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;

    @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<Long> 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, FbCheckOutData> fbCheckOutDataMap = new HashMap<>();
        testGradeVersion.getSectionNos().forEach(sectionNo -> fbCheckOutDataMap.put(sectionNo, null));
        return gradeService.testGradeVersion(gradeVersion, fbCheckOutDataMap, testGradeVersion.getIsTest(), (testGradeVersion.getIsTest() ? "评级测试" : "批量评级") + "-人工触发");
    }
}
