package dingzhen.controller;//推荐朋友控制器import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import dingzhen.entity.Choice;import dingzhen.entity.Student;import dingzhen.entity.User;import dingzhen.service.ChoiceService;import dingzhen.service.StudentService;import dingzhen.util.WriterUtil;@Controller@RequestMapping("friend")public class FriendController {private Choice choice;@Autowiredprivate ChoiceService<Choice> choiceService;@Autowiredprivate StudentService<Student> studentService;private Student student;@RequestMapping("friendIndex")public String index(){return "tuijian/friend";}@RequestMapping("friendList")public void list(HttpServletRequest request,HttpServletResponse response){try {User currentUser = (User)request.getSession().getAttribute("currentUser");String currentStudentno = currentUser.getUserName(); //当前学号choice = new Choice();choice.setStudentno(currentStudentno);List<Choice> currentList = choiceService.findChoice(choice);String currentKeyword = "";if(currentList != null && currentList.size() >0){// 设置筛选推荐朋友的匹配条件currentKeyword = currentList.get(0).getSeatkeyword();currentKeyword = currentKeyword.substring(11, 20); // 获得08点-12点-1这样的字符串,表示在8-12这个时间段在id=1的阅览室Choice c = new Choice();c.setSeatkeyword(currentKeyword); //相同的时间段和自习室c.setStudentno(currentStudentno); //排除自己Date today = new Date();Date lastDay = getNextDay(today, -20); //20天之内c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lastDay));c.setPipeishu(3); //至少有3次相同时间段相同教室List<Choice> friendList = choiceService.findFriend(c); //查询匹配的朋友List<Student> friendStudent = new ArrayList<Student>();if(friendList == null || friendList.size()==0){//没有匹配到一个相同的朋友} else if (friendList.size() > 10) {// 超过10取前10位for(int i=0;i<10;i++){Choice friendChoice = friendList.get(i);String friendStudentno = friendChoice.getStudentno(); //得到学号student = studentService.findOneStudentByno(friendStudentno); //通过学号查询学生friendStudent.add(student);}} else {// 不超过10条全部取出来for(int i=0;i<friendList.size();i++){Choice friendChoice = friendList.get(i);String friendStudentno = friendChoice.getStudentno();student = studentService.findOneStudentByno(friendStudentno);friendStudent.add(student);}}JSONArray array = new JSONArray();array.addAll(friendStudent);WriterUtil.write(response, array.toString());}} catch (Exception e) {e.printStackTrace();}}// 获取与今天相隔day天的日期,其中date表示某天,如今天。day正数表示之后,如1表示1天之后即明天。-1表示1天之前即昨天public static Date getNextDay(Date date,int day) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.DAY_OF_MONTH, day);date = calendar.getTime();return date;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。