package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.redis.service.RedisService;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.leader.vo.*;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.service.impl.LeaderCockpitService;
import cn.cecep.talroad.vo.PcFactoryVo;
import cn.cecep.talroad.vo.query.PcFactoryQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 领导驾驶舱控制层
 */
@RestController
@RequestMapping("/leaderCockpit")
@Api( tags =  "大屏--领导驾驶舱控制层")
public class LeaderCockpitController {

    @Autowired
    private LeaderCockpitService leaderCockpitService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private IPcFactoryService pcFactoryService;

    /**
     * 站点类型列表
     * @return
     */
    @GetMapping("/stationTypeList")
    @ApiOperation(value = "站点类型列表")
    public AjaxResults<List<StationTypeDictVO>> stationTypeList(){
        return leaderCockpitService.stationTypeList();
    }

    @GetMapping("/MapUpCount")
    @ApiOperation(value = "地图上方数量查看")
    public AjaxResults<MapUpVo> MapUpCount(){
        return leaderCockpitService.MapUpCount();
    }

    @GetMapping("/getFusionCount")
    @ApiOperation(value = "融合数据获取接口")
    public AjaxResults<FusionCountDataVO> getFusionCountData(){
        return leaderCockpitService.getFusionCountData();
    }

    @GetMapping("/getProblemDataCount")
    @ApiOperation(value = "关联分析数据接口")
    public AjaxResults<ProblemDataCountVO> getProblemDataCount(@RequestParam(value = "problemId",required = false) String problemId){
        return leaderCockpitService.getProblemDataCount(problemId);
    }

    @GetMapping("/getThreeMonitorData")
    @ApiOperation(value = "三监联动数据接口")
    public AjaxResults<ThreeMonitorCountVO> getThreeMonitorData(){
        return leaderCockpitService.getThreeMonitorData();
    }


    @GetMapping("/getMonitorEnforcementData")
    @ApiOperation(value = "监督执法数据接口")
    public AjaxResults<MonitorEnforcementCountVO> getMonitorEnforcementData(@RequestParam("selectType")Integer selectType){
        return leaderCockpitService.getMonitorEnforcementData(selectType);
    }


    @GetMapping("/getAirData")
    @ApiOperation(value = "污染防治数据接口")
    public AjaxResults<AirDataVO> getAirData(){
        return leaderCockpitService.getAirData();
    }


    @ApiOperation(value = "首页返回地图点位数据")
    @PostMapping("/getFactoryPoint")
    public AjaxResults<IPage<PcFactoryVo.MapPointVo>> getFactoryPoint(@RequestBody PcFactoryQuery.SearchVo searchVo) {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        //判断 只有查询全部企业信息时，存在，直接返回
        if (searchVo.getLabelMap().size() == 0 && StringUtils.isEmpty(searchVo.getFactoryName())) {
            if (redisService.hasKey("MapPcFactoryALlInfo"+regionCode)) {
                Page<PcFactoryVo.MapPointVo> page = redisService.getCacheObject("MapPcFactoryALlInfo"+regionCode);
                return AjaxResults.success(page);
            }
        }
        Page<PcFactoryVo.MapPointVo> page = new Page<>(searchVo.getPageNum(), searchVo.getPageSize());

        List<PcFactoryVo> search = pcFactoryService.search(null, searchVo, "home",regionCode);

        List<PcFactoryVo.MapPointVo> collect = search.stream().map(o -> {
            PcFactoryVo.MapPointVo vo = new PcFactoryVo.MapPointVo();
            BeanUtils.copyProperties(o, vo);
            return vo;
        }).collect(Collectors.toList());
        page.setRecords(collect);
        page.setTotal(collect.size());

        //判断 只有查询全部企业信息时，才进行缓存
        if (searchVo.getLabelMap().size() == 0 && StringUtils.isEmpty(searchVo.getFactoryName())) {
            //判断 key不存在时，进行缓存
            if (!redisService.hasKey("MapPcFactoryALlInfo"+regionCode)) {
                redisService.setCacheObject("MapPcFactoryALlInfo"+regionCode, page);
            }
        }
        return AjaxResults.success(page);
    }

}
