后端代码

master
wuyifan 1 year ago
parent 04bdb19ec9
commit 70ce44ad44

@ -0,0 +1,47 @@
package com.softegg.freetogo.HeatMap.bean;
public class HeatMap {
// 定义字段
private String name; // 省的名称
private int data; // 省中的一些数据示例中使用int可以根据需求修改为其他类型
// 构造方法
public HeatMap(String name, int data) {
this.name = name;
this.data = data;
}
// Getter和Setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
// 可以添加其他方法,比如打印省的信息等
@Override
public String toString() {
return "Province{" +
"name='" + name + '\'' +
", data=" + data +
'}';
}
// 示例:打印省的信息
public void printInfo() {
System.out.println("Province Name: " + name + ", Data: " + data);
}
}

@ -1,4 +1,78 @@
package com.softegg.freetogo.HeatMap.controller;
import com.softegg.freetogo.Demand.bean.Demands;
import com.softegg.freetogo.GuideService.bean.GuideService;
import com.softegg.freetogo.HeatMap.bean.HeatMap;
import com.softegg.freetogo.GuideService.service.GuideServiceService;
import com.softegg.freetogo.GuideMatch.service.GuideMatchService;
import com.softegg.freetogo.HeatMap.service.HeatMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
//@CrossOrigin(origins = "*")
@RequestMapping("/HeatMap")
public class HeatMapController {
@Autowired
GuideMatchService guideMatchService;
@Autowired
GuideServiceService guideServiceService;
@Autowired
HeatMapService heatMapService;
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/5/30 20:48
*/
@GetMapping("guideServiceHeatMapRegister")
public List<HeatMap> GuideServiceHeatMapRegister() {
return heatMapService.provinceGuideServiceHeatMap();
}
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/5/30 20:48
*/
@GetMapping("demandsHeatMapRegister")
public List<HeatMap> DemandsHeatMapRegister() {
return heatMapService.provinceDemandsHeatMap();
}
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/5/30 20:48
*/
@PostMapping("demandsCityHeatMap")
public int[] DemandsProvinceHeatMap(@RequestBody Map<String, Object> map) {
String province = map.get("province").toString();
return heatMapService.cityDemandsHeatMap(province);
}
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/5/30 20:48
*/
@PostMapping("guideServiceCityHeatMap")
public int[] GuideServiceProvinceHeatMap(@RequestBody Map<String, Object> map) {
String province = map.get("province").toString();
return heatMapService.cityGuideServiceHeatMap(province);
}
}

@ -1,11 +1,15 @@
package com.softegg.freetogo.HeatMap.service;
import com.softegg.freetogo.HeatMap.bean.HeatMap;
import java.util.List;
public interface HeatMapService {
int[] provinceDemandsHeatMap();//获得每个省份的游客需求数目
List<HeatMap> provinceDemandsHeatMap();//获得每个省份的游客需求数目
int[] cityDemandsHeatMap(String province);//获得目标省份的游客需求数目
int[] provinceGuideServiceHeatMap();
List<HeatMap> provinceGuideServiceHeatMap();//获得每个省份的导游服务数目
int[] cityGuideServiceHeatMap(String province);
int[] cityGuideServiceHeatMap(String province);//获得目标省份的游客服务数目
}

