package com.slodon.b2b2c.controller.cms.admin;

import com.slodon.b2b2c.cms.example.HelpArticleExample;
import com.slodon.b2b2c.core.constant.ArticleConst;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.cms.HelpArticleCategoryModel;
import com.slodon.b2b2c.model.cms.HelpArticleModel;
import com.slodon.b2b2c.seller.pojo.StoreCmsArticle;
import com.slodon.b2b2c.seller.pojo.StoreCmsArticleCategory;
import com.slodon.b2b2c.vo.cms.HelpArticleVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cyan
 */
@Api(tags = "admin-帮助文章管理")
@RestController
@Slf4j
@RequestMapping("v3/cms/doc")
public class AdminDocHelpController {

    @Resource
    private HelpArticleModel helpArticleModel;

    @Resource
    private HelpArticleCategoryModel helpArticleCategoryModel;

    /**
     * 平台端文章列表
     *
     * @param request
     * @return
     */
    @GetMapping("/admin/list")
    public JsonResult<PageVO<HelpArticleVO>> getAdminDocList(HttpServletRequest request, String searchWord) {
        return getHelpDocList(request, ArticleConst.ADMIN_CATEGORY, searchWord);
    }

    /**
     * 卖家端文章列表
     *
     * @param request
     * @return
     */
    @GetMapping("/seller/list")
    public JsonResult<PageVO<HelpArticleVO>> getSellerDocList(HttpServletRequest request, String searchWord) {
        return getHelpDocList(request, ArticleConst.SELLER_CATEGORY, searchWord);
    }

    /**
     * @param request
     * @param categoryId
     * @return
     */
    private JsonResult<PageVO<HelpArticleVO>> getHelpDocList(HttpServletRequest request, Integer categoryId, String searchWord) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        HelpArticleExample example = new HelpArticleExample();
        example.setCategoryId(categoryId);
        example.setState(ArticleConst.STATE_YES);
        example.setSearchWord(searchWord);
        List<StoreCmsArticle> list = helpArticleModel.getArticleList(example, null);
        List<HelpArticleVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeCmsArticle -> {
                HelpArticleVO vo = new HelpArticleVO(storeCmsArticle);
                StoreCmsArticleCategory articleCategory = helpArticleCategoryModel.getArticleCategoryByCategoryId(storeCmsArticle.getCategoryId());
                vo.setCategoryName(articleCategory.getCategoryName());
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    /**
     * 文章详情
     *
     * @param request
     * @param articleId
     * @return
     */
    @GetMapping("detail")
    public JsonResult<StoreCmsArticle> getDetail(HttpServletRequest request, @RequestParam("id") Integer articleId) {
        return SldResponse.success(helpArticleModel.getArticleByArticleId(articleId));
    }

}
