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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.sysnew.popedomData.dao.ISysPopedomDataDao;
import com.hzya.frame.sysnew.popedomData.entity.SysPopedomDataEntity;
import com.hzya.frame.sysnew.popedomHome.dao.ISysPopedomHomeDao;
import com.hzya.frame.sysnew.popedomHome.entity.SysPopedomHomeEntity;
import com.hzya.frame.sysnew.popedomInterface.service.impl.InterfaceCache;
import com.hzya.frame.sysnew.popedomMenu.dao.ISysPopedomMenuDao;
import com.hzya.frame.sysnew.popedomMenu.entity.SysPopedomMenuEntity;
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
import com.hzya.frame.sysnew.popedomOperate.entity.SysPopedomOperateEntity;
import com.hzya.frame.sysnew.role.dao.ISysRoleDao;
import com.hzya.frame.sysnew.role.entity.SysRoleEntity;
import com.hzya.frame.sysnew.role.service.ISysRoleService;
import com.hzya.frame.sysnew.userRoles.dao.ISysUserRolesDao;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
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.util.List;

/**
 * 角色表(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2024-03-27 11:49:39
 */
@Service(value = "sysRoleService")
public class SysRoleServiceImpl extends BaseService<SysRoleEntity, String> implements ISysRoleService {
	@Resource
	private InterfaceCache interfaceCache;

    private ISysRoleDao sysRoleDao;
    @Resource
	private ISysUserRolesDao sysUserRolesDao;
	@Resource
	private ISysPopedomDataDao sysPopedomDataDao;
	@Resource
	private ISysPopedomHomeDao sysPopedomHomeDao;
	@Resource
	private ISysPopedomMenuDao sysPopedomMenuDao;
	@Resource
	private ISysPopedomOperateDao sysPopedomOperateDao;
    @Autowired
	public void setSysRoleDao(ISysRoleDao dao) {
		this.sysRoleDao = dao;
		this.dao = dao;
	}

	/**
	 * @Author lvleigang
	 * @Description  查询角色列表分页
	 * @Date 11:52 上午 2024/3/27
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryEntityPage(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		//判断分页
		if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
		List<SysRoleEntity> sysApplicationVos = sysRoleDao.queryByLike(entity);
		PageInfo pageInfo = new PageInfo(sysApplicationVos);
		return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
	}


	/**
	 * @Author lvleigang
	 * @Description  查询角色列表
	 * @Date 11:52 上午 2024/3/27
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryEntity(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		if(entity == null ){
			entity = new SysRoleEntity();
		}
		List<SysRoleEntity> sysApplicationVos = sysRoleDao.queryByLike(entity);
		return BaseResult.getSuccessMessageEntity("查询数据成功", sysApplicationVos);
	}
	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 保存角色
	 * @Date 2:36 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity saveEntity(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getRoleName() == null || "".equals(entity.getRoleName())) {
			return BaseResult.getFailureMessageEntity("请先输入角色名称");
		}
		if (entity.getRoleCode() == null || "".equals(entity.getRoleCode())) {
			return BaseResult.getFailureMessageEntity("请先输入角色编码");
		}
		entity.setCreate();
		//保存
		sysRoleDao.save(entity);
		interfaceCache.reloadData("2");

		return BaseResult.getSuccessMessageEntity("保存角色成功", entity);
	}
	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 获取角色
	 * @Date 2:36 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity getEntity(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity = sysRoleDao.get(entity.getId());
		if(entity == null ){
			return BaseResult.getFailureMessageEntity("获取角色失败");
		}
		return BaseResult.getSuccessMessageEntity("获取角色成功", entity);
	}
	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 修改角色
	 * @Date 2:37 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity updateEntity(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getRoleName() == null || "".equals(entity.getRoleName())) {
			return BaseResult.getFailureMessageEntity("请先输入角色名称");
		}
		if (entity.getRoleCode() == null || "".equals(entity.getRoleCode())) {
			return BaseResult.getFailureMessageEntity("请先输入角色编码");
		}
		entity.setUpdate();
		//保存
		sysRoleDao.update(entity);
		interfaceCache.reloadData("2");

		return BaseResult.getSuccessMessageEntity("修改角色成功", entity);

	}

	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 删除角色
	 * @Date 2:37 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity deleteEntity(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if(!checkRoleUse(entity)){
			return BaseResult.getFailureMessageEntity("当前角色已经使用，不允许删除");
		}
		entity.setUpdate();
		sysRoleDao.logicRemove(entity);
		interfaceCache.reloadData("2");

		return BaseResult.getSuccessMessageEntity("删除角色成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  校验角色是否使用 目前只校验（用户角色、角色权限配置下的）
	 * @Date 3:27 下午 2024/3/27
	 * @param entity
	 * @return boolean
	 **/
	private boolean checkRoleUse(SysRoleEntity entity) {
		SysUserRolesEntity sysUserRolesEntity = new SysUserRolesEntity();
		sysUserRolesEntity.setRoleId(entity.getId());
		if(sysUserRolesDao.getCount(sysUserRolesEntity) > 0){
			return false;
		}
		SysPopedomDataEntity sysPopedomDataEntity = new SysPopedomDataEntity();
		sysPopedomDataEntity.setObjectId(entity.getId());
		sysPopedomDataEntity.setKindId("2");
		if(sysPopedomDataDao.getCount(sysPopedomDataEntity) > 0){
			return false;
		}
		SysPopedomHomeEntity sysPopedomHomeEntity = new SysPopedomHomeEntity();
		sysPopedomHomeEntity.setObjectId(entity.getId());
		sysPopedomHomeEntity.setKindId("2");
		if(sysPopedomHomeDao.getCount(sysPopedomHomeEntity) > 0){
			return false;
		}
		SysPopedomMenuEntity sysPopedomMenuEntity = new SysPopedomMenuEntity();
		sysPopedomMenuEntity.setObjectId(entity.getId());
		sysPopedomMenuEntity.setKindId("2");
		if(sysPopedomMenuDao.getCount(sysPopedomMenuEntity) > 0){
			return false;
		}
		SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
		sysPopedomOperateEntity.setObjectId(entity.getId());
		sysPopedomOperateEntity.setKindId("2");
		if(sysPopedomOperateDao.getCount(sysPopedomOperateEntity) > 0){
			return false;
		}
		return true;
	}


	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 启用停用角色
	 * @Date 2:37 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
		SysRoleEntity entity = getData("jsonStr", jsonObject, SysRoleEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getEnableState() == null || "".equals(entity.getEnableState())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		//0启用 1 停用
		if("0".equals(entity.getEnableState())){
			entity.setUpdate();
			sysRoleDao.update(entity);
			interfaceCache.reloadData("2");

			return BaseResult.getSuccessMessageEntity("启用角色成功");
		}else {
			//删除用户角色表
			SysUserRolesEntity sysUserRolesEntity = new SysUserRolesEntity();
			sysUserRolesEntity.setRoleId(entity.getId());
			sysUserRolesEntity.setUpdate();
			sysUserRolesDao.logicRemoveMultiCondition(sysUserRolesEntity);

			entity.setUpdate();
			sysRoleDao.update(entity);
			interfaceCache.reloadData("2");
			interfaceCache.reloadData("3");

			return BaseResult.getSuccessMessageEntity("停用角色成功");
		}
	}
}
