OrganizationsService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 List<Organizations> queryOrgs(Organizations organization){
  74. return getMapper().queryOrgs(organization);
  75. }
  76. public boolean delete(Organizations organization) {
  77. if(super.delete(organization)){
  78. kafkaPersistService.send(
  79. KafkaIdentityTopic.ORG_TOPIC, organization, KafkaIdentityAction.DELETE_ACTION);
  80. return true;
  81. }
  82. return false;
  83. }
  84. public boolean importing(MultipartFile file) {
  85. if(file ==null){
  86. return false;
  87. }
  88. InputStream is = null;
  89. Workbook wb = null;
  90. List<Organizations> orgsList = null;
  91. try {
  92. is = file.getInputStream();
  93. String xls = ".xls";
  94. String xlsx = ".xlsx";
  95. int columnSize = 46;
  96. orgsList = Lists.newArrayList();
  97. if (file.getOriginalFilename().toLowerCase().endsWith(xls)) {
  98. wb = new HSSFWorkbook(is);
  99. } else if (file.getOriginalFilename().toLowerCase().endsWith(xlsx)) {
  100. wb = new XSSFWorkbook(is);
  101. } else {
  102. throw new RuntimeException("maxKey用户导入没有Excel类型");
  103. }
  104. int sheetSize = wb.getNumberOfSheets();
  105. //遍历sheet页
  106. for (int i = 0; i < sheetSize; i++) {
  107. Sheet sheet = wb.getSheetAt(i);
  108. int rowSize = sheet.getLastRowNum() + 1;
  109. //遍历行
  110. for (int j = 1; j < rowSize; j++) {
  111. Row row = sheet.getRow(j);
  112. //略过空行和前3行
  113. if (row == null || j <3 ) {
  114. continue;
  115. } else {
  116. //其他行是数据行
  117. Organizations organization =new Organizations();
  118. for (int k = 0; k < columnSize; k++) {
  119. if (k == 0) {
  120. // 上级编码
  121. Cell cell = row.getCell(k);
  122. organization.setParentId(getValue(cell));
  123. } else if (k == 1) {
  124. // 上级名称
  125. Cell cell = row.getCell(k);
  126. organization.setParentName(getValue(cell));
  127. } else if (k == 2) {
  128. // 机构编码
  129. Cell cell = row.getCell(k);
  130. organization.setId(getValue(cell));
  131. } else if (k == 3) {
  132. // 机构名称
  133. Cell cell = row.getCell(k);
  134. organization.setName(getValue(cell));
  135. } else if (k == 4) {
  136. // 机构全称
  137. Cell cell = row.getCell(k);
  138. organization.setFullName(getValue(cell));
  139. } else if (k == 5) {
  140. // 编码路径
  141. Cell cell = row.getCell(k);
  142. organization.setCodePath(getValue(cell));
  143. } else if (k == 6) {
  144. // 名称路径
  145. Cell cell = row.getCell(k);
  146. organization.setNamePath(getValue(cell));
  147. } else if (k == 7) {
  148. // 机构类型
  149. Cell cell = row.getCell(k);
  150. organization.setType(getValue(cell));
  151. } else if (k == 8) {
  152. // 所属分支机构
  153. Cell cell = row.getCell(k);
  154. organization.setDivision(getValue(cell));
  155. } else if (k == 9) {
  156. // 级别
  157. Cell cell = row.getCell(k);
  158. String level=getValue(cell);
  159. organization.setLevel(level.equals("") ? "1" : level);
  160. } else if (k == 10) {
  161. // 排序
  162. Cell cell = row.getCell(k);
  163. String sortIndex=getValue(cell);
  164. organization.setSortIndex(sortIndex.equals("") ? 1 : Integer.parseInt(sortIndex));
  165. } else if (k == 11) {
  166. // 联系人
  167. Cell cell = row.getCell(k);
  168. organization.setContact(getValue(cell));
  169. } else if (k == 12) {
  170. // 联系电话
  171. Cell cell = row.getCell(k);
  172. organization.setPhone(getValue(cell));
  173. }else if (k == 13) {
  174. // 邮箱
  175. Cell cell = row.getCell(k);
  176. organization.setEmail(getValue(cell));
  177. }else if (k == 14) {
  178. // 传真
  179. Cell cell = row.getCell(k);
  180. organization.setFax(getValue(cell));
  181. }else if (k == 24) {
  182. // 工作-国家
  183. Cell cell = row.getCell(k);
  184. organization.setCountry(getValue(cell));
  185. }else if (k == 25) {
  186. // 工作-省
  187. Cell cell = row.getCell(k);
  188. organization.setRegion(getValue(cell));
  189. }else if (k == 26) {
  190. // 工作-城市
  191. Cell cell = row.getCell(k);
  192. organization.setLocality(getValue(cell));
  193. }else if (k == 27) {
  194. // 工作-地址
  195. Cell cell = row.getCell(k);
  196. organization.setLocality(getValue(cell));
  197. }else if (k == 28) {
  198. // 邮编
  199. Cell cell = row.getCell(k);
  200. organization.setPostalCode(getValue(cell));
  201. }else if (k == 29) {
  202. // 详细描述
  203. Cell cell = row.getCell(k);
  204. organization.setDescription(getValue(cell));
  205. }
  206. }
  207. organization.setStatus(1);
  208. orgsList.add(organization);
  209. }
  210. }
  211. }
  212. // 数据去重
  213. if(CollectionUtils.isEmpty(orgsList)){
  214. orgsList = orgsList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new));
  215. }
  216. } catch (IOException e) {
  217. e.printStackTrace();
  218. }finally {
  219. if (is != null) {
  220. try {
  221. is.close();
  222. } catch (IOException e) {
  223. e.printStackTrace();
  224. }
  225. }
  226. if(wb != null) {
  227. try {
  228. wb.close();
  229. } catch (IOException e) {
  230. e.printStackTrace();
  231. }
  232. }
  233. }
  234. return batchInsert(orgsList);
  235. }
  236. /**
  237. * 根据数据格式返回数据
  238. *
  239. * @param cell
  240. * @return
  241. */
  242. public static String getValue(Cell cell) {
  243. if (cell == null) {
  244. return "";
  245. } else if (cell.getCellType() == CellType.BOOLEAN) {
  246. return String.valueOf(cell.getBooleanCellValue());
  247. } else if (cell.getCellType() == CellType.NUMERIC) {
  248. cell.setBlank();
  249. return String.valueOf(cell.getStringCellValue().trim());
  250. } else {
  251. return String.valueOf(cell.getStringCellValue().trim());
  252. }
  253. }
  254. }