package org.inlighting.oj.web.controller;

import io.swagger.annotations.ApiOperation;
import org.inlighting.oj.web.controller.exception.WebErrorException;
import org.inlighting.oj.web.entity.ResponseEntity;
import org.inlighting.oj.web.entity.UserLogEntity;
import org.inlighting.oj.web.service.UserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Smith
 **/
@RestController
@Validated
@RequestMapping("/user_log")
public class UserLogController {

    private UserLogService userLogService;

    @Autowired
    public void setUserLogService(UserLogService userLogService) {
        this.userLogService = userLogService;
    }

    @ApiOperation("获取用户近期记录")
    @GetMapping
    public ResponseEntity getUserLog(@RequestParam("uid") int uid,
                                     @RequestParam("time") String time) {
        List<UserLogEntity> tempList;
        int timeLength = 0;
        if (time.equals("week")) {
            tempList = userLogService.getInWeek(uid);
            timeLength = 7;
        } else if (time.equals("month")) {
            tempList = userLogService.getInMonth(uid);
            timeLength = 30;
        } else {
            throw new WebErrorException("非法时间");
        }

        // format log
        Map<String, UserLogEntity> mapping = new HashMap<>(tempList.size());
        for (UserLogEntity entity: tempList) {
            Date date = entity.getDate();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR, 12);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            mapping.put(calendar.getTime().toString(), entity);
        }
        List<UserLogEntity> finalList = new ArrayList<>(timeLength);
        for (int i=timeLength; i>=0; i--) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.add(Calendar.DATE, -i);
            Date date = calendar.getTime();
            if (mapping.containsKey(date.toString())) {
                finalList.add(mapping.get(date.toString()));
            } else {
                UserLogEntity entity = new UserLogEntity();
                entity.setUid(uid);
                entity.setDate(date);
                finalList.add(entity);
            }
        }
        return new ResponseEntity(finalList);
    }
}

@@ -1848,32 +1848,45 @@
 te);%0A           
+ String key =
  calendar.set(Ca
@@ -1871,33 +1871,33 @@
  key = calendar.
-s
+g
 et(Calendar.HOUR
@@ -1896,31 +1896,18 @@
 dar.
-HOUR, 12);%0A            
+YEAR)+%22-%22+
 cale
@@ -1903,33 +1903,33 @@
 R)+%22-%22+calendar.
-s
+g
 et(Calendar.MINU
@@ -1925,39 +1925,26 @@
 lendar.M
-INUTE, 0);%0A            
+ONTH)+%22-%22+
 calendar
@@ -1936,33 +1936,33 @@
 H)+%22-%22+calendar.
-s
+g
 et(Calendar.SECO
@@ -1953,33 +1953,36 @@
 et(Calendar.
-SECOND, 0
+DAY_OF_MONTH
 );%0A         
@@ -2000,37 +2000,11 @@
 put(
-calendar.getTime().toString()
+key
 , en
@@ -2202,35 +2202,35 @@
        calendar.
-set
+add
 (Calendar.HOUR, 
@@ -2227,15 +2227,16 @@
 dar.
-HOUR, 0
+DATE, -i
 );%0A 
@@ -2237,32 +2237,45 @@
 -i);%0A           
+ String key =
  calendar.set(Ca
@@ -2260,33 +2260,33 @@
  key = calendar.
-s
+g
 et(Calendar.MINU
@@ -2285,32 +2285,18 @@
 dar.
-MINUTE, 0);%0A            
+YEAR)+%22-%22+
 cale
@@ -2300,17 +2300,17 @@
 alendar.
-s
+g
 et(Calen
@@ -2317,32 +2317,19 @@
 dar.
-SECOND, 0);%0A            
+MONTH)+%22-%22+
 cale
@@ -2325,35 +2325,35 @@
 H)+%22-%22+calendar.
-add
+get
 (Calendar.DATE, 
@@ -2352,58 +2352,18 @@
 r.DA
-TE, -i);%0A            Date date = calendar.getTime(
+Y_OF_MONTH
 );%0A 
@@ -2397,31 +2397,19 @@
 ainsKey(
-date.toString()
+key
 )) %7B%0A   
@@ -2451,23 +2451,11 @@
 get(
-date.toString()
+key
 ));%0A
@@ -2602,20 +2602,34 @@
 setDate(
-date
+calendar.getTime()
 );%0A     

fix bug