package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicAccidentDegree;
import com.lightinit.hsdatashow.entity.CriminalRate;
import com.lightinit.hsdatashow.entity.CriminalRateExample;
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.model.admin.MapModel;
import com.lightinit.hsdatashow.service.admin.ITrafficSecurityService;
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 11:12
 * @Description:
 **/
@Service
public class TrafficSecurityServiceImpl implements ITrafficSecurityService {

    @Autowired
    private TrafficDeathRateMapper trafficDeathRateMapper;
    @Autowired
    private TrafficAccidentProportionMapper trafficAccidentProportionMapper;
    @Autowired
    private TrafficAccidentTendencyMapper trafficAccidentTendencyMapper;
    @Autowired
    private TrafficAccidentRankingMapper trafficAccidentRankingMapper;
    @Autowired
    private TrafficAccidentDistributionMapper trafficAccidentDistributionMapper;
    @Autowired
    private TrafficAccidentCompleteMapper trafficAccidentCompleteMapper;

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

    @Override
    public ResultState getTrafficAccidentPro() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        int total=0;
        List<ChartModel> list=trafficAccidentProportionMapper.getTrafficAccidentPro();
        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("total",total);
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

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

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

    @Override
    public ResultState getTrafficAccidentDistribution() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<AccidentDistributionModel> slightDegree=new ArrayList<>();
        List<AccidentDistributionModel> genernalDegree=new ArrayList<>();
        List<AccidentDistributionModel> seriousDegree=new ArrayList<>();
        List<AccidentDistributionModel> list = trafficAccidentDistributionMapper.getTrafficAccidentDistribution();
        for (AccidentDistributionModel accidentDistributionModel : list) {
            if(DicAccidentDegree.SLIGHTDEGREE.equals(accidentDistributionModel.getDegree())){
                slightDegree.add(accidentDistributionModel);
            }else if(DicAccidentDegree.GENERALDEGREE.equals(accidentDistributionModel.getDegree())){
                genernalDegree.add(accidentDistributionModel);
            }else{
                seriousDegree.add(accidentDistributionModel);
            }
        }
        map.put("slightDegree",slightDegree);
        map.put("genernalDegree",genernalDegree);
        map.put("seriousDegree",seriousDegree);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getTrafficAccidentComplete() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> years = new ArrayList<>();
        ArrayList<Integer> totalAmount = new ArrayList<>();  //总数
        ArrayList<Integer> completeAmount = new ArrayList<>(); //结案总数
        ArrayList<Double>  proportionAmount= new ArrayList<>();//结案率
        List<ChartModel2> list=trafficAccidentCompleteMapper.getTrafficAccidentComplete();
        if(list !=null && list.size()>0){
            for (ChartModel2 chartModel : list) {
                years.add(chartModel.getName()+"年");
                totalAmount.add((Integer) chartModel.getValue());
                completeAmount.add((Integer) chartModel.getTemp1());
                proportionAmount.add((Double) chartModel.getTemp2());
            }
        }
        map.put("years",years);
        map.put("totalAmount",totalAmount);
        map.put("completeAmount",completeAmount);
        map.put("proportionAmount",proportionAmount);
        resultState.setData(map);
        return resultState;
    }
}
