package com.td.Dawa.game.complayersattri.controller;

import com.td.Dawa.common.controller.*;
import com.td.Dawa.common.pojo.Result;
import com.td.Dawa.config.enums.ResultEnum;
import com.td.Dawa.config.exception.AuthorizeException;
import com.td.Dawa.game.complayersattri.pojo.ComPlayersAttri;
import com.td.Dawa.game.complayersattri.vo.ComPlayersAttriClientVo;
import com.td.Dawa.game.complayersattri.vo.ComPlayersAttriVo;
import com.td.Dawa.game.complayersattri.service.ComPlayersAttriService;
import com.td.Dawa.game.excelparameter.service.ExcelParameterService;
import com.td.Dawa.game.excelparameter.vo.ExcelParameterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/game/comPlayersAttri/")
public class ComPlayersAttriController extends CommonController<ComPlayersAttriVo, ComPlayersAttri, Integer> {
    @Autowired
    private ComPlayersAttriService comPlayersAttriService;

    @Autowired
    private ExcelParameterService excelParameterService;
    /**
     * 获取玩家属性
     *
     * @param userId
     * @return
     */
    @PostMapping("getAttribute")
    public Result<ComPlayersAttriClientVo> getPlayerAttributeFunction(@RequestParam(value = "userId") String userId) {
        try {
            //获取数据库相关配置
            //ComSettingVo comSettingVo = redisSettingMap.getComSettingMap();//comSettingService.get("1").getData();
            //todo 暂时是id=1为体力
            ExcelParameterVo excelParameterVo = excelParameterService.getVo(1);
            if (excelParameterVo.equals(null)) {
                //return Result.of(null, false, ResultEnum.SETTING_IS_NULL);
                throw new AuthorizeException(ResultEnum.SETTING_IS_NULL);
            }
            //获取一下用户属性，新增表
            ComPlayersAttriVo attriVo = comPlayersAttriService.findByUserId(userId);
            //计算一下体力值
            ComPlayersAttriClientVo clientVo =  comPlayersAttriService.setStrengthAttriClientVo(attriVo,excelParameterVo.getValue().intValue());
            return Result.of(clientVo);
        } catch (AuthorizeException e){
            return Result.of(null, false, e.getMessage(),e.getCode());
        } catch (Exception e){
            return Result.of(null, false, ResultEnum.PLAYER_ATTRIBUTE_ERROR);
        }

    }

}
