SysConfigServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.crm.manager.service.impl;
  2. import com.crm.manager.dao.mapper.SysConfigMapper;
  3. import com.crm.manager.dao.repository.SysConfigRepository;
  4. import com.crm.manager.service.SysConfigService;
  5. import com.crm.rely.backend.core.constant.*;
  6. import com.crm.rely.backend.core.dto.base.PageDto;
  7. import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
  8. import com.crm.rely.backend.core.dto.system.config.SysConfigSearchDto;
  9. import com.crm.rely.backend.core.entity.system.config.*;
  10. import com.crm.rely.backend.core.entity.system.email.SysEmailSendEntity;
  11. import com.crm.rely.backend.core.pojo.table.SysConfigTable;
  12. import com.crm.rely.backend.core.exception.ServiceException;
  13. import com.crm.rely.backend.service.EmailService;
  14. import com.crm.rely.backend.service.RedisService;
  15. import com.crm.rely.backend.util.DateUtil;
  16. import com.crm.rely.backend.util.GetRandom;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import java.util.*;
  22. @Service
  23. public class SysConfigServiceImpl implements SysConfigService {
  24. @Autowired
  25. private SysConfigRepository sysConfigRepository;
  26. @Autowired
  27. private SysConfigMapper sysConfigMapper;
  28. @Autowired
  29. private EmailService emailService;
  30. @Autowired
  31. private RedisService redisService;
  32. @Override
  33. public SysConfigTable getByCode(String code) throws ServiceException {
  34. SysConfigTable sysConfigTable = redisService.getEntity(code, SysConfigTable.class);
  35. if (sysConfigTable == null) {
  36. sysConfigTable = sysConfigRepository.getByCode(code);
  37. if (sysConfigTable == null) {
  38. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  39. } else {
  40. redisService.save(sysConfigTable.getCode(), sysConfigTable);
  41. }
  42. }
  43. return sysConfigTable;
  44. }
  45. @Override
  46. @Transactional(rollbackFor = Exception.class)
  47. public void add(SysConfigAddEntity entity) throws ServiceException {
  48. existCode(entity.getCode());
  49. SysConfigTable sysConfigTable = new SysConfigTable();
  50. BeanUtils.copyProperties(entity, sysConfigTable);
  51. sysConfigRepository.save(sysConfigTable);
  52. saveCache(sysConfigTable);
  53. }
  54. @Override
  55. @Transactional(rollbackFor = Exception.class)
  56. public void update(SysConfigUpdateEntity entity) throws ServiceException {
  57. emailService.validateCode(entity.getEmail(), entity.getEmailCode());
  58. SysConfigTable sysConfigTable;
  59. if (entity.getId() == null) {
  60. sysConfigTable = sysConfigRepository.findFirstByCode(entity.getCode());
  61. } else {
  62. sysConfigTable = sysConfigRepository.findFirstByIdAndCode(entity.getId(), entity.getCode());
  63. }
  64. if (sysConfigTable == null) {
  65. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  66. }
  67. BeanUtils.copyProperties(entity, sysConfigTable);
  68. sysConfigRepository.save(sysConfigTable);
  69. saveCache(sysConfigTable);
  70. }
  71. @Override
  72. @Transactional(rollbackFor = Exception.class)
  73. public void updates(SysConfigUpdateEntitys entitys) throws ServiceException {
  74. if (entitys == null || entitys.getUpdateData() == null || entitys.getUpdateData().size() <= 0) {
  75. return;
  76. }
  77. emailService.validateCode(entitys.getEmail(), entitys.getEmailCode());
  78. List<String> codes = new ArrayList<>(entitys.getUpdateData().size());
  79. for (BaseSysConfigUpdateEntity entity : entitys.getUpdateData()) {
  80. codes.add(entity.getCode());
  81. entity.setModifyIp(entitys.getModifyIp());
  82. entity.setModifyUser(entitys.getModifyUser());
  83. entity.setModifyTime(entitys.getModifyTime());
  84. }
  85. List<SysConfigTable> sysConfigTables = sysConfigRepository.findAllByCodeIn(codes);
  86. if (sysConfigTables == null || sysConfigTables.size() <= 0) {
  87. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  88. }
  89. for (BaseSysConfigUpdateEntity entity : entitys.getUpdateData()) {
  90. for (int i = 0; i < sysConfigTables.size(); i++) {
  91. SysConfigTable sysConfigTable = sysConfigTables.get(i);
  92. if (entity.getCode() != null && entity.getCode().equals(sysConfigTable.getCode())) {
  93. entity.setId(sysConfigTable.getId());
  94. BeanUtils.copyProperties(entity, sysConfigTable);
  95. }
  96. }
  97. }
  98. sysConfigRepository.saveAll(sysConfigTables);
  99. saveCache(sysConfigTables);
  100. }
  101. @Override
  102. @Transactional(rollbackFor = Exception.class)
  103. public void delete(SysConfigDeleteEntity entity) throws ServiceException {
  104. SysConfigTable sysConfigTable = sysConfigRepository.findFirstByIdAndCode(entity.getId(), entity.getCode());
  105. if (sysConfigTable == null) {
  106. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  107. }
  108. sysConfigRepository.delete(sysConfigTable);
  109. redisService.remove(sysConfigTable.getCode());
  110. }
  111. @Override
  112. public ResultWithPagerDto<SysConfigSearchDto> searchPageList(SysConfigSearchEntity entity) throws ServiceException {
  113. Integer count = sysConfigMapper.countList(entity);
  114. PageDto pageDto = PageDto.format(entity, count);
  115. if (count == null || count <= 0) {
  116. return ResultWithPagerDto.success(pageDto);
  117. }
  118. List<SysConfigTable> sysConfigTables = sysConfigMapper.pageList(entity);
  119. if (sysConfigTables == null || sysConfigTables.size() <= 0) {
  120. throw new ServiceException(Constants.SYSTEM_ERROR);
  121. }
  122. List<SysConfigSearchDto> sysConfigSearchDtos = transform(sysConfigTables);
  123. return ResultWithPagerDto.success(pageDto, sysConfigSearchDtos);
  124. }
  125. @Override
  126. public SysConfigSearchDto searchSingle(SysConfigSingleEntity entity) throws ServiceException {
  127. SysConfigTable sysConfigTable = sysConfigRepository.getByIdAndCode(entity.getId(), entity.getCode());
  128. if (sysConfigTable == null) {
  129. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  130. }
  131. return transform(sysConfigTable);
  132. }
  133. @Override
  134. public SysConfigSearchDto searchSingleByCode(String code) throws ServiceException {
  135. SysConfigTable sysConfigTable = redisService.getEntity(code, SysConfigTable.class);
  136. if (sysConfigTable == null) {
  137. sysConfigTable = sysConfigRepository.getByCode(code);
  138. if (sysConfigTable == null) {
  139. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  140. } else {
  141. redisService.save(sysConfigTable.getCode(), sysConfigTable);
  142. }
  143. }
  144. return transform(sysConfigTable);
  145. }
  146. @Override
  147. public List<SysConfigSearchDto> searchSingleByCodes(List<String> codes) throws ServiceException {
  148. List<SysConfigTable> sysConfigTables = getByCodes(codes);
  149. return transform(sysConfigTables);
  150. }
  151. private List<SysConfigSearchDto> transform(List<SysConfigTable> sysConfigTables) {
  152. if (sysConfigTables == null || sysConfigTables.size() <= 0) {
  153. return null;
  154. }
  155. List<SysConfigSearchDto> sysConfigSearchDtos = new ArrayList<>(sysConfigTables.size());
  156. for (SysConfigTable sysConfigTable : sysConfigTables) {
  157. SysConfigSearchDto sysConfigSearchDto = transform(sysConfigTable);
  158. sysConfigSearchDtos.add(sysConfigSearchDto);
  159. }
  160. return sysConfigSearchDtos;
  161. }
  162. private SysConfigSearchDto transform(SysConfigTable sysConfigTable) {
  163. SysConfigSearchDto sysConfigSearchDto = new SysConfigSearchDto();
  164. BeanUtils.copyProperties(sysConfigTable, sysConfigSearchDto);
  165. return sysConfigSearchDto;
  166. }
  167. private void existCode(String code) throws ServiceException {
  168. boolean exist = sysConfigRepository.existsByCode(code);
  169. if (exist) {
  170. throw ServiceException.exception(ManagerConstant.CONFIG_CODE_EXIST);
  171. }
  172. }
  173. @Override
  174. public List<SysConfigTable> getByCodes(List<String> codes) throws ServiceException {
  175. List<SysConfigTable> sysConfigTables = new ArrayList<>(codes.size());
  176. for (int i = 0; i < codes.size(); ) {
  177. SysConfigTable sysConfigTable = redisService.getEntity(codes.get(i), SysConfigTable.class);
  178. if (sysConfigTable != null) {
  179. sysConfigTables.add(sysConfigTable);
  180. codes.remove(i);
  181. } else {
  182. i++;
  183. }
  184. }
  185. if (codes != null && codes.size() > 0) {
  186. List<SysConfigTable> supplementTables = sysConfigRepository.getByCodeIn(codes);
  187. if (sysConfigTables == null || supplementTables.size() <= 0) {
  188. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  189. }
  190. saveCache(supplementTables);
  191. sysConfigTables.addAll(supplementTables);
  192. }
  193. return sysConfigTables;
  194. }
  195. @Override
  196. public void sendUpdateEmailCode(SendUpdateEmailCodeEntity entity) throws ServiceException {
  197. //构建实体
  198. SysEmailSendEntity sysEmailSendEntity = createUpdateEmailSendEmailSendUpdateEmailCodeEntity(entity);
  199. //发送收据
  200. emailService.sendCode(sysEmailSendEntity, sysEmailSendEntity.getMap().get(MapConstants.CODE));
  201. }
  202. /**
  203. * 刷新redis config中的数据
  204. *
  205. * @throws ServiceException
  206. */
  207. @Override
  208. public void refresh() throws ServiceException {
  209. List<SysConfigTable> sysConfigTables = sysConfigRepository.getAllByIdIsNotNull();
  210. saveCache(sysConfigTables);
  211. }
  212. @Override
  213. public void cancel() throws ServiceException {
  214. List<String> configListKey = redisService.getList(GlobalConfigConstants.CONFIG_LIST, String.class);
  215. configListKey.add(GlobalConfigConstants.CONFIG_LIST);
  216. redisService.remove(configListKey);
  217. }
  218. private SysEmailSendEntity createUpdateEmailSendEmailSendUpdateEmailCodeEntity(SendUpdateEmailCodeEntity entity) {
  219. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  220. Map<String, String> map = new HashMap<>(2);
  221. String code = GetRandom.getRandom(6);
  222. map.put(MapConstants.CODE, code);
  223. Date time = DateUtil.operationMinute(new Date(), Constants.EMAIL_VALIDATECODE_TIME);
  224. map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTime(time));
  225. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  226. sysEmailSendEntity.setAddIp(entity.getIp());
  227. sysEmailSendEntity.setAddTime(entity.getTime());
  228. sysEmailSendEntity.setUsers(entity.getEmail());
  229. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.UPDATE_CONFIG_EMAIL_CODE);
  230. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CONFIG_UPDATE_EMAIL_CODE_SEND);
  231. sysEmailSendEntity.setMap(map);
  232. return sysEmailSendEntity;
  233. }
  234. private void saveCache(List<SysConfigTable> sysConfigTables) throws ServiceException {
  235. List<String> configListKey = redisService.getList(GlobalConfigConstants.CONFIG_LIST, String.class);
  236. sysConfigTables.forEach(v -> {
  237. try {
  238. saveCache(v, configListKey);
  239. } catch (ServiceException e) {
  240. e.printStackTrace();
  241. }
  242. });
  243. }
  244. private void saveCache(SysConfigTable sysConfigTable) throws ServiceException {
  245. saveCache(sysConfigTable, null);
  246. }
  247. private void saveCache(SysConfigTable sysConfigTable, List<String> configListKey) throws ServiceException {
  248. if (configListKey == null) {
  249. configListKey = redisService.getList(GlobalConfigConstants.CONFIG_LIST, String.class);
  250. }
  251. if (configListKey == null) {
  252. configListKey = new ArrayList<>(10);
  253. }
  254. if (!configListKey.contains(sysConfigTable.getCode())) {
  255. configListKey.add(sysConfigTable.getCode());
  256. }
  257. redisService.save(sysConfigTable.getCode(), sysConfigTable);
  258. redisService.save(GlobalConfigConstants.CONFIG_LIST, configListKey);
  259. }
  260. }