From cedfc78357148f44b77254edb84465676dcccfb2 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 6 Sep 2022 15:35:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3=EF=BC=9A?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=BB=91=E5=AE=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=9A=84=E6=8E=A8=E9=80=81=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/controller/PushController.java | 72 +++++++++++++++++++ src/main/java/com/xkrs/dao/SysUserDao.java | 3 +- .../com/xkrs/model/entity/SysUserEntity.java | 54 +++++++------- .../xkrs/model/qo/SysUserPushAccountQo.java | 38 ++++++++++ src/main/java/com/xkrs/utils/ListUtils.java | 57 +++++++++++++++ 5 files changed, 196 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/xkrs/controller/PushController.java create mode 100644 src/main/java/com/xkrs/model/qo/SysUserPushAccountQo.java create mode 100644 src/main/java/com/xkrs/utils/ListUtils.java diff --git a/src/main/java/com/xkrs/controller/PushController.java b/src/main/java/com/xkrs/controller/PushController.java new file mode 100644 index 0000000..cc1769b --- /dev/null +++ b/src/main/java/com/xkrs/controller/PushController.java @@ -0,0 +1,72 @@ +package com.xkrs.controller; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.dao.SysUserDao; +import com.xkrs.model.entity.SysUserEntity; +import com.xkrs.model.qo.SysUserPushAccountQo; +import com.xkrs.utils.ListUtils; +import org.apache.hc.core5.util.TextUtils; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Optional; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +@RestController +public class PushController { + + private final Locale locale = LocaleContextHolder.getLocale(); + + @Resource + private SysUserDao sysUserDao; + + @PostMapping("/bindPushInfo") + public String bindPushInfo(@RequestBody SysUserPushAccountQo sysUserPushAccountQo) { + Integer id = sysUserPushAccountQo.getId(); + String userAccount = sysUserPushAccountQo.getUserAccount(); + String regID = sysUserPushAccountQo.getRegID(); + if (null == id) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,id == null", locale); + } + Optional targetSysUserOptional = sysUserDao.findById(id); + if (targetSysUserOptional.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,id不存在", locale); + } + if (TextUtils.isEmpty(userAccount)) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,userAccount == null", locale); + } + if (TextUtils.isEmpty(regID)) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,regID == null", locale); + } + SysUserEntity targetSysUser = targetSysUserOptional.get(); + // 更新 userAccount + targetSysUser.setPushUserAccount(userAccount); + // 更新 regID + String pushRegIDs = targetSysUser.getPushRegID(); + if (TextUtils.isEmpty(pushRegIDs)) { + targetSysUser.setPushRegID(regID); + } else { + List pushRegIDList = new ArrayList<>(); + if (pushRegIDs.contains(",")) { + pushRegIDList.addAll(ListUtils.toStringList(pushRegIDs, ",")); + } else { + pushRegIDList.add(pushRegIDs); + } + if (!pushRegIDList.contains(regID)) { + pushRegIDList.add(regID); + } + String newPushRegIDs = ListUtils.fromStringList(pushRegIDList, ","); + targetSysUser.setPushRegID(newPushRegIDs); + } + sysUserDao.save(targetSysUser); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "绑定成功", locale); + } + +} diff --git a/src/main/java/com/xkrs/dao/SysUserDao.java b/src/main/java/com/xkrs/dao/SysUserDao.java index 97756ee..832c58c 100644 --- a/src/main/java/com/xkrs/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/dao/SysUserDao.java @@ -3,6 +3,7 @@ package com.xkrs.dao; import com.xkrs.model.entity.SysUserEntity; import com.xkrs.model.vo.SysUserVo; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -14,7 +15,7 @@ import java.util.List; * * @author tajochen */ -public interface SysUserDao extends JpaRepository { +public interface SysUserDao extends JpaRepository, JpaSpecificationExecutor { @Modifying(clearAutomatically = true) @Query(value = "UPDATE sys_user SET vip_level = ?2 WHERE id = ?1", nativeQuery = true) diff --git a/src/main/java/com/xkrs/model/entity/SysUserEntity.java b/src/main/java/com/xkrs/model/entity/SysUserEntity.java index e6ee5da..59e1b9d 100644 --- a/src/main/java/com/xkrs/model/entity/SysUserEntity.java +++ b/src/main/java/com/xkrs/model/entity/SysUserEntity.java @@ -124,6 +124,16 @@ public class SysUserEntity implements Serializable { */ private Long agentOrgId; + /** + * 推送的 userAccount + */ + private String pushUserAccount; + + /** + * 推送的 regID + */ + private String pushRegID; + public SysUserEntity() { } @@ -327,34 +337,24 @@ public class SysUserEntity implements Serializable { this.agentOrgId = agentOrgId; } + public String getPushUserAccount() { + return pushUserAccount; + } + + public void setPushUserAccount(String pushUserAccount) { + this.pushUserAccount = pushUserAccount; + } + + public String getPushRegID() { + return pushRegID; + } + + public void setPushRegID(String pushRegID) { + this.pushRegID = pushRegID; + } + @Override public String toString() { - return "SysUserEntity{" + - "id=" + id + - ", userName='" + userName + '\'' + - ", reallyName='" + reallyName + '\'' + - ", password='" + password + '\'' + - ", salt='" + salt + '\'' + - ", telephone='" + telephone + '\'' + - ", countyCode='" + countyCode + '\'' + - ", signature='" + signature + '\'' + - ", activeFlag=" + activeFlag + - ", statusCode=" + statusCode + - ", addTime='" + addTime + '\'' + - ", lastEntryTime=" + lastEntryTime + - ", deleteFlag=" + deleteFlag + - ", lastEntryIp='" + lastEntryIp + '\'' + - ", dayNum=" + dayNum + - ", overTime='" + overTime + '\'' + - ", accountType='" + accountType + '\'' + - ", countyName='" + countyName + '\'' + - ", loginNum=" + loginNum + - ", loginLastTime='" + loginLastTime + '\'' + - ", userAgent='" + userAgent + '\'' + - ", vipLevel=" + vipLevel + - ", receiveSms=" + receiveSms + - ", remark='" + remark + '\'' + - ", agentOrgId=" + agentOrgId + - '}'; + return "SysUserEntity{" + "id=" + id + ", userName='" + userName + '\'' + ", reallyName='" + reallyName + '\'' + ", password='" + password + '\'' + ", salt='" + salt + '\'' + ", telephone='" + telephone + '\'' + ", countyCode='" + countyCode + '\'' + ", signature='" + signature + '\'' + ", activeFlag=" + activeFlag + ", statusCode=" + statusCode + ", addTime='" + addTime + '\'' + ", lastEntryTime=" + lastEntryTime + ", deleteFlag=" + deleteFlag + ", lastEntryIp='" + lastEntryIp + '\'' + ", dayNum=" + dayNum + ", overTime='" + overTime + '\'' + ", accountType='" + accountType + '\'' + ", countyName='" + countyName + '\'' + ", loginNum=" + loginNum + ", loginLastTime='" + loginLastTime + '\'' + ", userAgent='" + userAgent + '\'' + ", vipLevel=" + vipLevel + ", receiveSms=" + receiveSms + ", remark='" + remark + '\'' + ", agentOrgId=" + agentOrgId + ", pushUserAccount='" + pushUserAccount + '\'' + ", pushRegID='" + pushRegID + '\'' + '}'; } } diff --git a/src/main/java/com/xkrs/model/qo/SysUserPushAccountQo.java b/src/main/java/com/xkrs/model/qo/SysUserPushAccountQo.java new file mode 100644 index 0000000..830fd44 --- /dev/null +++ b/src/main/java/com/xkrs/model/qo/SysUserPushAccountQo.java @@ -0,0 +1,38 @@ +package com.xkrs.model.qo; + +public class SysUserPushAccountQo { + + private Integer id; + + private String userAccount; + + private String regID; + + public SysUserPushAccountQo() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUserAccount() { + return userAccount; + } + + public void setUserAccount(String userAccount) { + this.userAccount = userAccount; + } + + public String getRegID() { + return regID; + } + + public void setRegID(String regID) { + this.regID = regID; + } + +} diff --git a/src/main/java/com/xkrs/utils/ListUtils.java b/src/main/java/com/xkrs/utils/ListUtils.java new file mode 100644 index 0000000..54e5dd4 --- /dev/null +++ b/src/main/java/com/xkrs/utils/ListUtils.java @@ -0,0 +1,57 @@ +package com.xkrs.utils; + +import org.apache.hc.core5.util.TextUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ListUtils { + + /** + * 字符串切割为集合 + * + * @param content + * @param splitRegex + * @return + */ + public static List toStringList(String content, String splitRegex) { + if (TextUtils.isEmpty(splitRegex)) { + throw new RuntimeException("splitRegex == null"); + } + List list = new ArrayList<>(); + if (content.contains(splitRegex)) { + String[] splitArray = content.split(splitRegex); + if (splitArray.length > 0) { + list.addAll(Arrays.asList(splitArray)); + } + } else { + list.add(content); + } + return list; + } + + /** + * 集合转字符串 + * + * @param list + * @param splitRegex + * @return + */ + public static String fromStringList(List list, String splitRegex) { + if (TextUtils.isEmpty(splitRegex)) { + throw new RuntimeException("splitRegex == null"); + } + StringBuilder builder = new StringBuilder(); + if (list != null && list.size() > 0) { + int size = list.size(); + for (int i = 0; i < size; i++) { + builder.append(list.get(i)); + if (i < (size - 1)) { + builder.append(splitRegex); + } + } + } + return builder.toString(); + } +}