package com.bootdo.portal.controller;

import com.bootdo.blog.domain.ContentDO;
import com.bootdo.blog.service.ContentService;
import com.bootdo.common.utils.R;
import com.bootdo.portal.common.CommonConstants;
import com.bootdo.portal.vo.IndexCarouselVO;
import com.bootdo.portal.vo.NameAndCountVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 首页API
 *
 * @author Mr.xiBel
 * @date 2022/4/22
 **/
@CrossOrigin
@RestController
@RequestMapping("/portal/index")
public class HomeApiController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 文章/轮播数据
     */
    @Resource
    private ContentService contentService;


    /**
     * 获取首页的轮播数据 默认为四条发布的轮播 根据时间倒序
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCarousel", method = RequestMethod.POST)
    public R getCarousel() {
        // 获取轮播图数据
        List<IndexCarouselVO> carouselList = contentService.getCarousel();
        if (!CollectionUtils.isEmpty(carouselList)) {
            return R.ok().put(CommonConstants.DATA_FIELD_DATA, carouselList);
        }

        return R.ok();
    }

    /**
     * 根据 轮播/文章 id 获取详情
     *
     * @param parameter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCarouselDetail", method = RequestMethod.POST)
    public R getCarouselDetail(@RequestBody Map<String, Long> parameter) {
        ContentDO contentDO = contentService.get(parameter.get("id"));
        if (Objects.nonNull(contentDO)) {
            return R.ok().put(CommonConstants.DATA_FIELD_DATA, contentDO);
        }

        return R.ok();
    }

    /**
     * 根据首页位置获取文章列表 每部分取四条
     *
     * @param parameter 位置 自上而下,自左向右 0 左1, 1 右1, 2 左2, 3 右2
     *                  -----------------
     *                  左1 ｜ 右1
     *                  -----------------
     *                  左2 ｜ 右2
     *                  -----------------
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getArticle", method = RequestMethod.POST)
    public R getArticle(@RequestBody Map<String, Integer> parameter) {
        // 获取首页位置获取文章列表
        List<IndexCarouselVO> carouselList = contentService.getArticleByTags(parameter.get("position"));
        if (!CollectionUtils.isEmpty(carouselList)) {
            return R.ok().put(CommonConstants.DATA_FIELD_DATA, carouselList);
        }

        return R.ok();
    }


    /**
     * 热门企业列表  取四条
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPopularEnterprise", method = RequestMethod.POST)
    public R getPopularEnterprise() {
        // 统计热门企业 前四
        List<NameAndCountVO> dataList = contentService.getPopularEnterprise();
        if (!CollectionUtils.isEmpty(dataList)) {
            return R.ok().put(CommonConstants.DATA_FIELD_DATA, dataList);
        }

        return R.ok();
    }

    /**
     * 热门行业列表  取四条
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPopularIndustry", method = RequestMethod.POST)
    public R getPopularIndustry() {
        // 统计热门行业 前四
        List<NameAndCountVO> datalList = contentService.getPopularIndustry();
        if (!CollectionUtils.isEmpty(datalList)) {
            return R.ok().put(CommonConstants.DATA_FIELD_DATA, datalList);
        }

        return R.ok();
    }
}
