|  |  |  | 
|---|
|  |  |  | package com.moral.api.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.moral.api.entity.SysDictData; | 
|---|
|  |  |  | import com.moral.api.entity.SysDictType; | 
|---|
|  |  |  | import com.moral.api.mapper.SysDictTypeMapper; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.sysDictType.SysDictTypeDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.sysDictType.SysDictTypeQueryDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.form.sysDictType.SysDictTypeDeleteForm; | 
|---|
|  |  |  | import com.moral.api.pojo.form.sysDictType.SysDictTypeInsertForm; | 
|---|
|  |  |  | import com.moral.api.pojo.form.sysDictType.SysDictTypeUpdateForm; | 
|---|
|  |  |  | import com.moral.api.service.SysDictDataService; | 
|---|
|  |  |  | import com.moral.api.service.SysDictTypeService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.util.CacheUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * <p> | 
|---|
|  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | SysDictTypeMapper sysDictTypeMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | SysDictDataService sysDictDataService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public SysDictTypeDTO deleteType(SysDictTypeDeleteForm form) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | SysDictTypeDTO dto = new SysDictTypeDTO(); | 
|---|
|  |  |  | //获取Id | 
|---|
|  |  |  | Integer typeId = form.getId(); | 
|---|
|  |  |  | //查询要删除的type用于插入日志 | 
|---|
|  |  |  | QueryWrapper<SysDictType> existWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | existWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | existWrapper.eq("id",typeId); | 
|---|
|  |  |  | SysDictType type = sysDictTypeMapper.selectOne(existWrapper); | 
|---|
|  |  |  | if(ObjectUtils.isEmpty(type)){ | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.DICTTYPE_NOT_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.DICTTYPE_NOT_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //执行逻辑删除 | 
|---|
|  |  |  | UpdateWrapper deleteTypeWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteTypeWrapper.eq("id",typeId); | 
|---|
|  |  |  | deleteTypeWrapper.set("is_delete",Constants.DELETE); | 
|---|
|  |  |  | sysDictTypeMapper.update(null,deleteTypeWrapper); | 
|---|
|  |  |  | //逻辑删除类型对应的数据 | 
|---|
|  |  |  | UpdateWrapper deleteDataWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteDataWrapper.eq("dict_type_id",typeId); | 
|---|
|  |  |  | deleteDataWrapper.set("is_delete",Constants.DELETE); | 
|---|
|  |  |  | sysDictDataService.update(null,deleteDataWrapper); | 
|---|
|  |  |  | //刷新缓存数据 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回数据 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public SysDictTypeDTO updateType(SysDictTypeUpdateForm form) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | SysDictTypeDTO dto = new SysDictTypeDTO(); | 
|---|
|  |  |  | //获取实体 | 
|---|
|  |  |  | SysDictType type = form.formConvertEntity(); | 
|---|
|  |  |  | //查询type用于插入日志 | 
|---|
|  |  |  | QueryWrapper<SysDictType> existNameWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | existNameWrapper.eq("id",type.getId()); | 
|---|
|  |  |  | existNameWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | SysDictType existType = sysDictTypeMapper.selectOne(existNameWrapper); | 
|---|
|  |  |  | if(ObjectUtils.isEmpty(existType)){ | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.DICTTYPE_NOT_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.DICTTYPE_NOT_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //执行更新 | 
|---|
|  |  |  | sysDictTypeMapper.updateById(type); | 
|---|
|  |  |  | //获取更新后的对象 | 
|---|
|  |  |  | SysDictType sysDictType = sysDictTypeMapper.selectById(type.getId()); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public SysDictTypeDTO insertType(SysDictTypeInsertForm form) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | SysDictTypeDTO dto = new SysDictTypeDTO(); | 
|---|
|  |  |  | //获取要插入的实体 | 
|---|
|  |  |  | SysDictType sysDictType = form.formConvertEntity(); | 
|---|
|  |  |  | //判断name是否已经存在 | 
|---|
|  |  |  | QueryWrapper<SysDictType> existNameWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | existNameWrapper.eq("name",sysDictType.getName()); | 
|---|
|  |  |  | existNameWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | SysDictType existType = sysDictTypeMapper.selectOne(existNameWrapper); | 
|---|
|  |  |  | if(!ObjectUtils.isEmpty(existType)){ | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.DICTTYPE_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.DICTTYPE_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //插入类型 | 
|---|
|  |  |  | sysDictTypeMapper.insert(sysDictType); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 获取所有字典类型和字典数据 | 
|---|
|  |  |  | * @Param: [] | 
|---|
|  |  |  | * @return: com.moral.api.pojo.dto.sysDictType.SysDictTypeQueryDTO | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/15 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public SysDictTypeQueryDTO queryTypeAndDatas() { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | SysDictTypeQueryDTO queryDTO = new SysDictTypeQueryDTO(); | 
|---|
|  |  |  | List<SysDictTypeDTO> dtos = new ArrayList<>(); | 
|---|
|  |  |  | //获取所有类型 | 
|---|
|  |  |  | List<SysDictType> types = getAllDictType(); | 
|---|
|  |  |  | //获取所有数据 | 
|---|
|  |  |  | Map<String, List<SysDictData>> datas = sysDictDataService.getAllDictData(); | 
|---|
|  |  |  | for (SysDictType type : types) { | 
|---|
|  |  |  | SysDictTypeDTO dto = new SysDictTypeDTO(); | 
|---|
|  |  |  | List<SysDictData> typeDatas = (List<SysDictData>) datas.get(type.getName()); | 
|---|
|  |  |  | dto.setSysDictType(type); | 
|---|
|  |  |  | dto.setSysDictDatas(typeDatas); | 
|---|
|  |  |  | dtos.add(dto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //封装返回数据 | 
|---|
|  |  |  | queryDTO.setSysDictTypeDTOS(dtos); | 
|---|
|  |  |  | queryDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | queryDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return queryDTO; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 获取所有字典类型,没有字典数据 | 
|---|
|  |  |  | * @Param: [] | 
|---|
|  |  |  | * @return: java.util.List<com.moral.api.entity.sysDictType> | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/4/15 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<SysDictType> getAllDictType() { | 
|---|
|  |  |  | //从缓存中取出 | 
|---|
|  |  |  | List<SysDictType> types = (List<SysDictType>) redisTemplate.opsForValue().get(RedisConstants.DICT_TYPE_KEY); | 
|---|
|  |  |  | if(ObjectUtils.isEmpty(types)){//从数据库取出并且插入缓存 | 
|---|
|  |  |  | types = getAllDictTypeFromDB(); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(RedisConstants.DICT_TYPE_KEY,types); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return types; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public SysDictType getDictTypeById(Integer id) { | 
|---|
|  |  |  | QueryWrapper<SysDictType> wrapper = new QueryWrapper(); | 
|---|
|  |  |  | wrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | wrapper.eq("id",id); | 
|---|
|  |  |  | return sysDictTypeMapper.selectOne(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<SysDictType> getAllDictTypeFromDB() { | 
|---|
|  |  |  | QueryWrapper<SysDictType> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | List<SysDictType> sysDictTypes = sysDictTypeMapper.selectList(wrapper); | 
|---|
|  |  |  | return sysDictTypes; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void refreshCache(){ | 
|---|
|  |  |  | redisTemplate.delete(RedisConstants.DICT_TYPE_KEY); | 
|---|
|  |  |  | List<SysDictType> types = getAllDictTypeFromDB(); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(RedisConstants.DICT_TYPE_KEY,types); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|