package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.entity.Cacheserver;
import com.lightinit.hsdatashow.entity.WaterqualityIndustry;
import com.lightinit.hsdatashow.entity.WaterqualitySurface;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.IWaterQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class WaterQualityServiceImpl implements IWaterQualityService {

    @Autowired
    private WaterqualityHandleMapper waterqualityHandleMapper ;

    @Autowired
    private WaterqualityCityHandleMapper waterqualityCityHandleMapper ;

    @Autowired
    private WaterqualitySurfaceMapper waterqualitySurfaceMapper ;

    @Autowired
    private WaterqualityIndustryMapper waterqualityIndustryMapper ;

    @Autowired
    private WaterqualityFocusMapper waterqualityFocusMapper ;

    @Override
    public BigDecimal getWaterqualityHandle() {
        return waterqualityHandleMapper.getWaterqualityHandle();
    }

    @Override
    public BigDecimal getWaterqualityCityHandle() {
        return waterqualityCityHandleMapper.getWaterqualityCityHandle();
    }

    @Override
    public WaterqualitySurfaceResultModel getWaterqualitySurface() {
        WaterqualitySurfaceResultModel waterqualitySurfaceResultModel = new WaterqualitySurfaceResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<WaterqualitySurface> waterqualitySurfaces= waterqualitySurfaceMapper.getWaterqualitySurface(year) ;
        if (waterqualitySurfaces.size()>0){
             List<String> names = new ArrayList<>();
             List<WaterqualityNameAndValueModel> values = new ArrayList<>();
            for (WaterqualitySurface waterqualitySurface : waterqualitySurfaces) {
                names.add(waterqualitySurface.getCategory()) ;
                values.add(new WaterqualityNameAndValueModel(waterqualitySurface.getCategory(),waterqualitySurface.getQuantity())) ;
            }
            waterqualitySurfaceResultModel.setNames(names);
            waterqualitySurfaceResultModel.setValues(values);
        }
        return waterqualitySurfaceResultModel;
    }

    @Override
    public WaterqualityIndustryResultModel getWaterqualityIndustry() {
        WaterqualityIndustryResultModel waterqualityIndustryResultModel = new WaterqualityIndustryResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) -5  ;
        List<WaterqualityIndustry> waterqualityIndustries = waterqualityIndustryMapper.getWaterqualityIndustry(year) ;
        if (waterqualityIndustries.size()>0){
             List<Integer> years = new ArrayList<>();
             List<BigDecimal> quantity = new ArrayList<>();
             List<BigDecimal> growth = new ArrayList<>() ;
            for (WaterqualityIndustry waterqualityIndustry : waterqualityIndustries) {
                years.add(waterqualityIndustry.getDataYear()) ;
                quantity.add(waterqualityIndustry.getQuantity()) ;
                growth.add(waterqualityIndustry.getGrowth()) ;
            }
            waterqualityIndustryResultModel.setYears(years);
            waterqualityIndustryResultModel.setGrowth(growth);
            waterqualityIndustryResultModel.setQuantity(quantity);
        }
        return waterqualityIndustryResultModel;
    }

    @Override
    public WaterqualityFocusResultModel getWaterqualityFocus() {
        WaterqualityFocusResultModel waterqualityFocusResultModel = new WaterqualityFocusResultModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR) ;
        List<WaterqualityFocusModel> waterqualityFocus = waterqualityFocusMapper.getWaterqualityFocus(year) ;
        if (waterqualityFocus.size()>0){
            List<String> regionName = new ArrayList<>() ;
            List<BigDecimal> quantity = new ArrayList<>() ;
            List<BigDecimal> quantityNo = new ArrayList<>() ;
            for (WaterqualityFocusModel focus : waterqualityFocus) {
                regionName.add(focus.getRegionName()) ;
                quantity.add(focus.getQuantity()) ;
                quantityNo.add(focus.getQuantityno()) ;
            }
            waterqualityFocusResultModel.setRegionName(regionName);
            waterqualityFocusResultModel.setQuantity(quantity);
            waterqualityFocusResultModel.setQuantityNo(quantityNo);
        }
        return waterqualityFocusResultModel;
    }
}
