package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicAccidentDegree;
import com.lightinit.hsdatashow.dictionary.DicFireDisasterDegree;
import com.lightinit.hsdatashow.entity.AreaFireDeathRate;
import com.lightinit.hsdatashow.entity.AreaFireDeathRateExample;
import com.lightinit.hsdatashow.entity.TrafficDeathRate;
import com.lightinit.hsdatashow.entity.TrafficDeathRateExample;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.AccidentDistributionModel;
import com.lightinit.hsdatashow.model.admin.ChartModel;
import com.lightinit.hsdatashow.model.admin.ChartModel2;
import com.lightinit.hsdatashow.service.admin.ILivingSecurityService;
import org.apache.ibatis.annotations.Mapper;
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.HashMap;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/8/10 16:38
 * @Description:
 **/
@Service
public class LivingSecurityServiceImpl implements ILivingSecurityService {

    @Autowired
    private AreaFireDeathRateMapper areaFireDeathRateMapper;
    @Autowired
    private AreaFireProportionMapper areaFireProportionMapper;
    @Autowired
    private AreaFireTendencyMapper areaFireTendencyMapper;
    @Autowired
    private AreaFireRankingMapper areaFireRankingMapper;
    @Autowired
    private AreaFireDistributionMapper areaFireDistributionMapper;

    @Override
    public ResultState getAreaFireDeathRate() {
        ResultState resultState = new ResultState();
        Calendar calendar = Calendar.getInstance();
        AreaFireDeathRateExample example = new AreaFireDeathRateExample();
        AreaFireDeathRateExample.Criteria criteria = example.createCriteria();
        criteria.andDataYearEqualTo(calendar.get(Calendar.YEAR)-1);
        List<AreaFireDeathRate> trafficDeathRates= areaFireDeathRateMapper.selectByExample(example);
        if(trafficDeathRates!=null && trafficDeathRates.size()>0){
            resultState.setData(trafficDeathRates.get(0).getRate());
        }
        return resultState;
    }

    @Override
    public ResultState getAreFireProportion() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        int total=0;
        List<ChartModel> list=areaFireProportionMapper.getAreFireProportion();
        ArrayList<String> names = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (ChartModel chartModel : list) {
                names.add(chartModel.getName());
                total+=(Integer) chartModel.getValue();
            }
        }
        map.put("names",names);
        map.put("total",total);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getAreFireTendency() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> years = new ArrayList<>();
        ArrayList<Integer> deathAmount = new ArrayList<>();  //死亡人数
        ArrayList<Double>  proportionAmount= new ArrayList<>();//死亡率
        List<ChartModel> list=areaFireTendencyMapper.getAreFireTendency();
        if(list !=null && list.size()>0){
            for (ChartModel chartModel : list) {
                years.add(chartModel.getName()+"年");
                deathAmount.add((Integer) chartModel.getValue());
                proportionAmount.add((Double) chartModel.getTemp());
            }
        }
        map.put("years",years);
        map.put("deathAmount",deathAmount);
        map.put("proportionAmount",proportionAmount);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getAreFireRank() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<ChartModel> list=areaFireRankingMapper.getAreFireRank();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Double> amount = new ArrayList<>();
        if(list !=null && list.size()>0){
            for (ChartModel chartModel : list) {
                names.add(chartModel.getName());
                amount.add((Double) chartModel.getValue());
            }
        }
        map.put("names",names);
        map.put("amount",amount);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getAreFireDistribution() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<AccidentDistributionModel> generalDegree=new ArrayList<>(); //一般事故
        List<AccidentDistributionModel> moreDegree=new ArrayList<>();   //较大事故
        List<AccidentDistributionModel> badDegree=new ArrayList<>();    //重大事故
        List<AccidentDistributionModel> seriousDegree=new ArrayList<>();    //特大事故
        List<AccidentDistributionModel> list =areaFireDistributionMapper.getAreFireDistribution();
        for (AccidentDistributionModel accidentDistributionModel : list) {
            if(DicFireDisasterDegree.GENERALDEGREE.equals(accidentDistributionModel.getDegree())){
                generalDegree.add(accidentDistributionModel);
            }else if(DicFireDisasterDegree.MOREDEGREE.equals(accidentDistributionModel.getDegree())){
                moreDegree.add(accidentDistributionModel);
            }else if(DicFireDisasterDegree.BADDEGREE.equals(accidentDistributionModel.getDegree())){
                badDegree.add(accidentDistributionModel);
            }else if(DicFireDisasterDegree.SERIOUSDEGREE.equals(accidentDistributionModel.getDegree())){
                seriousDegree.add(accidentDistributionModel);
            }
        }
        map.put("generalDegree",generalDegree);
        map.put("moreDegree",moreDegree);
        map.put("badDegree",badDegree);
        map.put("seriousDegree",seriousDegree);
        resultState.setData(map);
        return resultState;
    }
}
