package easydo.technology.service;

import easydo.technology.enums.ChengfaEnum;
import easydo.technology.model.SupplierEvaluateDetail;
import easydo.technology.model.vo.DictVo;
import easydo.technology.system.dto.DictDetailDto;
import easydo.technology.system.mapper.DictDetailMapper;
import easydo.technology.utils.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class RedisDictService {
    @Resource
    DictDetailMapper dictDetailMapper;

    public DictVo getGradeScore(double score) {
        DictDetailDto detailDto = new DictDetailDto();
        detailDto.setDictName(ChengfaEnum.REDIS_KEY_SUPPLIER_EVALUATE_RULE.getValue());
        List<DictDetailDto> detailList = dictDetailMapper.getDictDetailsByDictName(detailDto);

        DictVo vo = new DictVo();
        List<DictDetailDto> bMimList = detailList.stream().filter(m -> "b_min".equals(m.getLabel())).collect(Collectors.toList());
        List<DictDetailDto> aMimList = detailList.stream().filter(m -> "a_min".equals(m.getLabel())).collect(Collectors.toList());
        int bMim = 0;
        int aMim = 0;
        if (!bMimList.isEmpty()) {
            bMim = Integer.valueOf(bMimList.get(0).getValue());
        }
        if (!aMimList.isEmpty()) {
            aMim = Integer.valueOf(aMimList.get(0).getValue());
        }
        if (score < bMim) {
            vo.setGrade("C");
        } else if (bMim <= score && score <= aMim) {
            vo.setGrade("B");
        } else if (score > bMim) {
            vo.setGrade("A");
        }

        vo.setScore(new BigDecimal(score).setScale(2, RoundingMode.HALF_UP));
        return vo;
    }

    public DictVo getGradeScore(SupplierEvaluateDetail detail) {
        DictDetailDto detailDto = new DictDetailDto();
        detailDto.setDictName(ChengfaEnum.REDIS_KEY_SUPPLIER_EVALUATE_RULE.getValue());
        List<DictDetailDto> detailList = dictDetailMapper.getDictDetailsByDictName(detailDto);

        DictVo vo = new DictVo();
        List<DictDetailDto> bMimList = detailList.stream().filter(m -> 3 == m.getDictSort()).collect(Collectors.toList());
        List<DictDetailDto> aMimList = detailList.stream().filter(m -> 1 == m.getDictSort()).collect(Collectors.toList());
        List<DictDetailDto> zlList = detailList.stream().filter(m -> 6 == m.getDictSort()).collect(Collectors.toList());
        List<DictDetailDto> zsList = detailList.stream().filter(m -> 7 == m.getDictSort()).collect(Collectors.toList());
        List<DictDetailDto> fwList = detailList.stream().filter(m -> 8 == m.getDictSort()).collect(Collectors.toList());
        List<DictDetailDto> qtList = detailList.stream().filter(m -> 9 == m.getDictSort()).collect(Collectors.toList());
        int bMim = 0;
        int aMim = 0;
        BigDecimal zl = BigDecimal.ZERO;
        BigDecimal zs = BigDecimal.ZERO;
        BigDecimal fw = BigDecimal.ZERO;
        BigDecimal qt = BigDecimal.ZERO;
        if (!bMimList.isEmpty()) {
            bMim = Integer.valueOf(bMimList.get(0).getValue());
        }
        if (!aMimList.isEmpty()) {
            aMim = Integer.valueOf(aMimList.get(0).getValue());
        }
        if (!zlList.isEmpty()) {
            zl = BigDecimal.valueOf(Double.valueOf(zlList.get(0).getValue())).multiply(BigDecimal.valueOf(detail.getZlScore()));
        }
        if (!zsList.isEmpty()) {
            zs = BigDecimal.valueOf(Double.valueOf(zsList.get(0).getValue())).multiply(BigDecimal.valueOf(detail.getZsScore()));
        }
        if (!fwList.isEmpty()) {
            fw = BigDecimal.valueOf(Double.valueOf(fwList.get(0).getValue())).multiply(BigDecimal.valueOf(detail.getFwScore()));
        }
        if (!qtList.isEmpty()) {
            qt = BigDecimal.valueOf(Double.valueOf(qtList.get(0).getValue())).multiply(BigDecimal.valueOf(detail.getQtScore()));
        }
        double score = zl.add(zs).add(fw).add(qt).doubleValue();
        if (score < bMim) {
            vo.setGrade("C");
        } else if (bMim <= score && score <= aMim) {
            vo.setGrade("B");
        } else if (score > bMim) {
            vo.setGrade("A");
        }

        vo.setScore(new BigDecimal(score).setScale(2, RoundingMode.HALF_UP));
        return vo;
    }
}
