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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.message.entity.SysMessageEntity;
import com.hzya.frame.sysnew.message.dao.ISysMessageDao;
import com.hzya.frame.sysnew.message.service.ISysMessageService;
import com.hzya.frame.sysnew.organ.entity.SysOrganEntity;
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;

/**
 * (SysMessage)表服务实现类
 *
 * @author makejava
 * @since 2024-08-30 14:17:11
 */
@Service(value = "sysMessageService")
public class SysMessageServiceImpl extends BaseService<SysMessageEntity, String> implements ISysMessageService {
     
    private ISysMessageDao sysMessageDao;
    
    @Autowired
	public void setSysMessageDao(ISysMessageDao dao) {
		this.sysMessageDao = dao;
		this.dao = dao;
	}

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

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

	@Override
	public JsonResultEntity getEntity(JSONObject jsonObject){
		SysMessageEntity entity = getData("jsonStr", jsonObject, SysMessageEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity = sysMessageDao.get(entity.getId());
		if(entity == null){
			return BaseResult.getFailureMessageEntity("获取站内消息失败");
		}
		return BaseResult.getSuccessMessageEntity("获取站内消息成功", entity);
	}

	@Override
	public JsonResultEntity saveEntity(JSONObject jsonObject) {
		SysMessageEntity entity = getData("jsonStr", jsonObject, SysMessageEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		entity.setCreate();
		//保存
		sysMessageDao.save(entity);
		return BaseResult.getSuccessMessageEntity("保存消息成功", entity);
	}

	@Override
	public JsonResultEntity updateEntity(JSONObject jsonObject) {
		SysMessageEntity entity = getData("jsonStr", jsonObject, SysMessageEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setUpdate();
		sysMessageDao.update(entity);
		return BaseResult.getSuccessMessageEntity("修改站内消息成功", entity);
	}

	@Override
	public JsonResultEntity deleteEntity(JSONObject jsonObject) {
		SysMessageEntity entity = getData("jsonStr", jsonObject, SysMessageEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		sysMessageDao.logicRemove(entity);
		return BaseResult.getSuccessMessageEntity("删除站内消息成功");
	}
}
