Parcourir la source

完善Answer等的定义,添加service方法

MengyangDuan il y a 5 ans
Parent
commit
07666ece2d

+ 13 - 2
src/main/java/edu/nju/controller/ExtraController.java

@@ -449,8 +449,19 @@ public class ExtraController {
 
 	@RequestMapping(value = "/uploadJob", method = RequestMethod.POST)
 	@ResponseBody
-	public String uploadExamUrl(String jobJson) {
-		return null;
+	public void uploadExamUrl(String jobJson, HttpServletResponse response) {
+		try {
+			PrintWriter out = response.getWriter();
+			JSONObject result = new JSONObject();
+			String job_id = extraService.saveJob(jobJson);
+			result.put("id", job_id);
+			out.print(result);
+			out.flush();
+			out.close();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 
 	}
 

+ 32 - 17
src/main/java/edu/nju/controller/ItemController.java

@@ -25,6 +25,21 @@ public class ItemController {
     @Autowired
     ItemService iservice;
 
+    @RequestMapping(value = "/getJob")
+    @ResponseBody
+    public void getJobById(String job_id, HttpServletResponse response){
+        try {
+            PrintWriter out = response.getWriter();
+            Job job = iservice.getJob(job_id);
+            out.print(new JSONObject(job));
+            out.flush();
+            out.close();
+        }catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+
     @RequestMapping(value = "/getItemsByJob")
     @ResponseBody
     public void getItemListByJobId(String job_id, HttpServletResponse response){
@@ -41,28 +56,29 @@ public class ItemController {
 
     }
 
-    @RequestMapping(value = "/getJob")
+    @RequestMapping(value = "/getItemsByWorker")
     @ResponseBody
-    public void getJobById(String job_id, HttpServletResponse response){
+    public void getItemListByWorkerJob(String job_id, String worker_id, HttpServletResponse response){
         try {
             PrintWriter out = response.getWriter();
-            Job job = iservice.getJob(job_id);
-            out.print(new JSONObject(job));
+            List<Item> items = iservice.getItemsByWorkerJob(worker_id, job_id);
+            out.print(new JSONArray(items));
             out.flush();
             out.close();
         }catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
+
     }
 
-    @RequestMapping(value = "/getAnswersByWorker")
+    @RequestMapping(value = "/getAnswerByWorkerItem")
     @ResponseBody
-    public void getAnswerByWorkerId(String item_id,String worker_id, HttpServletResponse response){
+    public void getAnswerByWorkerItem(String item_id,String worker_id, HttpServletResponse response){
         try {
             PrintWriter out = response.getWriter();
-            List<Answer> answers = iservice.getAnswerByWorker(item_id, worker_id);
-            out.print(new JSONArray(answers));
+            Answer answer = iservice.getAnswerByItemWorker(item_id, worker_id);
+            out.print(new JSONObject(answer));
             out.flush();
             out.close();
         }catch (IOException e) {
@@ -71,27 +87,26 @@ public class ItemController {
         }
     }
 
-    @RequestMapping(value = "/getItemsByWorker")
+    @RequestMapping(value = "/getAnswersByWorkerJob")
     @ResponseBody
-    public void getItemListByWorkerJob(String job_id, String worker_id, HttpServletResponse response){
+    public void getAnswersByWorkerJob(String job_id,String worker_id, HttpServletResponse response){
         try {
             PrintWriter out = response.getWriter();
-            List<Item> items = iservice.getItemsByWorkerJob(worker_id, job_id);
-            out.print(new JSONArray(items));
+            List<Answer>answers=iservice.getAnswersByWorkerJob(job_id, worker_id);
+            out.print(new JSONArray(answers));
             out.flush();
             out.close();
         }catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
-
     }
 
     @RequestMapping(value = "/saveAnswer")
     @ResponseBody
-    public void saveAnswer(String item_id, String worker_id,List<String>answers,String attachment_location, HttpServletResponse response){
+    public void saveAnswer(String item_id, String worker_id,String job_id,List<String>answers,List<String> attachment_location, HttpServletResponse response){
         JSONObject result = new JSONObject();
-        String id = iservice.saveAnswer(item_id, worker_id, answers, attachment_location);
+        String id = iservice.saveAnswer(item_id, worker_id, job_id, answers, attachment_location);
         if(id.equals("")) {
             result.put("status", "200");
             result.put("id", id);
@@ -112,9 +127,9 @@ public class ItemController {
 
     @RequestMapping(value = "/updateAnswer")
     @ResponseBody
-    public void saveAnswer(String id, String item_id, String worker_id,List<String>answers,String attachment_location, HttpServletResponse response){
+    public void updateAnswer(String id, String item_id, String worker_id, String job_id, List<String>answers,List<String> attachment_location, HttpServletResponse response){
         JSONObject result = new JSONObject();
-        if(iservice.updateAnswer(id, item_id, worker_id, answers, attachment_location)){
+        if(iservice.updateAnswer(id, item_id, worker_id, job_id, answers, attachment_location)){
             result.put("status", "200");
         } else {
             result.put("status", "500");

+ 13 - 2
src/main/java/edu/nju/dao/AnswerDao.java

@@ -15,10 +15,21 @@ public class AnswerDao {
     @Autowired
     private MongoOperations mongoOperations;
 
-    public List<Answer> findAnswersByItemWorker(String item_id, String worker_id){
+    public Answer findAnswerByItemWorker(String item_id, String worker_id){
         Query query = new Query();
         query.addCriteria(Criteria.where("item_id").is(item_id).and("worker_id").is(worker_id));
-        return mongoOperations.find(query, Answer.class);
+        List<Answer>answers= mongoOperations.find(query, Answer.class);
+        if(answers!=null&&answers.size()!=0){
+            return answers.get(0);
+        }
+        return null;
+    }
+
+    public List<Answer> findAnswersByJobWorker(String job_id, String worker_id){
+        Query query = new Query();
+        query.addCriteria(Criteria.where("job_id").is(job_id).and("worker_id").is(worker_id));
+        List<Answer>answers= mongoOperations.find(query, Answer.class);
+        return answers;
     }
 
     //存在则更新,不存在则插入

+ 6 - 0
src/main/java/edu/nju/dao/UserToItemDao.java

@@ -22,4 +22,10 @@ public class UserToItemDao {
         if(list.size() == 0 || list == null) {return null;}
         return list.get(0).getItem_id();
     }
+
+    //存在则更新,不存在则插入
+    public String save(UserToItem userToItem){
+        mongoOperations.save(userToItem);
+        return userToItem.getId();
+    }
 }

+ 15 - 4
src/main/java/edu/nju/entities/Answer.java

@@ -20,13 +20,16 @@ public class Answer implements java.io.Serializable{
 
     private String worker_id;
 
-    private String attachment_location;
+    private String job_id;
 
-    public Answer(String item_id, List<String> answers, String worker_id, String attachment_location) {
+    private List<String> attachment_location;
+
+    public Answer(String item_id, List<String> answers, String worker_id, List<String> attachment_location,String job_id) {
         this.item_id = item_id;
         this.answers = answers;
         this.worker_id = worker_id;
         this.attachment_location = attachment_location;
+        this.job_id=job_id;
     }
 
 
@@ -62,14 +65,22 @@ public class Answer implements java.io.Serializable{
         this.worker_id = worker_id;
     }
 
-    public String getAttachment_location() {
+    public List<String> getAttachment_location() {
         return attachment_location;
     }
 
-    public void setAttachment_location(String attachment_location) {
+    public void setAttachment_location(List<String> attachment_location) {
         this.attachment_location = attachment_location;
     }
 
+    public String getJob_id() {
+        return job_id;
+    }
+
+    public void setJob_id(String job_id) {
+        this.job_id = job_id;
+    }
+
 
 
 }

+ 1 - 2
src/main/java/edu/nju/entities/UserToItem.java

@@ -19,8 +19,7 @@ public class UserToItem implements java.io.Serializable{
 
     private String job_id;
 
-    public UserToItem(String id, String worker_id, List<String> item_id, String job_id) {
-        this.id = id;
+    public UserToItem(String worker_id, List<String> item_id, String job_id) {
         this.item_id = item_id;
         this.worker_id = worker_id;
         this.job_id = job_id;

+ 10 - 5
src/main/java/edu/nju/service/ExtraService.java

@@ -32,6 +32,9 @@ public class ExtraService {
 
 	@Autowired
 	ItemDao itemDao;
+
+	@Autowired
+	ItemService itemService;
 	
 	//测试用例相关
 	public String saveTestCase(String report_id, String name, String front, String behind, String description) {
@@ -156,7 +159,7 @@ public class ExtraService {
 
 		//worker_id-item_id_list
 		JSONArray workerArray=jsonObject.getJSONArray("workerList");
-		createWorkerToItem(item_index_to_id,workerArray);
+		createWorkerToItem(item_index_to_id,workerArray,job_id);
 
 		return job_id;
 
@@ -181,7 +184,6 @@ public class ExtraService {
 				JSONObject fileObject=fileArray.getJSONObject(j);
 //				System.out.println(fileObject.getString("url"));
 				file_urls.add(fileObject.getString("url"));
-
 			}
 
 			List<String>  options=new ArrayList<>();
@@ -198,7 +200,7 @@ public class ExtraService {
 
 	}
 
-	private void createWorkerToItem(HashMap<String,String> item_index_to_id,JSONArray workerArray){
+	private void createWorkerToItem(HashMap<String,String> item_index_to_id,JSONArray workerArray,String job_id){
 		HashMap<String,List<String>> worker_to_item=new HashMap<>();
 
 		for(int i=0;i<workerArray.length();i++){
@@ -211,9 +213,12 @@ public class ExtraService {
 				itemIdList.add(item_index_to_id.get(itemIndexObject.getString("index")));
 			}
 			worker_to_item.put(worker_id,itemIdList);
+			itemService.saveUserToItem(worker_id,itemIdList,job_id);
+			for(int j=0;j<itemIdList.size();j++){
+				String item_id=itemIdList.get(j);
+				String answer_id=itemService.saveAnswer(item_id,worker_id,job_id,new ArrayList<>(),new ArrayList<>());
+			}
 		}
-
-		//todo worker-to-item
 	}
 
 

+ 15 - 6
src/main/java/edu/nju/service/ItemService.java

@@ -7,6 +7,7 @@ import edu.nju.dao.UserToItemDao;
 import edu.nju.entities.Answer;
 import edu.nju.entities.Item;
 import edu.nju.entities.Job;
+import edu.nju.entities.UserToItem;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -32,19 +33,22 @@ public class ItemService {
         return jobDao.findJob(job_id);
     }
 
-    public List<Answer> getAnswerByWorker(String item_id, String worker_id){
-        return answerDao.findAnswersByItemWorker(item_id, worker_id);
+    public Answer getAnswerByItemWorker(String item_id, String worker_id){
+        return answerDao.findAnswerByItemWorker(item_id, worker_id);
+    }
+    public List<Answer> getAnswersByWorkerJob(String job_id, String worker_id){
+        return answerDao.findAnswersByJobWorker(job_id, worker_id);
     }
 
-    public String saveAnswer(String item_id, String worker_id,List<String>answers,String attachment_location){
-        Answer answer=new Answer(item_id,answers,worker_id,attachment_location);
+    public String saveAnswer(String item_id, String worker_id,String job_id, List<String>answers,List<String> attachment_location){
+        Answer answer=new Answer(item_id,answers,worker_id,attachment_location,job_id);
         return answerDao.save(answer);
     }
 
-    public boolean updateAnswer(String id, String item_id, String worker_id,List<String>answers,String attachment_location){
+    public boolean updateAnswer(String id, String item_id, String worker_id, String job_id, List<String>answers,List<String> attachment_location){
         try {
             if(id == null || id.equals("undefined")) { return false; }
-            Answer answer=new Answer(item_id,answers,worker_id,attachment_location);
+            Answer answer=new Answer(item_id,answers,worker_id,attachment_location,job_id);
             answer.setId(id);
             answerDao.save(answer);
             return true;
@@ -67,4 +71,9 @@ public class ItemService {
         return items;
     }
 
+    public String saveUserToItem(String worker_id, List<String> item_id, String job_id){
+        UserToItem userToItem=new UserToItem(worker_id, item_id, job_id);
+        return userToItemDao.save(userToItem);
+    }
+
 }