package com.syjy.calculate.conotroller;

import cn.hutool.json.JSONObject;
import com.syjy.calculate.entity.CalculateRequest;
import com.syjy.calculate.entity.CalculateResult;
import com.syjy.calculate.entity.CalculationFormula;
import com.syjy.calculate.repository.CalculationFormulaRepository;
import com.syjy.calculate.service.AccuracyPassRateCalculateService;
import com.syjy.calculate.util.Common;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 公式管理
 *
 * @author zcl
 * @version 1.0
 * @since 2022/8/8 9:30
 */
@RestController
@RequestMapping("apiCalculate")
public class ApiCalculate {
    @Autowired
    CalculationFormulaRepository calculationFormulaRepository;
    @Autowired
    private AccuracyPassRateCalculateService accuracyPassRateCalculateService;
    @Autowired
    private Common common;

    /**
     * 计算单个公式准确率
     *
     * @param request
     * @return
     */
    @PostMapping("/calculate")
    public CalculateResult calculate(HttpServletRequest request, @RequestBody JSONObject data) {
        // 校验信息
        CalculateResult checkResult = checkAuthorization(request);
        // 如果校验不成功，返回失败信息
        if (!CalculateResult.Type.SUCCESS.value().equals(String.valueOf(checkResult.get(CalculateResult.CODE_TAG)))) {
            return checkResult;
        }
        CalculateRequest calculateRequest;
        // 将传入的数据转为bean
        calculateRequest = data.toBean(CalculateRequest.class);
        // 调用计算方法
        CalculateResult resultMap = accuracyPassRateCalculateService.calculate(calculateRequest);
        return resultMap;
    }

//    /**
//     * 计算所有公式准确率
//     *
//     * @param request
//     * @return
//     */
//    @PostMapping("/calculateAll")
//    public CalculateResult calculateAll(HttpServletRequest request, @RequestBody JSONObject data) {
//        // 校验信息
//        CalculateResult checkResult = checkAuthorization(request);
//        // 如果校验不成功，返回失败信息
//        if (!CalculateResult.Type.SUCCESS.value().equals(String.valueOf(checkResult.get(CalculateResult.CODE_TAG)))) {
//            return checkResult;
//        }
//        CalculateRequest calculateRequest;
//        // 将传入的数据转为bean
//        calculateRequest = data.toBean(CalculateRequest.class);
//        // 调用计算方法
//        CalculateResult resultMap = accuracyPassRateCalculateService.calculate(calculateRequest);
//        return resultMap;
//    }

    /**
     * 校验权限信息
     *
     * @param request
     * @return
     */
    public CalculateResult checkAuthorization(HttpServletRequest request) {
        String authorization = request.getHeader(CalculateResult.AUTHORIZATION);
        // 如果授权信息不存在，返回失败
        if (authorization == null) {
            return CalculateResult.error(CalculateResult.MSG_AUTHORIZATION_ERROR);
        }
        // 查询用户列表
        List<CalculationFormula> userList = calculationFormulaRepository.getUser();
        if (userList == null || userList.size() == 0) {
            return CalculateResult.error(CalculateResult.MSG_USERINFO_ERROR);
        }

        // 获取解密后的用户信息
        String deCodeUser = common.getBase64Decode(authorization);
        // 将数据库中的用户循环和传入的用户对比
        for(CalculationFormula calculationFormula:userList){
            // 如果匹配上则返回成功，否则返回失败
            if(deCodeUser.equals(calculationFormula.getFormula())){
                return CalculateResult.success();
            }
        }
        return CalculateResult.error(CalculateResult.MSG_AUTHORIZATION_ERROR);
    }

}
