OrganizationsService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.persistence.service;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.ArrayList;
  20. import java.util.Comparator;
  21. import java.util.List;
  22. import java.util.TreeSet;
  23. import java.util.stream.Collectors;
  24. import org.apache.mybatis.jpa.persistence.JpaBaseService;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.ss.usermodel.Cell;
  27. import org.apache.poi.ss.usermodel.CellType;
  28. import org.apache.poi.ss.usermodel.Row;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.ss.usermodel.Workbook;
  31. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  32. import org.maxkey.entity.Organizations;
  33. import org.maxkey.persistence.kafka.KafkaIdentityAction;
  34. import org.maxkey.persistence.kafka.KafkaIdentityTopic;
  35. import org.maxkey.persistence.kafka.KafkaPersistService;
  36. import org.maxkey.persistence.mapper.OrganizationsMapper;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.stereotype.Repository;
  39. import org.springframework.util.CollectionUtils;
  40. import org.springframework.web.multipart.MultipartFile;
  41. import com.google.common.collect.Lists;
  42. @Repository
  43. public class OrganizationsService extends JpaBaseService<Organizations>{
  44. @Autowired
  45. KafkaPersistService kafkaPersistService;
  46. public OrganizationsService() {
  47. super(OrganizationsMapper.class);
  48. }
  49. /* (non-Javadoc)
  50. * @see com.connsec.db.service.BaseService#getMapper()
  51. */
  52. @Override
  53. public OrganizationsMapper getMapper() {
  54. // TODO Auto-generated method stub
  55. return (OrganizationsMapper)super.getMapper();
  56. }
  57. public boolean insert(Organizations organization) {
  58. if(super.insert(organization)){
  59. kafkaPersistService.send(
  60. KafkaIdentityTopic.ORG_TOPIC, organization, KafkaIdentityAction.CREATE_ACTION);
  61. return true;
  62. }
  63. return false;
  64. }
  65. public boolean update(Organizations organization) {
  66. if(super.update(organization)){
  67. kafkaPersistService.send(
  68. KafkaIdentityTopic.ORG_TOPIC, organization, KafkaIdentityAction.UPDATE_ACTION);
  69. return true;
  70. }
  71. return false;
  72. }
  73. public boolean delete(Organizations organization) {
  74. if(super.delete(organization)){
  75. kafkaPersistService.send(
  76. KafkaIdentityTopic.ORG_TOPIC, organization, KafkaIdentityAction.DELETE_ACTION);
  77. return true;
  78. }
  79. return false;
  80. }
  81. public boolean importing(MultipartFile file) {
  82. if(file ==null){
  83. return false;
  84. }
  85. InputStream is = null;
  86. Workbook wb = null;
  87. List<Organizations> orgsList = null;
  88. try {
  89. is = file.getInputStream();
  90. String xls = ".xls";
  91. String xlsx = ".xlsx";
  92. int columnSize = 46;
  93. orgsList = Lists.newArrayList();
  94. if (file.getOriginalFilename().toLowerCase().endsWith(xls)) {
  95. wb = new HSSFWorkbook(is);
  96. } else if (file.getOriginalFilename().toLowerCase().endsWith(xlsx)) {
  97. wb = new XSSFWorkbook(is);
  98. } else {
  99. throw new RuntimeException("maxKey用户导入没有Excel类型");
  100. }
  101. int sheetSize = wb.getNumberOfSheets();
  102. //遍历sheet页
  103. for (int i = 0; i < sheetSize; i++) {
  104. Sheet sheet = wb.getSheetAt(i);
  105. int rowSize = sheet.getLastRowNum() + 1;
  106. //遍历行
  107. for (int j = 1; j < rowSize; j++) {
  108. Row row = sheet.getRow(j);
  109. //略过空行和前3行
  110. if (row == null || j <3 ) {
  111. continue;
  112. } else {
  113. //其他行是数据行
  114. Organizations organization =new Organizations();
  115. for (int k = 0; k < columnSize; k++) {
  116. if (k == 0) {
  117. // 上级编码
  118. Cell cell = row.getCell(k);
  119. organization.setParentId(getValue(cell));
  120. } else if (k == 1) {
  121. // 上级名称
  122. Cell cell = row.getCell(k);
  123. organization.setParentName(getValue(cell));
  124. } else if (k == 2) {
  125. // 机构编码
  126. Cell cell = row.getCell(k);
  127. organization.setId(getValue(cell));
  128. } else if (k == 3) {
  129. // 机构名称
  130. Cell cell = row.getCell(k);
  131. organization.setName(getValue(cell));
  132. } else if (k == 4) {
  133. // 机构全称
  134. Cell cell = row.getCell(k);
  135. organization.setFullName(getValue(cell));
  136. } else if (k == 5) {
  137. // 编码路径
  138. Cell cell = row.getCell(k);
  139. organization.setCodePath(getValue(cell));
  140. } else if (k == 6) {
  141. // 名称路径
  142. Cell cell = row.getCell(k);
  143. organization.setNamePath(getValue(cell));
  144. } else if (k == 7) {
  145. // 机构类型
  146. Cell cell = row.getCell(k);
  147. organization.setType(getValue(cell));
  148. } else if (k == 8) {
  149. // 所属分支机构
  150. Cell cell = row.getCell(k);
  151. organization.setDivision(getValue(cell));
  152. } else if (k == 9) {
  153. // 级别
  154. Cell cell = row.getCell(k);
  155. String level=getValue(cell);
  156. organization.setLevel(level.equals("") ? "1" : level);
  157. } else if (k == 10) {
  158. // 排序
  159. Cell cell = row.getCell(k);
  160. String sortIndex=getValue(cell);
  161. organization.setSortIndex(sortIndex.equals("") ? "1" : sortIndex);
  162. } else if (k == 11) {
  163. // 联系人
  164. Cell cell = row.getCell(k);
  165. organization.setContact(getValue(cell));
  166. } else if (k == 12) {
  167. // 联系电话
  168. Cell cell = row.getCell(k);
  169. organization.setPhone(getValue(cell));
  170. }else if (k == 13) {
  171. // 邮箱
  172. Cell cell = row.getCell(k);
  173. organization.setEmail(getValue(cell));
  174. }else if (k == 14) {
  175. // 传真
  176. Cell cell = row.getCell(k);
  177. organization.setFax(getValue(cell));
  178. }else if (k == 24) {
  179. // 工作-国家
  180. Cell cell = row.getCell(k);
  181. organization.setCountry(getValue(cell));
  182. }else if (k == 25) {
  183. // 工作-省
  184. Cell cell = row.getCell(k);
  185. organization.setRegion(getValue(cell));
  186. }else if (k == 26) {
  187. // 工作-城市
  188. Cell cell = row.getCell(k);
  189. organization.setLocality(getValue(cell));
  190. }else if (k == 27) {
  191. // 工作-地址
  192. Cell cell = row.getCell(k);
  193. organization.setLocality(getValue(cell));
  194. }else if (k == 28) {
  195. // 邮编
  196. Cell cell = row.getCell(k);
  197. organization.setPostalCode(getValue(cell));
  198. }else if (k == 29) {
  199. // 详细描述
  200. Cell cell = row.getCell(k);
  201. organization.setDescription(getValue(cell));
  202. }
  203. }
  204. organization.setStatus("1");
  205. orgsList.add(organization);
  206. }
  207. }
  208. }
  209. // 数据去重
  210. if(CollectionUtils.isEmpty(orgsList)){
  211. orgsList = orgsList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new));
  212. }
  213. } catch (IOException e) {
  214. e.printStackTrace();
  215. }finally {
  216. if (is != null) {
  217. try {
  218. is.close();
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. }
  222. }
  223. if(wb != null) {
  224. try {
  225. wb.close();
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. }
  231. return batchInsert(orgsList);
  232. }
  233. /**
  234. * 根据数据格式返回数据
  235. *
  236. * @param cell
  237. * @return
  238. */
  239. public static String getValue(Cell cell) {
  240. if (cell == null) {
  241. return "";
  242. } else if (cell.getCellType() == CellType.BOOLEAN) {
  243. return String.valueOf(cell.getBooleanCellValue());
  244. } else if (cell.getCellType() == CellType.NUMERIC) {
  245. cell.setBlank();
  246. return String.valueOf(cell.getStringCellValue().trim());
  247. } else {
  248. return String.valueOf(cell.getStringCellValue().trim());
  249. }
  250. }
  251. }