package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.ChartModel;
import com.lightinit.hsdatashow.model.admin.MapModel;
import com.lightinit.hsdatashow.service.admin.ICommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author: sunxiang
 * @Date: 2018/8/7 16:07
 * @Description:
 **/
@Service
public class CommunicationServiceImpl implements ICommunicationService {

    @Autowired
    private VideoMonitorMapper videoMonitorMapper;
    @Autowired
    private TvWifiMapper tvWifiMapper;
    @Autowired
    private AreaCommunicationBreakMapper areaCommunicationBreakMapper;
    @Autowired
    private FiberMapper fiberMapper;
    @Autowired
    private AreaCommunicationBreakProportionMapper areaCommunicationBreakProportionMapper;

    @Override
    public ResultState getVideoMonitor() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<MapModel> normalArray = videoMonitorMapper.getVideoMonitor((byte)0);
        List<MapModel> breakArray= videoMonitorMapper.getVideoMonitor((byte)1);
        map.put("normal",normalArray);
        map.put("break",breakArray);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getTVAndWifi() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> areas = new ArrayList<>();
        ArrayList<Integer> tvAmount = new ArrayList<>();
        ArrayList<Integer> wifiAmount = new ArrayList<>();
        List<ChartModel> list=tvWifiMapper.getTVAndWifi();
        if(list !=null && list.size()>0) {
            for (ChartModel chartModel : list) {
                if(!areas.contains(chartModel.getName())){
                    areas.add((String) chartModel.getName());
                }
                if("数字电视".equals(chartModel.getTemp())){
                    tvAmount.add((Integer) chartModel.getValue());
                } else if("wifi".equals(chartModel.getTemp())){
                    wifiAmount.add((Integer) chartModel.getValue());
                }
            }
        }
        map.put("wifiAmount",wifiAmount);
        map.put("tvAmount",tvAmount);
        map.put("areas",areas);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getAreaCommBreak() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        Integer total=0;
        BigDecimal newGrowth=null;
        ArrayList<String> years = new ArrayList<>();
        ArrayList<Integer> amount = new ArrayList<>();
        ArrayList<BigDecimal> growth = new ArrayList<>();
        List<ChartModel> list= areaCommunicationBreakMapper.getAreaCommBreak();
        if(list !=null && list.size()>0) {
            for (ChartModel chartModel : list) {
                years.add(chartModel.getName()+"年");
                total+=(Integer) chartModel.getValue();
                amount.add((Integer) chartModel.getValue());
                growth.add((BigDecimal) chartModel.getTemp());
                newGrowth=(BigDecimal) chartModel.getTemp();
            }
        }
        map.put("total",total);
        map.put("newGrowth",newGrowth);
        map.put("years",years);
        map.put("amount",amount);
        map.put("growth",growth);
        resultState.setData(map);
        return resultState;
    }


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

    @Override
    public ResultState getFiber() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        Double total=0.0;
        BigDecimal newGrowth=null;
        ArrayList<String> years = new ArrayList<>();
        ArrayList<Double> amount = new ArrayList<>();
        ArrayList<BigDecimal> growth = new ArrayList<>();
        List<ChartModel> list= fiberMapper.getFiber();
        if(list !=null && list.size()>0) {
            for (ChartModel chartModel : list) {
                years.add(chartModel.getName()+"年");
                total+=(Double) chartModel.getValue();
                amount.add((Double) chartModel.getValue());
                growth.add((BigDecimal) chartModel.getTemp());
                newGrowth=(BigDecimal) chartModel.getTemp();
            }
        }
        map.put("total",total);
        map.put("newGrowth",newGrowth);
        map.put("years",years);
        map.put("amount",amount);
        map.put("growth",growth);
        resultState.setData(map);
        return resultState;
    }
}
