package com.dgtis.dorm.manager.admin.web;

import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.domain.AlumniUser;
import com.dgtis.dorm.manager.db.domain.NewsComment;
import com.dgtis.dorm.manager.db.domain.QuestionsAnswers;
import com.dgtis.dorm.manager.db.service.NewsCommentService;
import com.dgtis.dorm.manager.db.service.QuestionsAnswersService;
import com.dgtis.dorm.manager.db.service.UserService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/dashboard")
public class DashbordController {
    private final Log logger = LogFactory.getLog(DashbordController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private NewsCommentService newsCommentService;

    @Autowired
    private QuestionsAnswersService questionsAnswersService;


    @GetMapping("")
    public Object info(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        AlumniUser user = null;
        //校友录数量
        int userTotal = userService.selectCountNotYK();
        //游客数量
        user = new AlumniUser();
        user.setRoleType("游客");
        int visitorsTotal = userService.count(user);
        //待审核数量
        user = new AlumniUser();
        user.setAuditStatus(1);
        int awaitAuditTotal = userService.count(user);
        //昨日浏览人数
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        String lastLoginDate = new SimpleDateFormat("yyyy-MM-dd").format(time);
        user = new AlumniUser();
        user.setLastLoginTimeStr(lastLoginDate);
        int yesterdayTotal = userService.count(user);

        //今日评论数量
        NewsComment comment = new NewsComment();
        comment.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format( new Date()));
        int todayTotal = newsCommentService.countSelective(comment);

        //提问待回答数量
        QuestionsAnswers questionsAnswers = new QuestionsAnswers();
        questionsAnswers.setIsAnswer(false);
        int questionTotal = questionsAnswersService.countSelective(questionsAnswers);
//        int goodsTotal = goodsService.count();
//        int productTotal = productService.count();
//        int orderTotal = orderService.count();
        Map<String, Integer> data = new HashMap<>();
        data.put("userTotal", userTotal);
        data.put("visitorsTotal", visitorsTotal);
        data.put("awaitAuditTotal", awaitAuditTotal);
        data.put("yesterdayTotal", yesterdayTotal);
        data.put("todayTotal",todayTotal);
        data.put("questionTotal",questionTotal);
//        data.put("goodsTotal", goodsTotal);
//        data.put("productTotal", productTotal);
//        data.put("orderTotal", orderTotal);

        return ResponseUtil.ok(data);
    }

}
