AccountsService.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.util.List;
  18. import org.apache.mybatis.jpa.persistence.JpaBaseService;
  19. import org.maxkey.constants.ConstsStatus;
  20. import org.maxkey.crypto.password.PasswordReciprocal;
  21. import org.maxkey.entity.Accounts;
  22. import org.maxkey.entity.AccountsStrategy;
  23. import org.maxkey.entity.OrganizationsCast;
  24. import org.maxkey.entity.UserInfo;
  25. import org.maxkey.persistence.mapper.AccountsMapper;
  26. import org.maxkey.provision.ProvisionService;
  27. import org.maxkey.provision.ProvisionAction;
  28. import org.maxkey.provision.ProvisionTopic;
  29. import org.maxkey.util.StringUtils;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.stereotype.Repository;
  32. import net.sourceforge.pinyin4j.PinyinHelper;
  33. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  34. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  35. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  36. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  37. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  38. @Repository
  39. public class AccountsService extends JpaBaseService<Accounts>{
  40. @Autowired
  41. ProvisionService mqPersistService;
  42. @Autowired
  43. UserInfoService userInfoService;
  44. @Autowired
  45. AccountsStrategyService accountsStrategyService;
  46. @Autowired
  47. OrganizationsCastService organizationsCastService;
  48. public AccountsService() {
  49. super(AccountsMapper.class);
  50. }
  51. /* (non-Javadoc)
  52. * @see com.connsec.db.service.BaseService#getMapper()
  53. */
  54. @Override
  55. public AccountsMapper getMapper() {
  56. return (AccountsMapper)super.getMapper();
  57. }
  58. public boolean insert(Accounts account) {
  59. if (super.insert(account)) {
  60. if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
  61. UserInfo loadUserInfo = userInfoService.findUserRelated(account.getUserId());
  62. account.setUserInfo(loadUserInfo);
  63. OrganizationsCast cast = new OrganizationsCast();
  64. cast.setProvider(account.getAppId());
  65. cast.setOrgId(loadUserInfo.getDepartmentId());
  66. account.setOrgCast(organizationsCastService.query(cast));
  67. mqPersistService.send(
  68. ProvisionTopic.ACCOUNT_TOPIC,
  69. account,
  70. ProvisionAction.CREATE_ACTION);
  71. }
  72. return true;
  73. }
  74. return false;
  75. }
  76. public boolean update(Accounts account) {
  77. if (super.update(account)) {
  78. if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
  79. UserInfo loadUserInfo = userInfoService.findUserRelated(account.getUserId());
  80. account.setUserInfo(loadUserInfo);
  81. OrganizationsCast cast = new OrganizationsCast();
  82. cast.setProvider(account.getAppId());
  83. cast.setOrgId(loadUserInfo.getDepartmentId());
  84. account.setOrgCast(organizationsCastService.query(cast));
  85. mqPersistService.send(
  86. ProvisionTopic.ACCOUNT_TOPIC,
  87. account,
  88. ProvisionAction.UPDATE_ACTION);
  89. }
  90. return true;
  91. }
  92. return false;
  93. }
  94. public boolean updateStatus(Accounts accounts) {
  95. return this.getMapper().updateStatus(accounts) > 0;
  96. }
  97. public boolean remove(String id) {
  98. Accounts account = this.get(id);
  99. if (super.remove(id)) {
  100. UserInfo loadUserInfo = null;
  101. if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
  102. loadUserInfo = userInfoService.findUserRelated(account.getUserId());
  103. account.setUserInfo(loadUserInfo);
  104. mqPersistService.send(
  105. ProvisionTopic.ACCOUNT_TOPIC,
  106. account,
  107. ProvisionAction.DELETE_ACTION);
  108. }
  109. return true;
  110. }
  111. return false;
  112. }
  113. public void refreshByStrategy(AccountsStrategy strategy) {
  114. if(StringUtils.isNotBlank(strategy.getOrgIdsList())) {
  115. strategy.setOrgIdsList("'"+strategy.getOrgIdsList().replace(",", "','")+"'");
  116. }
  117. List<UserInfo> userList = queryUserNotInStrategy(strategy);
  118. for(UserInfo user : userList) {
  119. Accounts account = new Accounts();
  120. account.setAppId(strategy.getAppId());
  121. account.setAppName(strategy.getAppName());
  122. account.setUserId(user.getId());
  123. account.setUsername(user.getUsername());
  124. account.setDisplayName(user.getDisplayName());
  125. account.setRelatedUsername(generateAccount(user,strategy));
  126. account.setRelatedPassword(PasswordReciprocal.getInstance().encode(userInfoService.randomPassword()));
  127. account.setInstId(strategy.getInstId());
  128. account.setCreateType("automatic");
  129. account.setStatus(ConstsStatus.ACTIVE);
  130. account.setStrategyId(strategy.getId());
  131. insert(account);
  132. }
  133. deleteByStrategy(strategy);
  134. }
  135. public void refreshAllByStrategy() {
  136. AccountsStrategy queryStrategy = new AccountsStrategy();
  137. queryStrategy.setCreateType("automatic");
  138. for( AccountsStrategy strategy : accountsStrategyService.query(queryStrategy)) {
  139. refreshByStrategy(strategy);
  140. }
  141. }
  142. public List<UserInfo> queryUserNotInStrategy(AccountsStrategy strategy){
  143. return getMapper().queryUserNotInStrategy(strategy);
  144. }
  145. public long deleteByStrategy(AccountsStrategy strategy) {
  146. return getMapper().deleteByStrategy(strategy);
  147. }
  148. public List<Accounts> queryByAppIdAndDate(Accounts account) {
  149. return getMapper().queryByAppIdAndDate(account);
  150. }
  151. public List<Accounts> queryByAppIdAndAccount(String appId,String relatedUsername){
  152. return getMapper().queryByAppIdAndAccount(appId,relatedUsername);
  153. }
  154. public String generateAccount(UserInfo userInfo,AccountsStrategy accountsStrategy) {
  155. String shortAccount = generateAccount(userInfo,accountsStrategy,true);
  156. String account = generateAccount(userInfo,accountsStrategy,false);
  157. String accountResult = shortAccount;
  158. List<Accounts> AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),shortAccount +accountsStrategy.getSuffixes());
  159. if(!AccountsList.isEmpty()) {
  160. if(accountsStrategy.getMapping().equalsIgnoreCase("email")) {
  161. accountResult = account;
  162. AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),account + accountsStrategy.getSuffixes());
  163. }
  164. if(!AccountsList.isEmpty()) {
  165. for(int i =1 ;i < 100 ;i++) {
  166. accountResult = account + i;
  167. AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),accountResult + accountsStrategy.getSuffixes());
  168. if(AccountsList.isEmpty())break;
  169. }
  170. }
  171. }
  172. if(StringUtils.isNotBlank(accountsStrategy.getSuffixes())){
  173. accountResult = accountResult + accountsStrategy.getSuffixes();
  174. }
  175. return accountResult;
  176. }
  177. private String generateAccount(UserInfo userInfo,AccountsStrategy strategy,boolean isShort) {
  178. String account = "";
  179. if(strategy.getMapping().equalsIgnoreCase("username")) {
  180. account = userInfo.getUsername();
  181. }else if(strategy.getMapping().equalsIgnoreCase("mobile")) {
  182. account = userInfo.getMobile();
  183. }else if(strategy.getMapping().equalsIgnoreCase("email")) {
  184. try {
  185. if(isShort) {
  186. account = getPinYinShortName(userInfo.getDisplayName());
  187. }else {
  188. account = getPinYinName(userInfo.getDisplayName());
  189. }
  190. }catch(Exception e) {
  191. e.printStackTrace();
  192. }
  193. }else if(strategy.getMapping().equalsIgnoreCase("employeeNumber")) {
  194. account = userInfo.getEmployeeNumber();
  195. }else if(strategy.getMapping().equalsIgnoreCase("windowsAccount")) {
  196. account = userInfo.getWindowsAccount();
  197. }else if(strategy.getMapping().equalsIgnoreCase("idCardNo")) {
  198. account = userInfo.getIdCardNo();
  199. }else {
  200. account = userInfo.getUsername();
  201. }
  202. return account;
  203. }
  204. public static String getPinYinName(String name) throws BadHanyuPinyinOutputFormatCombination {
  205. HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
  206. pinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  207. pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  208. pinyinFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
  209. return PinyinHelper.toHanYuPinyinString(name, pinyinFormat, "",false);
  210. }
  211. public static String getPinYinShortName(String name) throws BadHanyuPinyinOutputFormatCombination {
  212. char[] strs = name.toCharArray();
  213. String pinyinName = "";
  214. for(int i=0;i<strs.length;i++) {
  215. if(i == 0) {
  216. pinyinName += getPinYinName(strs[i]+"");
  217. }else {
  218. pinyinName += getPinYinName(strs[i]+"").charAt(0);
  219. }
  220. }
  221. return pinyinName;
  222. }
  223. }