UserInfoService.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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.text.DecimalFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.util.ArrayList;
  22. import java.util.Comparator;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.TreeSet;
  26. import java.util.stream.Collectors;
  27. import org.apache.mybatis.jpa.persistence.JpaBaseService;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.ss.usermodel.Cell;
  30. import org.apache.poi.ss.usermodel.CellType;
  31. import org.apache.poi.ss.usermodel.Row;
  32. import org.apache.poi.ss.usermodel.Sheet;
  33. import org.apache.poi.ss.usermodel.Workbook;
  34. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  35. import org.maxkey.constants.ConstantsStatus;
  36. import org.maxkey.crypto.ReciprocalUtils;
  37. import org.maxkey.crypto.password.PasswordReciprocal;
  38. import org.maxkey.entity.Accounts;
  39. import org.maxkey.entity.ChangePassword;
  40. import org.maxkey.entity.UserInfo;
  41. import org.maxkey.persistence.db.PasswordPolicyValidator;
  42. import org.maxkey.persistence.kafka.KafkaIdentityAction;
  43. import org.maxkey.persistence.kafka.KafkaIdentityTopic;
  44. import org.maxkey.persistence.kafka.KafkaPersistService;
  45. import org.maxkey.persistence.mapper.UserInfoMapper;
  46. import org.maxkey.util.DateUtils;
  47. import org.maxkey.util.StringUtils;
  48. import org.maxkey.web.WebContext;
  49. import org.slf4j.Logger;
  50. import org.slf4j.LoggerFactory;
  51. import org.springframework.beans.factory.annotation.Autowired;
  52. import org.springframework.jdbc.core.JdbcTemplate;
  53. import org.springframework.security.crypto.password.PasswordEncoder;
  54. import org.springframework.stereotype.Repository;
  55. import org.springframework.util.CollectionUtils;
  56. import org.springframework.web.multipart.MultipartFile;
  57. import com.google.common.collect.Lists;
  58. /**
  59. * @author Crystal.Sea
  60. *
  61. */
  62. @Repository
  63. public class UserInfoService extends JpaBaseService<UserInfo> {
  64. final static Logger _logger = LoggerFactory.getLogger(UserInfoService.class);
  65. @Autowired
  66. private PasswordEncoder passwordEncoder;
  67. @Autowired
  68. PasswordPolicyValidator passwordPolicyValidator;
  69. @Autowired
  70. KafkaPersistService kafkaPersistService;
  71. @Autowired
  72. protected JdbcTemplate jdbcTemplate;
  73. AccountsService accountsService;
  74. public UserInfoService() {
  75. super(UserInfoMapper.class);
  76. }
  77. /* (non-Javadoc)
  78. * @see com.connsec.db.service.BaseService#getMapper()
  79. */
  80. @Override
  81. public UserInfoMapper getMapper() {
  82. return (UserInfoMapper)super.getMapper();
  83. }
  84. public boolean insert(UserInfo userInfo) {
  85. userInfo = passwordEncoder(userInfo);
  86. if (super.insert(userInfo)) {
  87. if(kafkaPersistService.getApplicationConfig().isKafkaSupport()) {
  88. UserInfo loadUserInfo = loadUserRelated(userInfo.getId());
  89. kafkaPersistService.send(
  90. KafkaIdentityTopic.USERINFO_TOPIC,
  91. loadUserInfo,
  92. KafkaIdentityAction.CREATE_ACTION);
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98. public boolean update(UserInfo userInfo) {
  99. userInfo = passwordEncoder(userInfo);
  100. if (super.update(userInfo)) {
  101. if(kafkaPersistService.getApplicationConfig().isKafkaSupport()) {
  102. UserInfo loadUserInfo = loadUserRelated(userInfo.getId());
  103. accountUpdate(loadUserInfo);
  104. kafkaPersistService.send(
  105. KafkaIdentityTopic.USERINFO_TOPIC,
  106. loadUserInfo,
  107. KafkaIdentityAction.UPDATE_ACTION);
  108. }
  109. changePasswordProvisioning(userInfo);
  110. return true;
  111. }
  112. return false;
  113. }
  114. public boolean delete(UserInfo userInfo) {
  115. UserInfo loadUserInfo = null;
  116. if(kafkaPersistService.getApplicationConfig().isKafkaSupport()) {
  117. loadUserInfo = loadUserRelated(userInfo.getId());
  118. }
  119. if( super.delete(userInfo)){
  120. kafkaPersistService.send(
  121. KafkaIdentityTopic.USERINFO_TOPIC,
  122. loadUserInfo,
  123. KafkaIdentityAction.DELETE_ACTION);
  124. accountUpdate(loadUserInfo);
  125. return true;
  126. }
  127. return false;
  128. }
  129. //更新账号状态
  130. public void accountUpdate(UserInfo userInfo) {
  131. if(userInfo.getStatus() != ConstantsStatus.ACTIVE) {
  132. if(accountsService==null) {
  133. accountsService =
  134. (AccountsService)WebContext.getBean("accountsService");
  135. }
  136. Accounts queryAcount =new Accounts();
  137. queryAcount.setUserId(userInfo.getId());
  138. for (Accounts acount : accountsService.query(queryAcount)) {
  139. acount.setStatus(ConstantsStatus.INACTIVE);
  140. accountsService.update(acount);
  141. }
  142. }
  143. }
  144. public UserInfo loadUserRelated(String userId) {
  145. UserInfo loadUserInfo =this.get(userId);
  146. loadUserInfo.setDepts(getMapper().loadDeptsByUserId(userId));
  147. loadUserInfo.setAdjoints(getMapper().loadAdjointsByUserId(userId));
  148. return loadUserInfo;
  149. }
  150. public boolean updateGridList(String gridList) {
  151. try {
  152. if (gridList != null && !gridList.equals("")) {
  153. WebContext.getUserInfo().setGridList(Integer.parseInt(gridList));
  154. getMapper().updateGridList(WebContext.getUserInfo());
  155. }
  156. }catch(Exception e) {
  157. e.printStackTrace();
  158. return false;
  159. }
  160. return true;
  161. }
  162. public boolean updateProtectedApps(UserInfo userinfo) {
  163. try {
  164. if(WebContext.getUserInfo() != null) {
  165. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  166. }
  167. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  168. return getMapper().updateProtectedApps(userinfo) > 0;
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. }
  172. return false;
  173. }
  174. public UserInfo loadByUsername(String username) {
  175. return getMapper().loadByUsername(username);
  176. }
  177. public UserInfo loadByAppIdAndUsername(String appId,String username){
  178. try {
  179. UserInfo userinfo = new UserInfo();
  180. userinfo.setUsername(username);
  181. return getMapper().loadByAppIdAndUsername(userinfo) ;
  182. } catch (Exception e) {
  183. e.printStackTrace();
  184. }
  185. return null;
  186. }
  187. public void logisticDeleteAllByCid(String cid){
  188. try {
  189. getMapper().logisticDeleteAllByCid(cid);
  190. } catch(Exception e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. public UserInfo passwordEncoder(UserInfo userInfo) {
  195. //密码不为空,则需要进行加密处理
  196. if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
  197. String password = passwordEncoder.encode(userInfo.getPassword());
  198. userInfo.setDecipherable(ReciprocalUtils.encode(PasswordReciprocal.getInstance().rawPassword(userInfo.getUsername(), userInfo.getPassword())));
  199. _logger.debug("decipherable : "+userInfo.getDecipherable());
  200. userInfo.setPassword(password);
  201. userInfo.setPasswordLastSetTime(DateUtils.getCurrentDateTimeAsString());
  202. userInfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  203. }
  204. return userInfo;
  205. }
  206. public boolean changePassword( String oldPassword,
  207. String newPassword,
  208. String confirmPassword,
  209. int passwordSetType) {
  210. try {
  211. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT, "");
  212. UserInfo userInfo = WebContext.getUserInfo();
  213. UserInfo changeUserInfo = new UserInfo();
  214. changeUserInfo.setUsername(userInfo.getUsername());
  215. changeUserInfo.setPassword(newPassword);
  216. changeUserInfo.setId(userInfo.getId());
  217. changeUserInfo.setDecipherable(userInfo.getDecipherable());
  218. changeUserInfo.setPasswordSetType(passwordSetType);
  219. if(newPassword.equals(confirmPassword)){
  220. if(oldPassword==null ||
  221. passwordEncoder.matches(oldPassword, userInfo.getPassword())){
  222. if(changePassword(changeUserInfo,true) ){
  223. userInfo.setPassword(changeUserInfo.getPassword());
  224. userInfo.setDecipherable(changeUserInfo.getDecipherable());
  225. return true;
  226. }
  227. return false;
  228. }else {
  229. if(oldPassword!=null &&
  230. passwordEncoder.matches(newPassword, userInfo.getPassword())) {
  231. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT,
  232. WebContext.getI18nValue("PasswordPolicy.OLD_PASSWORD_MATCH"));
  233. }else {
  234. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT,
  235. WebContext.getI18nValue("PasswordPolicy.OLD_PASSWORD_NOT_MATCH"));
  236. }
  237. }
  238. }else {
  239. WebContext.setAttribute(PasswordPolicyValidator.PASSWORD_POLICY_VALIDATE_RESULT,
  240. WebContext.getI18nValue("PasswordPolicy.CONFIRMPASSWORD_NOT_MATCH"));
  241. }
  242. } catch (Exception e) {
  243. e.printStackTrace();
  244. }
  245. return false;
  246. }
  247. public boolean changePassword(UserInfo changeUserInfo,boolean passwordPolicy) {
  248. try {
  249. _logger.debug("decipherable old : " + changeUserInfo.getDecipherable());
  250. _logger.debug("decipherable new : " + ReciprocalUtils.encode(PasswordReciprocal.getInstance()
  251. .rawPassword(changeUserInfo.getUsername(), changeUserInfo.getPassword())));
  252. if (passwordPolicy && passwordPolicyValidator.validator(changeUserInfo) == false) {
  253. return false;
  254. }
  255. if (WebContext.getUserInfo() != null) {
  256. changeUserInfo.setModifiedBy(WebContext.getUserInfo().getId());
  257. }
  258. changeUserInfo = passwordEncoder(changeUserInfo);
  259. if (getMapper().changePassword(changeUserInfo) > 0) {
  260. changePasswordProvisioning(changeUserInfo);
  261. return true;
  262. }
  263. return false;
  264. } catch (Exception e) {
  265. e.printStackTrace();
  266. }
  267. return false;
  268. }
  269. public String randomPassword() {
  270. return passwordPolicyValidator.generateRandomPassword();
  271. }
  272. public void changePasswordProvisioning(UserInfo userInfo) {
  273. if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) {
  274. ChangePassword changePassword=new ChangePassword();
  275. changePassword.setId(userInfo.getId());
  276. changePassword.setUserId(userInfo.getId());
  277. changePassword.setUsername(userInfo.getUsername());
  278. changePassword.setDecipherable(userInfo.getDecipherable());
  279. changePassword.setPassword(userInfo.getPassword());
  280. kafkaPersistService.send(
  281. KafkaIdentityTopic.PASSWORD_TOPIC,
  282. changePassword,
  283. KafkaIdentityAction.PASSWORD_ACTION);
  284. }
  285. }
  286. public boolean changeAppLoginPassword(UserInfo userinfo) {
  287. try {
  288. if(WebContext.getUserInfo() != null) {
  289. userinfo.setModifiedBy(WebContext.getUserInfo().getId());
  290. }
  291. userinfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString());
  292. return getMapper().changeAppLoginPassword(userinfo) > 0;
  293. } catch (Exception e) {
  294. e.printStackTrace();
  295. }
  296. return false;
  297. }
  298. /**
  299. * 锁定用户:islock:1 用户解锁 2 用户锁定
  300. * @param userInfo
  301. */
  302. public void locked(UserInfo userInfo) {
  303. try {
  304. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  305. userInfo.setIsLocked(ConstantsStatus.STOP);
  306. getMapper().locked(userInfo);
  307. }
  308. } catch(Exception e) {
  309. e.printStackTrace();
  310. }
  311. }
  312. /**
  313. * 用户登录成功后,重置错误密码次数和解锁用户
  314. * @param userInfo
  315. */
  316. public void unlock(UserInfo userInfo) {
  317. try {
  318. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  319. userInfo.setIsLocked(ConstantsStatus.START);
  320. userInfo.setBadPasswordCount(0);
  321. getMapper().unlock(userInfo);
  322. }
  323. } catch(Exception e) {
  324. e.printStackTrace();
  325. }
  326. }
  327. /**
  328. * 更新错误密码次数
  329. * @param userInfo
  330. */
  331. public void updateBadPasswordCount(UserInfo userInfo) {
  332. try {
  333. if(userInfo != null && StringUtils.isNotEmpty(userInfo.getId())) {
  334. int updateBadPWDCount = userInfo.getBadPasswordCount() + 1;
  335. userInfo.setBadPasswordCount(updateBadPWDCount);
  336. getMapper().updateBadPWDCount(userInfo);
  337. }
  338. } catch(Exception e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. public boolean importing(MultipartFile file) {
  343. if(file ==null){
  344. return false;
  345. }
  346. InputStream is = null;
  347. Workbook wb = null;
  348. List<UserInfo> userInfoList = null;
  349. try {
  350. is = file.getInputStream();
  351. String xls = ".xls";
  352. String xlsx = ".xlsx";
  353. int columnSize = 46;
  354. userInfoList = Lists.newArrayList();
  355. if (file.getOriginalFilename().toLowerCase().endsWith(xls)) {
  356. wb = new HSSFWorkbook(is);
  357. } else if (file.getOriginalFilename().toLowerCase().endsWith(xlsx)) {
  358. wb = new XSSFWorkbook(is);
  359. } else {
  360. throw new RuntimeException("maxKey用户导入没有Excel类型");
  361. }
  362. int recordCount = 0;
  363. int sheetSize = wb.getNumberOfSheets();
  364. //遍历sheet页
  365. for (int i = 0; i < sheetSize; i++) {
  366. Sheet sheet = wb.getSheetAt(i);
  367. int rowSize = sheet.getLastRowNum() + 1;
  368. //遍历行
  369. for (int j = 1; j < rowSize; j++) {
  370. Row row = sheet.getRow(j);
  371. //略过空行和前3行
  372. if (row == null || j <3 ) {
  373. continue;
  374. } else {
  375. //其他行是数据行
  376. UserInfo userInfo = new UserInfo();
  377. userInfo.setCreatedDate(DateUtils.formatDateTime(new Date()));
  378. for (int k = 0; k < columnSize; k++) {
  379. if (k == 0) {
  380. // 登录账号
  381. Cell cell = row.getCell(k);
  382. userInfo.setUsername(getValue(cell));
  383. } else if (k == 1) {
  384. // 密码
  385. Cell cell = row.getCell(k);
  386. userInfo.setPassword(getValue(cell));
  387. } else if (k == 2) {
  388. // 用户显示
  389. Cell cell = row.getCell(k);
  390. userInfo.setDisplayName(getValue(cell));
  391. } else if (k == 3) {
  392. // 姓
  393. Cell cell = row.getCell(k);
  394. userInfo.setFamilyName(getValue(cell));
  395. } else if (k == 4) {
  396. // 名
  397. Cell cell = row.getCell(k);
  398. userInfo.setGivenName(getValue(cell));
  399. } else if (k == 5) {
  400. // 中间名
  401. Cell cell = row.getCell(k);
  402. userInfo.setMiddleName(getValue(cell));
  403. } else if (k == 6) {
  404. // 昵称
  405. Cell cell = row.getCell(k);
  406. userInfo.setNickName(getValue(cell));
  407. } else if (k == 7) {
  408. // 性别
  409. Cell cell = row.getCell(k);
  410. String gender = getValue(cell);
  411. userInfo.setGender(gender.equals("")? 1 : Integer.valueOf(getValue(cell)));
  412. } else if (k == 8) {
  413. // 语言偏好
  414. Cell cell = row.getCell(k);
  415. userInfo.setPreferredLanguage(getValue(cell));
  416. } else if (k == 9) {
  417. // 时区
  418. Cell cell = row.getCell(k);
  419. userInfo.setTimeZone(getValue(cell));
  420. } else if (k == 10) {
  421. // 用户类型
  422. Cell cell = row.getCell(k);
  423. userInfo.setUserType(getValue(cell));
  424. } else if (k == 11) {
  425. // 员工编码
  426. Cell cell = row.getCell(k);
  427. userInfo.setEmployeeNumber(getValue(cell));
  428. } else if (k == 12) {
  429. // AD域账号
  430. Cell cell = row.getCell(k);
  431. userInfo.setWindowsAccount(getValue(cell));
  432. }else if (k == 13) {
  433. // 所属机构
  434. Cell cell = row.getCell(k);
  435. userInfo.setOrganization(getValue(cell));
  436. }else if (k == 14) {
  437. // 分支机构
  438. Cell cell = row.getCell(k);
  439. userInfo.setDivision(getValue(cell));
  440. }else if (k == 15) {
  441. // 部门编号
  442. Cell cell = row.getCell(k);
  443. userInfo.setDepartmentId(getValue(cell));
  444. }else if (k == 16) {
  445. // 部门名称
  446. Cell cell = row.getCell(k);
  447. userInfo.setDepartment(getValue(cell));
  448. }else if (k == 17) {
  449. // 成本中心
  450. Cell cell = row.getCell(k);
  451. userInfo.setCostCenter(getValue(cell));
  452. }else if (k == 18) {
  453. // 职位
  454. Cell cell = row.getCell(k);
  455. userInfo.setJobTitle(getValue(cell));
  456. }else if (k == 19) {
  457. // 级别
  458. Cell cell = row.getCell(k);
  459. userInfo.setJobLevel(getValue(cell));
  460. }else if (k == 20) {
  461. // 上级经理
  462. Cell cell = row.getCell(k);
  463. userInfo.setManager(getValue(cell));
  464. }else if (k == 21) {
  465. // 助理
  466. Cell cell = row.getCell(k);
  467. userInfo.setAssistant(getValue(cell));
  468. }else if (k == 22) {
  469. // 入职时间
  470. Cell cell = row.getCell(k);
  471. userInfo.setEntryDate(getValue(cell));
  472. }else if (k == 23) {
  473. // 离职时间
  474. Cell cell = row.getCell(k);
  475. userInfo.setQuitDate(getValue(cell));
  476. }else if (k == 24) {
  477. // 工作-国家
  478. Cell cell = row.getCell(k);
  479. userInfo.setWorkCountry(getValue(cell));
  480. }else if (k == 25) {
  481. // 工作-省
  482. Cell cell = row.getCell(k);
  483. userInfo.setWorkRegion(getValue(cell));
  484. }else if (k == 26) {
  485. // 工作-城市
  486. Cell cell = row.getCell(k);
  487. userInfo.setTimeZone(getValue(cell));
  488. }else if (k == 27) {
  489. // 工作-地址
  490. Cell cell = row.getCell(k);
  491. userInfo.setWorkLocality(getValue(cell));
  492. }else if (k == 28) {
  493. // 邮编
  494. Cell cell = row.getCell(k);
  495. userInfo.setWorkPostalCode(getValue(cell));
  496. }else if (k == 29) {
  497. // 传真
  498. Cell cell = row.getCell(k);
  499. userInfo.setWorkFax(getValue(cell));
  500. }else if (k == 30) {
  501. // 工作电话
  502. Cell cell = row.getCell(k);
  503. userInfo.setWorkPhoneNumber(getValue(cell));
  504. }else if (k == 31) {
  505. // 工作邮件
  506. Cell cell = row.getCell(k);
  507. userInfo.setWorkEmail(getValue(cell));
  508. }else if (k == 32) {
  509. // 证件类型 todo 现在数据库中存储的是tinyint
  510. // Cell cell = row.getCell(k);
  511. // userInfo.setIdType(getValue(cell));
  512. }else if (k == 33) {
  513. // 证件号码
  514. Cell cell = row.getCell(k);
  515. userInfo.setIdCardNo(getValue(cell));
  516. } else if (k == 34) {
  517. // 出生日期
  518. Cell cell = row.getCell(k);
  519. userInfo.setBirthDate(getValue(cell));
  520. }else if (k == 35) {
  521. // 婚姻状态 todo 现在数据字段类型是 tinyint
  522. // Cell cell = row.getCell(k);
  523. // userInfo.setMarried(getValue(cell));
  524. }else if (k == 36) {
  525. // 开始工作时间
  526. Cell cell = row.getCell(k);
  527. userInfo.setStartWorkDate(getValue(cell));
  528. }else if (k == 37) {
  529. // 个人主页
  530. Cell cell = row.getCell(k);
  531. userInfo.setWebSite(getValue(cell));
  532. }else if (k == 38) {
  533. // 即时通讯
  534. Cell cell = row.getCell(k);
  535. userInfo.setDefineIm(getValue(cell));
  536. }else if (k == 39) {
  537. // 国家
  538. Cell cell = row.getCell(k);
  539. userInfo.setHomeCountry(getValue(cell));
  540. }else if (k == 40) {
  541. // 省
  542. Cell cell = row.getCell(k);
  543. userInfo.setHomeRegion(getValue(cell));
  544. }else if (k == 41) {
  545. // 城市
  546. Cell cell = row.getCell(k);
  547. userInfo.setHomeLocality(getValue(cell));
  548. }else if (k == 42) {
  549. // 家庭地址
  550. Cell cell = row.getCell(k);
  551. userInfo.setHomeStreetAddress(getValue(cell));
  552. }else if (k == 43) {
  553. // 家庭邮编
  554. Cell cell = row.getCell(k);
  555. userInfo.setHomePostalCode(getValue(cell));
  556. }else if (k == 44) {
  557. // 家庭传真
  558. Cell cell = row.getCell(k);
  559. userInfo.setHomeFax(getValue(cell));
  560. }else if (k == 45) {
  561. // 家庭电话
  562. Cell cell = row.getCell(k);
  563. userInfo.setHomePhoneNumber(getValue(cell));
  564. }else if (k == 46) {
  565. // 家庭邮箱
  566. Cell cell = row.getCell(k);
  567. userInfo.setHomeEmail(getValue(cell));
  568. }
  569. }
  570. userInfo.setStatus(1);
  571. userInfoList.add(passwordEncoder(userInfo));
  572. recordCount ++;
  573. _logger.debug("record {} user {} account {}",recordCount,userInfo.getDisplayName(),userInfo.getUsername());
  574. }
  575. }
  576. }
  577. // 数据去重
  578. if(CollectionUtils.isEmpty(userInfoList)){
  579. userInfoList = userInfoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUsername()))), ArrayList::new));
  580. }
  581. } catch (IOException e) {
  582. e.printStackTrace();
  583. }finally {
  584. if (is != null) {
  585. try {
  586. is.close();
  587. } catch (IOException e) {
  588. e.printStackTrace();
  589. }
  590. }
  591. if(wb != null) {
  592. try {
  593. wb.close();
  594. } catch (IOException e) {
  595. e.printStackTrace();
  596. }
  597. }
  598. }
  599. return batchInsert(userInfoList);
  600. }
  601. /**
  602. * 根据数据格式返回数据
  603. *
  604. * @param cell
  605. * @return
  606. */
  607. public static String getValue(Cell cell) {
  608. if (cell == null) {
  609. return "";
  610. } else if (cell.getCellType() == CellType.BOOLEAN) {
  611. return String.valueOf(cell.getBooleanCellValue());
  612. } else if (cell.getCellType() == CellType.NUMERIC) {
  613. if("General".equals(cell.getCellStyle().getDataFormatString())){
  614. return new DecimalFormat("0").format(cell.getNumericCellValue());
  615. }else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
  616. return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  617. }else{
  618. return new DecimalFormat("0").format(cell.getNumericCellValue());
  619. }
  620. } else {
  621. return String.valueOf(cell.getStringCellValue().trim());
  622. }
  623. }
  624. public boolean changeSharedSecret(UserInfo userInfo){
  625. return getMapper().changeSharedSecret(userInfo)>0;
  626. }
  627. public boolean changePasswordQuestion(UserInfo userInfo){
  628. return getMapper().changePasswordQuestion(userInfo)>0;
  629. }
  630. public boolean changeAuthnType(UserInfo userInfo){
  631. return getMapper().changeAuthnType(userInfo)>0;
  632. }
  633. public boolean changeEmail(UserInfo userInfo){
  634. return getMapper().changeEmail(userInfo)>0;
  635. }
  636. public boolean changeMobile(UserInfo userInfo){
  637. return getMapper().changeMobile(userInfo)>0;
  638. }
  639. public UserInfo queryUserInfoByEmailMobile(String emailMobile) {
  640. return getMapper().queryUserInfoByEmailMobile(emailMobile);
  641. }
  642. public int updateProfile(UserInfo userInfo){
  643. return getMapper().updateProfile(userInfo);
  644. }
  645. public void setPasswordPolicyValidator(PasswordPolicyValidator passwordPolicyValidator) {
  646. this.passwordPolicyValidator = passwordPolicyValidator;
  647. }
  648. }