@ -5,6 +5,7 @@ import com.softegg.freetogo.Demand.service.DemandsService;
import com.softegg.freetogo.GuideService.service.GuideServiceService;
import com.softegg.freetogo.GuideService.bean.GuideService;
import com.softegg.freetogo.HeatMap.bean.HeatMap;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ -29,6 +30,8 @@ public class HeatMapServiceImpl implements HeatMapService {
@Autowired
GuideServiceService guideServiceService;
public static void main(String[] args) {
List<String> provinceList = new ArrayList<>();
JSONParser parser = new JSONParser();
@ -118,16 +121,14 @@ public class HeatMapServiceImpl implements HeatMapService {
* @date: 2024/6/6 15:48
*/
@Override
public int[] provinceDemandsHeatMap(){
public List<HeatMap> provinceDemandsHeatMap(){
List<String> provinceList = getAllProvinces();
int[] sum = new int[provinceList.size()];
int i = 0;
List<HeatMap> dataList = new ArrayList<>();
for (String province : provinceList) {
List<Demands> demandsList = demandsService.getDemandsByProvince(province);
sum[i] = demandsList.size();
i++;
dataList.add(new HeatMap(province, demandsList.size()));
}
return sum;
return dataList;
}
/**
@ -158,16 +159,14 @@ public class HeatMapServiceImpl implements HeatMapService {
* @date: 2024/6/6 15:48
*/
@Override
public int[] provinceGuideServiceHeatMap(){
public List<HeatMap> provinceGuideServiceHeatMap(){
List<String> provinceList = getAllProvinces();
int[] sum = new int[provinceList.size()];
int i = 0;
List<HeatMap> dataList = new ArrayList<>();
for (String province : provinceList) {
List<GuideService> guideServiceList = guideServiceService.getGuideServiceByProvince(province);
sum[i] = guideServiceList.size();
i++;
dataList.add(new HeatMap(province, guideServiceList.size()));
}
return sum;
return dataList;
}
/**
@ -189,4 +188,5 @@ public class HeatMapServiceImpl implements HeatMapService {
}
return sum;
}
}

@ -1,4 +1,120 @@
package com.softegg.freetogo.Search.controller;
import com.softegg.freetogo.Demand.bean.Demands;
import com.softegg.freetogo.GuideService.bean.GuideService;
import com.softegg.freetogo.GuideService.service.GuideServiceService;
import com.softegg.freetogo.GuideMatch.service.GuideMatchService;
import com.softegg.freetogo.User.bean.Users;
import com.softegg.freetogo.User.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* @description:
* @author:wuyifan
* @date:2024/5/30 10:47
*/
@RestController
//@CrossOrigin(origins = "*")
@RequestMapping("/Search")
public class SearchController {
@Autowired
GuideMatchService guideMatchService;
@Autowired
UsersService usersService;
@Autowired
GuideServiceService guideServiceService;
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/5/30 20:48
*/
@GetMapping("register")
public List<Demands> GuideMatch(int gid) {
System.out.println("需匹配的gid:" + gid);
return guideMatchService.guideMatchAccount(gid);
}
/**
* @description:
* @param: map
* @return: java.lang.int
* @author: wuyifan
* @date: 2024/6/11 1012
*/
@PostMapping("match")
public int Match(@RequestBody Map<String, Object> map) {
int gid = Integer.parseInt(map.get("gid").toString());
System.out.println("接收的gid:"+gid);
int did = Integer.parseInt(map.get("did").toString());
System.out.println("接收的did:"+did);
return guideMatchService.match(gid, did);
}
/**
* @description:
* @param: gid
* @return: java.lang.Demands
* @author: wuyifan
* @date: 2024/6/11 1012
*/
@GetMapping("confirmedPage")
public List<Demands> confirmedpage(int gid) {
System.out.println("接收的gid:"+gid);
return guideMatchService.confirmedPage(gid);
}
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/6/11 1012
*/
@PostMapping("confirmed")
public int confirmed(@RequestBody Map<String, Object> map) {
int gid = Integer.parseInt(map.get("gid").toString());
System.out.println("接收的did:"+gid);
int did = Integer.parseInt(map.get("did").toString());
System.out.println("接收的did:"+did);
return guideMatchService.confirmed(gid, did);
}
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/6/11 1012
*/
@PostMapping("refuse")
public int refuse(@RequestBody Map<String, Object> map) {
int gid = (int) map.get("gid");
System.out.println("接收的gid:"+gid);
return guideMatchService.refuse(gid);
}
/**
* @description:
* @param: map
* @return: java.lang.String
* @author: wuyifan
* @date: 2024/6/11 1012
*/
@PostMapping("delete")
public int delete(@RequestBody Map<String, Object> map) {
int gid = (int) map.get("gid");
System.out.println("接收的gid:"+gid);
return guideMatchService.delete(gid);
}
}

@ -1,4 +1,32 @@
package com.softegg.freetogo.Search.service;
import com.softegg.freetogo.Demand.bean.Demands;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @description:
* @author:wuyifan
* @date:2024/5/28 19:45
*/
@Service
public interface SearchService {
List<Demands> guideMatchAccount(int gid);
int match(int gid, int did);
List<Demands> confirmedPage(int gid);
int confirmed(int did, int gid);
int refuse(int gid);
int delete(int gid);
double timeScore(int gsum, String gddate, String gedate, int dsum, String dddate, String dedate);
double messageScore(String gmessage, String dmessage);
}

@ -1,4 +1,384 @@
package com.softegg.freetogo.Search.service;
import com.softegg.freetogo.Demand.bean.Demands;
import com.softegg.freetogo.GuideMatch.service.GuideMatchService;
import com.softegg.freetogo.GuideService.bean.GuideService;
import com.softegg.freetogo.Demand.service.DemandsService;
import com.softegg.freetogo.User.service.UsersService;
import com.softegg.freetogo.GuideService.service.GuideServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class SearchServiceImpl {
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import static java.lang.Math.abs;
/**
* description:
* @author:wuyifan
* date:2024/5/28 19:51
*/
@Component
public class SearchServiceImpl implements SearchService {
@Autowired
DemandsService demandsService;
@Autowired
UsersService usersService;
@Autowired
GuideServiceService guideServiceService;
/**
* @description:
* @param: city
* @return: int
* author: wuyifan
* @date: 2024/5/28 19:52
*/
@Override
public List<Demands> guideMatchAccount(int gid){
GuideService guideService = guideServiceService.getGuideServiceById(gid);
int guideServiceSumDay = Integer.parseInt(guideService.getSumDay());
String guideServiceDepartureDate = guideService.getDepartureDate();
String guideServiceEndDate = guideService.getEndDate();
String guideServiceMessage = guideService.getMessage();
List<Demands> dlist = demandsService.getDemandsByCity(guideService.getCity());
// 使用迭代器的 remove 方法
dlist.removeIf(demand -> demand.getStatus() > 1);
int length = dlist.size();
double[] sumScores = new double[length];
int i = 0;
for (Demands demands : dlist) {
double sumScore = 0;
int demandsSumDay = Integer.parseInt(demands.getSumDay());
String demandsDepartureDate = demands.getDepartureDate();
String demandsEndDate = demands.getEndDate();
String demandsMessage = demands.getMessage();
double timeScore = timeScore(guideServiceSumDay,guideServiceDepartureDate,guideServiceEndDate,demandsSumDay,demandsDepartureDate,demandsEndDate);
double messageScore = messageScore(guideServiceMessage, demandsMessage);
sumScore += timeScore + messageScore;
sumScores[i] = sumScore;
i++;
}
// 创建一个包含索引和值的列表
List<Map.Entry<Double, Demands>> list = new ArrayList<>();
for (int j = 0; j < sumScores.length; j++) {
list.add(new AbstractMap.SimpleEntry<>(sumScores[j], dlist.get(j)));
}
// 根据sumScores的元素大小对 list 进行排序
list.sort((o1, o2) -> o2.getKey().compareTo(o1.getKey()));
List<Demands> newdList = new ArrayList<>();
for (Map.Entry<Double, Demands> entry : list) {
newdList.add(entry.getValue());
}
return newdList;
}
/**
* @description:
* @param: did,gid
* @return: int
* author: wuyifan
* @date: 2024/5/28 19:52
*/
@Override
public int match(int gid, int did){
Demands demand = demandsService.getDemandById(did);
GuideService guideService = guideServiceService.getGuideServiceById(gid);
demand.setStatus(1);
List<Integer> guideServiceIdList = demand.getGuideServiceIdList();
if (guideServiceIdList == null) {
guideServiceIdList = new ArrayList<>();
}
if(!guideServiceIdList.contains(gid)){
guideServiceIdList.add(gid);
}
demand.setGuideServiceIdList(guideServiceIdList);
// List<Integer> demandIdList = guideService.getDemandIdList();
// if (demandIdList == null) {
// demandIdList = new ArrayList<>();
// }
// demandIdList.add(did);
// guideService.setDemandIdList(demandIdList);
demandsService.update(demand);
guideServiceService.update(guideService);
System.out.println(demand.getGuideServiceIdList());
return 1006;
}
/**
* @description:
* @param: did,gid
* @return: int
* author: wuyifan
* @date: 2024/5/28 19:52
*/
@Override
public List<Demands> confirmedPage(int gid){
GuideService guideService = guideServiceService.getGuideServiceById(gid);
List<Integer> demandIdList = guideService.getDemandIdList();
List<Demands> demandsList = new ArrayList<>();
for(int number : demandIdList) {
Demands demand = demandsService.getDemandById(number);
demandsList.add(demand);
}
return demandsList;
}
/**
* @description:
* @param: did,gid
* @return: int
* author: wuyifan
* @date: 2024/5/28 19:52
*/
@Override
public int confirmed(int gid, int did){
Demands demand = demandsService.getDemandById(did);
GuideService guideService = guideServiceService.getGuideServiceById(gid);
demand.setStatus(2);
demand.setGuideServiceId(gid);
guideService.setStatus(2);
guideService.setDemandId(did);
demandsService.update(demand);
guideServiceService.update(guideService);
return 1007;
}
/**
* @description:
* @param: did,gid
* @return: int
* author: wuyifan
* @date: 2024/5/28 19:52
*/
@Override
public int refuse(int gid){
GuideService guideService = guideServiceService.getGuideServiceById(gid);
int did = guideService.getDemandId();
Demands demand = demandsService.getDemandById(gid);
List<Integer> demandIdList = guideService.getDemandIdList();
if (demandIdList == null) {
guideService.setStatus(0);
}else {
guideService.setStatus(1);
}
List<Integer> guideServiceIdList = demand.getGuideServiceIdList();
if(guideServiceIdList == null){
demand.setStatus(0);
}else {
demand.setStatus(1);
}
guideService.setDemandId(null);
demand.setGuideServiceId(null);
demandsService.update(demand);
guideServiceService.update(guideService);
return 1;
}
@Override
public int delete(int gid){
GuideService guideService = guideServiceService.getGuideServiceById(gid);
guideService.setStatus(4);
guideServiceService.update(guideService);
return 1;
}
public static boolean isRangeWithinRange(LocalDate startDate1, LocalDate endDate1, LocalDate startDate2, LocalDate endDate2) {
return !startDate1.isBefore(startDate2) && !endDate1.isAfter(endDate2);
}
/**
* @description:
* @param: gsumdsumddateedate
* @return: float
* @author: wuyifan
* @date: 2024/5/28 20:35
*/
@Override
public double timeScore(int gsum, String gddate, String gedate, int dsum, String dddate, String dedate){
double score = 0;
int difference = gsum - dsum;
if (abs(difference) < 15 ){
score += 10 / (float)(abs(difference) + 1);
}
// 使用DateTimeFormatter将字符串转换为LocalDate对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate1 = LocalDate.parse(dddate, formatter);
LocalDate endDate1 = LocalDate.parse(dedate, formatter);
System.out.println(startDate1);
System.out.println(endDate1);
LocalDate startDate2 = LocalDate.parse(gddate, formatter);
LocalDate endDate2 = LocalDate.parse(gedate, formatter);
System.out.println(startDate2);
System.out.println(endDate2);
// 判断第一个范围是否在第二个范围内
boolean isRange1WithinRange2 = isRangeWithinRange(startDate1, endDate1, startDate2, endDate2);
if (isRange1WithinRange2){
score += 100;
}
return score;
}
/**
* @description: Jaccard
* @param: String
* @return: float
* @author: wuyifan
* @date: 2024/5/30 20:35
*/
public static double jaccardSimilarity(String s1, String s2) {
if (s1 == null || s2 == null || s1.isEmpty() || s2.isEmpty()) {
return 0.0; // 如果任何一个字符串为空直接返回0
}
Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();
for (char c : s1.toCharArray()) {
set1.add(c);
}
for (char c : s2.toCharArray()) {
set2.add(c);
}
Set<Character> intersection = new HashSet<>(set1);
intersection.retainAll(set2);
Set<Character> union = new HashSet<>(set1);
union.addAll(set2);
return (double) intersection.size() / union.size();
}
/**
* @description:
* @param: String
* @return: float
* @author: wuyifan
* @date: 2024/5/30 20:35
*/
private static Map<Character, Integer> getCharacterFrequency(String s) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : s.toCharArray()) {
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
}
return frequencyMap;
}
public static double cosineSimilarity(String s1, String s2) {
if (s1 == null || s2 == null || s1.isEmpty() || s2.isEmpty()) {
return 0.0; // 如果任何一个字符串为空直接返回0
}
Map<Character, Integer> freq1 = getCharacterFrequency(s1);
Map<Character, Integer> freq2 = getCharacterFrequency(s2);
double dotProduct = 0.0;
double mag1 = 0.0;
double mag2 = 0.0;
for (char c : freq1.keySet()) {
if (freq2.containsKey(c)) {
dotProduct += freq1.get(c) * freq2.get(c);
}
mag1 += Math.pow(freq1.get(c), 2);
}
for (char c : freq2.keySet()) {
mag2 += Math.pow(freq2.get(c), 2);
}
mag1 = Math.sqrt(mag1);
mag2 = Math.sqrt(mag2);
return dotProduct / (mag1 * mag2);
}
/**
* @description: Dice
* @param: String
* @return: float
* @author: wuyifan
* @date: 2024/5/30 20:25
*/
public static double diceCoefficient(String s1, String s2) {
if (s1 == null || s2 == null || s1.isEmpty() || s2.isEmpty()) {
return 0.0; // 如果任何一个字符串为空直接返回0
}
int intersection = 0;
for (int i = 0; i < s1.length() - 1; i++) {
String bigram = s1.substring(i, i + 2);
if (s2.contains(bigram)) {
intersection++;
}
}
int union = s1.length() + s2.length() - intersection;
return (double) (2 * intersection) / union;
}
/**
* @description: N-gram
* @param: String
* @return: float
* @author: wuyifan
* @date: 2024/5/30 20:22
*/
private static Set<String> generateNGrams(String s, int n) {
if (s == null || s.length() < n || n <= 0) {
throw new IllegalArgumentException("Invalid input string or n-gram size");
}
Set<String> nGrams = new HashSet<>();
for (int i = 0; i <= s.length() - n; i++) {
nGrams.add(s.substring(i, i + n));
}
return nGrams;
}
public static double bigramSimilarity(String s1, String s2) {
if (s1 == null || s2 == null || s1.isEmpty() || s2.isEmpty()) {
return 0.0; // 如果任何一个字符串为空直接返回0
}
Set<String> bigrams1 = generateNGrams(s1, 2);
Set<String> bigrams2 = generateNGrams(s2, 2);
Set<String> commonBigrams = new HashSet<>(bigrams1);
commonBigrams.retainAll(bigrams2);
Set<String> allBigrams = new HashSet<>(bigrams1);
allBigrams.addAll(bigrams2);
return (double) commonBigrams.size() / allBigrams.size();
}
/**
* @description:
* @param: gmessagedmessage
* @return: float
* @author: wuyifan
* @date: 2024/5/28 20:35
*/
@Override
public double messageScore(String gmessage, String dmessage){
double score;
double similarity1 = jaccardSimilarity(gmessage, dmessage);
double similarity2 = cosineSimilarity(gmessage, dmessage);
double similarity3 = diceCoefficient(gmessage, dmessage);
double similarity4 = bigramSimilarity(gmessage, dmessage);
score = similarity1 + similarity2 + similarity3 + similarity4;
return score;
}
}

Loading…
Cancel
Save