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

import com.dgtis.dorm.manager.db.domain.Banner;
import com.dgtis.dorm.manager.db.domain.News;
import com.dgtis.dorm.manager.db.domain.NewsCriterion;
import com.dgtis.dorm.manager.db.service.BannerService;
import com.dgtis.dorm.manager.db.service.NewsService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/home")
public class HomeController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private BannerService bannerService;

    @RequestMapping("index")
    public Object index(Integer userIdForZan) {

        Map<String, Object> data = new HashMap<String, Object>();

        // 轮播图片
        Banner banner = new Banner();
        banner.setEnabled(true);
        List<Banner> lstBanner = bannerService.querySelective(banner, 1, 300, null);
        data.put("banner", lstBanner);

        // 行政动态
        NewsCriterion newsCriterion = new NewsCriterion();
        newsCriterion.setType("administrationInfo");
        newsCriterion.setLimit(3);

        if(userIdForZan != null){
            newsCriterion.setUserIdForZan(userIdForZan);
        }
        News news = new News();
        news.setNewsCriterion(newsCriterion);

        //List<News> lstNews = newsService.querySelective(news);
        List<News> lstNews = newsService.querySelctiveWithComment(news);
        data.put("newsList", lstNews);

        // 服务标兵 (teachers and student 简写 TS )
        newsCriterion = new NewsCriterion();
        //newsCriterion.setLimit(3);
        newsCriterion.setType("servicePacesetter");
        news = new News();
        news.setNewsCriterion(newsCriterion);
        lstNews = newsService.querySelective(news);
        data.put("TSList", lstNews);

        // 中心活动
        newsCriterion = new NewsCriterion();
        newsCriterion.setLimit(3);
        newsCriterion.setType("centraActivities");
        news = new News();
        news.setNewsCriterion(newsCriterion);
        lstNews = newsService.querySelectiveForActivityNews(news);
        data.put("activityNewsList", lstNews);

        return ResponseUtil.ok(data);
    }
}
