Files
hellogithub/src/com/hellogithub/service/projectService.java
2023-07-09 11:01:31 +08:00

293 lines
11 KiB
Java

package com.hellogithub.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.dao.*;
import com.hellogithub.entity.*;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class projectService {
private projectDao projectDao = new projectDao();
private categoryDao categoryDao = new categoryDao();
private supportDao supportDao = new supportDao();
private commentDao commentDao = new commentDao();
private UserDao userDao = new UserDao();
private projectCategoryDao projectCategoryDao = new projectCategoryDao();
/**
* 按照期刊号查询
*/
public String retTerm(String num){
List<projectEntity> projectEntityList = projectDao.retTerm(num);
List<projectEntity> projectEntityListTem =new ArrayList<>();
Map<String,List<projectEntity>> dataMap= new HashMap<>();
String tem =null;
String now =null;
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
now =projectEntity.getCategoryName();
if(i == 0) tem = now;
if(!tem.equals(now)){
dataMap.put(tem,projectEntityListTem);
tem=now;
projectEntityListTem = new ArrayList<>();
}
projectEntityListTem.add(projectEntity);
}
if(projectEntityListTem.size()!= 0) dataMap.put(now,projectEntityListTem);
return JSONObject.toJSONString(dataMap);
}
/**
* 返回最新期刊数字
*
*
*/
public int latestNum() {return projectDao.latestNum();}
/**
* 返回最所有项目总数
*
*
*/
public long countAll() {return projectDao.countAll();}
/**
* 按照分类检索项目
*
*
*/
public List<projectEntity> retCate(int num) {
return projectDao.retCate(num);
}
public List<projectEntity> retLatestProject(int cate,String name) {
List<projectEntity> projectEntityList;
if (cate == 0) {
projectEntityList =projectDao.retLatestProject();
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
int num = commentDao.selectByProjectId(String.valueOf(projectEntity.getProjectId())).size();
projectEntity.setNum(num);
}
} else {
projectEntityList =projectDao.retSelcetByCate(cate,name);
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
int num = commentDao.selectByProjectId(String.valueOf(projectEntity.getProjectId())).size();
projectEntity.setNum(num);
}
}
return projectEntityList;
}
public List<projectEntity> retSelcetByStar(int cate,String name){
List<projectEntity> projectEntityList;
if (cate == 0){
projectEntityList =projectDao.retMostStar();
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
int num = commentDao.selectByProjectId(String.valueOf(projectEntity.getProjectId())).size();
projectEntity.setNum(num);
}
}else {
projectEntityList =projectDao.retSelcetByStartNum(cate,name);
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
int num = commentDao.selectByProjectId(String.valueOf(projectEntity.getProjectId())).size();
projectEntity.setNum(num);
}
}
return projectEntityList;
}
public List<projectEntity> selectByInput(String str){
List<projectEntity> projectEntityList = projectDao.selectByInput(str);
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
}
return projectEntityList;
}
public projectEntity selectProById(String id ){
projectEntity projectEntity = projectDao.selectProById(id);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
List<commentEntity> commentEntityList = commentDao.selectByProjectId(id);
if(commentEntityList.size() != 0)
{
double sum = 0;
for(int i= 0;i<commentEntityList.size();i++)
{
sum +=commentEntityList.get(i).getStar();
}
double average = sum/commentEntityList.size();
BigDecimal two = new BigDecimal(average);
double three = two.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
projectEntity.setAverage(three);
}
else{
projectEntity.setAverage(0);
}
String username = userDao.selectNameById(projectEntity.getUserId());
projectEntity.setUsername(username);
userEntity userEntity = userDao.selectUserByName(username);
projectEntity.setUserUri(userEntity.getUserAvatar());
return projectEntity;
}
public int addProjectLookcount(String id ){
return projectDao.addProjectLookcount(id);
}
public String retProjectLabel(String id ){
Map<String,List<String>> dataMap = new HashMap<>();
List<String> strs=new ArrayList<>();
List<categoryEntity> categoryEntityList = projectDao.retProjectLabel(id);
for(int i=0;i<categoryEntityList.size();i++)
{
categoryEntity categoryEntity=categoryEntityList.get(i);
strs.add(categoryEntity.getCategoryName());
}
dataMap.put("CategoryName",strs);
return JSONArray.toJSONString(dataMap);
}
public List<projectEntity> randomSelect(){
List<projectEntity> projectEntityList = new ArrayList<>();
long t = System.currentTimeMillis();
Random random = new Random(t);
List<projectEntity> projectEntityList1 = projectDao.retLatestProject();
Set<Integer> integers =new HashSet<>();
while(integers.size()<5){
int num = random.nextInt(projectEntityList1.size());
integers.add(num);
}
Iterator<Integer> it = integers.iterator();
while(it.hasNext())
{
int id = it.next();
projectEntity projectEntity =projectEntityList1.get(id);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
projectEntityList.add(projectEntity);
}
return projectEntityList;
}
public String retProjectByUserId(int id){
List<projectEntity> projectEntityList = projectDao.retProjectByUserId(id);
for(int i =0;i<projectEntityList.size();i++)
{
projectEntity projectEntity = projectEntityList.get(i);
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
}
return JSONObject.toJSONString(projectEntityList);
}
public String retProBySupport(int userId)
{
List<supportEntity> supportEntityList = supportDao.selectProject(userId);
List<projectEntity> projectEntityList = new ArrayList<>();
for(int i=0;i<supportEntityList.size();i++)
{
supportEntity supportEntity =supportEntityList.get(i);
projectEntity projectEntity = projectDao.selectProById(String.valueOf(supportEntity.getProjectId()));
categoryEntity categoryEntity = categoryDao.retNum(projectEntity.getCategoryId());
projectEntity.setCategoryName(categoryEntity.getCategoryName());
projectEntityList.add(projectEntity);
}
return JSONObject.toJSONString(projectEntityList);
}
public int insertProject(String username,String projectName,String projectUrl,String projectIco,String projectTitle,String projectDescription,List<Integer> categoryIdList){
if(projectDao.selectProByProName(projectName) != null){
return 0;
}
String mes="success";
int pnum;
int flag = 1 ;
DateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date time = new Date();
String submitTime = format.format(time).toString();
int categoryId = categoryIdList.get(0);
int userId = userDao.selectIdByName(username);
pnum = projectDao.insertProject(userId,projectName,projectUrl,projectIco,projectTitle,projectDescription,submitTime,categoryId);
if(pnum == 0 )
{
flag=0;
}
int proId = projectDao.selectProByProName(projectName).getProjectId();
for(int i=0;i<categoryIdList.size();i++)
{
int id = categoryIdList.get(i);
int num = projectCategoryDao.insertProject(id,proId);
if(num == 0 )
{
flag=0;
break;
}
}
return flag ;
}
public int updateProject(String projectName,String fileAddress)
{
int num = projectDao.updateProject(projectName,fileAddress);
return num ;
}
public int selectProjectByPname(String projectName){
return projectDao.selectProjectByPname(projectName);
}
public List<projectEntity> selectAllProject(){return projectDao.selectAllProject();}
public int updateProjectDetail(int projectId , int userId , String projectName, String projectUrl ,String projectIco ,String projectTitle ,String projectDescription ,int startNum, String submitTime ,int categoryId,int isValid,String fileAddress,int periodicals,int lookCount)
{
return projectDao.updateProjectDetail( projectId , userId , projectName, projectUrl , projectIco , projectTitle , projectDescription , startNum, submitTime ,categoryId,isValid,fileAddress, periodicals, lookCount);
}
public int deleteProject(int id ){return projectDao.deleteProject(id);}
}