package com.pcloud.book.applet.facade;

import com.pcloud.book.applet.biz.AppletBannerBiz;
import com.pcloud.book.applet.biz.AppletBooklistBiz;
import com.pcloud.book.applet.biz.AppletNewsBiz;
import com.pcloud.book.applet.biz.AppletUserBookcaseBiz;
import com.pcloud.book.applet.dto.AddBookParamDTO;
import com.pcloud.book.applet.dto.AppletBannerDTO;
import com.pcloud.book.applet.dto.AppletBooklistClassifyDTO;
import com.pcloud.book.applet.dto.AppletBooklistDTO;
import com.pcloud.book.applet.dto.AppletNewsClassifyDTO;
import com.pcloud.book.applet.dto.AppletNewsCommentDTO;
import com.pcloud.book.applet.dto.AppletNewsCustomTagDTO;
import com.pcloud.book.applet.dto.AppletNewsDTO;
import com.pcloud.book.applet.dto.AppletUserBookcaseDTO;
import com.pcloud.book.applet.dto.BookDTO4Booklist;
import com.pcloud.book.applet.entity.AppletBanner;
import com.pcloud.book.applet.entity.AppletBooklist;
import com.pcloud.book.applet.entity.AppletBooklistClassify;
import com.pcloud.book.applet.entity.AppletLinkClick;
import com.pcloud.book.applet.entity.AppletNews;
import com.pcloud.book.applet.entity.AppletNewsClassify;
import com.pcloud.book.applet.entity.AppletNewsComment;
import com.pcloud.book.applet.entity.AppletNewsCustomTag;
import com.pcloud.book.applet.entity.AppletUserBookcase;
import com.pcloud.book.applet.entity.AppletUserClickRecord;
import com.pcloud.book.applet.vo.AppletNewsClassifyVO;
import com.pcloud.book.applet.vo.AppletNewsShowStateVO;
import com.pcloud.book.base.exception.BookBizException;
import com.pcloud.book.skill.biz.PcloudGroupActivityBiz;
import com.pcloud.common.dto.ResponseDto;
import com.pcloud.common.page.PageBeanNew;
import com.pcloud.common.permission.PermissionException;
import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.NumberUtil;
import com.pcloud.common.utils.SessionUtil;
import com.pcloud.common.utils.cookie.Cookie;
import com.pcloud.common.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@Api(description = "小睿小程序首页")
@RestController("appletHomeFacade")
@RequestMapping("appletHome")
public class AppletHomeFacade {

    @Autowired
    private AppletBooklistBiz appletBooklistBiz;
    @Autowired
    private AppletBannerBiz appletBannerBiz;
    @Autowired
    private AppletNewsBiz appletNewsBiz;
    @Autowired
    private AppletUserBookcaseBiz appletUserBookcaseBiz;
    @Autowired
    private PcloudGroupActivityBiz pcloudGroupActivityBiz;

