Skip to content

Commit

Permalink
Merge pull request #184 from cheng521521/main
Browse files Browse the repository at this point in the history
feat: share rank top 3
  • Loading branch information
liberhe authored Mar 3, 2024
2 parents 046d484 + 1ed6d84 commit 2e962cc
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/dl/officialsite/bounty/Bounty.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand Down Expand Up @@ -65,6 +66,17 @@ public class Bounty {
//流支付ID
private String streamId;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime streamStart;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime streamEnd;

private String streamChainId;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime deadLine;


@CreationTimestamp
@Column(updatable = false, nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public BaseResponse detail(@RequestParam Long id,
@RequestParam String address,
@RequestParam(defaultValue = "1") Integer pageNumber,
@RequestParam(defaultValue = "10") Integer pageSize) {
BountyVo bountyVo = bountyService.findById(id);
BountyVo bountyVo = bountyService.findByIdInternal(id);
return BaseResponse.successWithData(bountyVo);
}

Expand Down Expand Up @@ -133,9 +133,8 @@ public BaseResponse isApply(@RequestParam Long bountyId, @RequestParam String ad

//关联流支付与bounty
@PostMapping("/link")
public BaseResponse link(@RequestParam Long bountyId, @RequestParam String address,
@RequestParam String streamId) {
bountyService.link(bountyId, address, streamId);
public BaseResponse link(@RequestParam String address,@RequestBody BountyVo bountyVo) {
bountyService.link(bountyVo, address);
return BaseResponse.successWithData(null);
}

Expand Down
91 changes: 53 additions & 38 deletions src/main/java/com/dl/officialsite/bounty/BountyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.dl.officialsite.common.exception.BizException;
import com.dl.officialsite.member.Member;
import com.dl.officialsite.member.MemberRepository;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.criteria.Predicate;
Expand Down Expand Up @@ -46,7 +47,7 @@ public BountyService(BountyRepository bountyRepository,
public BountyVo add(BountyVo bountyVo, String address) {
Bounty bounty = new Bounty();
BeanUtils.copyProperties(bountyVo, bounty);
bounty.setCreator(bountyVo.getCreator().getAddress());
bounty.setCreator(address);
bountyRepository.save(bounty);
bountyVo.setId(bounty.getId());
Member creatorInfo = memberRepository.findByAddress(bounty.getCreator()).orElse(null);
Expand All @@ -63,32 +64,49 @@ public void update(BountyVo bountyVo, String address) {
}

public Page<BountyVo> search(BountySearchVo bountySearchVo, Pageable pageable) {
return bountyRepository.findAll((Specification<Bounty>) (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (bountySearchVo.getCreator() != null) {
predicates.add(
criteriaBuilder.equal(root.get("creator"), bountySearchVo.getCreator()));
}
if (bountySearchVo.getTitle() != null) {
predicates.add(criteriaBuilder.like(root.get("title"),
"%" + bountySearchVo.getTitle() + "%"));
}
if (bountySearchVo.getStatus() != null) {
predicates.add(
criteriaBuilder.equal(root.get("status"), bountySearchVo.getStatus()));
}
predicates.add(criteriaBuilder.notEqual(root.get("status"), 5));
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
}, pageable).map(bounty -> {
BountyVo bountyVo = new BountyVo();
BeanUtils.copyProperties(bounty, bountyVo);
List<BountyMemberMap> bountyMember = findBountyMemberMapByBountyId(
bounty.getId());
bountyVo.setBountyMemberMaps(bountyMember);
Member creatorInfo = memberRepository.findByAddress(bounty.getCreator()).orElse(null);
bountyVo.setCreator(creatorInfo);
return bountyVo;
});
Specification<Bounty> spec = Specification.where(hasCreator(bountySearchVo.getCreator()))
.and(hasTitle(bountySearchVo.getTitle()))
.and(hasStatus(bountySearchVo.getStatus()))
.and(hasDeadLineBefore(bountySearchVo.getDeadLine()))
.and(isNotStatus(5));

Page<Bounty> bountyPage = bountyRepository.findAll(spec, pageable);
return bountyPage.map(this::mapToBountyVo);
}

private Specification<Bounty> hasCreator(String creator) {
return (root, query, criteriaBuilder) -> creator != null ?
criteriaBuilder.equal(root.get("creator"), creator) : null;
}

private Specification<Bounty> hasTitle(String title) {
return (root, query, criteriaBuilder) -> title != null ?
criteriaBuilder.like(root.get("title"), "%" + title + "%") : null;
}

private Specification<Bounty> hasStatus(Integer status) {
return (root, query, criteriaBuilder) -> status != null ?
criteriaBuilder.equal(root.get("status"), status) : null;
}

private Specification<Bounty> hasDeadLineBefore(LocalDateTime deadline) {
return (root, query, criteriaBuilder) -> deadline != null ?
criteriaBuilder.lessThan(root.get("deadLine"), deadline) : null;
}

private Specification<Bounty> isNotStatus(Integer status) {
return (root, query, criteriaBuilder) ->
criteriaBuilder.notEqual(root.get("status"), status);
}

private BountyVo mapToBountyVo(Bounty bounty) {
BountyVo bountyVo = new BountyVo();
BeanUtils.copyProperties(bounty, bountyVo);
List<BountyMemberMap> bountyMember = findBountyMemberMapByBountyId(bounty.getId());
bountyVo.setBountyMemberMaps(bountyMember);
Member creatorInfo = memberRepository.findByAddress(bounty.getCreator()).orElse(null);
bountyVo.setCreator(creatorInfo);
return bountyVo;
}

public void delete(Long id, String address) {
Expand All @@ -99,17 +117,11 @@ public void delete(Long id, String address) {
bountyRepository.save(bounty);
}

public BountyVo findById(Long id) {
public BountyVo findByIdInternal(Long id) {
Bounty bounty = bountyRepository.findById(id)
.orElseThrow(
() -> new BizException(NOT_FOUND_BOUNTY.getCode(), NOT_FOUND_BOUNTY.getMsg()));
BountyVo bountyVo = new BountyVo();
BeanUtils.copyProperties(bounty, bountyVo);
List<BountyMemberMap> bountyMemberMas = findBountyMemberMapByBountyId(id);
bountyVo.setBountyMemberMaps(bountyMemberMas);
Member creatorInfo = memberRepository.findByAddress(bounty.getCreator()).orElse(null);
bountyVo.setCreator(creatorInfo);
return bountyVo;
return mapToBountyVo(bounty);
}

public void apply(Long bountyId, String address) {
Expand Down Expand Up @@ -192,14 +204,17 @@ public Integer isApply(Long bountyId, String address) {
bountyId, address).map(BountyMemberMap::getStatus).orElse(null);
}

public void link(Long bountyId, String address, String streamId) {
Bounty bounty = bountyRepository.findById(bountyId).orElseThrow(
public void link(BountyVo bountyVo, String address) {
Bounty bounty = bountyRepository.findById(bountyVo.getId()).orElseThrow(
() -> new BizException(NOT_FOUND_BOUNTY.getCode(), NOT_FOUND_BOUNTY.getMsg()));
//String loginUser = UserSecurityUtils.getUserLogin().getAddress();
if (!bounty.getCreator().equals(address)) {
throw new BizException("2003", "not link bounty by creator");
}
bounty.setStreamId(streamId);
bounty.setStreamId(bountyVo.getStreamId());
bounty.setStreamEnd(bountyVo.getStreamStart());
bounty.setStreamEnd(bountyVo.getStreamEnd());
bounty.setStreamChainId(bountyVo.getStreamChainId());
bountyRepository.save(bounty);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dl.officialsite.bounty.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import lombok.Data;

/**
Expand All @@ -22,4 +24,7 @@ public class BountySearchVo {
//状态
private Integer status;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime deadLine;

}
12 changes: 12 additions & 0 deletions src/main/java/com/dl/officialsite/bounty/vo/BountyVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dl.officialsite.bounty.BountyMemberMap;
import com.dl.officialsite.member.Member;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import lombok.Data;
Expand Down Expand Up @@ -52,6 +53,17 @@ public class BountyVo {
//流支付ID
private String streamId;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime streamStart;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime streamEnd;

private String streamChainId;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime deadLine;

//申请人列表
private List<BountyMemberMap> bountyMemberMaps;

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/dl/officialsite/sharing/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,7 @@ public class Share {
@LastModifiedDate
@Column(updatable = false, nullable = false)
private Long updateTime;

@Transient
private String shareCount;
}
11 changes: 11 additions & 0 deletions src/main/java/com/dl/officialsite/sharing/SharingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.dl.officialsite.common.base.BaseResponse;
import com.dl.officialsite.login.Auth;
import com.dl.officialsite.sharing.constant.SharingStatus;
import com.dl.officialsite.sharing.model.bo.RankDto;
import com.dl.officialsite.sharing.model.req.UpdateSharingReq;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -116,4 +118,13 @@ public BaseResponse loadSharingByUser(@RequestParam("memberAddress") String memb
@RequestParam(value = "pageSize",defaultValue = "20") int pageSize) {
return BaseResponse.successWithData(this.sharingService.loadSharingByUser(memberAddress, pageNo, pageSize));
}

/**
* 分享排行榜
*/
@GetMapping("/rank")
public BaseResponse rank(@RequestParam("rankNumber") Integer rankNumber) {
List<RankDto> rankList = sharingService.rank(rankNumber);
return BaseResponse.successWithData(rankList);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.dl.officialsite.sharing;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SharingRepository extends JpaRepository<Share, Long>, JpaSpecificationExecutor<Share> {

@Query(value = "select * from share limit :offset, :limit", nativeQuery = true)
Expand All @@ -30,4 +29,8 @@ List<Share> findAllSharesByUidPaged(@Param("memberAddress") String memberAddress
// desc,id desc", countQuery = "SELECT count(*) FROM share", nativeQuery = true)
@Query(value = "SELECT * FROM share order by STR_TO_DATE(date,'%Y/%m/%d') desc,time desc,id desc", countQuery = "SELECT count(*) FROM share", nativeQuery = true)
Page<Share> findAllByPage(Pageable pageable);

@Query(value = "SELECT presenter, COUNT(*) AS shareCount FROM share GROUP BY presenter ORDER"
+ " BY shareCount DESC LIMIT :rankNumber" ,nativeQuery = true)
List<Object[]> findTopGroups(@Param("rankNumber") Integer rankNumber);
}
15 changes: 15 additions & 0 deletions src/main/java/com/dl/officialsite/sharing/SharingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import com.dl.officialsite.member.MemberRepository;
import com.dl.officialsite.sharing.constant.SharingLockStatus;
import com.dl.officialsite.sharing.constant.SharingStatus;
import com.dl.officialsite.sharing.model.bo.RankDto;
import com.dl.officialsite.sharing.model.req.UpdateSharingReq;
import com.dl.officialsite.team.TeamService;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -209,4 +212,16 @@ public Page<Share> searchSharing(ShareSearchVo searchVo, Pageable pageable) {
}, pageable);
return page;
}

public List<RankDto> rank(Integer rankNumber) {
List<Object[]> resultList = sharingRepository.findTopGroups(rankNumber);
List<RankDto> rankDtoList = new ArrayList<>();
for (Object[] row : resultList) {
RankDto rankDto = new RankDto();
rankDto.setPresenter((String) row[0]);
rankDto.setShareCount((BigInteger) row[1]);
rankDtoList.add(rankDto);
}
return rankDtoList;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/dl/officialsite/sharing/model/bo/RankDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dl.officialsite.sharing.model.bo;

import java.math.BigInteger;
import lombok.Data;

/**
* @ClassName RankDto
* @Author jackchen
* @Date 2024/3/2 19:27
* @Description RankDto
**/
@Data
public class RankDto {

private String presenter;

private BigInteger shareCount;
}

0 comments on commit 2e962cc

Please sign in to comment.