package com.ruoyi.web.controller.shate;

import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain_shate.*;

import com.ruoyi.system.service_shate.ILandUseServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: JinSheng Song
 * @Date: 2022/7/8 11:06
 */
@RestController
@RequestMapping(value = "/api/LandUse", produces = "application/json;charset=UTF-8")
@Api(tags = "土地利用类型")
public class LandUseController {

    @Resource
    private ILandUseServices Service;
    /**
     * 查询方法
     * @return  实体类
     */
    @ApiOperation(value = "查询方法",httpMethod = "POST")
    @RequestMapping(value = "/SelectSeeding",method = {RequestMethod.POST})
    public AjaxResult SelectSeeding(@RequestBody HelpLandUse user)
    {
        if(user.getLandUses().length==0)
        {
            List<LandUse> Seeding= Service.sqlSeeding1(user.getZone(),user.getYear());
            return AjaxResult.success(Seeding);
        }

        List<LandUse> Seeding= Service.sqlSeeding(user.getZone(), user.getLandUses(),user.getYear());

        List<lanUseNew> lanUseNews=selectLandUses1(Seeding);

        return AjaxResult.success(lanUseNews);
    }



    @RequestMapping(value = "/InsertLandUses",method = {RequestMethod.POST})
    public AjaxResult InsertLandUses(@RequestBody UplodFile uplodFile) {
        for (int i=0;i<uplodFile.getLandUses().size();i++){
            uplodFile.getLandUses().get(i).setYear(uplodFile.getLandUses().get(i).getImageDate().substring(0,4));
            uplodFile.getLandUses().get(i).setImageDate(uplodFile.getLandUses().get(i).getImageDate().replace("-",""));

            Integer Seeding =Service.insertSeeding(uplodFile.getLandUses().get(i));
        }
        return AjaxResult.success("成功！");
    }

    public List<lanUseNew> selectLandUses1(List<LandUse> value) {

        List<String> zone=new ArrayList<>();

        for(int i=0;i<value.size();i++)
        {
            zone.add(value.get(i).getZone());
        }
        List<String> zonenew =zone.stream().distinct().collect(Collectors.toList());

        List<lanUseNew> lanUseNews=new ArrayList<>();


        for (int j=0;j<zonenew.size();j++) {
            lanUseNew value1 = new lanUseNew();
            for (int i = 0; i < value.size(); i++) {

                if (value.get(i).getLandUse().equals("Fields") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setFields(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setArea(value.get(i).getArea());
                    value1.setImageDate(value.get(i).getImageDate());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Buildings") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setBuildings(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Roads") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setRoads(value.get(i).getArea());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Water") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setWater(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Forest") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setForest(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Grass") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setGrass(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Soil") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setSoil(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Desert") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setDesert(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Rocks") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setRocks(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                } else if (value.get(i).getLandUse().equals("Other") && value.get(i).getZone().equals(zonenew.get(j))) {
                    value1.setOther(value.get(i).getArea());
                    value1.setZone(value.get(i).getZone());
                    value1.setProductCode1(value.get(i).getProductCode1());
                    value1.setProductCode2(value.get(i).getProductCode2());
                    value1.setProductCode3(value.get(i).getProductCode3());
                    value1.setYear(value.get(i).getYear());
                }
            }
            lanUseNews.add(value1);

        }

        return lanUseNews;
    }




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