package com.ruoyi.system.service_shate.impl;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain_shate.*;
import com.ruoyi.system.mapper_shate.*;
import com.ruoyi.system.service_shate.ILandUseServices;
import com.ruoyi.system.service_shate.IRegionalFactorService;
import com.ruoyi.system.service_shate.ISoilFactorService;
import com.ruoyi.system.service_shate.ITopographicFactorService;
import org.springframework.stereotype.Service;

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

/**
 * @Author: JinSheng Song
 * @Date: 2022/7/8 11:20
 */
@Service
public class LandUseServiceipml implements ILandUseServices {

    @Resource
    private  PlantingSuitabilityMapper plantingSuitabilityMapper;

    @Resource
    private SeedingSuccessMapper seedingSuccessMapper;

    @Resource
    private VegetationHealthMapper vegetationHealthMapper;

    @Resource
    private LandUseMapper mapper;

    @Resource
    private updateAllRegionNameMapper updateAllRegionNameMapper;

    @Resource
    private ITopographicFactorService factorService;

    @Resource
    private ISoilFactorService soilFactorService;

    @Resource
    private IRegionalFactorService regionalFactorService;

    @Override
    public List<LandUse> sqlSeeding(String zone, String[] landUses, String year) {
        List<LandUse> value = mapper.sqlSeeding(zone, landUses, year);
        return value;
    }

    @Override
    public List<LandUse> sqlSeeding1(String zone, String year) {
        List<LandUse> value = mapper.sqlSeeding1(zone, year);
        return value;
    }

    @Override
    public Integer insertSeeding(LandUse use) {
        return mapper.insertSeeding(use);
    }

    @Override
    public List<LandUse> sqlYear() {
        return mapper.sqlYear();
    }

    @Override
    public UplodFile mainClass(String zone, String year) {

        UplodFile file = new UplodFile();
        //播种成活率
        List<SeedingSuccessRate> information= seedingSuccessMapper.sqlSeeding(zone,year);
        //植被健康状况
        List<VegetationHealth> vegetationHealths=vegetationHealthMapper.sqlInfo(zone,year);
        //种植适宜性
        List<PlantingSuitability> plantingSuitabilities=plantingSuitabilityMapper.sqlSeeding(zone);
        //海拔
        List<Altitude> altitude = factorService.sqlAltitude(zone, year);
        //坡度
        List<Slope> slopes = factorService.sqlSlope(zone, year);
        //坡向
        List<Aspect> aspects = factorService.sqlAspect(zone, year);
        //沙化指数
        List<SoilDesertification> desertifications = soilFactorService.sqlDesert(zone, year);
        //湿度指数
        List<SoilMoisture> moistures = soilFactorService.sqlMoisture(zone, year);
        //盐渍化指数
        List<SoilSalinization> salinizations = soilFactorService.sqlSalin(zone, year);
        //水域因子
        List<WaterFactor> waterFactors = regionalFactorService.sqlWater(zone, year);
        //道路因子
        List<RoadFactor> roadFactors = regionalFactorService.sqlRoad(zone, year);
        file.setInformation(information);
        file.setVegetationHealths(vegetationHealths);
        file.setPlantingSuitabilities(plantingSuitabilities);
        file.setWaterFactors(waterFactors);
        file.setRoadFactors(roadFactors);
        file.setSoilMoistures(moistures);
        file.setSoilSalinizations(salinizations);
        file.setSoilDesertifications(desertifications);
        file.setAspects(aspects);
        file.setSlopes(slopes);
        file.setAltitudes(altitude);
        return file;
    }

    @Override
    public AjaxResult updateAllRegionName(List<RegionNameVO> region) {
        try {
            List<RegionNameVO> regionNameLists = region.stream().distinct().collect(Collectors.toList());
            regionNameLists.forEach(e -> {
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }

                String regionName = e.getCurrentName();
                String regionName1 = e.getOriginalName();
                System.out.println("输出参数"+regionName+regionName1);
                updateAllRegionNameMapper.desertification(regionName, regionName1);
                updateAllRegionNameMapper.altitude(regionName, regionName1);
                updateAllRegionNameMapper.Aspect(regionName, regionName1);
                updateAllRegionNameMapper.Slope(regionName, regionName1);
                updateAllRegionNameMapper.salinization(regionName, regionName1);
                updateAllRegionNameMapper.moisture(regionName, regionName1);
                updateAllRegionNameMapper.road(regionName, regionName1);
                updateAllRegionNameMapper.water(regionName, regionName1);
                updateAllRegionNameMapper.landuse(regionName, regionName1);
                updateAllRegionNameMapper.dataUpload(regionName,regionName1);
                updateAllRegionNameMapper.suitability(regionName,regionName1);
                updateAllRegionNameMapper.waterArea(regionName,regionName1);
                updateAllRegionNameMapper.seedSuccess(regionName,regionName1);
                updateAllRegionNameMapper.healthy(regionName,regionName1);
            });
            return AjaxResult.success("成功！");
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
