package com.jfinal.club.my.newsfeed;

import com.jfinal.aop.Inject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.club.common.model.Users;
import com.jfinal.club.download.DownloadService;
import com.jfinal.club.my.ct.UserCtService;
import com.jfinal.club.my.doc.UsersDocService;
import com.jfinal.club.my.ques.QuestionsService;
import com.jfinal.club.ws.DocWsService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import static com.jfinal.club.login.LoginService.loginUsersCacheName;

/**
 * @ClassName UsersInfoIntreceptor
 * @Description TODO  用户基本信息
 * @Author caolong
 * @Date 2020/3/17 15:42
 */
public class UsersInfoIntreceptor implements Interceptor {

    @Inject
    UsersDocService usersDocService;

    @Inject
    DownloadService downloadService;

    @Inject
    UserCtService userCtService;

    @Inject
    QuestionsService questionsService;

    @Inject
    DocWsService docWsService;

    @Override
    public void intercept(Invocation inv) {
        List<Integer> year = new ArrayList<>();
        List<Integer> month = new ArrayList<>();
        List<Integer> day = new ArrayList<>();
        Calendar a = Calendar.getInstance();
        for (int i = a.get(Calendar.YEAR); i >= 1940; i--) {
            year.add(i);
        }
        for (int i = 1; i <= 12; i++) {
            month.add(i);
        }
        for (int i = 1; i <= 31; i++) {
            day.add(i);
        }
        inv.getController().setAttr("year", year);
        inv.getController().setAttr("month", month);
        inv.getController().setAttr("day", day);
        Users users = (Users) inv.getController().getAttr(loginUsersCacheName);
        inv.getController().setAttr("phone", users.getPhone().substring(0, 3) + "****" + users.getPhone().substring(7, 11));
        Long countCt = userCtService.countCtById(users.getId());
        Long countDoc = usersDocService.countDocById(users.getId());
        Long countDownload = downloadService.countDownloadById(users.getId());
        Long countQuesById = questionsService.countQuesById(users.getId());
        Long countDocws = docWsService.countDocwsById(users.getId());
        inv.getController().setAttr("docwsCount", countDocws);
        inv.getController().setAttr("quesCount", countQuesById);
        inv.getController().setAttr("ctCount", countCt);
        inv.getController().setAttr("docCount", countDoc);
        inv.getController().setAttr("countDownload", countDownload);
        inv.invoke();
    }
}
