package com.ho.filecenter.controller;

import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.filecenter.entity.PvEleAnalysis;
import com.ho.filecenter.service.PvEleAnalysisService;
import com.ho.filecenter.vo.req.PvEleAnalysisAddReq;
import com.ho.filecenter.vo.req.PvEleAnalysisQueryReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description 光伏发电分析
 * @Author xueweizhi
 * @Date 2023/4/07
 */
@RestController
@RequestMapping(ContextConstant.MEDIA_CONTEXT + "pvEleAnalysis")
@Api(tags = "光伏发电分析")
@Slf4j
public class PvEleAnalysisController {

    @Autowired
    PvEleAnalysisService pvEleAnalysisService;

    @Autowired
    RedisService redisService;

    @PostMapping("pvEleAnalysisList")
    @ApiOperation(value = "光伏发电分析列表查询")
    public DataResult<List<PvEleAnalysis>> pvEleAnalysisList(@RequestBody PvEleAnalysisQueryReq vo, HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        PvEleAnalysis pvEleAnalysis = new PvEleAnalysis();
        pvEleAnalysis.setYear(vo.getYear());
        pvEleAnalysis.setGroupId(user.getGroupId());
        List<PvEleAnalysis> pvEleAnalysisList = pvEleAnalysisService.getPvEleAnalysisList(pvEleAnalysis);
        return DataResult.success(pvEleAnalysisList);
    }

    @PostMapping("pvEleAnalysisAdd")
    @ApiOperation(value = "光伏发电分析数据新增")
    @LogAnnotation(title = "光伏发电分析", action = "光伏发电分析数据新增")
    public DataResult pvEleAnalysisAdd(@RequestBody PvEleAnalysisAddReq vo, HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        pvEleAnalysisService.savePvEleAnalysisList(vo.getList(),user);
        return DataResult.success();
    }

}
