@Entity
@Table(name = "t_user_set_info",
indexes = {@Index(name = "USERID_INDX", columnList = "userId")}
)
public class UserSetInfo extends SqlSerializeState implements SqlSerialize {
private long id;
private long userId;
private String openStatus;//0是开启,1是关闭
//根据PushType里面的类型就有几个,就存放几个状态使用逗号隔开
@Transient
private List<Integer> statusList;
public void updateStatusList(int pos,int value) {
getStatusList();
statusList.set(pos, value);
}
public List<Integer> getStatusList() {
if (statusList == null) {
statusList = new ArrayList<>();
for (String str : openStatus.split(",")) {
statusList.add(Integer.parseInt(str));
}
}
return statusList;
}
public void setStatusList(List<Integer> statusList) {
this.statusList = statusList;
}
public UserSetInfo(long userId, String openStatus) {
this.userId = userId;
this.openStatus = openStatus;
}
@Override
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
@Override
public void setId(long id) {
this.id = id;
}
public UserSetInfo() {
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getOpenStatus() {
return openStatus;
}
public void setOpenStatus(String openStatus) {
this.openStatus = openStatus;
}
}
@Component
public class UserSetInfoService {
private static final Logger logger = LoggerFactory.getLogger(UserSetInfoService.class);
@Autowired
private UserSetInfoDao userSetInfoDao;
public void changeStatus(long userId, int pos, int value) {
UserSetInfo userSetInfo = getUserSetInfoByUserId(userId);
userSetInfo.updateStatusList(pos, value);
userSetInfo.setOpenStatus(userSetInfo.getStatusList().stream().map(String::valueOf).collect(Collectors.joining(",")));
userSetInfoDao.saveOrUpdate(userSetInfo);
}
public UserSetInfo getUserSetInfoByUserId(long userId) {
UserSetInfo userSetInfo = userSetInfoDao.getUserSetInfoByUserId(userId);
if (userSetInfo == null) {
List<Integer> statusList = new ArrayList<>();
for (int i = 0; i < PushType.values().length; i++) {
statusList.add(0);//默认是开启的状态
}
userSetInfo = new UserSetInfo(userId, statusList.stream().map(String::valueOf).collect(Collectors.joining(",")));
userSetInfoDao.saveOrUpdate(userSetInfo);
}
return userSetInfo;
}
}
由List<Integer> statusList 转成String status : statusList.stream().map(String::valueOf).collect(Collectors.joining(","))
<使用逗号隔开>