package cn.cecep.talroad.controller.effective;

import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.service.effective.ReductionEmissionService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.effective.PcNationalCityRanking;
import cn.cecep.talroad.vo.effective.PcNationalCountyRanking;
import cn.cecep.talroad.vo.export.PcNationalCityRankingExport;
import cn.cecep.talroad.vo.export.PcNationalCountyRankingExport;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

@AllArgsConstructor
@RestController
@RequestMapping("/reduction")
@Api(value = "减排目标达成评估", tags = "减排目标达成评估接口")
public class ReductionEmissionController {

    private ReductionEmissionService service;

    @GetMapping("/realTimeMonitoring")
    @ApiOperation("获取地区下实时监控")
    public AjaxResults<PcNationalCityRanking> realTimeMonitoring() {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        PcNationalCityRanking pcNationalCityRanking = service.realTimeMonitoringService(regionCode);
        return AjaxResults.success(pcNationalCityRanking);
    }

    @GetMapping("/cityRanking")
    @ApiOperation("获取168，377，省内城市排名城市排名")
    public AjaxResults<Map<String, Object>> cityRanking() {
        String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
        Map<String, Object> map = service.cityRankingService(regionCode);
        return AjaxResults.success(map);
    }

    /**
     * @Method 获取省区县城市排名
     * @Author wyg
     * @Date 2023/6/16 17:02
     */
    @GetMapping("/countyRanking")
    @ApiOperation("获取省区县城市排名")
    public AjaxResults<Map<String, Object>> countyRanking(@RequestParam String name) {
        Map<String, Object> map = service.countyRankingService(name);
        return AjaxResults.success(map);
    }


    @GetMapping("/list")
    @ApiOperation("获取城市排行榜列表-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "排名类型：168，377，省内城市排名：province_city"),
            @ApiImplicitParam(name = "dateType", value = "时间类型：小时：H，日：D，月：M，年：Y"),
            @ApiImplicitParam(name = "date", value = "时间,格式 yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "cityname", value = "城市名称"),
            @ApiImplicitParam(name = "pageSize", value = "页码"),
            @ApiImplicitParam(name = "pageNum", value = "页数")
    })
    public AjaxResults<IPage<PcNationalCityRanking>> list(@RequestParam String type,
                                                          @RequestParam String dateType,
                                                          @RequestParam String date,
                                                          @RequestParam(required = false, name = "cityname") String cityName,
                                                          @RequestParam Integer pageSize,
                                                          @RequestParam Integer pageNum) {
        Assert.notNull(type, "type参数为空");
        Assert.notNull(dateType, "dateType参数为空");
        IPage<PcNationalCityRanking> page = service.list(dateType, type, date, pageNum, pageSize, cityName);
        return AjaxResults.success(page);
    }

    @GetMapping("/provinceCountyList")
    @ApiOperation("获取省区县列表-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "排名类型：省区县 province_county"),
            @ApiImplicitParam(name = "dateType", value = "时间类型：小时：H，日：D，月：M，年：Y"),
            @ApiImplicitParam(name = "date", value = "时间,格式 yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "countryname", value = "区县名称"),
            @ApiImplicitParam(name = "pageSize", value = "页码"),
            @ApiImplicitParam(name = "pageNum", value = "页数")
    })
    public AjaxResults<IPage<PcNationalCountyRanking>> provinceCountyList(@RequestParam String type,
                                                                          @RequestParam String dateType,
                                                                          @RequestParam String date,
                                                                          @RequestParam(required = false,name = "countryname") String countyName,
                                                                          @RequestParam Integer pageSize,
                                                                          @RequestParam Integer pageNum) {
        Assert.notNull(type, "type参数为空");
        Assert.notNull(dateType, "dateType参数为空");
        IPage<PcNationalCountyRanking> page = service.provinceCounty(dateType, type, date, pageNum, pageSize, countyName);
        return AjaxResults.success(page);
    }


    @GetMapping("/map")
    @ApiOperation("获取168，377，省内城市排名折线图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "排名类型：168，377，省内城市排名：province_city")
    })
    public AjaxResults<Map<String, Object>> getMap(@RequestParam String type) {
        Assert.notNull(type, "type参数为空");
        return AjaxResults.success(service.getMapService(type));
    }


    @GetMapping("/mapCounty/{countryCode}")
    @ApiOperation("获取省区县getMapCounty数组")
    public AjaxResults<Map<String, Object>> getMapCounty(@PathVariable String[] countryCode) {
        Assert.notNull(countryCode, "countryCode参数为空");
        StringJoiner sj1 = new StringJoiner(",");
        for (String str : countryCode) {
            if (!str.contains("唐山市")) {
                sj1.add("'唐山市" + str + "'");
            }

        }
        String[] strs = sj1.toString().split(",");
        return AjaxResults.success(service.getMapCountyService(strs));
    }

    @ApiOperation(value = "导出获取城市排行榜列表")
    @Log(title = "导出获取城市排行榜列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PcNationalCityRankingExport pcNationalCityRankingExport) {
        Assert.notNull(pcNationalCityRankingExport.getType(), "type参数为空");
        Assert.notNull(pcNationalCityRankingExport.getDateType(), "dateType参数为空");
        Assert.notNull(pcNationalCityRankingExport.getDate(), "date参数为空");
        List<PcNationalCityRankingExport> list = service.export(pcNationalCityRankingExport);
        ExcelUtil<PcNationalCityRankingExport> util = new ExcelUtil<>(PcNationalCityRankingExport.class);
        util.exportExcel(response, list, "获取城市排行榜列表");
    }

    @ApiOperation(value = "导出省区县列表")
    @Log(title = "导出省区县列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCounty")
    public void exportCounty(HttpServletResponse response, @RequestBody PcNationalCountyRankingExport pcNationalCountyRankingExport) {
        Assert.notNull(pcNationalCountyRankingExport.getType(), "type参数为空");
        Assert.notNull(pcNationalCountyRankingExport.getDateType(), "dateType参数为空");
        Assert.notNull(pcNationalCountyRankingExport.getDate(), "date参数为空");
        List<PcNationalCountyRankingExport> list = service.countyExport(pcNationalCountyRankingExport);
        ExcelUtil<PcNationalCountyRankingExport> util = new ExcelUtil<>(PcNationalCountyRankingExport.class);
        util.exportExcel(response, list, "获取省区县列表");
    }

    @GetMapping("/countryMap")
    @ApiOperation("获取省内区县折线图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "names", value = "区县集合")
    })
    public AjaxResults<EChartsVO> countryMap(@RequestParam List<String> names) {
        return AjaxResults.success(service.getCountryMapService(names));
    }

    @GetMapping("/countryNameList")
    @ApiOperation("获取168，377，省内城市排名地区列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称"),
            @ApiImplicitParam(name = "type", value = "查询：168，377")
    })
    public AjaxResults<List<String>> countryNameList(@RequestParam(required = false) String name, String type) {
        return AjaxResults.success(service.countryNameList(name, type));
    }

    @GetMapping("/cityNameList")
    @ApiOperation("获取167省内区县排名名称列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称")
    })
    public AjaxResults<List<String>> cityNameList(@RequestParam(required = false) String name) {
        return AjaxResults.success(service.cityNameList(name));
    }

}
