|  |  |  | 
|---|
|  |  |  | import com.moral.api.service.SysDictTypeService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.util.CacheUtils; | 
|---|
|  |  |  | import com.moral.api.util.LogUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | 
|---|
|  |  |  | SysDictTypeDTO dto = new SysDictTypeDTO(); | 
|---|
|  |  |  | //获取Id | 
|---|
|  |  |  | Integer typeId = form.getId(); | 
|---|
|  |  |  | //判断type是否存在 | 
|---|
|  |  |  | //查询要删除的type用于插入日志 | 
|---|
|  |  |  | QueryWrapper<SysDictType> existWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | existWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | existWrapper.eq("id",typeId); | 
|---|
|  |  |  | 
|---|
|  |  |  | deleteDataWrapper.eq("dict_type_id",typeId); | 
|---|
|  |  |  | deleteDataWrapper.set("is_delete",Constants.DELETE); | 
|---|
|  |  |  | sysDictDataService.update(null,deleteDataWrapper); | 
|---|
|  |  |  | //清除缓存数据 | 
|---|
|  |  |  | CacheUtils.clearDictionariesCache(); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(request,"删除字典类型:"+type.getName(),Constants.DELETE_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存数据 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回数据 | 
|---|
|  |  |  | dto.setSysDictType(type); | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | 
|---|
|  |  |  | SysDictTypeDTO dto = new SysDictTypeDTO(); | 
|---|
|  |  |  | //获取实体 | 
|---|
|  |  |  | SysDictType type = form.formConvertEntity(); | 
|---|
|  |  |  | //判断type是否存在 | 
|---|
|  |  |  | //查询type用于插入日志 | 
|---|
|  |  |  | QueryWrapper<SysDictType> existNameWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | existNameWrapper.eq("id",type.getId()); | 
|---|
|  |  |  | existNameWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | 
|---|
|  |  |  | sysDictTypeMapper.updateById(type); | 
|---|
|  |  |  | //获取更新后的对象 | 
|---|
|  |  |  | SysDictType sysDictType = sysDictTypeMapper.selectById(type.getId()); | 
|---|
|  |  |  | //清除缓存 | 
|---|
|  |  |  | CacheUtils.clearDictionariesCache(); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setSysDictType(sysDictType); | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | 
|---|
|  |  |  | //获取要插入的实体 | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | SysDictType existType = sysDictTypeMapper.listOne(sysDictType.getName()); | 
|---|
|  |  |  | if(!ObjectUtils.isEmpty(existType)){ | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.DICTTYPE_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.DICTTYPE_EXIST.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //插入类型 | 
|---|
|  |  |  | sysDictTypeMapper.insert(sysDictType); | 
|---|
|  |  |  | //清除缓存 | 
|---|
|  |  |  | CacheUtils.clearDictionariesCache(); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(request,"添加字典类型:"+sysDictType.getName(),Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setSysDictType(sysDictType); | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | 
|---|
|  |  |  | private List<SysDictType> getAllDictTypeFromDB() { | 
|---|
|  |  |  | QueryWrapper<SysDictType> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | wrapper.orderByAsc("id"); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|