package com.hzya.frame.sysnew.warningInterface.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationDto;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.application.service.impl.ApplicationCache;
import com.hzya.frame.sysnew.sendMessageLog.dao.ISysSendMessageLogDao;
import com.hzya.frame.sysnew.sendMessageLog.entity.SysSendMessageLogEntity;
import com.hzya.frame.sysnew.warningConfig.entity.SysWarningConfigEntity;
import com.hzya.frame.sysnew.warningInterface.entity.SysWarningInterfaceEntity;
import com.hzya.frame.sysnew.warningInterface.dao.ISysWarningInterfaceDao;
import com.hzya.frame.sysnew.warningInterface.service.ISysWarningInterfaceService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import com.hzya.frame.basedao.service.impl.BaseService;

import java.util.ArrayList;
import java.util.List;

/**
 * 预警接口表(SysWarningInterface)表服务实现类
 *
 * @author makejava
 * @since 2024-09-03 10:06:19
 */
@Service(value = "sysWarningInterfaceService")
public class SysWarningInterfaceServiceImpl extends BaseService<SysWarningInterfaceEntity, String> implements ISysWarningInterfaceService {
     
    private ISysWarningInterfaceDao sysWarningInterfaceDao;

	@Resource
	private ISysApplicationDao sysApplicationDao;

	@Resource
	private ISysSendMessageLogDao sysSendMessageLogDao;


    @Autowired
	public void setSysWarningInterfaceDao(ISysWarningInterfaceDao dao) {
		this.sysWarningInterfaceDao = dao;
		this.dao = dao;
	}

	@Override
	public JsonResultEntity queryEntityPage(JSONObject jsonObject){
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if(entity == null || entity.getPageNum() == null || entity.getPageSize() == null){
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
		List<SysWarningInterfaceEntity> list = sysWarningInterfaceDao.queryByLike(entity);
		PageInfo<SysWarningInterfaceEntity> pageInfo = new PageInfo<>(list);
		return BaseResult.getSuccessMessageEntity("查询数据成功",pageInfo);
	}

	@Override
	public JsonResultEntity queryEntity(JSONObject jsonObject){
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if(entity == null){
			entity = new SysWarningInterfaceEntity();
		}
		List<SysWarningInterfaceEntity> list = sysWarningInterfaceDao.queryByLike(entity);
		return BaseResult.getSuccessMessageEntity("查询数据成功",list);
	}

	@Override
	public JsonResultEntity getEntity(JSONObject jsonObject){
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("查询参数不存在");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity = sysWarningInterfaceDao.get(entity.getId());
		if(entity == null){
			return BaseResult.getFailureMessageEntity("获取预警接口失败");
		}
		return BaseResult.getSuccessMessageEntity("获取预警接口成功",entity);
	}

	@Override
	public JsonResultEntity saveEntity(JSONObject jsonObject){
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("未传入保存参数");
		}
		if(entity.getApiCode() == null || "".equals(entity.getApiCode())){
			return BaseResult.getFailureMessageEntity("接口编码不能为空");
		}
		if(entity.getPushMethod() == null || "".equals(entity.getPushMethod())){
			return BaseResult.getFailureMessageEntity("推送方式不能为空");
		}
		if(entity.getStatus() == null || "".equals(entity.getStatus())){
			return BaseResult.getFailureMessageEntity("状态不能为空");
		}
		if(entity.getWarningConfigId() == null || "".equals(entity.getWarningConfigId())){
			return BaseResult.getFailureMessageEntity("预警配置id不能为空");
		}
		entity.setCreate();
		sysWarningInterfaceDao.save(entity);
		return BaseResult.getSuccessMessageEntity("保存预警接口成功",entity);
	}

	@Override
	public JsonResultEntity updateEntity(JSONObject jsonObject){
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("查询参数不存在");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if(entity.getApiCode() == null || "".equals(entity.getApiCode())){
			return BaseResult.getFailureMessageEntity("接口编码不能为空");
		}
		if(entity.getPushMethod() == null || "".equals(entity.getPushMethod())){
			return BaseResult.getFailureMessageEntity("推送方式不能为空");
		}
		if(entity.getStatus() == null || "".equals(entity.getStatus())){
			return BaseResult.getFailureMessageEntity("状态不能为空");
		}
		if(entity.getWarningConfigId() == null || "".equals(entity.getWarningConfigId())){
			return BaseResult.getFailureMessageEntity("预警配置id不能为空");
		}
		entity.setUpdate();
		sysWarningInterfaceDao.update(entity);
		return BaseResult.getSuccessMessageEntity("修改预警接口成功",entity);
	}

	@Override
	public JsonResultEntity deleteEntity(JSONObject jsonObject){
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("查询参数不存在");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setUpdate();
		sysWarningInterfaceDao.logicRemove(entity);

		return BaseResult.getSuccessMessageEntity("删除预警接口成功");
	}

	@Override
	public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
		SysWarningInterfaceEntity entity = getData("jsonStr", jsonObject, SysWarningInterfaceEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getStatus() == null || "".equals(entity.getStatus())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		//0停用，1启用
		if("0".equals(entity.getStatus())){
			entity.setUpdate();
			sysWarningInterfaceDao.update(entity);
			return BaseResult.getSuccessMessageEntity("停用预警接口成功");
		}else{
			//停用消息模版
			entity.setUpdate();
			sysWarningInterfaceDao.update(entity);
			return BaseResult.getSuccessMessageEntity("启用预警接口成功");
		}

	}

	/**查询已经配置了预警的应用列表,并返回应用列表及其未读消息数*/
	@Override
	public JsonResultEntity queryWarningAppList(JSONObject jsonObject){
		List<SysWarningInterfaceEntity> appIdList = sysWarningInterfaceDao.getWarningAppList();
        List<SysApplicationEntity> appList = new ArrayList<>();
		for (SysWarningInterfaceEntity entity : appIdList) {
			if (entity == null) {
				continue;
			}
			String appId = entity.getAppId();
			SysApplicationEntity app = sysApplicationDao.get(appId);
			if (app == null) {
				continue;
			}
			SysSendMessageLogEntity logEntity = new SysSendMessageLogEntity();
			logEntity.setAppId(appId);
			logEntity.setState(0);
			List<SysSendMessageLogEntity> list = sysSendMessageLogDao.queryByLike(logEntity);
			app.setNewMessageCount(list.size());
			if (app != null) {
				appList.add(app);
			}
		}
		return BaseResult.getSuccessMessageEntity("查询预警应用列表成功",appList);
	}

}
