package com.manage.service.ipml;

import com.manage.dao.Power_UserMapper;
import com.manage.service.cache.CacheManager;
import com.manage.util.SigarUtils;
import org.hyperic.sigar.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.util.*;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/10/23 9:56
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/10/23 9:56
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class PageServiceImpl {
    @Value("${login.times}")
    private Integer LOGINTIMES;
    //获取cpu温度cvs文件路径
    @Value("${TEMPERATURECVSFILEDIR}")
    private String TEMPERATURECVSFILEDIR;
    //cpu温度cvs文件显示温度数据的行数
    @Value("${TEMPERATUREROWNUM}")
    private int TEMPERATUREROWNUM;
    @Autowired
    private Power_UserMapper userMapper;
    private int byteToMb = 1024 * 1024;
    public Integer selectEffectiveCount(){
        return userMapper.selectEffectiveCount();
    }

    public Map<String,List> getEcharts2(){
        Map<String,List> returnMap = new HashMap();
        /*List<String> dateList = getDayListOfMonth();
        //日期集合
        List<Integer> dayList = new ArrayList<>();
        //意向客户集合
        List<Integer> intentionalList   = new ArrayList<>();
        //签约客户集合
        List<Integer> contractedList  = new ArrayList<>();
        //长期客户集合
        List<Integer> longTermList = new ArrayList<>();
        //流失客户集合
        List<Integer> lossList = new ArrayList<>();
        //最大登录数集合
        List<Integer> maxCountList = new ArrayList<>();
        //记录有数据开始标识
        Boolean startFlag = false;
        //定义前一天的登录数量
        Integer beforeLoginCount = 0;
        //定义最大登录数量
        Integer maxCount = 0;
        for (int i = 0; i < dateList.size(); i++) {
            //获取当天登录次数缓存
            HashMap<String, Integer> map = CacheManager.getCurrentDayCount(dateList.get(i));
            if(null != map){
                //运算最大登录数
                if(map.size() > maxCount){
                    maxCount = map.size();
                }
                //添加签约
                contractedList.add(map.size());
                Integer intentionalUser = 0;
                Integer longUser = 0;
                //遍历运算意向和长期客户
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
                    if(entry.getValue() < LOGINTIMES){
                        //意向客户
                        intentionalUser++;
                    }else{
                        longUser++;
                    }
                }
                //添加意向客户
                intentionalList.add(intentionalUser);
                //添加长期客户
                longTermList.add(longUser);
                //添加流失客户
                if(beforeLoginCount - map.size() < 0){
                    //比昨天还多流失为0
                    lossList.add(0);
                }else{
                    //否则计算流失
                    lossList.add(beforeLoginCount - map.size());
                }
                //赋值当天数量
                beforeLoginCount = map.size();
                if(!startFlag){
                    startFlag = true;
                }
            }
            //开始有数据，添加进日期集合
            if(startFlag){
                //添加日期
                dayList.add(i+1);
            }
        }
        maxCountList.add(maxCount);
        //添加最大登录数
        returnMap.put("maxCountList",maxCountList);
        //添加日期集合
        returnMap.put("dayList",dayList);
        //添加意向客户
        returnMap.put("intentionalList",intentionalList);
        //添加签约客户
        returnMap.put("contractedList",contractedList);
        //添加长期客户
        returnMap.put("longTermList",longTermList);
        //添加流失客户
        returnMap.put("lossList",lossList);*/
        return returnMap;
    }

    public Map<String,List> getEcharts3(){
        Map<String,List> returnMap = new HashMap();
        /*List<String> dateList = getDayListOfMonth();
        //日期集合
        List<Integer> dayList = new ArrayList<>();
        //登录次数集合
        List<Integer> dataList  = new ArrayList<>();
        //最大登录数集合
        List<Integer> maxCountList = new ArrayList<>();
        //记录有数据开始标识
        Boolean startFlag = false;
        //定义统计登录数量
        Integer maxCount = 0;
        for (int i = 0; i < dateList.size(); i++) {
            //定义当天登录数
            Integer loginCount = 0;
            //获取当天登录次数缓存
            HashMap<String, Integer> map = CacheManager.getCurrentDayCount(dateList.get(i));
            if(null != map){
                for(Map.Entry<String,Integer> entry : map.entrySet()){
                    //运算最大登录数
                    maxCount += entry.getValue();
                    //赋值当天数量
                    loginCount += entry.getValue();
                }
                if(!startFlag){
                    startFlag = true;
                }
                //添加每天登录数
                dataList.add(loginCount);
            }
            //开始有数据，添加进日期集合
            if(startFlag){
                //添加日期
                dayList.add(i+1);
            }
        }
        maxCountList.add(maxCount);
        //添加最大登录数
        returnMap.put("maxCountList",maxCountList);
        //添加日期集合
        returnMap.put("dayList",dayList);
        //日期对应登录数
        returnMap.put("dataList",dataList);*/
        return returnMap;
    }

    public int getEcharts4() throws Exception{
        SigarUtils.initSigar();
        int cpuCount = cpu();
        return cpuCount;
    }

    private static int cpu() throws SigarException {
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuList[] = null;
        cpuList = sigar.getCpuPercList();
        double combined = 0;
        for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
            combined += cpuList[i].getCombined()*100.0D;
        }
        return (int)combined/6;
    }

    public long getEcharts5(){
        long temperature = 0;
        try {
            File file = new File(TEMPERATURECVSFILEDIR);
            InputStream input = null;
            try {
                input = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            StringBuilder buffer = new StringBuilder();
            byte[] bytes = new byte[1024];
            try {
                assert input != null;
                for(int n; (n = input.read(bytes))!=-1 ; ){
                    buffer.append(new String(bytes,0,n,"GBK"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            int i = buffer.toString().lastIndexOf("</TD>");
            String substring = buffer.substring(0, i);
            String substring1 = substring.substring(substring.length() - 4, substring.length());
            float f = Float.valueOf(substring1);
            temperature = Math.round(f);
        }catch (Exception e){
            e.printStackTrace();
        }
        return temperature;
    }

    public List<String> getEcharts6() throws Exception{
        List<String> list = new ArrayList<>();
        //初始化
        SigarUtils.initSigar();
        Sigar sigar = new Sigar();
        //获取内存对象
        Mem mem = sigar.getMem();
        //计算总内存
        String total= String.format("%.0f", mem.getTotal() / byteToMb / 1000.0);
        //计算实际内存(物理总内存)
        String totalPhysicalMemorySize = String.format("%.1f", mem.getTotal() / byteToMb / 1024.0);
        //计算内存使用量
        String user =  String.format("%.1f",mem.getUsed() / byteToMb / 1024.0);
        list.add(total);
        list.add(totalPhysicalMemorySize);
        list.add(user);
        return list;
    }

    //获取当月所有天
    /**
     * 获取当月所有天
     * @return
     */
    public static List<String> getDayListOfMonth() {
        List<String> list = new ArrayList<String>();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate = year+"-"+month+"-"+i;
            list.add(aDate);
        }
        return list;
    }
}
