HomeAction.java 4.51 KB
package com.cjs.site.action.pub;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cjs.site.dao.entrust.EntrustDataDao;
import com.cjs.site.dao.entrust.OtcCodeDao;
import com.cjs.site.dao.info.ArticleCategoryDao;
import com.cjs.site.dao.info.ArticleDao;
import com.cjs.site.dao.pub.BannerDao;
import com.cjs.site.model.article.ArticleCategoryInfo;
import com.cjs.site.model.article.ArticleInfo;
import com.cjs.site.model.entrust.EntrustDataInfo;
import com.cjs.site.util.web.PageUtils;

/**
 * 首页
 * 
 * @author tongyufu
 *
 */
@Controller
public class HomeAction {

    Logger                     log = LogManager.getLogger();
    @Autowired
    private ArticleDao         articleDao;
    @Autowired
    private EntrustDataDao     entrustDao;
    @Autowired
    private BannerDao          bannerDao;
    @Autowired
    private ArticleCategoryDao articleCategoryDao;
    @Autowired
    private OtcCodeDao         otcCodeDao;

    @RequestMapping("/")
    public String home(Model model, HttpServletRequest request, HttpServletResponse response) {
        this.handlerRequest(request, response);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("_limit", 5);
        params = PageUtils.processPage(params);

        // 托管公告、公告通知
        params.put("cids", Arrays.asList(13, 24));
        List<ArticleInfo> list  = articleDao.search(params);
        model.addAttribute("gonggao",list );
        params.remove("cids");
        if(list.size()>0){
            model.addAttribute("showggBox",articleDao.queryById(list.get(0).getId()));
        }

        /** 新闻动态 */
        params.put("cid", 25);
        model.addAttribute("news", articleDao.search(params));
        model.addAttribute("newCategoryId", 25);

        /** 托管数据 */
        //model.addAttribute("entrusts", entrustDao.getEntrustList());
        model.addAttribute("entrusts", this.queryMinSellPrice());

        /** 首页广告图片 */
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", 0);
        map.put("bannerLocation", 0);
        model.addAttribute("banners", bannerDao.getBannerList(map));

        /**首页行情banner*/
        map.put("type", 0);
        map.put("bannerLocation", 1);
        model.addAttribute("hangqingB", bannerDao.getBannerList(map));

        model.addAttribute("bannerBottom", bannerDao.queryBannerById(71));
        model.addAttribute("bannerRight", bannerDao.queryBannerById(72));

        /**友情链接*/
        ArticleCategoryInfo categoryInfo = articleCategoryDao.queryByName("友情链接");
        Map<String, Object> articleParams = new HashMap<String, Object>();
        articleParams.put("_limit", 1000);
        articleParams = PageUtils.processPage(articleParams);
        articleParams.put("categoryId", categoryInfo.getId());
        List<ArticleInfo> youqingArticles = articleDao.queryByCategoryId(articleParams);
        model.addAttribute("youqingArticles", youqingArticles);
        //return "index.jsp";
        return "redirect:/user/balance";
    }

    /**
     * 301跳转
     * 
     * @param request
     * @param response
     */
    private void handlerRequest(HttpServletRequest request, HttpServletResponse response) {
        String url = request.getRequestURL().toString();
        if (url.contains("cangjs")) {
            log.info("old url:" + url);
            response.setStatus(301);
            response.setHeader("Location", "http://www.zhao.com/");
            response.setHeader("Connection", "close");
        }
    }

    private List<Map<String, Object>> queryMinSellPrice() {
        List<Map<String, Object>> otcs = otcCodeDao.queryLastPrice();
        List<EntrustDataInfo> datas = entrustDao.getEntrustList();
        for (Map<String, Object> otc : otcs) {
            for (EntrustDataInfo data : datas) {
                if (StringUtils.equals(otc.get("SDC_CODE").toString(), data.getEntrustId())) {
                    otc.put("IMAGE", data.getEntrustImage());
                    break;
                }
            }
        }
        return otcs;
    }
}