DictionaryDataServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package jnpf.base.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import jnpf.base.ActionResult;
  4. import jnpf.base.service.SuperServiceImpl;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import jnpf.base.entity.DictionaryDataEntity;
  7. import jnpf.base.entity.DictionaryTypeEntity;
  8. import jnpf.base.mapper.DictionaryDataMapper;
  9. import jnpf.base.model.dictionarydata.DictionaryDataExportModel;
  10. import jnpf.base.model.dictionarytype.DictionaryExportModel;
  11. import jnpf.base.service.DictionaryDataService;
  12. import jnpf.base.service.DictionaryTypeService;
  13. import jnpf.base.vo.DownloadVO;
  14. import jnpf.config.ConfigValueUtil;
  15. import jnpf.constant.FileTypeConstant;
  16. import jnpf.constant.MsgCode;
  17. import jnpf.exception.DataException;
  18. import jnpf.util.*;
  19. import jnpf.emnus.ModuleTypeEnum;
  20. import jnpf.util.FileExport;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  24. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27. /**
  28. * 字典数据
  29. *
  30. * @author JNPF开发平台组
  31. * @version V3.1.0
  32. * @copyright 引迈信息技术有限公司
  33. * @date 2019年9月27日 上午9:18
  34. */
  35. @Service
  36. public class DictionaryDataServiceImpl extends SuperServiceImpl<DictionaryDataMapper, DictionaryDataEntity> implements DictionaryDataService {
  37. @Autowired
  38. private DictionaryTypeService dictionaryTypeService;
  39. @Autowired
  40. private FileExport fileExport;
  41. @Autowired
  42. private ConfigValueUtil configValueUtil;
  43. @Override
  44. public List<DictionaryDataEntity> getList(String dictionaryTypeId, Boolean enable) {
  45. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  46. queryWrapper.lambda().eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
  47. if (enable) {
  48. queryWrapper.lambda().eq(DictionaryDataEntity::getEnabledMark, 1);
  49. }
  50. queryWrapper.lambda().orderByAsc(DictionaryDataEntity::getSortCode).orderByDesc(DictionaryDataEntity::getCreatorTime).orderByDesc(DictionaryDataEntity::getLastModifyTime);
  51. return this.list(queryWrapper);
  52. }
  53. @Override
  54. public List<DictionaryDataEntity> getList(String dictionaryTypeId) {
  55. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  56. queryWrapper.lambda().eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
  57. queryWrapper.lambda().orderByAsc(DictionaryDataEntity::getSortCode)
  58. .orderByDesc(DictionaryDataEntity::getCreatorTime);
  59. return this.list(queryWrapper);
  60. }
  61. @Override
  62. public List<DictionaryDataEntity> getDicList(String dictionaryTypeId) {
  63. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  64. queryWrapper.lambda().and(
  65. t -> t.eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId)
  66. .or().eq(DictionaryDataEntity::getEnCode, dictionaryTypeId)
  67. );
  68. queryWrapper.lambda().select(DictionaryDataEntity::getId, DictionaryDataEntity::getFullName, DictionaryDataEntity::getEnCode);
  69. return this.list(queryWrapper);
  70. }
  71. @Override
  72. public List<DictionaryDataEntity> geDicList(String dictionaryTypeId) {
  73. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  74. queryWrapper.lambda().and(
  75. t -> t.eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId)
  76. .or().eq(DictionaryDataEntity::getEnCode, dictionaryTypeId)
  77. );
  78. queryWrapper.lambda().select(DictionaryDataEntity::getId, DictionaryDataEntity::getFullName, DictionaryDataEntity::getEnabledMark);
  79. return this.list(queryWrapper);
  80. }
  81. @Override
  82. public Boolean isExistSubset(String parentId) {
  83. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  84. queryWrapper.lambda().eq(DictionaryDataEntity::getParentId, parentId);
  85. return this.list(queryWrapper).size() > 0;
  86. }
  87. @Override
  88. public DictionaryDataEntity getInfo(String id) {
  89. if (id == null) {
  90. return null;
  91. }
  92. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  93. queryWrapper.lambda().eq(DictionaryDataEntity::getId, id);
  94. return this.getOne(queryWrapper);
  95. }
  96. @Override
  97. public DictionaryDataEntity getSwapInfo(String value, String dictionaryTypeId) {
  98. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  99. queryWrapper.lambda().eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId).and(
  100. t -> t.eq(DictionaryDataEntity::getId, value)
  101. .or().eq(DictionaryDataEntity::getEnCode, value)
  102. );
  103. return this.getOne(queryWrapper);
  104. }
  105. @Override
  106. public boolean isExistByFullName(String dictionaryTypeId, String fullName, String id) {
  107. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  108. queryWrapper.lambda().eq(DictionaryDataEntity::getFullName, fullName).eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
  109. if (!StringUtil.isEmpty(id)) {
  110. queryWrapper.lambda().ne(DictionaryDataEntity::getId, id);
  111. }
  112. return this.count(queryWrapper) > 0 ? true : false;
  113. }
  114. @Override
  115. public boolean isExistByEnCode(String dictionaryTypeId, String enCode, String id) {
  116. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  117. queryWrapper.lambda().eq(DictionaryDataEntity::getEnCode, enCode).eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
  118. if (!StringUtil.isEmpty(id)) {
  119. queryWrapper.lambda().ne(DictionaryDataEntity::getId, id);
  120. }
  121. return this.count(queryWrapper) > 0 ? true : false;
  122. }
  123. @Override
  124. public void delete(DictionaryDataEntity entity) {
  125. this.removeById(entity.getId());
  126. }
  127. @Override
  128. public void create(DictionaryDataEntity entity) {
  129. //判断id是否为空,为空则为新建
  130. if (StringUtil.isEmpty(entity.getId())) {
  131. entity.setId(RandomUtil.uuId());
  132. entity.setSimpleSpelling(PinYinUtil.getFirstSpell(entity.getFullName()).toUpperCase());
  133. entity.setCreatorUserId(UserProvider.getUser().getUserId());
  134. }
  135. this.save(entity);
  136. }
  137. @Override
  138. public boolean update(String id, DictionaryDataEntity entity) {
  139. entity.setId(id);
  140. entity.setLastModifyTime(DateUtil.getNowDate());
  141. entity.setLastModifyUserId(UserProvider.getUser().getUserId());
  142. return this.updateById(entity);
  143. }
  144. @Override
  145. public boolean first(String id) {
  146. boolean isOk = false;
  147. //获取要上移的那条数据的信息
  148. DictionaryDataEntity upEntity = this.getById(id);
  149. Long upSortCode = upEntity.getSortCode() == null ? 0 : upEntity.getSortCode();
  150. //查询上几条记录
  151. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  152. queryWrapper.lambda()
  153. .eq(DictionaryDataEntity::getDictionaryTypeId, upEntity.getDictionaryTypeId())
  154. .eq(DictionaryDataEntity::getParentId, upEntity.getParentId())
  155. .lt(DictionaryDataEntity::getSortCode, upSortCode)
  156. .orderByDesc(DictionaryDataEntity::getSortCode);
  157. List<DictionaryDataEntity> downEntity = this.list(queryWrapper);
  158. if (downEntity.size() > 0) {
  159. //交换两条记录的sort值
  160. Long temp = upEntity.getSortCode();
  161. upEntity.setSortCode(downEntity.get(0).getSortCode());
  162. downEntity.get(0).setSortCode(temp);
  163. updateById(downEntity.get(0));
  164. updateById(upEntity);
  165. isOk = true;
  166. }
  167. return isOk;
  168. }
  169. @Override
  170. public boolean next(String id) {
  171. boolean isOk = false;
  172. //获取要下移的那条数据的信息
  173. DictionaryDataEntity downEntity = this.getById(id);
  174. Long upSortCode = downEntity.getSortCode() == null ? 0 : downEntity.getSortCode();
  175. //查询下几条记录
  176. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  177. queryWrapper.lambda()
  178. .eq(DictionaryDataEntity::getDictionaryTypeId, downEntity.getDictionaryTypeId())
  179. .eq(DictionaryDataEntity::getParentId, downEntity.getParentId())
  180. .gt(DictionaryDataEntity::getSortCode, upSortCode)
  181. .orderByAsc(DictionaryDataEntity::getSortCode);
  182. List<DictionaryDataEntity> upEntity = this.list(queryWrapper);
  183. if (upEntity.size() > 0) {
  184. //交换两条记录的sort值
  185. Long temp = downEntity.getSortCode();
  186. downEntity.setSortCode(upEntity.get(0).getSortCode());
  187. upEntity.get(0).setSortCode(temp);
  188. updateById(upEntity.get(0));
  189. updateById(downEntity);
  190. isOk = true;
  191. }
  192. return isOk;
  193. }
  194. @Override
  195. public List<DictionaryDataEntity> getDictionName(List<String> id) {
  196. List<DictionaryDataEntity> dictionList = new ArrayList<>();
  197. if (id != null && id.size() > 0) {
  198. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  199. queryWrapper.lambda().and(
  200. t -> t.in(DictionaryDataEntity::getEnCode, id)
  201. .or().in(DictionaryDataEntity::getId, id)
  202. );
  203. queryWrapper.lambda().orderByAsc(DictionaryDataEntity::getParentId);
  204. dictionList = this.list(queryWrapper);
  205. }
  206. return dictionList;
  207. }
  208. @Override
  209. public DownloadVO exportData(String id) {
  210. //获取数据分类字段详情
  211. DictionaryTypeEntity typeEntity = dictionaryTypeService.getInfo(id);
  212. if (typeEntity == null) {
  213. throw new DataException(MsgCode.FA001.get());
  214. }
  215. DictionaryExportModel exportModel = new DictionaryExportModel();
  216. //递归子分类
  217. List<DictionaryTypeEntity> typeList = new ArrayList<>();
  218. List<DictionaryTypeEntity> typeEntityList = dictionaryTypeService.getList();
  219. typeList.add(typeEntity);
  220. getDictionaryTypeEntitySet(typeEntity, typeList, typeEntityList);
  221. List<DictionaryTypeEntity> collect = typeList.stream().distinct().collect(Collectors.toList());
  222. //判断是否有子分类
  223. if (collect.size() > 0) {
  224. exportModel.setList(collect);
  225. }
  226. //获取该类型下的数据
  227. List<DictionaryDataExportModel> modelList = new ArrayList<>();
  228. for (DictionaryTypeEntity dictionaryTypeEntity : exportModel.getList()) {
  229. List<DictionaryDataEntity> entityList = getList(dictionaryTypeEntity.getId());
  230. for (DictionaryDataEntity dictionaryDataEntity : entityList) {
  231. DictionaryDataExportModel dataExportModel = JsonUtil.getJsonToBean(dictionaryDataEntity, DictionaryDataExportModel.class);
  232. modelList.add(dataExportModel);
  233. }
  234. }
  235. exportModel.setModelList(modelList);
  236. //导出文件
  237. DownloadVO downloadVO = fileExport.exportFile(exportModel, FileTypeConstant.TEMPORARY, typeEntity.getFullName(), ModuleTypeEnum.SYSTEM_DICTIONARYDATA.getTableName());
  238. return downloadVO;
  239. }
  240. /**
  241. * 递归字典分类
  242. *
  243. * @param dictionaryTypeEntity 数据字典类型实体
  244. */
  245. private void getDictionaryTypeEntitySet(DictionaryTypeEntity dictionaryTypeEntity, List<DictionaryTypeEntity> set, List<DictionaryTypeEntity> typeEntityList) {
  246. //是否含有子分类
  247. List<DictionaryTypeEntity> collect = typeEntityList.stream().filter(t -> dictionaryTypeEntity.getId().equals(t.getParentId())).collect(Collectors.toList());
  248. if (collect.size() > 0) {
  249. for (DictionaryTypeEntity typeEntity : collect) {
  250. set.add(typeEntity);
  251. getDictionaryTypeEntitySet(typeEntity, set, typeEntityList);
  252. }
  253. }
  254. }
  255. @Override
  256. @DSTransactional
  257. public ActionResult importData(DictionaryExportModel exportModel, Integer type) throws DataException {
  258. try {
  259. StringBuilder message = new StringBuilder();
  260. StringJoiner exceptionMessage = new StringJoiner("、");
  261. List<DictionaryTypeEntity> list = JsonUtil.getJsonToList(exportModel.getList(), DictionaryTypeEntity.class);
  262. List<DictionaryDataEntity> entityList = JsonUtil.getJsonToList(exportModel.getModelList(), DictionaryDataEntity.class);
  263. //遍历插入分类
  264. StringJoiner IDMessage = new StringJoiner("、");
  265. StringJoiner fullNameMessage = new StringJoiner("、");
  266. StringJoiner enCodeMessage = new StringJoiner("、");
  267. Map<String, String> idFor = new HashMap<>();
  268. for (DictionaryTypeEntity entity : list) {
  269. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  270. if (dictionaryTypeService.getInfo(entity.getId()) != null) {
  271. IDMessage.add(entity.getId());
  272. }
  273. if (dictionaryTypeService.isExistByFullName(entity.getFullName(), null)) {
  274. fullNameMessage.add(entity.getFullName());
  275. }
  276. if (dictionaryTypeService.isExistByEnCode(entity.getEnCode(), null)) {
  277. enCodeMessage.add(entity.getEnCode());
  278. }
  279. if ((IDMessage.length() > 0 || fullNameMessage.length() > 0 || enCodeMessage.length() > 0)) {
  280. if (ObjectUtil.equal(type, 1)) {
  281. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  282. entity.setEnCode(entity.getEnCode() + copyNum);
  283. String oldId = entity.getId();
  284. entity.setId(RandomUtil.uuId());
  285. dictionaryTypeService.setIgnoreLogicDelete().removeById(entity);
  286. if (Optional.ofNullable(idFor.get(entity.getParentId())).isPresent()) {
  287. entity.setParentId(idFor.get(entity.getParentId()));
  288. }
  289. dictionaryTypeService.setIgnoreLogicDelete().saveOrUpdate(entity);
  290. idFor.put(oldId, entity.getId());
  291. }
  292. } else {
  293. dictionaryTypeService.setIgnoreLogicDelete().removeById(entity);
  294. dictionaryTypeService.setIgnoreLogicDelete().saveOrUpdate(entity);
  295. }
  296. }
  297. if (IDMessage.length() > 0) {
  298. exceptionMessage.add("ID(" + IDMessage.toString() + ")重复");
  299. }
  300. if (enCodeMessage.length() > 0) {
  301. exceptionMessage.add("编码(" + IDMessage.toString() + ")重复");
  302. }
  303. if (fullNameMessage.length() > 0) {
  304. exceptionMessage.add("名称(" + IDMessage.toString() + ")重复");
  305. }
  306. if (exceptionMessage.length() > 0) {
  307. message.append(exceptionMessage.toString()).append(";");
  308. exceptionMessage = new StringJoiner("、");
  309. IDMessage = new StringJoiner("、");
  310. fullNameMessage = new StringJoiner("、");
  311. enCodeMessage = new StringJoiner("、");
  312. }
  313. for (DictionaryDataEntity entity1 : entityList) {
  314. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  315. if (this.getInfo(entity1.getId()) != null) {
  316. IDMessage.add(entity1.getId());
  317. }
  318. if (this.isExistByFullName(entity1.getDictionaryTypeId(), entity1.getFullName(), null)) {
  319. fullNameMessage.add(entity1.getFullName());
  320. }
  321. if (this.isExistByEnCode(entity1.getDictionaryTypeId(), entity1.getEnCode(), null)) {
  322. enCodeMessage.add(entity1.getEnCode());
  323. }
  324. if (ObjectUtil.equal(type, 1)) {
  325. entity1.setId(RandomUtil.uuId());
  326. if (Optional.ofNullable(idFor.get(entity1.getDictionaryTypeId())).isPresent()) {
  327. entity1.setDictionaryTypeId(idFor.get(entity1.getDictionaryTypeId()));
  328. }
  329. if (this.isExistByFullName(entity1.getDictionaryTypeId(), entity1.getFullName(), null)
  330. || this.isExistByEnCode(entity1.getDictionaryTypeId(), entity1.getEnCode(), null)) {
  331. entity1.setFullName(entity1.getFullName() + ".副本" + copyNum);
  332. entity1.setEnCode(entity1.getEnCode() + copyNum);
  333. }
  334. this.setIgnoreLogicDelete().saveOrUpdate(entity1);
  335. } else if (IDMessage.length() == 0 && fullNameMessage.length() == 0 && enCodeMessage.length() == 0) {
  336. this.setIgnoreLogicDelete().removeById(entity1);
  337. this.setIgnoreLogicDelete().saveOrUpdate(entity1);
  338. }
  339. }
  340. if (IDMessage.length() > 0) {
  341. exceptionMessage.add("ID(" + IDMessage.toString() + ")重复");
  342. }
  343. if (enCodeMessage.length() > 0) {
  344. exceptionMessage.add("编码(" + enCodeMessage.toString() + ")重复");
  345. }
  346. if (fullNameMessage.length() > 0) {
  347. exceptionMessage.add("名称(" + fullNameMessage.toString() + ")重复");
  348. }
  349. if (exceptionMessage.length() > 0) {
  350. message.append("modelList:" + exceptionMessage.toString() + ";");
  351. }
  352. if (ObjectUtil.equal(type, 0) && message.length() > 0) {
  353. return ActionResult.fail(message.toString().substring(0, message.lastIndexOf(";")));
  354. }
  355. return ActionResult.success(MsgCode.IMP001.get());
  356. } catch (Exception e) {
  357. //手动回滚事务
  358. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  359. throw new DataException(e.getMessage());
  360. } finally {
  361. this.clearIgnoreLogicDelete();
  362. dictionaryTypeService.clearIgnoreLogicDelete();
  363. this.clearIgnoreLogicDelete();
  364. }
  365. }
  366. @Override
  367. public List<DictionaryDataEntity> getListByTypeDataCode(String typeCode) {
  368. DictionaryTypeEntity dictionaryTypeEntity = dictionaryTypeService.getInfoByEnCode(typeCode);
  369. List<DictionaryDataEntity> list = new ArrayList<>();
  370. if (dictionaryTypeEntity != null) {
  371. list = this.getList(dictionaryTypeEntity.getId());
  372. }
  373. return list;
  374. }
  375. @Override
  376. public List<DictionaryDataEntity> getByTypeCodeEnable(String typeCode) {
  377. DictionaryTypeEntity dictionaryTypeEntity = dictionaryTypeService.getInfoByEnCode(typeCode);
  378. List<DictionaryDataEntity> list = new ArrayList<>();
  379. if (dictionaryTypeEntity != null) {
  380. QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
  381. queryWrapper.lambda().eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeEntity.getId());
  382. queryWrapper.lambda().eq(DictionaryDataEntity::getEnabledMark, 1);
  383. queryWrapper.lambda().orderByAsc(DictionaryDataEntity::getSortCode)
  384. .orderByDesc(DictionaryDataEntity::getCreatorTime);
  385. list= this.list(queryWrapper);
  386. }
  387. return list;
  388. }
  389. }