package com.nantian.cloud.dbcapp.service.impl;

import com.nantian.cloud.dbcapp.config.OpsConfig;
import com.nantian.cloud.dbcapp.mapper.jly.SysInfoMapper;
import com.nantian.cloud.dbcapp.mapper.record.EnterpriseEnergyDataMapper;
import com.nantian.cloud.dbcapp.service.OpsRecordService;
import com.nantian.sc.cloud.common.core.util.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("opsRecordService")
public class OpsRecordServiceImpl implements OpsRecordService {

    private static String DB_NAME = OpsConfig.getOpsConfig("ops-db");

    @Autowired
    private SysInfoMapper sysInfoMapper;

    @Autowired
    private EnterpriseEnergyDataMapper energyDataMapper;

    @Override
    public R getSummaryInfo(String startDate, String endDate, String type) {
        if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate) || StringUtils.isBlank(type)){
            return R.ok("报告起止时间和数据汇总类型必传！");
        }

        //type取值 不传值时，默认查询报告起止时间内数据
        if("1".equals(type)){ //平台运行至报告截止日期
            startDate = ""; //报告开始时间置为空
        }else if("2".equals(type)){ //报告所属年1月至报告截止日期
            startDate = StringUtils.substring(startDate,0,4)+"-01-01";
        }

        sysInfoMapper.renewLoginLog(DB_NAME, "用户登录成功"); //更新登录日志
        Map<String, Object> map = new HashMap<>();

        List<Map<String, String>> account = sysInfoMapper.getAccount(DB_NAME, startDate, endDate);
        map.put("account",account.get(0)); //累计分配账号
        map.put("login",account.get(1)); //累计登录
        map.put("activeAccount",account.get(2)); //累计活跃账号

        List<Map<String, String>> enterpriseDetail = energyDataMapper.getEnterpriseDetail(startDate, endDate, type);
        map.put("accessEnterprise",enterpriseDetail.get(0)); //累计接入完成企业家数
        map.put("formulaEnterprise",enterpriseDetail.get(1)); //累计公式配置企业家数

        Map<String, String> enterpriseDataDetail = energyDataMapper.getEnterpriseDataDetail(startDate, endDate);
        map.put("enterpriseData",enterpriseDataDetail); //累计公式配置、在线采集数据量

        //Map<String, String> exception = energyDataMapper.getEnterpriseByException(startDate, endDate);
        //map.put("exception",exception); //数据不合格企业

        Map<String, String> exceptionDataDetail = energyDataMapper.getExceptionDetailGroupByType(startDate, endDate);
        map.put("exceptionData",exceptionDataDetail); //按异常类型获取各异常情况
        map.put("remark","备注：数据统计周期为 "+("".equals(startDate)?"平台运行":startDate)+" 至 "+endDate);
        return R.ok(map);
    }

    @Override
    public R getAccountInfo(String startDate, String endDate, String type) {
        if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate) || StringUtils.isBlank(type)){
            return R.ok("报告起止时间和数据汇总类型必传！");
        }

        //type取值 不传值时，默认查询报告起止时间内数据
        if("1".equals(type)){ //平台运行至报告截止日期
            startDate = ""; //报告开始时间置为空
        }else if("2".equals(type)){ //报告所属年1月至报告截止日期
            startDate = StringUtils.substring(startDate,0,4)+"-01-01";
        }

        Map<String, Object> map = new HashMap<>();
        sysInfoMapper.renewLoginLog(DB_NAME, "用户登录成功"); //更新登录日志

        List<Map<String, String>> login = sysInfoMapper.getLoginGroupByDate(DB_NAME, startDate, endDate);
        map.put("login", login); //累计登录
        List<Map<String, String>> deptLogin = sysInfoMapper.getDeptLoginGroupByDate(DB_NAME, startDate, endDate);
        map.put("deptLogin", deptLogin); //政府各级部门累计登录

        List<Map<String, String>> activeAccount = sysInfoMapper.getActiveAccountGroupByDate(DB_NAME, startDate, endDate);
        map.put("activeAccount", activeAccount); //累计活跃账户
        List<Map<String, String>> deptActiveAccount = sysInfoMapper.getDeptActiveAccountGroupByDate(DB_NAME, startDate, endDate);
        map.put("deptActiveAccount", deptActiveAccount); //政府各级部门累计活跃账户
        map.put("remark","备注：数据统计周期为 "+("".equals(startDate)?"平台运行":startDate)+" 至 "+endDate);
        return R.ok(map);
    }
}
