package com.energy.manage.service.controller.analysis;

import com.energy.manage.common.base.Page;
import com.energy.manage.common.po.analysis.AnalysisResultPo;
import com.energy.manage.common.po.windfield.WindFieldPO;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.constant.analysis.AnalysisConstants;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.domain.dto.analysis.AnalysisDto;
import com.energy.manage.service.domain.dto.analysis.AnalysisResultDto;
import com.energy.manage.service.domain.vo.analysis.AnalysisResultVo;
import com.energy.manage.service.domain.vo.analysis.FieldAndAnalysisInfoVo;
import com.energy.manage.service.service.analysis.AnalysisService;
import com.energy.manage.service.util.IPUtils;
import com.energy.manage.service.util.NetUtils;
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.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 分析
 * @author chy
 * @date 2024/5/23 12:57
 * @desc
 */
@Api(value = "AnalysisController",tags = "分析")
@Slf4j
@RestController
@RequestMapping("/analysis")
public class AnalysisController extends BaseServiceController {

    @Autowired
    private AnalysisService analysisService;


    @UserLoginToken
    @ApiOperation(value = "分析结果列表")
    @GetMapping("/analysisResultList")
    public ResultResp<Page<AnalysisResultVo>> analysisResultList(HttpServletRequest request,AnalysisResultDto analysisResultDto)
    {
        analysisResultDto.setRoleId(getRoleId());
        boolean isIntranet = NetUtils.isIntranet(IPUtils.getIpAddr(request));
        return pageResult(analysisService.analysisResultList(analysisResultDto,isIntranet),analysisResultDto);
    }

    @UserLoginToken
    @ApiOperation(value = "分析结果风场列表")
    @GetMapping("/analysisResulFieldList")
    public ResultResp<Page<WindFieldPO>> analysisResulFieldList(AnalysisResultDto analysisResultDto)
    {
        analysisResultDto.setRoleId(getRoleId());
        return success(analysisService.analysisResulFieldList(analysisResultDto));
    }

    @UserLoginToken
    @ApiOperation(value = "风场下所有的分析编号")
    @GetMapping("/analysisResultListByFieldCode")
    public ResultResp<List<AnalysisResultVo>> analysisResultListByAnalysisCode(@RequestParam(value = "fieldCode") String fieldCode)
    {
        return success(analysisService.analysisResultListByAnalysisCode(fieldCode));
    }


    @UserLoginToken
    @ApiOperation(value = "增加分析")
    @PostMapping("/addAnalysisResult")
    public ResultResp addAnalysisResult(@RequestBody AnalysisResultPo analysisResultPo)
    {
        Integer loginUserId = getUserId();
        analysisResultPo.setCreateBy(loginUserId).setUpdateBy(loginUserId);
        return success(analysisService.addAnalysisResult(analysisResultPo));
    }

    @UserLoginToken
    @ApiOperation(value = "手动分析触发")
    @PostMapping("/analysis")
    public ResultResp analysis(@Valid @RequestBody AnalysisDto analysisto)
    {
        //设置为手动分析
        analysisto.setAutoOrManual(AnalysisConstants.MANUAL_ANALYSIS);
        analysisto.setUpdateBy(getLoginUser().getUserId());
        return analysisService.analysis(analysisto);
    }

    @UserLoginToken
    @ApiOperation(value = "分析编辑查询")
    @GetMapping("/analysisEditQuery")
    public ResultResp analysisEditQuery(@RequestParam("batchCode") String batchCode)
    {
        return success(analysisService.analysisEditQuery(batchCode));
    }

    @UserLoginToken
    @ApiOperation(value = "分析详情")
    @GetMapping("/analysisDetail")
    public ResultResp analysisDetail(@RequestParam("batchCode") String batchCode,
                                     @RequestParam(value = "analysisTypeCode",required = false) String analysisTypeCode,
                                     @RequestParam(value = "fieldEngineCodes",required = false) List<String> fieldEngineCodes,
                                     HttpServletRequest request)
    {
        //判断是否是内网
        boolean isIntranet = NetUtils.isIntranet(IPUtils.getIpAddr(request));
        return success(analysisService.analysisDetail(batchCode,analysisTypeCode,fieldEngineCodes,isIntranet));
    }

    @UserLoginToken
    @ApiOperation(value = "开启/关闭自动分析")
    @PostMapping("/onOffAutoAnalysis")
    public ResultResp onOffAutoAnalysis(@RequestParam(value = "batchCode") String batchCode,
                                        @RequestParam(value = "onOffCall") Integer onOffCall)
    {
        return success(analysisService.onOffAutoAnalysis(batchCode,onOffCall));
    }

    @UserLoginToken
    @ApiOperation(value = "根据批次查询已分析的分析类型")
    @GetMapping("/queryAnalysisedType")
    public ResultResp queryAnalysisedType(@RequestParam(value = "batchCode") String batchCode)
    {
        return success(analysisService.queryAnalysisedType(batchCode));
    }

    @UserLoginToken
    @ApiOperation(value = "查询所有分析类型")
    @GetMapping("/queryAllAnalysisType")
    public ResultResp queryAllAnalysisType()
    {
        return success(analysisService.queryAllAnalysisType());
    }

    @UserLoginToken
    @ApiOperation(value = "根据批次与分析类型查询已有分析结果的风机")
    @GetMapping("/queryAnalysisedEngine")
    public ResultResp queryAnalysisedEngine(@RequestParam(value = "batchCode") String batchCode,
                                            @RequestParam(value = "analysisTypeCode") String analysisTypeCode)
    {
        return success(analysisService.queryAnalysisedEngine(batchCode,analysisTypeCode));
    }

    @UserLoginToken
    @ApiOperation(value = "分析类型结果图配置接口")
    @GetMapping("/queryAnalysisTypeConfig")
    public ResultResp queryAnalysisTypeConfig()
    {
        return success(analysisService.queryAnalysisTypeConfig());
    }

    @UserLoginToken
    @ApiOperation(value = "根据角色获取风场及分析编号信息")
    @GetMapping("/getAnalysisCodeInfo")
    public ResultResp<List<String>> getAnalysisCodeInfo()
    {
        Integer roleId = getRoleId();
        return success(analysisService.getAnalysisCodeInfo(roleId));
    }

    @UserLoginToken
    @ApiOperation(value = "当前批次风场及分析信息")
    @GetMapping("/getFieldInfo")
    public ResultResp<FieldAndAnalysisInfoVo> getFieldInfo(@RequestParam String batchCode)
    {
        return success(analysisService.getFieldInfo(batchCode));
    }



    @UserLoginToken
    @ApiOperation(value = "全部文件下载查询接口")
    @GetMapping("/queryDownloadFile")
    public ResultResp<List<String>> queryDownloadFile(@RequestParam(value = "fieldCode") String fieldCode,
                                                      @RequestParam("batchCode") String batchCode)
    {
        return success(analysisService.queryDownloadFile(fieldCode,batchCode));
    }


    @UserLoginToken
    @ApiOperation(value = "文件下载")
    @PostMapping("/downloadFile")
    public ResultResp downloadFile(@RequestParam(value = "fieldCode") String fieldCode,
                             @RequestParam("batchCode") String batchCode,
                             @RequestParam(value = "analysisTypeCode",required = false) String analysisTypeCode,
                             HttpServletResponse response)
    {
        return analysisService.downloadFile(fieldCode,batchCode,analysisTypeCode,response);
    }
}
