package com.ruoyi.web.controller.shate;

import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.WeatherUtils;
import com.ruoyi.system.domain_shate.*;
import com.ruoyi.system.service_shate.IRegionalFactorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: JinSheng Song
 * @Date: 2022/7/8 11:06
 */
@RestController
@RequestMapping(value = "/api/Regional", produces = "application/json;charset=UTF-8")
@Api(tags = "地域因子")
public class RegionalFactorController
{
    @Resource
    private IRegionalFactorService service;



    @RequestMapping(value = "/login",method = {RequestMethod.POST})
    public AjaxResult login(String phone, String password)
    {
        String value= WeatherUtils.HttpHeader(phone,password);
        if (value==null){
            return  AjaxResult.error("错误");
        }


        return  AjaxResult.success(value);
    }
    /**
     * 水域因素查询
     * @return  实体类
     */
    @ApiOperation(value = "查询方法",httpMethod = "GET")
    @RequestMapping(value = "/SelectWaterFactor",method = {RequestMethod.GET})
    public AjaxResult SelectWaterFactor(String zone,String year) {
        List<WaterFactor> Seeding = service.sqlWater(zone,year);
        Double Max=0.0;
        for (WaterFactor ce : Seeding)
        {
            if (Max<ce.getLevel1())
            {
                Max= ce.getLevel1();
            }
            if (Max<ce.getLevel2())
            {
                Max=ce.getLevel2();
            }
            if (Max<ce.getLevel3())
            {
                Max=ce.getLevel3();
            }
            if (Max<ce.getLevel4())
            {
                Max=ce.getLevel4();
            }
            if (Max<ce.getLevel5())
            {
                Max=ce.getLevel5();
            }

        }
        for (int i=0;i<Seeding.size();i++)
        {
            Seeding.get(i).setMax(Max);
        }
        return AjaxResult.success(Seeding);
    }

    /**
     * 地域指数查询
     * @return  实体类
     */
    @ApiOperation(value = "查询方法",httpMethod = "GET")
    @RequestMapping(value = "/SelectRegionIndex",method = {RequestMethod.GET})
    public AjaxResult SelectRegionIndex(String zone,String year) {
        List<RegionIndex> Seeding = service.sqlRegion(zone,year);
        return AjaxResult.success(Seeding);
    }

    /**
     * 气象因子查询
     * @return  实体类
     */
    @ApiOperation(value = "查询方法",httpMethod = "GET")
    @RequestMapping(value = "/sqlClimate",method = {RequestMethod.GET})
    public AjaxResult sqlClimate(String zone) {
        List<Climate> Seeding = service.sqlClimate(zone);
        return AjaxResult.success(Seeding);
    }



    /**
     * 道路指数查询
     * @return  实体类
     */
    @ApiOperation(value = "查询方法",httpMethod = "GET")
    @RequestMapping(value = "/SelectRoadFactor",method = {RequestMethod.GET})
    public AjaxResult SelectRoadFactor(String zone,String year) {
        List<RoadFactor> Seeding = service.sqlRoad(zone,year);
        Double Max=0.0;
        for (RoadFactor ce : Seeding)
        {
            if (Max<ce.getLevel1())
            {
                Max= ce.getLevel1();
            }
            if (Max<ce.getLevel2())
            {
                Max=ce.getLevel2();
            }
            if (Max<ce.getLevel3())
            {
                Max=ce.getLevel3();
            }
            if (Max<ce.getLevel4())
            {
                Max=ce.getLevel4();
            }
            if (Max<ce.getLevel5())
            {
                Max=ce.getLevel5();
            }

        }
        for (int i=0;i<Seeding.size();i++)
        {
            Seeding.get(i).setMax(Max);
        }
        return AjaxResult.success(Seeding);
    }

    /**
     * 水域因素添加
     * @return  实体类
     */
    @RequestMapping(value = "/InsertWater",method = {RequestMethod.POST})
    public AjaxResult InsertWater(@RequestBody UplodFile uplodFile) {
        List<WaterFactor> value=uplodFile.getWaterFactors();
        for (int i=0;i<value.size();i++){
            value.get(i).setYear(value.get(i).getImageDate().substring(0,4));
            value.get(i).setImageDate(value.get(i).getImageDate().replace("-",""));
            Integer Seeding = service.InsertWater(value.get(i));
        }

        return AjaxResult.success("成功！");
    }

    /**
     * 水域面积
     * @return  实体类
     */
    @RequestMapping(value = "/InsertWaterArea",method = {RequestMethod.POST})
    public AjaxResult InsertWaterArea(@RequestBody UplodFile uplodFile) {

        List<RegionIndex>  indices=uplodFile.getRegionIndexs();
        for (int i=0;i<indices.size();i++){
            indices.get(i).setYear(indices.get(i).getImageDate().substring(0,4));
            indices.get(i).setImageDate(indices.get(i).getImageDate().replace("-",""));
            Integer Seeding = service.InsertWaterArea(indices.get(i));
        }
        return AjaxResult.success("成功！");
    }

    /**
     * 道路长度
     * @return  实体类
     */
    @RequestMapping(value = "/InsertRoadLength",method = {RequestMethod.POST})
    public AjaxResult InsertRoadLength(@RequestBody UplodFile uplodFile) {

        List<RegionIndex>  indices=uplodFile.getRegionIndexs();
        for (int i=0;i<indices.size();i++){
            indices.get(i).setYear(indices.get(i).getImageDate().substring(0,4));
            indices.get(i).setImageDate(indices.get(i).getImageDate().replace("-",""));
            Integer Seeding = service.InsertRoadLength(indices.get(i));
        }
        return AjaxResult.success("成功！");
    }

    /**
     * 道路指数添加
     * @return  实体类
     */
    @RequestMapping(value = "/InsertRoad",method = {RequestMethod.POST})
    public AjaxResult InsertRoad(@RequestBody UplodFile uplodFile) {

        for (int i=0;i<uplodFile.getRoadFactors().size();i++){
            uplodFile.getRoadFactors().get(i).setImageDate(uplodFile.getRoadFactors().get(i).getImageDate().replace("-",""));
            Integer Seeding = service.InsertRoad(uplodFile.getRoadFactors().get(i));

        }
        return AjaxResult.success("成功！");
    }


    @RequestMapping(value = "/SelectYear",method = {RequestMethod.GET})
    public AjaxResult selectYear()
    {
        List<WaterFactor> Seeding= service.sqlYear();
        return AjaxResult.success(Seeding);
    }

    @RequestMapping(value = "/SelectYear1",method = {RequestMethod.GET})
    public AjaxResult selectYear1()
    {
        List<RegionIndex> Seeding= service.sqlYear1();
        return AjaxResult.success(Seeding);
    }

    @RequestMapping(value = "/SelectYear2",method = {RequestMethod.GET})
    public AjaxResult selectYear2()
    {
        List<RoadFactor> Seeding= service.sqlYear2();
        return AjaxResult.success(Seeding);
    }
}
