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(); + } +}