AccountsStrategy

This commit is contained in:
MaxKey
2021-11-16 11:48:08 +08:00
parent 29b5181adf
commit 633247aca1
24 changed files with 370 additions and 95 deletions
@@ -22,6 +22,8 @@ package org.maxkey.persistence.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.mybatis.jpa.persistence.IJpaBaseMapper;
import org.maxkey.entity.Accounts;
import org.maxkey.entity.AccountsStrategy;
@@ -37,4 +39,9 @@ public interface AccountsMapper extends IJpaBaseMapper<Accounts> {
public List<UserInfo> queryUserNotInStrategy(AccountsStrategy strategy);
public long deleteByStrategy(AccountsStrategy strategy);
public List<Accounts> queryByAppIdAndDate(Accounts account);
@Select("select * from mxk_accounts where appid=#{appId} and relatedusername=#{relatedUsername}")
public List<Accounts> queryByAppIdAndAccount(@Param ("appId") String appId,@Param ("relatedUsername") String relatedUsername);
}
@@ -34,6 +34,13 @@ import org.maxkey.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
@Repository
public class AccountsService extends JpaBaseService<Accounts>{
@@ -133,22 +140,7 @@ public class AccountsService extends JpaBaseService<Accounts>{
account.setUserId(user.getId());
account.setUsername(user.getUsername());
account.setDisplayName(user.getDisplayName());
if(strategy.getMapping().equalsIgnoreCase("username")) {
account.setRelatedUsername(user.getUsername());
}else if(strategy.getMapping().equalsIgnoreCase("mobile")) {
account.setRelatedUsername(user.getMobile());
}else if(strategy.getMapping().equalsIgnoreCase("email")) {
account.setRelatedUsername(user.getEmail());
}else if(strategy.getMapping().equalsIgnoreCase("employeeNumber")) {
account.setRelatedUsername(user.getEmployeeNumber());
}else if(strategy.getMapping().equalsIgnoreCase("windowsAccount")) {
account.setRelatedUsername(user.getWindowsAccount());
}else if(strategy.getMapping().equalsIgnoreCase("idCardNo")) {
account.setRelatedUsername(user.getIdCardNo());
}else {
account.setRelatedUsername(user.getUsername());
}
account.setRelatedUsername(generateAccount(user,strategy));
account.setRelatedPassword(ReciprocalUtils.encode(userInfoService.randomPassword()));
account.setCreateType("automatic");
@@ -160,7 +152,9 @@ public class AccountsService extends JpaBaseService<Accounts>{
deleteByStrategy(strategy);
}
public void refreshAllByStrategy() {
for( AccountsStrategy strategy : accountsStrategyService.query(null)) {
AccountsStrategy queryStrategy = new AccountsStrategy();
queryStrategy.setCreateType("automatic");
for( AccountsStrategy strategy : accountsStrategyService.query(queryStrategy)) {
refreshByStrategy(strategy);
}
}
@@ -174,4 +168,89 @@ public class AccountsService extends JpaBaseService<Accounts>{
return getMapper().deleteByStrategy(strategy);
}
public List<Accounts> queryByAppIdAndDate(Accounts account) {
return getMapper().queryByAppIdAndDate(account);
}
public List<Accounts> queryByAppIdAndAccount(String appId,String relatedUsername){
return getMapper().queryByAppIdAndAccount(appId,relatedUsername);
}
public String generateAccount(UserInfo userInfo,AccountsStrategy accountsStrategy) {
String shortAccount = generateAccount(userInfo,accountsStrategy,true);
String account = generateAccount(userInfo,accountsStrategy,false);
String accountResult = shortAccount;
List<Accounts> AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),shortAccount +accountsStrategy.getSuffixes());
if(!AccountsList.isEmpty()) {
if(accountsStrategy.getMapping().equalsIgnoreCase("email")) {
accountResult = account;
AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),account + accountsStrategy.getSuffixes());
}
if(!AccountsList.isEmpty()) {
for(int i =1 ;i < 100 ;i++) {
accountResult = account + i;
AccountsList =getMapper().queryByAppIdAndAccount(accountsStrategy.getAppId(),accountResult + accountsStrategy.getSuffixes());
if(AccountsList.isEmpty())break;
}
}
}
if(StringUtils.isNotBlank(accountsStrategy.getSuffixes())){
accountResult = accountResult + accountsStrategy.getSuffixes();
}
return accountResult;
}
private String generateAccount(UserInfo userInfo,AccountsStrategy strategy,boolean isShort) {
String account = "";
if(strategy.getMapping().equalsIgnoreCase("username")) {
account = userInfo.getUsername();
}else if(strategy.getMapping().equalsIgnoreCase("mobile")) {
account = userInfo.getMobile();
}else if(strategy.getMapping().equalsIgnoreCase("email")) {
try {
if(isShort) {
account = getPinYinShortName(userInfo.getDisplayName());
}else {
account = getPinYinName(userInfo.getDisplayName());
}
}catch(Exception e) {
e.printStackTrace();
}
}else if(strategy.getMapping().equalsIgnoreCase("employeeNumber")) {
account = userInfo.getEmployeeNumber();
}else if(strategy.getMapping().equalsIgnoreCase("windowsAccount")) {
account = userInfo.getWindowsAccount();
}else if(strategy.getMapping().equalsIgnoreCase("idCardNo")) {
account = userInfo.getIdCardNo();
}else {
account = userInfo.getUsername();
}
return account;
}
public static String getPinYinName(String name) throws BadHanyuPinyinOutputFormatCombination {
HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
pinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
pinyinFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
return PinyinHelper.toHanYuPinyinString(name, pinyinFormat, "",false);
}
public static String getPinYinShortName(String name) throws BadHanyuPinyinOutputFormatCombination {
char[] strs = name.toCharArray();
String pinyinName = "";
for(int i=0;i<strs.length;i++) {
if(i == 0) {
pinyinName += getPinYinName(strs[i]+"");
}else {
pinyinName += getPinYinName(strs[i]+"").charAt(0);
}
}
return pinyinName;
}
}
@@ -3,27 +3,29 @@
<mapper namespace="org.maxkey.persistence.mapper.AccountsStrategyMapper">
<sql id="where_statement">
<if test="id != null and id != ''">
and id = #{id}
</if>
<if test="appId != null and appId != ''">
and appid = #{appId}
</if>
<if test="name != null and name != ''">
and name = #{name}
</if>
<if test="id != null and id != ''">
and mas.id = #{id}
</if>
<if test="appId != null and appId != ''">
and mas.appid = #{appId}
</if>
<if test="name != null and name != ''">
and mas.name = #{name}
</if>
</sql>
<select id="queryPageResults" parameterType="AccountsStrategy" resultType="AccountsStrategy">
select
*
from
mxk_accounts_strategy
where
(1=1)
<include refid="where_statement"/>
</select>
<select id="queryPageResults" parameterType="AccountsStrategy" resultType="AccountsStrategy">
select
mas.*,
ma.icon appicon
from
mxk_accounts_strategy mas,
mxk_apps ma
where
mas.appid = ma.id
<include refid="where_statement"/>
</select>
</mapper>
@@ -21,9 +21,6 @@
<if test="vendor != null and vendor != ''">
and vendor = #{vendor}
</if>
<if test="accountMgmt == 1 or accountMgmt == 2">
and accountmgmt = #{accountMgmt}
</if>
</sql>
@@ -71,7 +68,6 @@
principal,
credentials,
accountmgmt,
visible,
@@ -113,7 +109,6 @@
#{principal},
#{credentials},
#{accountMgmt},
#{visible},
@@ -162,7 +157,6 @@
</if>
principal = #{principal},
credentials = #{credentials},
accountmgmt = #{accountMgmt},
visible = #{visible},
sortindex = #{sortIndex},