package com.bootdo.datas.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.datas.dto.*;
import com.bootdo.datas.service.DataService;
import com.bootdo.datas.service.GyDataService;
import com.bootdo.system.domain.UserDO;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 仪表盘数据控制层
 *
 * @author Mr.xiBel
 * @date 2022/3/24
 **/
@Controller
@RequestMapping("/dashboard/data")
public class DashboardController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private GyDataService gyDataService;
    @Autowired
    private DataService dataService;

    @Value("${deployType:#{null}}")
    public String deployType;

    @Value("${sysCity:#{null}}")
    public String sysCity;

    @GetMapping("/stat")
    @Log("访问统计仪表盘页面")
    @RequiresPermissions("dashboard:data:stat")
    String Data(Model model) {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        if (StringUtils.isNotEmpty(city)) {
            logger.info("市级");
            model.addAttribute("loadURl", "dashboard-2");

        } else {
            logger.info("省级");
            model.addAttribute("loadURl", "dashboard-1");
        }
        return "dashboard/dashboard";
    }

    // "dashboard/dashboard-1"; //省
    // "dashboard/dashboard-2"; //市
    @GetMapping("/dashboard-1")
    String dashboard1() {
        return "dashboard/dashboard-1";
    }

    @GetMapping("/dashboard-2")
    String dashboard2() {
        return "dashboard/dashboard-2";
    }

    /**
     * 首页地图数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/queryData")
    public MapDataResultDTO queryData() {
        MapDataResultDTO mapDataResultDTO = new MapDataResultDTO();
        String auditStage = null;
        if("1".equals(deployType)){ //市级单独部署
            auditStage = "1";
        } else {
            auditStage = "2";
        }
        List<MapDataDTO> mapData1 = dataService.mapDataList("重要数据",auditStage);
        List<MapDataDTO> mapData2 = dataService.mapDataList("核心数据",auditStage);

        if (!CollectionUtils.isEmpty(mapData1) && !CollectionUtils.isEmpty(mapData2)) {
            mapDataResultDTO.setMapData1(mapData1);
            mapDataResultDTO.setMapData2(mapData2);
            mapDataResultDTO.setMaxData(mapData2.get(0).getAmount() + mapData1.get(0).getAmount());
            return mapDataResultDTO;
        }

        if (!CollectionUtils.isEmpty(mapData1)) {
            mapDataResultDTO.setMapData1(mapData1);
            mapDataResultDTO.setMaxData(mapData1.get(0).getAmount());
            return mapDataResultDTO;
        }

        if (!CollectionUtils.isEmpty(mapData2)) {
            mapDataResultDTO.setMapData2(mapData2);
            mapDataResultDTO.setMaxData(mapData2.get(0).getAmount());
            return mapDataResultDTO;
        }

        return mapDataResultDTO;
    }


    /**
     * 首页获取重要数据和核心数据 列表数据
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        String dataStatus = (String) params.get("dataStatus");
        if(StringUtils.isNotEmpty(dataStatus)) {
            query.put("dataStatusArrs", Lists.newArrayList((dataStatus)));
        }
        query.put("neqDataStatus", "4");
        List<GyDataImportDTO> dataList = gyDataService.list(query);
        int total = gyDataService.countTotal(query);
        PageUtils pageUtils = new PageUtils(dataList, total);
        return pageUtils;
    }


    /**
     * 数据上报趋势图 和 数据分类使用同一个数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/trendData")
    public List<DateAndCountDTO> list2() {
        return dataService.trendData();
    }


    /**
     * 所属行业子类统计
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/industryData")
//    @RequiresPermissions("datas:data:data")
    public List<NameAndValueDTO> reportForm3() {
        return dataService.industryData();
    }

    /**
     * 所属行业
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/industryType")
//    @RequiresPermissions("datas:data:data")
    public List<NameAndValueDTO> reportForm40() {
        return dataService.industryTypeData();
    }

    /**
     * 数据级别
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/dataLevelData")
//    @RequiresPermissions("datas:data:data")
    public DateAndCountListDTO reportForm4() {
        return dataService.dataLevelData();
    }

    /**
     * 数据审核 各省份角色时需要
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/dataAudit")
//    @RequiresPermissions("datas:data:data")
    public Double reportForm5() {
        return dataService.dataAuditData();
    }


    /**
     * 统计数据 所属类别
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/dataCategory")
//    @RequiresPermissions("datas:data:data")
    public List<NameAndValueDTO> reportForm6() {
        return dataService.dataCategoryData();
    }
}
