package com.hzya.frame.home.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.home.dao.IHomeDao;
import com.hzya.frame.home.entity.HomeEntity;
import com.hzya.frame.home.service.IHomeService;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@Service(value = "homeService")
public class HomeServiceImpl extends BaseService<HomeEntity, String> implements IHomeService {
     
    private IHomeDao homeDao;
    @Resource
	private ISysApplicationDao sysApplicationDao;
    @Autowired
	public void setSysApplicationApiDao(IHomeDao dao) {
		this.homeDao = dao;
		this.dao = dao;
	}
	/**
	 * @Author lvleigang
	 * @Description  分组统计应用下接口日志表失败的数量（返回应用名称、失败数）
	 * @Date 10:17 上午 2024/5/11
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity appErrorNum(JSONObject jsonObject) {
		HomeEntity homeEntity = new HomeEntity();
		List<HomeEntity> homeEntities = homeDao.getAppErrorNum(homeEntity);
		if(homeEntities != null && homeEntities.size() > 0){
			SysApplicationEntity sysApplicationEntity = new SysApplicationEntity();
			sysApplicationEntity.setSts("Y");
			List<SysApplicationEntity> sysApplicationEntities = sysApplicationDao.queryByLike(sysApplicationEntity);
			if(sysApplicationEntities != null && sysApplicationEntities.size() > 0){
				for (int i = 0; i < homeEntities.size(); i++) {
					for (int a = 0; a < sysApplicationEntities.size(); a++) {
						if(homeEntities.get(i).getAppId()!= null && sysApplicationEntities.get(a).getId().equals(homeEntities.get(i).getAppId())){
							homeEntities.get(i).setPath(sysApplicationEntities.get(a).getAppLogo());
							homeEntities.get(i).setName(sysApplicationEntities.get(a).getName());
							continue;
						}
					}
				}
			}
		}
		return BaseResult.getSuccessMessageEntity("查询数据成功",homeEntities);
	}
	/**
	 * @Author lvleigang
	 * @Description  分组统计应用下面api的总数量，以及是否有授权的api数量，（返回应用名称、api总数量、启用数量、停用数量）
	 * @Date 10:17 上午 2024/5/11
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity appApiNum(JSONObject jsonObject) {
		HomeEntity homeEntity = new HomeEntity();
		List<HomeEntity> homeEntities = homeDao.appApiNum(homeEntity);
		if(homeEntities != null && homeEntities.size() > 0){
			List<HomeEntity> useApiNum = homeDao.useApiNum(homeEntity);
			if(useApiNum != null && useApiNum.size() > 0){
				for (int i = 0; i < homeEntities.size(); i++) {
					for (int a = 0; a < useApiNum.size(); a++) {
						if(homeEntities.get(i).getAppId()!= null && useApiNum.get(a).getAppId().equals(homeEntities.get(i).getAppId())){
							homeEntities.get(i).setNormalNum(useApiNum.get(a).getNormalNum());
							homeEntities.get(i).setAbnormalNum(homeEntities.get(i).getNum() - useApiNum.get(a).getNormalNum());
							continue;
						}
					}
				}
			}

			SysApplicationEntity sysApplicationEntity = new SysApplicationEntity();
			sysApplicationEntity.setSts("Y");
			List<SysApplicationEntity> sysApplicationEntities = sysApplicationDao.queryByLike(sysApplicationEntity);
			if(sysApplicationEntities != null && sysApplicationEntities.size() > 0){
				for (int i = 0; i < homeEntities.size(); i++) {
					for (int a = 0; a < sysApplicationEntities.size(); a++) {
						if(homeEntities.get(i).getAppId()!= null && sysApplicationEntities.get(a).getId().equals(homeEntities.get(i).getAppId())){
							homeEntities.get(i).setPath(sysApplicationEntities.get(a).getAppLogo());
							continue;
						}
					}
				}
			}
		}
		return BaseResult.getSuccessMessageEntity("查询数据成功",homeEntities);
	}
	/**
	 * @Author lvleigang
	 * @Description  查询任务监控表数据（返回任务名称、状态、总数、上次耗时、上次执行时间）
	 * @Date 10:17 上午 2024/5/11
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity taskNum(JSONObject jsonObject) {
		HomeEntity homeEntity = new HomeEntity();
		HomeEntity runhome = homeDao.taskNumRun(homeEntity);
		HomeEntity stophome = homeDao.taskNumStop(homeEntity);
		homeEntity.setNormalNum(runhome.getNormalNum());
		homeEntity.setAbnormalNum(stophome.getAbnormalNum());
		homeEntity.setNum(runhome.getNormalNum() + stophome.getAbnormalNum());
		return BaseResult.getSuccessMessageEntity("查询数据成功",homeEntity);
	}

	/**
	 * @Author lvleigang
	 * @Description  统计最近7天内api成功和失败数量
	 * @Date 10:17 上午 2024/5/11
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity sevenNum(JSONObject jsonObject) {
		List<HomeEntity> homeEntities = new ArrayList<>();
		HomeEntity homeEntity = new HomeEntity();
		List<HomeEntity> errornum = homeDao.sevenerrornum(homeEntity);
		List<HomeEntity> successnum = homeDao.sevensuccessnum(homeEntity);
		for (int i = 6; i >=0; i--) {
			String date = LocalDate.now().minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
			HomeEntity entity = new HomeEntity();
			entity.setName(date);
			entity.setNormalNum(0);
			entity.setAbnormalNum(0);
			for (int a = 0; a < errornum.size(); a++) {
				if(date.equals(errornum.get(a).getName())){
					entity.setAbnormalNum(errornum.get(a).getAbnormalNum());
					continue;
				}
			}
			for (int a = 0; a < successnum.size(); a++) {
				if(date.equals(successnum.get(a).getName())){
					entity.setNormalNum(successnum.get(a).getNormalNum());
					continue;
				}
			}
			homeEntities.add(entity);
		}
		return BaseResult.getSuccessMessageEntity("查询数据成功",homeEntities);
	}
}
