package com.cyksj.service.mange.cms.manager.service.impl;


import com.cyksj.mapper.manage.cms.CmsMenuMapper;
import com.cyksj.mapper.manage.cms.CmsRoleMapper;
import com.cyksj.mapper.manage.cms.CmsUserMapper;
import com.cyksj.model.entity.CmsMenu;
import com.cyksj.model.entity.CmsRole;
import com.cyksj.model.entity.CmsUser;
import com.cyksj.model.views.CmsUserVO;
import com.cyksj.redis.RedisService;
import com.cyksj.service.mange.cms.manager.service.CacheService;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *项目名: yicang-admin
 *文件名: CacheServiceImpl
 *创建者: JavaZou
 *创建时间:2022/9/30 14:11
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CacheServiceImpl implements CacheService {
	private final CmsUserMapper cmsUserMapper;

	private final CmsRoleMapper cmsRoleMapper;

	private final CmsMenuMapper cmsMenuMapper;

	private final RedisService redisService;

	private final ObjectMapper mapper;

	@Override
	public void saveUser(Long userId) throws Exception {
		CmsUserVO cmsUserVO = cmsUserMapper.getCmsUserDetailByUserId(userId);
		this.deleteUserCache(userId);
		redisService.set(RedisService.key.CMS_USER_KEY.getNameFormat(userId), mapper.writeValueAsString(cmsUserVO));
	}

	@Override
	public void saveRoles(Long userId) throws Exception {
		List<CmsRole> roles = cmsRoleMapper.getCmsRolesByUserId(userId);
		if (roles.size() > 0) {
			this.deleteUserRolesCache(userId);
			redisService.set(RedisService.key.CMS_USER_ROLE_KEY.getNameFormat(userId), mapper.writeValueAsString(roles));
		}
	}

	@Override
	public void savePermissions(Long userId) throws Exception {
		List<CmsMenu> cmsMenus = cmsMenuMapper.getCmsMenusByUserId(userId);
		if (cmsMenus.size() > 0) {
			this.deleteUserPermissionsCache(userId);
			redisService.set(RedisService.key.CMS_USER_PERMISSION_KEY.getNameFormat(userId), mapper.writeValueAsString(cmsMenus));
		}
	}

	private void deleteUserRolesCache(Long userId) {
		redisService.del(RedisService.key.CMS_USER_ROLE_KEY.getNameFormat(userId));
	}

	private void deleteUserPermissionsCache(Long userId) {
		redisService.del(RedisService.key.CMS_USER_PERMISSION_KEY.getNameFormat(userId));
	}

	@Override
	public void deleteUserCache(Long userId) {
		redisService.del(RedisService.key.CMS_USER_KEY.getNameFormat(userId));
	}

	@SneakyThrows
	@Override
	public List<CmsRole> getUserRoleCache(Long userId){
		Object cmsRoleStr = redisService.get(RedisService.key.CMS_USER_ROLE_KEY.getNameFormat(userId));
		if (cmsRoleStr == null) {
			return null;
		}
		JavaType type = mapper.getTypeFactory().constructParametricType(List.class, CmsRole.class);
		return mapper.readValue(cmsRoleStr.toString(), type);
	}

	@SneakyThrows
	@Override
	public List<CmsMenu> getUserPermsCache(Long userId){
		Object cmsMenusStr = redisService.get(RedisService.key.CMS_USER_PERMISSION_KEY.getNameFormat(userId));
		if (cmsMenusStr == null) {
			return null;
		}
		JavaType type = mapper.getTypeFactory().constructParametricType(List.class, CmsMenu.class);
		return mapper.readValue(cmsMenusStr.toString(), type);
	}

	@SneakyThrows
	@Override
	public CmsUser getUserCache(Long userId) {
		Object o = redisService.get(RedisService.key.CMS_USER_KEY.getNameFormat(userId));
		if (o == null) {
			return null;
		}
		return mapper.readValue(o.toString(), CmsUser.class);
	}

	@Override
	public void delUserRoleCache(Long userId) {
		redisService.del(RedisService.key.CMS_USER_ROLE_KEY.getNameFormat(userId));
	}

	@Override
	public void delUserPermissionsCache(Long userId) {
		redisService.del(RedisService.key.CMS_USER_PERMISSION_KEY.getNameFormat(userId));
	}
}