    @ApiOperation("新增书单栏目")
    @PostMapping("addBooklistClassify")
    public ResponseDto<Long> addBooklistClassify(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("书单栏目") AppletBooklistClassify appletBooklistClassify) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletBooklistClassify || StringUtil.isEmpty(appletBooklistClassify.getClassifyName())){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少栏目名称");
        }
        Long id = appletBooklistBiz.addBooklistClassify(appletBooklistClassify);
        return new ResponseDto<>(id);
    }

    @ApiOperation("删除书单栏目")
    @GetMapping("deleteBooklistClassifyById")
    public ResponseDto<?> deleteBooklistClassifyById(
            @RequestHeader("token") String token,
            @RequestParam("id") @ApiParam("书刊栏目id") Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少栏目id");
        }
        appletBooklistBiz.deleteBooklistClassifyById(id);
        return new ResponseDto<>();
    }

    @ApiOperation("修改书单栏目")
    @PostMapping("updateBooklistClassify")
    public ResponseDto<?> updateBooklistClassify(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("书单栏目")AppletBooklistClassify appletBooklistClassify) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null ==appletBooklistClassify || null==appletBooklistClassify.getId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletBooklistBiz.updateBooklistClassify(appletBooklistClassify);
        return new ResponseDto<>();
    }

    @ApiOperation("书单栏目列表")
    @GetMapping("listBooklistClassify")
    public ResponseDto<PageBeanNew<AppletBooklistClassifyDTO>> listBooklistClassify(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "name", required = false) @ApiParam("名称查询") String name) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletBooklistBiz.listBooklistClassify(currentPage, numPerPage, name));
    }

    @ApiOperation("新增书单")
    @PostMapping("addBooklist")
    public ResponseDto<Long> addBooklist(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("书单") AppletBooklist appletBooklist) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletBooklist){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        Long id = appletBooklistBiz.addBooklist(appletBooklist);
        return new ResponseDto<>(id);
    }

    @ApiOperation("删除书单")
    @GetMapping("deleteBooklistById")
    public ResponseDto<?> deleteBooklistById(
            @RequestHeader("token") String token,
            @RequestParam("id") @ApiParam("书刊id")Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少书刊id");
        }
        appletBooklistBiz.deleteBooklistById(id);
        return new ResponseDto<>();
    }

    @ApiOperation("修改书单")
    @PostMapping("updateBooklist")
    public ResponseDto<?> updateBooklist(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("书单") AppletBooklist appletBooklist) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletBooklist || null == appletBooklist.getId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletBooklistBiz.updateBooklist(appletBooklist);
        return new ResponseDto<>();
    }

    @ApiOperation("根据id查书单")
    @GetMapping("getBooklistById")
    public ResponseDto<AppletBooklistDTO> getBooklistById(
            @RequestHeader("token") String token,
            @RequestParam("id") @ApiParam("书刊id")Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少书刊id");
        }
        return new ResponseDto<>(appletBooklistBiz.getBooklistById(id));
    }

    @ApiOperation("书单管理列表")
    @GetMapping("listBooklist")
    public ResponseDto<PageBeanNew<AppletBooklistDTO>> listBooklist(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "name", required = false) @ApiParam("名称查询") String name,
            @RequestParam(value = "classifyId", required = false) @ApiParam("书单栏目id") Long classifyId) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletBooklistBiz.listBooklist(currentPage, numPerPage, name, classifyId));
    }

    @ApiOperation("书单添加图书")
    @PostMapping("addBookToBooklist")
    public ResponseDto<?> addBookToBooklist(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("添加图书参数") AddBookParamDTO addBookParamDTO) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == addBookParamDTO || null == addBookParamDTO.getBooklistId() || ListUtils.isEmpty(addBookParamDTO.getBookDTO4Booklists())){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        for (BookDTO4Booklist bookDTO : addBookParamDTO.getBookDTO4Booklists()){
            if (null==bookDTO.getBookId() || null==bookDTO.getAdviserId() ||null == bookDTO.getChannelId()){
                throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少bookId/adviserId/channelId");
            }
        }
        appletBooklistBiz.addBookToBooklist(addBookParamDTO);
        return new ResponseDto<>();
    }

    @ApiOperation("新增banner")
    @PostMapping("addBanner")
    public ResponseDto<Long> addBanner(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("banner") AppletBanner appletBanner) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletBanner){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        Long id = appletBannerBiz.addBanner(appletBanner);
        return new ResponseDto<>(id);
    }

    @ApiOperation("修改banner")
    @PostMapping("updateBanner")
    public ResponseDto<?> updateBanner(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("banner") AppletBanner appletBanner) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletBanner || null == appletBanner.getId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletBannerBiz.updateBanner(appletBanner);
        return new ResponseDto<>();
    }

    @ApiOperation("删除banner")
    @GetMapping("deleteBanner")
    public ResponseDto<?> deleteBanner(
            @RequestHeader("token") String token,
            @RequestParam("id") @ApiParam("bannerId")Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少bannerId");
        }
        appletBannerBiz.deleteBanner(id);
        return new ResponseDto<>();
    }

    @ApiOperation("根据id查banner")
    @GetMapping("getBannerById")
    public ResponseDto<AppletBannerDTO> getBannerById(
            @RequestHeader("token") String token,
            @RequestParam("bannerId") @ApiParam("bannerId")Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少bannerId");
        }
        return new ResponseDto<>(appletBannerBiz.getBannerById(id));
    }

    @ApiOperation("banner列表")
    @GetMapping("listBanner")
    public ResponseDto<PageBeanNew<AppletBannerDTO>> listBanner(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletBannerBiz.listBanner(currentPage, numPerPage,null));
    }

    @ApiOperation("客户端banner列表")
    @GetMapping("listBanner4Wechat")
    public ResponseDto<PageBeanNew<AppletBannerDTO>> listBanner4Wechat(
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage) {
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletBannerBiz.listBanner(currentPage, numPerPage, true));
    }

    @ApiOperation("获取书单下书籍列表")
    @GetMapping("listPageBook4AppletBooklist")
    public ResponseDto<PageBeanNew<BookDTO4Booklist>> listPageBook4AppletBooklist(
            @RequestHeader("token") String token,
            @RequestParam("booklistId") @ApiParam("书单id") Long booklistId,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==booklistId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少书单id");
        }
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletBooklistBiz.listPageBook4AppletBooklist(booklistId,currentPage,numPerPage));
    }

    @ApiOperation("更新书籍排序")
    @GetMapping("updateBookSeq")
    public ResponseDto<?> updateBookSeq(
            @RequestHeader("token") String token,
            @RequestParam("configId") @ApiParam("配置id") Long configId,
            @RequestParam("seq") @ApiParam("排序值")Integer seq) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==configId || null == seq){
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少参数");
        }
        appletBooklistBiz.updateBookSeq(configId, seq);
        return new ResponseDto<>();
    }

    @ApiOperation("获取所有书单栏目")
    @GetMapping("getAllBooklistClassify")
    public ResponseDto<List<AppletBooklistClassifyDTO>> getAllBooklistClassify(
            @RequestParam(value = "showState", required = false) @ApiParam("是否展示") Boolean showState){
        return new ResponseDto<>(appletBooklistBiz.getAllBooklistClassify(showState));
    }

    @ApiOperation("客户端获取书单列表")
    @GetMapping("listBooklist4Wechat")
    public ResponseDto<PageBeanNew<AppletBooklistDTO>> listBooklist4Wechat(
            @CookieValue("userInfo") String userInfo,
            @RequestParam(value = "classifyId", required = false) @ApiParam("书单栏目id") Long classifyId,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletBooklistBiz.listBooklist4Wechat(classifyId,currentPage,numPerPage,wechatUserId));
    }

    @ApiOperation("客户端根据id获取书单")
    @GetMapping("getBooklistById4Wechat")
    public ResponseDto<AppletBooklistDTO> getBooklistById4Wechat(
            @RequestParam("booklistId") @ApiParam("书单id")Long booklistId){
        if (null==booklistId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(appletBooklistBiz.getBooklistById4Wechat(booklistId));
    }

    @ApiOperation("新增资讯栏目")
    @PostMapping("addAppletNewsClassify")
    public ResponseDto<Long> addAppletNewsClassify(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("资讯栏目") AppletNewsClassify appletNewsClassify) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletNewsClassify){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(appletNewsBiz.addAppletNewsClassify(appletNewsClassify));
    }

    @ApiOperation("修改资讯栏目")
    @PostMapping("updateAppletNewsClassify")
    public ResponseDto<?> updateAppletNewsClassify(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("资讯栏目")AppletNewsClassify appletNewsClassify) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletNewsClassify || null == appletNewsClassify.getId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.updateAppletNewsClassify(appletNewsClassify);
        return new ResponseDto<>();
    }

    @ApiOperation("删除资讯栏目")
    @GetMapping("deleteNewsClassifyById")
    public ResponseDto<?> deleteNewsClassifyById(
            @RequestHeader("token") String token,
            @RequestParam("id") @ApiParam("资讯栏目")Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.deleteNewsClassifyById(id);
        return new ResponseDto<>();
    }

    @ApiOperation("资讯栏目列表")
    @GetMapping("listNewsClassify")
    public ResponseDto<PageBeanNew<AppletNewsClassifyDTO>> listNewsClassify(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "name", required = false) @ApiParam("名称查询") String name) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletNewsBiz.listNewsClassify(currentPage, numPerPage, name));
    }

    @ApiOperation("获取所有资讯栏目")
    @GetMapping("getAllNewsClassify")
    public ResponseDto<List<AppletNewsClassifyDTO>> getAllNewsClassify(
            @RequestParam(value = "showState", required = false) @ApiParam("是否展示") Boolean showState){
        return new ResponseDto<>(appletNewsBiz.getAllNewsClassify(showState));
    }

    @ApiOperation("添加资讯")
    @PostMapping("addAppletNews")
    public ResponseDto<Long> addAppletNews(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("资讯") AppletNews appletNews) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletNews){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(appletNewsBiz.addAppletNews(appletNews));
    }

    @ApiOperation("修改资讯")
    @PostMapping("updateAppletNews")
    public ResponseDto<?> updateAppletNews(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("资讯") AppletNews appletNews) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletNews || null == appletNews.getId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.updateAppletNews(appletNews);
        return new ResponseDto<>();
    }

    @ApiOperation("删除资讯")
    @GetMapping("deleteAppletNewsById")
    public ResponseDto<?> deleteAppletNewsById(
            @RequestHeader("token") String token,
            @RequestParam("id") @ApiParam("资讯id") Long id) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == id){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.deleteAppletNewsById(id);
        return new ResponseDto<>();
    }

    @ApiOperation("资讯列表")
    @GetMapping("listAppletNews")
    public ResponseDto<PageBeanNew<AppletNewsDTO>> listAppletNews(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "name", required = false) @ApiParam("名称查询") String name,
            @RequestParam(value = "firstClassify", required = false) @ApiParam("一级书刊分类") Long firstClassify,
            @RequestParam(value = "secondClassify", required = false) @ApiParam("二级书刊分类")Long secondClassify,
            @RequestParam(value = "gradeLabelId", required = false) @ApiParam("年级标签")Long gradeLabelId,
            @RequestParam(value = "subjectLabelId", required = false) @ApiParam("科目标签")Long subjectLabelId,
            @RequestParam(value = "rightsClassifyId", required = false) @ApiParam("权益分类") Long rightsClassifyId,
            @RequestParam(value = "source", required = false) @ApiParam("来源") String source,
            @RequestParam(value = "showState", required = false) @ApiParam("上架") Integer showState,
            @RequestParam(value = "newsClassifyId", required = false) @ApiParam("资讯栏目id") Long newsClassifyId,
            @RequestParam(value = "customTagId", required = false) @ApiParam("自定义标识id") Long customTagId
            ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletNewsBiz.listAppletNews(currentPage, numPerPage, name, firstClassify,secondClassify,gradeLabelId,
                subjectLabelId,rightsClassifyId,source,showState,newsClassifyId,customTagId));
    }

    @ApiOperation("客户端资讯列表")
    @GetMapping("listAppletNews4Wechat")
    public ResponseDto<PageBeanNew<AppletNewsDTO>> listAppletNews4Wechat(
            @CookieValue("userInfo") String userInfo,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "newsClassifyId", required = false) @ApiParam("资讯栏目id") Long newsClassifyId) {
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletNewsBiz.listAppletNews4Wechat(currentPage, numPerPage, newsClassifyId, wechatUserId));
    }

    @ApiOperation("添加资讯评论")
    @PostMapping("addAppletNewsComment")
    public ResponseDto<?> addAppletNewsComment(
            @CookieValue("userInfo") String userInfo,
            @RequestBody @ApiParam("资讯评论") AppletNewsComment appletNewsComment){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null==appletNewsComment || null == appletNewsComment.getNewsId() || StringUtil.isEmpty(appletNewsComment.getComment())){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.addAppletNewsComment(appletNewsComment,wechatUserId);
        return new ResponseDto<>();
    }

    @ApiOperation("查询资讯评论列表")
    @GetMapping("listNewsCommentByNewsId")
    public ResponseDto<PageBeanNew<AppletNewsCommentDTO>> listNewsCommentByNewsId(
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam("newsId") @ApiParam("资讯id") Long newsId){
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        if (null == newsId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少资讯id");
        }
        return new ResponseDto<>(appletNewsBiz.listNewsCommentByNewsId(currentPage, numPerPage, newsId));
    }

    @ApiOperation("添加图书到书架")
    @PostMapping("addUserBook")
    public ResponseDto<?> addUserBook(
            @CookieValue("userInfo") String userInfo,
            @RequestBody @ApiParam("图书") AppletUserBookcase appletUserBookcase){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null == appletUserBookcase || null == appletUserBookcase.getBookId() || null == appletUserBookcase.getAdviserId()
            || null == appletUserBookcase.getChannelId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletUserBookcase.setWechatUserId(wechatUserId);
        appletUserBookcaseBiz.addUserBook(appletUserBookcase);
        return new ResponseDto<>();
    }

    @ApiOperation("查用户书架列表")
    @GetMapping("listUserBookcaseByWechatUserId")
    public ResponseDto<PageBeanNew<AppletUserBookcaseDTO>> listUserBookcaseByWechatUserId(
            @CookieValue("userInfo") String userInfo,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null==currentPage || null == numPerPage){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少分页参数");
        }
        return new ResponseDto<>(appletUserBookcaseBiz.listByWechatUserId(wechatUserId,currentPage,numPerPage));
    }

    @ApiOperation("小程序首页书籍信息")
    @GetMapping("getUserBookInfoByWechatUserId")
    public ResponseDto<AppletUserBookcaseDTO> getUserBookInfoByWechatUserId (
            @CookieValue("userInfo") String userInfo,
            @RequestParam(value = "bookId") @ApiParam("BookId") Long bookId,
            @RequestParam(value = "channelId") @ApiParam("channelId") Long channelId,
            @RequestParam(value = "rightsSettingId",required = false) @ApiParam("rightsSettingId") Long rightsSettingId,
            @RequestParam(value = "adviserId") @ApiParam("adviserId") Long adviserId){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        return new ResponseDto<>(appletUserBookcaseBiz.getUserBookInfoByWechatUserId(bookId,channelId,adviserId,rightsSettingId));
    }

    @ApiOperation("新增用户资源点击记录")
    @PostMapping("addUserClickRecord")
    public ResponseDto<?>  addUserClickRecord(
            @CookieValue("userInfo") String userInfo,
            @RequestBody @ApiParam("资源点击记录") AppletUserClickRecord appletUserClickRecord){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null == appletUserClickRecord || null == appletUserClickRecord.getBookId()
            || null == appletUserClickRecord.getFromId() ||StringUtil.isEmpty(appletUserClickRecord.getFromType())){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletUserClickRecord.setWechatUserId(wechatUserId);
        appletUserBookcaseBiz.addUserClickRecord(appletUserClickRecord);
        return new ResponseDto<>();
    }

    @ApiOperation("用户图书资源点击统计")
    @GetMapping("getUserClickStatistic")
    public ResponseDto<Map<String,Object>> getUserClickStatistic(
            @CookieValue("userInfo") String userInfo,
            @RequestParam("bookId") @ApiParam("图书id")Long bookId,
            @RequestParam("channelId") @ApiParam("运营id")Long channelId,
            @RequestParam("adviserId") @ApiParam("编辑id")Long adviserId){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null == bookId || null== channelId || null == adviserId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(appletUserBookcaseBiz.getUserClickStatistic(wechatUserId,bookId,channelId,adviserId));
    }

    @ApiOperation("根据ID查资讯详情")
    @GetMapping("getAppletNewsById")
    public ResponseDto<AppletNewsDTO> getAppletNewsById(
            @RequestParam("newsId") @ApiParam("资讯id")Long newsId){
        if (null == newsId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(appletNewsBiz.getNewsById(newsId));
    }

    @ApiOperation("查图书详情-出版作者分类书名")
    @GetMapping("getByBookIdAdviserChannel")
    public ResponseDto<BookDTO4Booklist> getByBookIdAdviserChannel(
            @RequestParam("adviserId") @ApiParam("编辑id")Long adviserId,
            @RequestParam("channelId") @ApiParam("运营id")Long channelId,
            @RequestParam("bookId") @ApiParam("图书id")Long bookId){
        if (null == bookId || null== channelId || null == adviserId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(appletBooklistBiz.getByBookIdAdviserChannel(adviserId, channelId, bookId));
    }

    @ApiOperation("更新资讯上架状态")
    @PostMapping("updateNewsShowState")
    public ResponseDto<?> updateNewsShowState(
            @RequestHeader("token") String token,
            @RequestBody @ApiParam("资讯") AppletNews appletNews) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletNews || null == appletNews.getId() || null == appletNews.getShowState()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.updateNewsShowState(appletNews);
        return new ResponseDto<>();
    }

    @ApiOperation("获取用户最后一次点击的书（有社群书的取最后一次社群书，没有的取现代纸书）")
    @GetMapping("getUserLastBookRe")
    public ResponseDto<?> getUserLastBookRe(
            @CookieValue("userInfo") String userInfo
    ) throws PermissionException{
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        return new ResponseDto<>(appletUserBookcaseBiz.getUserLastBookRe(wechatUserId));
    }

    @ApiOperation("获取社群共读")
    @GetMapping("getGroupActivity4Applet")
    public ResponseDto<?> getGroupActivity4Applet(
        @CookieValue("userInfo") String userInfo,
        @RequestParam(value = "adviserId") Long adviserId,
        @RequestParam(value = "bookId") Long bookId,
        @RequestParam(value = "rightsSettingId") Long rightsSettingId,
        @RequestParam(value = "channelId") Long channelId,
        @RequestParam(value = "readType",required = false)@ApiParam("阅读方式1轻松2高效3深度") Integer readType
        ) {
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null == wechatUserId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        return new ResponseDto<>(pcloudGroupActivityBiz.getGroupActivity4Applet(rightsSettingId, adviserId ,bookId, channelId, readType));
    }

    @ApiOperation("资讯来源列表")
    @GetMapping("getNewsSourceList")
    public ResponseDto<List<String>> getNewsSourceList(){
        return new ResponseDto<>(appletNewsBiz.getNewsSourceList());
    }

    @ApiOperation("批量修改分类")
    @PostMapping("batchUpdateClassify")
    public ResponseDto<?> batchUpdateClassify(
        @RequestHeader("token") String token,
        @RequestBody AppletNewsClassifyVO appletNewsClassifyVO
    ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        appletNewsBiz.batchUpdateClassify(appletNewsClassifyVO);
        return new ResponseDto<>();
    }

    @ApiOperation("批量上下架")
    @PostMapping("batchUpdateShowState")
    public ResponseDto<?> batchUpdateShowState(
        @RequestHeader("token") String token,
        @RequestBody AppletNewsShowStateVO appletNewsShowStateVO
    ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == appletNewsShowStateVO || ListUtils.isEmpty(appletNewsShowStateVO.getAppletNewsIds()) || null == appletNewsShowStateVO.getShowState()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.batchUpdateShowState(appletNewsShowStateVO);
        return new ResponseDto<>();
    }

    @ApiOperation("批量删除")
    @PostMapping("batchDeleteAppletNews")
    public ResponseDto<?> batchDeleteAppletNews(
        @RequestHeader("token") String token,
        @RequestBody List<Long> appletNewsIds
    ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (ListUtils.isEmpty(appletNewsIds)){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.batchDeleteAppletNews(appletNewsIds);
        return new ResponseDto<>();
    }

    @ApiOperation("删除书架记录")
    @PostMapping("deleteBookcaseRecordByIds")
    public ResponseDto<?> deleteBookcaseRecordByIds(@CookieValue("userInfo") String userInfo, @RequestBody List<Long> ids) {
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        appletUserBookcaseBiz.deleteByIds(ids, wechatUserId);
        return new ResponseDto<>();
    }

    @ApiOperation("资讯和券的资源外链访问埋点")
    @PostMapping("addClickRecord")
    public ResponseDto<?> addClickRecord(@RequestBody @Validated AppletLinkClick appletLinkClick){
        if (null==appletLinkClick){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletNewsBiz.addClickRecord(appletLinkClick);
        return new ResponseDto<>();
    }

    @ApiOperation("根据链接地址获取外链编号")
    @GetMapping("getUrlNumberByUrl")
    public ResponseDto<String> getUrlNumberByUrl(@RequestHeader("token") String token, @RequestParam(value = "url",required = false) String url){
        SessionUtil.getToken4Redis(token);
        return new ResponseDto<>(appletNewsBiz.getUrlNumberByUrl(url));
    }

    @ApiOperation("资讯统计分析")
    @GetMapping("listAppletNews4Analysis")
    public ResponseDto<PageBeanNew<AppletNewsDTO>> listAppletNews4Analysis(
            @RequestHeader("token") String token,
            @RequestParam("currentPage") @ApiParam("当前页") Integer currentPage,
            @RequestParam("numPerPage") @ApiParam("每页数量") Integer numPerPage,
            @RequestParam(value = "name", required = false) @ApiParam("名称/外链编号查询") String name,
            @RequestParam(value = "firstClassify", required = false) @ApiParam("一级书刊分类") Long firstClassify,
            @RequestParam(value = "secondClassify", required = false) @ApiParam("二级书刊分类") Long secondClassify,
            @RequestParam(value = "gradeLabelId", required = false) @ApiParam("年级标签") Long gradeLabelId,
            @RequestParam(value = "subjectLabelId", required = false) @ApiParam("科目标签") Long subjectLabelId,
            @RequestParam(value = "linkOnly", required = false) @ApiParam("只看第三方") Integer linkOnly,
            @RequestParam(value = "source", required = false) @ApiParam("来源") String source,
            @RequestParam(value = "browseQty", required = false) @ApiParam("浏览量") Integer browseQty,
            @RequestParam(value = "clickQty", required = false) @ApiParam("点击量") Integer clickQty,
            @RequestParam(value = "rightsClassifyId", required = false) @ApiParam("权益分类") Long rightsClassifyId
    ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        if (null == currentPage || null == numPerPage) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少分页参数");
        }
        return new ResponseDto<>(appletNewsBiz.listAppletNews4Analysis(currentPage, numPerPage, name, firstClassify, secondClassify, gradeLabelId,
                subjectLabelId, linkOnly,rightsClassifyId,source,browseQty,clickQty));
    }


    @ApiOperation("用户选择资讯栏目")
    @PostMapping("addUserNewsClassify")
    public ResponseDto<?> addUserNewsClassify(@CookieValue("userInfo") String userInfo, @RequestBody List<Long> newsClassifyIds){
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        appletNewsBiz.addUserNewsClassify(newsClassifyIds,wechatUserId);
        return new ResponseDto<>();
    }

    @ApiOperation("所有资讯栏目列表+用户选择的资讯栏目")
    @GetMapping("getAllNewsClassify4Wechat")
    public ResponseDto<List<AppletNewsClassifyDTO>> getAllNewsClassify4Wechat(@CookieValue("userInfo") String userInfo){
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        return new ResponseDto<>(appletNewsBiz.getAllNewsClassify4Wechat(wechatUserId));
    }

    @ApiOperation("用户选择的资讯栏目")
    @GetMapping("getNewsClassifyByUser")
    public ResponseDto<List<AppletNewsClassifyDTO>> getNewsClassifyByUser(@CookieValue("userInfo") String userInfo){
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        return new ResponseDto<>(appletNewsBiz.getNewsClassifyByUser(wechatUserId));
    }

    @ApiOperation("用户选择书单栏目")
    @PostMapping("addUserBooklistClassify")
    public ResponseDto<?> addUserBooklistClassify(@CookieValue("userInfo") String userInfo, @RequestBody List<Long> classifyIds) {
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        appletBooklistBiz.addUserBooklistClassify(classifyIds, wechatUserId);
        return new ResponseDto<>();
    }

    @ApiOperation("所有书单栏目列表+用户选择的书单栏目")
    @GetMapping("getAllBooklistClassify4Wechat")
    public ResponseDto<List<AppletBooklistClassifyDTO>> getAllBooklistClassify4Wechat(@CookieValue("userInfo") String userInfo) {
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        return new ResponseDto<>(appletBooklistBiz.getAllBooklistClassify4Wechat(wechatUserId));
    }

    @ApiOperation("用户选择的书单栏目")
    @GetMapping("getBooklistClassifyByUser")
    public ResponseDto<List<AppletBooklistClassifyDTO>> getBooklistClassifyByUser(@CookieValue("userInfo") String userInfo) {
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        return new ResponseDto<>(appletBooklistBiz.getBooklistClassifyByUser(wechatUserId));
    }

    @ApiOperation("已读书刊随机换一本书展示")
    @GetMapping("randomChangeBook")
    public ResponseDto<?> randomChangeBook(@CookieValue("userInfo") String userInfo){
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        appletUserBookcaseBiz.randomChangeBook(wechatUserId);
        return new ResponseDto<>();
    }


    @ApiOperation("更新读者关于本书的阅读方式")
    @PostMapping("updateUserReadType")
    public ResponseDto<?> updateUserReadType(
            @CookieValue("userInfo") String userInfo,
            @RequestBody @ApiParam("图书") AppletUserBookcase appletUserBookcase){
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null == appletUserBookcase || null == appletUserBookcase.getBookId() || null == appletUserBookcase.getAdviserId()
                || null == appletUserBookcase.getChannelId()){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        appletUserBookcase.setWechatUserId(wechatUserId);
        appletUserBookcaseBiz.updateUserReadType(appletUserBookcase);
        return new ResponseDto<>();
    }

    @ApiOperation("获取用户当前本书的阅读方式")
    @GetMapping("getUserReadType")
    public ResponseDto<AppletUserBookcase> getUserReadType(
            @CookieValue("userInfo") String userInfo,
            @RequestParam("bookId") Long bookId,
            @RequestParam("adviserId") Long adviserId,
            @RequestParam("channelId") Long channelId) {
        Long wechatUserId = Cookie.getId(userInfo,Cookie._WECHAT_USER_ID);
        if (null == bookId || null == adviserId || null == channelId){
            throw new BookBizException(BookBizException.PARAM_IS_NULL,"缺少参数");
        }
        AppletUserBookcase appletUserBookcase = appletUserBookcaseBiz.getUserReadType(wechatUserId,bookId,adviserId,channelId);
        return new ResponseDto<>(appletUserBookcase);
    }

    @ApiOperation("批量修改资讯所属栏目")
    @PostMapping("batchUpdateNewsClassify")
    public ResponseDto<?> batchUpdateNewsClassify(
            @RequestHeader("token") String token,
            @RequestBody AppletNewsClassifyVO appletNewsClassifyVO
    ) throws PermissionException {
        SessionUtil.getInfoToken4Redis(token);
        appletNewsBiz.batchUpdateNewsClassify(appletNewsClassifyVO);
        return new ResponseDto<>();
    }

    @ApiOperation("用户已读书刊记录id列表")
    @GetMapping("getBookcaseIdListByUser")
    public ResponseDto<?> getBookcaseIdListByUser(@CookieValue("userInfo") String userInfo){
        Long wechatUserId = Cookie.getId(userInfo, Cookie._WECHAT_USER_ID);
        if(!NumberUtil.isNumber(wechatUserId)){
            throw BookBizException.PARAM_DELETION;
        }
        return new ResponseDto<>(appletUserBookcaseBiz.getBookcaseIdListByUser(wechatUserId));
    }

    @ApiOperation("批量更新资讯自定义标识")
    @PostMapping("batchUpdateCustomTagId")
    public ResponseDto<?> batchUpdateCustomTagId(@RequestHeader("token") String token, @RequestBody AppletNewsCustomTagDTO appletNewsCustomTagDTO) {
        SessionUtil.getToken4Redis(token);
        if (null == appletNewsCustomTagDTO) {
            throw new BookBizException(BookBizException.PARAM_IS_NULL, "参数为空");
        }
        appletNewsBiz.batchUpdateCustomTagId(appletNewsCustomTagDTO.getNewsIds(), appletNewsCustomTagDTO.getCustomTagId(), appletNewsCustomTagDTO.getCustomTag());
        return new ResponseDto<>();
    }

    @ApiOperation("新增资讯自定义标识")
    @PostMapping("insertCustomTag")
    public ResponseDto<?> insertCustomTag(@RequestHeader("token") String token, @RequestBody AppletNewsCustomTag appletNewsCustomTag) {
        SessionUtil.getToken4Redis(token);
        return new ResponseDto<>(appletNewsBiz.insertCustomTag(appletNewsCustomTag));
    }

    @ApiOperation("修改资讯自定义标识")
    @PostMapping("updateCustomTag")
    public ResponseDto<?> updateCustomTag(@RequestHeader("token") String token, @RequestBody AppletNewsCustomTag appletNewsCustomTag) {
        SessionUtil.getToken4Redis(token);
        appletNewsBiz.updateCustomTag(appletNewsCustomTag);
        return new ResponseDto<>();
    }

    @ApiOperation("资讯自定义标识列表")
    @GetMapping("getNewsCustomTagList")
    public ResponseDto<?> getNewsCustomTagList(@RequestHeader("token") String token) {
        SessionUtil.getToken4Redis(token);
        return new ResponseDto<>(appletNewsBiz.getCustomTagList());
    }

    @ApiOperation("根据标识id查资讯id列表")
    @GetMapping("getNewsIdListByCustomTagId")
    public ResponseDto<?> getNewsIdListByCustomTagId(@RequestParam("customTagId") Long customTagId) {
        return new ResponseDto<>(appletNewsBiz.getNewsIdListByCustomTagId(customTagId));
    }
}



