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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.warningConfig.entity.SysWarningConfigEntity;
import com.hzya.frame.sysnew.warningConfig.dao.ISysWarningConfigDao;
import com.hzya.frame.sysnew.warningConfig.service.ISysWarningConfigService;
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.List;

/**
 * 预警配置表(SysWarningConfig)表服务实现类
 *
 * @author makejava
 * @since 2024-09-03 09:28:23
 */
@Service(value = "sysWarningConfigService")
public class SysWarningConfigServiceImpl extends BaseService<SysWarningConfigEntity, String> implements ISysWarningConfigService {
     
    private ISysWarningConfigDao sysWarningConfigDao;
    
    @Autowired
	public void setSysWarningConfigDao(ISysWarningConfigDao dao) {
		this.sysWarningConfigDao = dao;
		this.dao = dao;
	}

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

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

	@Override
	public JsonResultEntity getEntity(JSONObject jsonObject){
		SysWarningConfigEntity entity = getData("jsonStr", jsonObject, SysWarningConfigEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		 entity = sysWarningConfigDao.get(entity.getId());
		if(entity == null){
			return BaseResult.getFailureMessageEntity("获取预警配置失败");
		}
		return BaseResult.getSuccessMessageEntity("获取预警配置成功",entity);
	}

	@Override
	public JsonResultEntity saveEntity(JSONObject jsonObject){
		SysWarningConfigEntity entity = getData("jsonStr", jsonObject, SysWarningConfigEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getMessageTemplateId() == null || "".equals(entity.getMessageTemplateId())){
			return BaseResult.getFailureMessageEntity("消息模版不允许为空");
		}
		if(entity.getAppId() == null || "".equals(entity.getAppId())){
			return BaseResult.getFailureMessageEntity("应用ID不允许为空");
		}
		if(entity.getWarningAppId() == null || "".equals(entity.getWarningAppId())){
			return BaseResult.getFailureMessageEntity("预警应用类型不允许为空");
		}
		if(entity.getStatus() == null || "".equals(entity.getStatus())){
			return BaseResult.getFailureMessageEntity("状态不允许为空");
		}
		if(entity.getRecipientId() == null || "".equals(entity.getRecipientId())){
			return BaseResult.getFailureMessageEntity("接收者ID不允许为空");
		}
		entity.setCreate();
		sysWarningConfigDao.save(entity);
		return BaseResult.getSuccessMessageEntity("保存预警配置成功",entity);
	}

	@Override
	public JsonResultEntity updateEntity(JSONObject jsonObject){
		SysWarningConfigEntity entity = getData("jsonStr", jsonObject, SysWarningConfigEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if(entity.getMessageTemplateId() == null || "".equals(entity.getMessageTemplateId())){
			return BaseResult.getFailureMessageEntity("消息模版不允许为空");
		}
		if(entity.getAppId() == null || "".equals(entity.getAppId())){
			return BaseResult.getFailureMessageEntity("应用ID不允许为空");
		}
		if(entity.getWarningAppId() == null || "".equals(entity.getWarningAppId())){
			return BaseResult.getFailureMessageEntity("预警应用类型不允许为空");
		}
		if(entity.getStatus() == null || "".equals(entity.getStatus())){
			return BaseResult.getFailureMessageEntity("状态不允许为空");
		}
		if(entity.getRecipientId() == null || "".equals(entity.getRecipientId())){
			return BaseResult.getFailureMessageEntity("接收者ID不允许为空");
		}
		entity.setUpdate();
		sysWarningConfigDao.update(entity);
		return BaseResult.getSuccessMessageEntity("修改预警配置成功",entity);
	}

	@Override
	public JsonResultEntity deleteEntity(JSONObject jsonObject) {
		SysWarningConfigEntity entity = getData("jsonStr", jsonObject, SysWarningConfigEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setUpdate();
		sysWarningConfigDao.logicRemove(entity);
		return BaseResult.getSuccessMessageEntity("删除预警配置成功");
	}

	@Override
	public JsonResultEntity enableDisableEntity(JSONObject jsonObject){
		SysWarningConfigEntity entity = getData("jsonStr", jsonObject, SysWarningConfigEntity.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();
			sysWarningConfigDao.update(entity);
			return BaseResult.getSuccessMessageEntity("启用模版成功");
		}else{
			//停用消息模版
			entity.setUpdate();
			sysWarningConfigDao.update(entity);
			return BaseResult.getSuccessMessageEntity("停用模版成功");
		}
	}

}
