SubjectServiceImpl.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.bowintek.practice.services.impl;
  2. import com.bowintek.practice.filter.exception.BaseException;
  3. import com.bowintek.practice.mapper.SrSubjectMapper;
  4. import com.bowintek.practice.mapper.SrSubjectfieldMapper;
  5. import com.bowintek.practice.mapper.cquery.SubjectCQuery;
  6. import com.bowintek.practice.model.*;
  7. import com.bowintek.practice.services.service.SubjectService;
  8. import com.bowintek.practice.services.service.system.DictionaryService;
  9. import com.bowintek.practice.util.Constant;
  10. import com.bowintek.practice.util.StringUtils;
  11. import com.bowintek.practice.vo.SubjectfieldVo;
  12. import com.github.pagehelper.PageHelper;
  13. import com.github.pagehelper.PageInfo;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.UUID;
  20. import java.util.stream.Collectors;
  21. @Service("SubjectService")
  22. public class SubjectServiceImpl implements SubjectService {
  23. @Autowired
  24. private SrSubjectMapper subjectMapper;
  25. @Autowired
  26. private SrSubjectfieldMapper subjectfieldMapper;
  27. @Autowired
  28. private SubjectCQuery subjectCQuery;
  29. @Autowired
  30. private StringUtils stringUtils;
  31. @Autowired
  32. private DictionaryService dictionaryService;
  33. @Override
  34. public PageInfo<SrSubject> getList(Integer page, Integer rows,
  35. String subjectName, String tabName, String tabCode, List<String> idList) {
  36. PageHelper.startPage(page, rows);
  37. List<SrSubject> dataList = subjectCQuery.getList(subjectName, tabName,
  38. tabCode, stringUtils.ListToInSql(idList));
  39. PageInfo<SrSubject> result = new PageInfo(dataList);
  40. return result;
  41. }
  42. @Override
  43. public SrSubject getSubject(String subjectId) {
  44. return subjectMapper.selectByPrimaryKey(subjectId);
  45. }
  46. @Override
  47. public int saveSubject(SrSubject model, List<SrSubjectfield> fieldList, String userId) {
  48. if (model.getIsReferences() == 0) {
  49. model.setExecSql(model.getTabCode());
  50. }
  51. SrSubject dbData = subjectMapper.selectByPrimaryKey(model.getSubId());
  52. int result = 0;
  53. if (dbData == null) {
  54. model.setSubId(UUID.randomUUID().toString());
  55. model.setStatus(1);
  56. model.setCreatedBy(userId);
  57. model.setCreateTime(new Date());
  58. model.setModiTime(new Date());
  59. result = subjectMapper.insert(model);
  60. } else {
  61. model.setModiTime(new Date());
  62. result = subjectMapper.updateByPrimaryKey(model);
  63. }
  64. if (fieldList != null && fieldList.size() > 0) {
  65. SrSubjectfieldExample example = new SrSubjectfieldExample();
  66. SrSubjectfieldExample.Criteria criteria = example.or();
  67. criteria.andSubIdEqualTo(model.getSubId());
  68. List<SrSubjectfield> dbList = subjectfieldMapper.selectByExample(example);
  69. List<String> ids = fieldList.stream().map(x -> x.getFieldId()).collect(Collectors.toList());
  70. criteria.andFieldIdNotIn(ids);
  71. subjectfieldMapper.deleteByExample(example);
  72. fieldList.stream().forEach(x -> {
  73. long count = dbList.stream().filter(f -> f.getFieldId() == x.getFieldId()).count();
  74. //不是外键字段编码和显示别名需一致
  75. x.setFieldAlias(x.getIsForeignKey() == 0 ? x.getFieldCode() : x.getFieldAlias());
  76. if (count > 0) {
  77. subjectfieldMapper.updateByPrimaryKeySelective(x);
  78. } else {
  79. x.setSubId(model.getSubId());
  80. x.setFieldId(UUID.randomUUID().toString());
  81. x.setStatus(1);
  82. subjectfieldMapper.insertSelective(x);
  83. }
  84. });
  85. }
  86. return result;
  87. }
  88. @Override
  89. public List<SrSubjectfield> getFieldList(String subjectId) {
  90. SrSubjectfieldExample example = new SrSubjectfieldExample();
  91. SrSubjectfieldExample.Criteria criteria = example.or();
  92. criteria.andSubIdEqualTo(subjectId);
  93. List<SrSubjectfield> dbList = subjectfieldMapper.selectByExample(example);
  94. return dbList;
  95. }
  96. @Override
  97. public int delete(List<String> idList) {
  98. try {
  99. SrSubjectfieldExample example = new SrSubjectfieldExample();
  100. SrSubjectfieldExample.Criteria criteria = example.or();
  101. criteria.andSubIdIn(idList);
  102. subjectfieldMapper.selectByExample(example);
  103. SrSubjectExample subExp = new SrSubjectExample();
  104. subExp.or().andSubIdIn(idList);
  105. subjectMapper.deleteByExample(subExp);
  106. } catch (Exception e) {
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. @Override
  112. public List<SubjectfieldVo> importData(List<SubjectfieldVo> dataList, String loginUserID) {
  113. if (dataList.size() <= 0)
  114. throw new BaseException("", "请添加导入数据!");
  115. List<SysDictionaryItem> dicQueryTypeList = dictionaryService.getDictionaryItemList("queryType");
  116. List<SysDictionaryItem> dicSettingTypeList = dictionaryService.getDictionaryItemList("settingType");
  117. dataList.forEach(item -> {
  118. String errorInfo = "";
  119. SysDictionaryItem dicQueryType = dicQueryTypeList.stream().filter(e -> e.getName().equals(item.getQueryTypeName())).findFirst().orElse(null);
  120. SysDictionaryItem dicSettingType = dicSettingTypeList.stream().filter(e -> e.getName().equals(item.getSettingTypeName())).findFirst().orElse(null);
  121. if (stringUtils.IsNullOrEmpty(item.getFieldCode()))
  122. errorInfo += "请填写字段编码!";
  123. if (stringUtils.IsNullOrEmpty(item.getFieldName()))
  124. errorInfo += "请填写字段名称!";
  125. if (stringUtils.IsNullOrEmpty(item.getSettingTypeName()))
  126. errorInfo += "请填写配置类型!";
  127. if (stringUtils.IsNullOrEmpty(item.getDataType()))
  128. errorInfo += "请填写数据类型!";
  129. if ("字典值".equals(item.getDataType())) {
  130. if (stringUtils.IsNullOrEmpty(item.getDictionaryCode()))
  131. errorInfo += "查询类型为字典值请填写取数字典编码!";
  132. }
  133. if (item.getDisOrder() == null)
  134. errorInfo += "请填写显示排序!";
  135. if (stringUtils.IsNullOrEmpty(item.getIsSearchFieldText()))
  136. errorInfo += "请填写是否查询字段!";
  137. else if (!Constant.YES.equals(item.getIsSearchFieldText()) && !Constant.No.equals(item.getIsSearchFieldText())) {
  138. errorInfo += "是否查询字段请填是或者否!";
  139. }
  140. if (stringUtils.IsNullOrEmpty(item.getIsForeignKeyText()))
  141. errorInfo += "请填写是否关联字段!";
  142. else if (!Constant.YES.equals(item.getIsForeignKeyText()) && !Constant.No.equals(item.getIsForeignKeyText())) {
  143. errorInfo += "是否关联字段请填是或者否!";
  144. }
  145. if (Constant.YES.equals(item.getIsForeignKeyText())) {
  146. if (stringUtils.IsNullOrEmpty(item.getFieldAlias()))
  147. errorInfo += "关联字段请填写显示别名!";
  148. if (stringUtils.IsNullOrEmpty(item.getReferencesTab()))
  149. errorInfo += "关联字段请填写外键表!";
  150. if (stringUtils.IsNullOrEmpty(item.getReferencesTab()))
  151. errorInfo += "关联字段请填写外键列!";
  152. if (stringUtils.IsNullOrEmpty(item.getReferencesTab()))
  153. errorInfo += "关联字段请填写外键表显示列!";
  154. }
  155. if (dicSettingType == null)
  156. errorInfo += "配置类型数据有误!";
  157. if (dicQueryType == null)
  158. errorInfo += "查询类型不在支持范围!";
  159. if (stringUtils.IsNullOrEmpty(errorInfo)) {
  160. item.setQueryTypeId(dicQueryType.getValue());
  161. item.setSettingTypeId(dicSettingType.getValue());
  162. item.setIsSearchField(Constant.YES.equals(item.getIsSearchFieldText()) ? 1 : 0);
  163. item.setIsForeignKey(Constant.YES.equals(item.getIsForeignKeyText()) ? 1 : 0);
  164. }
  165. item.setErrorMessage(errorInfo);
  166. });
  167. return dataList;
  168. }
  169. }