Skip to content

Commit

Permalink
Merge pull request #55 from yanyanho/main
Browse files Browse the repository at this point in the history
add graph quer redpacket
  • Loading branch information
yanyanho authored Nov 9, 2023
2 parents 15a89a1 + 7654270 commit 2a364ba
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 36 deletions.
19 changes: 14 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ plugins {
id 'org.springframework.boot' version '2.7.16'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
// id "org.web3j" version "4.9.8"
// id("com.apollographql.apollo").version("2.5.14"
}

group = 'com.dl'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '1.8'
sourceCompatibility = '8'
}

configurations {
Expand All @@ -37,7 +38,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'



implementation 'mysql:mysql-connector-java:8.0.28'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-aop'
Expand All @@ -52,9 +52,18 @@ dependencies {
implementation 'cn.hutool:hutool-all:5.8.4'
implementation("com.google.guava:guava:31.1-jre")
implementation 'com.github.ipfs:java-ipfs-http-client:1.3.3'
implementation 'org.springframework.boot:spring-boot-starter-actuator'


implementation 'com.google.code.gson:gson:2.8.9'
// implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-graphql
//implementation 'org.springframework.boot:spring-boot-starter-graphql:2.7.17'


//graphql
// implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:latest.release"))
// implementation "com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter"
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation("com.apollographql.apollo:apollo-runtime:2.5.14")
//implementation 'org.hibernate.validator:hibernate-validator:6.0.10.Final'

compileOnly 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
import java.util.Map;
import java.util.stream.Collectors;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {

Map<String, List<String>> body = new HashMap<>();

List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
.map(x-> x.getDefaultMessage())
.collect(Collectors.toList());

body.put("errors", errors);

return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
}
}
//@ControllerAdvice
//public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
// @Override
// protected ResponseEntity<Object> handleMethodArgumentNotValid(
// MethodArgumentNotValidException ex, HttpHeaders headers,
// HttpStatus status, WebRequest request) {
//
// Map<String, List<String>> body = new HashMap<>();
//
// List<String> errors = ex.getBindingResult()
// .getFieldErrors()
// .stream()
// .map(x-> x.getDefaultMessage())
// .collect(Collectors.toList());
//
// body.put("errors", errors);
//
// return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
// }
//}
2 changes: 2 additions & 0 deletions src/main/java/com/dl/officialsite/hiring/Hiring.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class Hiring {
private String salary;
private String memo;

private String contact;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@ public interface RedPacketRepository extends JpaRepository<RedPacket, String>,
@Query(value = "select * from red_packet where address_list like ?1 and status = ?2 ", nativeQuery = true)
List<RedPacket> findByAddressAndStatus(@Param("address") String address, @Param("status") Integer status);


@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and status = ?2", nativeQuery = true)
List<RedPacket> findByUnclaimedPacket(@Param("address") String address, @Param("status") Integer status);


RedPacket findByIdAndStatus(@Param("id") String id, @Param("status") Integer status);

}

142 changes: 142 additions & 0 deletions src/main/java/com/dl/officialsite/redpacket/RedPacketService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.dl.officialsite.redpacket;


import com.dl.officialsite.common.constants.Constants;
import com.dl.officialsite.common.enums.CodeEnums;
import com.dl.officialsite.common.exception.BizException;
import com.dl.officialsite.mail.EmailService;
import com.dl.officialsite.member.Member;
import com.dl.officialsite.member.MemberRepository;
import com.dl.officialsite.team.Team;
import com.dl.officialsite.team.TeamMember;
import com.dl.officialsite.team.TeamMemberRepository;
import com.dl.officialsite.team.TeamRepository;
import com.dl.officialsite.team.vo.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @ClassName TeamService
* @Author jackchen
* @Date 2023/10/21 17:23
* @Description TODO
**/
@Service
@Slf4j
public class RedPacketService {

@Autowired
private RedPacketRepository redPacketRepository;

public CloseableHttpClient httpClient = HttpClients.createDefault();

@Scheduled(cron = "0 0/3 * * * ? ")
public void updateRedpacketStatus() throws IOException {
log.info("schedule task begin --------------------- " );
HttpPost request = new HttpPost("https://api.studio.thegraph.com/proxy/55957/dapp-learning-redpacket/version/latest");
request.setHeader("Content-Type", "application/json");
// Define your GraphQL query

String graphQL = "\" {" +
" redpackets {" +
" id " +
" hasRefundedOrAllClaimed " +
" claimers {" +
" claimer" +
" }" +
" }" +
"}\"";

String query = "{ \"query\": " +
graphQL +
" }";

request.setEntity(new StringEntity(query));
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();

if (entity != null) {
String jsonResponse = EntityUtils.toString(entity);

JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonObject data = jsonObject.getAsJsonObject("data");
JsonArray redpacketsArray = data.getAsJsonArray("redpackets");

//log.info("redpacket array : " + redpacketsArray);
for (int i = 0; i < redpacketsArray.size(); i++) {
// Access each element in the array
JsonObject redpacketObject = redpacketsArray.get(i).getAsJsonObject();
// log.info("redpackt object : " + redpacketObject);
String id = redpacketObject.get("id").getAsString();
// log.info("id: " + id);
RedPacket redPacket = redPacketRepository.findByIdAndStatus(id,0);
log.info("redPacket: " + redPacket);
if(redPacket == null )
continue;
String hasRefundedOrAllClaimed = redpacketObject.get("hasRefundedOrAllClaimed").getAsString();
Boolean claimed = redpacketObject.get("hasRefundedOrAllClaimed").getAsBoolean();

if (claimed) {
log.info( "redpacket id: "+id + "claimed: " );
redPacket.setStatus(1);
redPacketRepository.save(redPacket);
continue;
}
JsonArray claimers = redpacketObject.getAsJsonArray("claimers");

// redPacket.getAddressList() is the total claimer.
if(claimers.size() == redPacket.getAddressList().size()) {
log.info( "redpacket id: "+id + " aLL claimed " );
redPacket.setStatus(1);
redPacketRepository.save(redPacket);
continue;
}

if (claimers.size() > redPacket.getClaimedAddress().size()) {
ArrayList<String> claimersList = new ArrayList<>();

for (int j = 0; j < claimers.size(); j++) {
String s = claimers.get(j).getAsJsonObject().get("claimer").getAsString();
//log.info("claimer address: " + s);
claimersList.add(s);
}
redPacket.setClaimedAddress(claimersList);
log.info( "update claimed address : " + id );
redPacketRepository.save(redPacket);
}


}
}
}
}





11 changes: 4 additions & 7 deletions src/main/java/com/dl/officialsite/sharing/Sharing.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@

public class Sharing {


private Long id;
private String theme;
private String date;
private String time;

private int Language; // 0 Chinese 1 English


private String presenter;

private String Org;

private String twitter;

private Member member;
private String memberId;

private String sharingDoc;

private String Org;

//defi zk underlying
private String topic;
private String label;

private Integer reward;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server:
port: 8080
servlet:
session:
timeout: 6m
timeout: 60m
cookie:
max-age: -1
spring:
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ server:
port: 8080
servlet:
session:
timeout: 6m
timeout: 60m
cookie:
max-age: -1
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/dl?allowPublicKeyRetrieval=true&useSSL=false
#type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 12345678
# password: {{SPRING_DATASOURCE_PASSWORD}}
#password: 12345678
password: {{SPRING_DATASOURCE_PASSWORD}}
jpa:
hibernate:
ddl-auto: update
Expand Down
Loading

0 comments on commit 2a364ba

Please sign in to comment.