package com.ruoyi.web.controller.screen;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.chenyanlogin.dto.VillageNewsDataDto;
import com.ruoyi.web.controller.screen.dto.VillageServiceDto;
import com.ruoyi.web.controller.screen.vo.ZhongZhiQuXianVo;
import com.ruoyi.web.domain.dto.Industry.IndustryDataQueryRequest;
import com.ruoyi.web.domain.dto.warning.drowningWarning.DrowningWarningQueryRequest;
import com.ruoyi.web.domain.entity.*;
import com.ruoyi.web.domain.vo.DrowningWarningVO;
import com.ruoyi.web.domain.vo.FacilityManagement.BaseFacilityVO;
import com.ruoyi.web.domain.vo.TimeAndValueVo.TimeAndValueVo;
import com.ruoyi.web.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;


@Api(tags ="大屏接口")
@RestController
@RequestMapping("/screen")
public class ScreenController {
    @Autowired
    private IndustryDataService industryDataService;
    @Autowired
    private VillageServiceService villageServiceService;
    @Autowired
    private VillageTraditionService villageTraditionService;
    @Autowired
    private FacilityManagementService facilityManagementService;

    /**

     * @return
     */
    @ApiOperation("大屏主要产业数据年份分组列表")
    @GetMapping("/listGroupByYear")
    public AjaxResult listGroupByYear() {
        List<TimeAndValueVo> listIndustryDataByPage = industryDataService.  listGroupByYear();
        int year = DateUtil.year(new Date());
        int lastYear = DateUtil.year(DateUtil.offset(new Date(), DateField.YEAR,-1));
        TimeAndValueVo timeAndValueVo = industryDataService.listGroupByYearTwo(year);
        TimeAndValueVo timeAndValueVo2 = industryDataService.listGroupByYearTwo(lastYear);
        Map<String,Object> map = new HashMap<>();
        map.put("industryList",listIndustryDataByPage);
        map.put("now",timeAndValueVo.getYvalue());
        String yvalue = ObjectUtil.isNotEmpty(timeAndValueVo2) ?timeAndValueVo2.getYvalue(): "0";
        String yvalue1 = ObjectUtil.isNotEmpty(timeAndValueVo) ?timeAndValueVo.getYvalue(): "0";

        Double aDouble = calculateGrowthRate(Double.parseDouble(yvalue), Double.parseDouble(yvalue1));
        if (aDouble != null) {
            map.put("rate",String.format("%.2f",aDouble));
        }else {
            map.put("rate","--");
        }
        return success(map);
    }
    public static Double calculateGrowthRate(Double oldValue, Double newValue) {
        if (oldValue == null || newValue == null) {
            return null;
        }
        if (oldValue == 0) {
            return null; // 或者返回Double.POSITIVE_INFINITY/Double.NEGATIVE_INFINITY，取决于业务需求
        }
        return ((newValue - oldValue) / oldValue) * 100;
    }
    /**
     * @return
     */
    @ApiOperation("产业服务与支持")
    @GetMapping("/villageService")
    public AjaxResult villageService() {
        VillageServiceDto villageServiceDto = new VillageServiceDto();

        villageServiceDto.setPeiXun(villageServiceService.list(new QueryWrapper<VillageService>().eq("tag", 1).orderByDesc("id")));
        villageServiceDto.setFuChi(villageServiceService.list(new QueryWrapper<VillageService>().eq("tag", 2).orderByDesc("id")));
        return success(villageServiceDto);
    }
    /**
     * @return
     */
    @ApiOperation("种植产业品种")
    @GetMapping("/zhongZhiPingZhong")
    public AjaxResult zhongZhiPingZhong() {
        return success( villageTraditionService.zhongZhiPingZhong());
    }

    /**
     * @return
     */
    @ApiOperation("种植产业曲线")
    @PostMapping("/zhongZhiQuXian")
    public AjaxResult zhongZhiQuXian(@RequestBody ZhongZhiQuXianVo zhongZhiQuXianVo) {
        return success( villageTraditionService.zhongZhiQuXian(zhongZhiQuXianVo));
    }
    /**
     * @return
     */
    @ApiOperation("养殖产业曲线")
    @PostMapping("/yagnZhiQuXian")
    public AjaxResult yagnZhiQuXian() {
        return success( villageTraditionService.yagnZhiQuXian());
    }

    /**
     * @return
     */
    @ApiOperation("劳务产业曲线")
    @PostMapping("/laoWuChanYe")
    public AjaxResult laoWuChanYe() {
        List<TimeAndValueVo> listIndustryDataByPage = industryDataService.listLaoWu();
        return success(listIndustryDataByPage);
    }


    @ApiOperation("根据id获取设施")
    @GetMapping("/getSheShi")
    public AjaxResult getFacilityById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        BaseFacilityVO facilityVO = facilityManagementService.getFacilityById(id);
        return success(facilityVO);
    }
    @Autowired
    private EthnicActivitiesService ethnicActivitiesService;

    @ApiOperation("民族活动")
    @GetMapping("/minZhuHuoDong")
    public AjaxResult minZhuHuoDong() {
        return success(ethnicActivitiesService.list(new QueryWrapper<EthnicActivities>().eq("status",0)));
    }

    @Autowired
    private MediaPropagandaService mediaPropagandaService;

    @ApiOperation("民族宣传")
    @GetMapping("/mingZhuXvuanChuan")
    public AjaxResult mingZhuXvuanChuan() {
        return success(mediaPropagandaService.list(new QueryWrapper<MediaPropaganda>().orderByDesc("id")));
    }

    @Autowired
    private EarthquakeWarningService earthquakeWarningService;

    @ApiOperation("地震灾害")
    @GetMapping("/diZhenZhaiHai")
    public AjaxResult diZhenZhaiHai() {
        return success(earthquakeWarningService.list(new QueryWrapper<EarthquakeWarning>().orderByDesc("id").last("limit 1")));
    }

    @Autowired
    private LotEnvironmentService lotEnvironmentService;
    @ApiOperation("河流数据")
    @GetMapping("/heLiuQuXian")
    public AjaxResult heLiuQuXian() {
        return success(lotEnvironmentService.heLiuQuXian());
    }
    //drowning_warning
    @Autowired
    private DrowningWarningService drowningWarningService;
    /**
     *
     * @param drowningWarningQueryRequest
     * @return
     */
    @ApiOperation("分页获取防溺水预警列表")
    @PostMapping("/list/page")
    public AjaxResult listDrowningWarningByPage(@RequestBody DrowningWarningQueryRequest drowningWarningQueryRequest) {
        if (drowningWarningQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<DrowningWarningVO> listDrowningWarningVOByPage = drowningWarningService.getListDrowningWarningByPage(drowningWarningQueryRequest);
        return success(listDrowningWarningVOByPage);
    }

}
