package com.slodon.b2b2c.controller.video.front;

import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.core.controller.BaseController;
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.*;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.video.*;
import com.slodon.b2b2c.video.example.*;
import com.slodon.b2b2c.video.pojo.*;
import com.slodon.b2b2c.vo.video.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(tags = "front-短视频列表")
@RestController
@Slf4j
@RequestMapping("v3/video/front/video")
public class FrontSVideoController extends BaseController {

    @Resource
    private SvideoLabelModel svideoLabelModel;
    @Resource
    private SvideoThemeModel svideoThemeModel;
    @Resource
    private SvideoVideoModel svideoVideoModel;
    @Resource
    private AuthorModel authorModel;
    @Resource
    private SvideoThemeVideoModel svideoThemeVideoModel;
    @Resource
    private VideoGoodsModel videoGoodsModel;
    @Resource
    private SvideoVideoLikeModel svideoVideoLikeModel;
    @Resource
    private AuthorFollowModel authorFollowModel;
    @Resource
    private VideoSettingModel videoSettingModel;
    @Resource
    private GoodsModel goodsModel;

    @ApiOperation("短视频列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "标签id", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<FrontVideoVO> list(HttpServletRequest request, Integer labelId) {
        String videoSwitch = videoSettingModel.getVideoSettingBySettingName("video_switch").getValue();
        String defaultProfile = videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue();

        //key-作者id，value-作者信息
        Map<Integer, Author> map = new HashMap<>();

        Member member = UserUtil.getUser(request, Member.class);
        String memberAvatar;
        //设置默认头像
        if (member.getMemberId() == null) {
            memberAvatar = FileUrlUtil.getFileUrl(defaultProfile, null);
        } else {
            Author author = authorModel.getAuthorByMemberId(member.getMemberId());
            if (StringUtil.isEmpty(author.getMemberAvatar())) {
                memberAvatar = FileUrlUtil.getFileUrl(defaultProfile, null);
            } else {
                memberAvatar = author.getMemberAvatar().startsWith("http") ? author.getMemberAvatar() : author.getMemberAvatar().startsWith("http") ? author.getMemberAvatar() : FileUrlUtil.getFileUrl(author.getMemberAvatar(), null);
            }
            map.put(author.getAuthorId(), author);
        }
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        //查询短视频标签
        SvideoLabelExample example = new SvideoLabelExample();
        example.setIsShow(VideoConst.IS_SHOW_1);
        example.setOrderBy("sort asc, create_time desc");
        List<SvideoLabel> labelList = svideoLabelModel.getSvideoLabelList(example, null);
        List<VideoLabelVO> labelVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(labelList)) {
            labelList.forEach(label -> {
                labelVos.add(new VideoLabelVO(label));
            });
        }
        //查询短视频主题
        SvideoThemeExample themeExample = new SvideoThemeExample();
        if (!StringUtil.isNullOrZero(labelId)) {
            themeExample.setLabelId(labelId);
        } else {
            if (!CollectionUtils.isEmpty(labelList)) {
                themeExample.setLabelId(labelList.get(0).getLabelId());
            }
        }
        themeExample.setIsShow(VideoConst.IS_SHOW_1);
        themeExample.setOrderBy("sort asc, create_time desc");
        List<SvideoTheme> themeList = svideoThemeModel.getSvideoThemeList(themeExample, pager);
        List<FrontVideoVO.ThemeVO> themeVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(themeList)) {
            themeList.forEach(theme -> {
                themeVos.add(new FrontVideoVO.ThemeVO(theme));
            });
        }
        //查询标签下的视频
        SvideoVideoExample videoExample = new SvideoVideoExample();
        if (!StringUtil.isNullOrZero(labelId)) {
            videoExample.setLabelId(labelId);
        } else {
            if (!CollectionUtils.isEmpty(labelList)) {
                videoExample.setLabelId(labelList.get(0).getLabelId());
            }
        }
        if (videoSwitch.equals(VideoConst.VIDEO_SWITCH_0)) {
            //短视频关闭只查询图文
            videoExample.setVideoType(VideoConst.SVIDEO_TYPE_2);
        }
        videoExample.setState(VideoConst.STATE_2);
        videoExample.setIsDelete(VideoConst.IS_DELETE_0);
        List<SvideoVideo> videoList = svideoVideoModel.getSvideoVideoList(videoExample, pager);
        List<VideoSearchVO> videoVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(videoList)) {
            videoList.forEach(video -> {
                //查询短视频作者信息
                Author videoAuthor = null;
                if (!map.containsKey(video.getAuthorId())) {
                    //查询短视频作者信息
                    videoAuthor = authorModel.getAuthorByAuthorId(video.getAuthorId());
                    AssertUtil.notNull(videoAuthor, "短视频作者不存在");

                    map.put(video.getAuthorId(), videoAuthor);
                } else {
                    videoAuthor = map.get(video.getAuthorId());
                }
                //无昵称,用会员账号
                if (StringUtil.isEmpty(videoAuthor.getMemberNickname())) {
                    video.setMemberNickname(videoAuthor.getMemberName());
                } else {
                    video.setMemberNickname(videoAuthor.getMemberNickname());
                }
                video.setMemberName(videoAuthor.getMemberName());
                VideoSearchVO vo = new VideoSearchVO(video);
                //设置默认头像
                if (StringUtil.isEmpty(videoAuthor.getMemberAvatar())) {
                    vo.setMemberAvatar(FileUrlUtil.getFileUrl(defaultProfile, null));
                } else {
                    vo.setMemberAvatar(videoAuthor.getMemberAvatar().startsWith("http") ? videoAuthor.getMemberAvatar() : FileUrlUtil.getFileUrl(videoAuthor.getMemberAvatar(), null));
                }
                videoVos.add(vo);
            });
        }
        return SldResponse.success(new FrontVideoVO(labelVos, themeVos, videoVos, memberAvatar, pager));
    }

    @ApiOperation("推荐主题详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "themeId", value = "主题id", required = true, paramType = "query")
    })
    @GetMapping("themeDetail")
    public JsonResult<FrontThemeVO> themeDetail(HttpServletRequest request, Integer themeId) {
        SvideoTheme svideoTheme = svideoThemeModel.getSvideoThemeByThemeId(themeId);
        AssertUtil.notNull(svideoTheme, "获取主题信息为空，请重试");

        FrontThemeVO.ThemeInfoVO themeInfoVO = new FrontThemeVO.ThemeInfoVO(svideoTheme);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoThemeVideoExample example = new SvideoThemeVideoExample();
        example.setThemeId(themeId);

        String videoSwitch = videoSettingModel.getVideoSettingBySettingName("video_switch").getValue();
        if (videoSwitch.equals(VideoConst.VIDEO_SWITCH_0)) {
            //短视频关闭只查询图文
            example.setVideoType(VideoConst.SVIDEO_TYPE_2);
        }

        List<SvideoThemeVideo> list = svideoThemeVideoModel.getSvideoThemeVideoList(example, pager);
        List<SelectVideoVO> videoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(themeVideo -> {
                SvideoVideo svideoVideo = svideoVideoModel.getSvideoVideoByVideoId(themeVideo.getVideoId());
                AssertUtil.notNull(svideoVideo, "视频不存在");
                //设置默认头像
                if (StringUtil.isEmpty(svideoVideo.getMemberAvatar())) {
                    svideoVideo.setMemberAvatar(videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue());
                }
                //无昵称,用会员账号
                if (StringUtil.isEmpty(svideoVideo.getMemberNickname())) {
                    svideoVideo.setMemberNickname(svideoVideo.getMemberName());
                }
                videoList.add(new SelectVideoVO(themeVideo, svideoVideo));
            });
        }
        return SldResponse.success(new FrontThemeVO(themeInfoVO, videoList, pager));
    }

    @ApiOperation("视频播放页信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query")
    })
    @GetMapping("playingPage")
    public JsonResult<VideoPlayPageVO> playingPage(HttpServletRequest request, Integer videoId) {
        Member member = UserUtil.getUser(request, Member.class);
        Author author = null;

        VideoPlayPageVO pageVO = new VideoPlayPageVO();
        //查询视频信息
        SvideoVideo svideoVideo = svideoVideoModel.getSvideoVideoByVideoId(videoId);
        AssertUtil.notNull(svideoVideo, "视频不存在");
        if (svideoVideo.getIsDelete() == VideoConst.IS_DELETE_1) {
            return SldResponse.failSpecial("该视频已被删除");
        }
        VideoPlayPageVO.VideoInfoVO videoInfoVO = new VideoPlayPageVO.VideoInfoVO(svideoVideo);
        //是否关注
        boolean isFollow = false;
        //默认头像
        String defaultProfile = videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue();
        if (member.getMemberId() == null) {
            //查询作者信息
            author = authorModel.getAuthorByAuthorId(svideoVideo.getAuthorId());
            AssertUtil.notNull(author, "获取作者信息为空，请重试");
            //未登录取默认头像
            pageVO.setMemberAvatar(FileUrlUtil.getFileUrl(defaultProfile, null));
            if (StringUtil.isEmpty(author.getMemberAvatar())) {
                author.setMemberAvatar(defaultProfile);
            }
        } else {
            //查询作者信息
            author = authorModel.getAuthorByMemberId(member.getMemberId());
            AssertUtil.notNull(author, "获取作者信息为空，请重试");
            //设置默认头像
            if (StringUtil.isEmpty(author.getMemberAvatar())) {
                pageVO.setMemberAvatar(FileUrlUtil.getFileUrl(defaultProfile, null));
                author.setMemberAvatar(defaultProfile);
            } else {
                pageVO.setMemberAvatar(author.getMemberAvatar().startsWith("http") ? author.getMemberAvatar() : FileUrlUtil.getFileUrl(author.getMemberAvatar(), null));
            }
            //查询是否点赞
            SvideoVideoLikeExample videoLikeExample = new SvideoVideoLikeExample();
            videoLikeExample.setAuthorId(author.getAuthorId());
            videoLikeExample.setVideoId(videoId);
            List<SvideoVideoLike> videoLikeList = svideoVideoLikeModel.getSvideoVideoLikeList(videoLikeExample, null);
            if (!CollectionUtils.isEmpty(videoLikeList)) {
                videoInfoVO.setIsLike(true);
            }
            //是否自己的视频
            if (author.getAuthorId().equals(svideoVideo.getAuthorId())) {
                videoInfoVO.setIsSelf(true);
            } else {
                //不是自己的
                //查询是否关注
                AuthorFollowExample followExample = new AuthorFollowExample();
                followExample.setQueryAuthorId("notNull");
                followExample.setFromAuthorId(author.getAuthorId());
                followExample.setToAuthorId(svideoVideo.getAuthorId());
                List<AuthorFollow> followList = authorFollowModel.getAuthorFollowList(followExample, null);
                if (!CollectionUtils.isEmpty(followList)) {
                    isFollow = true;
                }
                //查询作者信息
                author = authorModel.getAuthorByAuthorId(svideoVideo.getAuthorId());
                AssertUtil.notNull(author, "获取作者信息为空，请重试");
                //设置默认头像
                if (StringUtil.isEmpty(author.getMemberAvatar())) {
                    author.setMemberAvatar(defaultProfile);
                }
            }
        }
        pageVO.setVideoInfo(videoInfoVO);

        VideoPlayPageVO.AuthorInfoVO authorInfoVO = new VideoPlayPageVO.AuthorInfoVO(author);
        authorInfoVO.setIsFollow(isFollow);
        pageVO.setAuthorInfo(authorInfoVO);

        //查询视频下的商品列表
        VideoGoodsExample example = new VideoGoodsExample();
        example.setVideoId(videoId);
        example.setVideoType(VideoConst.VIDEO_TYPE_1);
        List<VideoGoods> list = videoGoodsModel.getVideoGoodsList(example, null);
        List<VideoPlayPageVO.GoodsInfoVO> goodsList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(videoGoods -> {
                Goods goods = goodsModel.getGoodsByGoodsId(videoGoods.getGoodsId());
                AssertUtil.notNull(goods, "商品不存在");
                goodsList.add(new VideoPlayPageVO.GoodsInfoVO(goods));
            });
        }
        pageVO.setGoodsList(goodsList);
        return SldResponse.success(pageVO);
    }

    @ApiOperation("关注作者")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorId", value = "作者id", required = true, paramType = "query")
    })
    @PostMapping("followAuthor")
    public JsonResult followAuthor(HttpServletRequest request, Integer authorId) {
        Member member = UserUtil.getUser(request, Member.class);
        //查询作者信息
        Author author = authorModel.getAuthorByMemberId(member.getMemberId());

        String followAuthor = authorModel.followAuthor(authorId, author);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("FansNum", followAuthor);
        return SldResponse.success("关注成功", dataMap);
    }

    @ApiOperation("取消关注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorId", value = "作者id", required = true, paramType = "query")
    })
    @PostMapping("cancelFollow")
    public JsonResult cancelFollow(HttpServletRequest request, Integer authorId) {
        Member member = UserUtil.getUser(request, Member.class);
        //查询作者信息
        Author author = authorModel.getAuthorByMemberId(member.getMemberId());

        String cancelFollow = authorModel.cancelFollow(authorId, author);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("FansNum", cancelFollow);
        return SldResponse.success("取消关注成功", dataMap);
    }

    @ApiOperation("视频点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query")
    })
    @PostMapping("clickPraise")
    public JsonResult clickPraise(HttpServletRequest request, Integer videoId) {
        Member member = UserUtil.getUser(request, Member.class);
        //查询作者信息
        Author author = authorModel.getAuthorByMemberId(member.getMemberId());

        String clickPraise = svideoVideoModel.clickPraise(videoId, author);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("likeNum", clickPraise);
        return SldResponse.success("点赞成功", dataMap);
    }

    @ApiOperation("更新视频分享数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query")
    })
    @PostMapping("updateShareNum")
    public JsonResult updateShareNum(HttpServletRequest request, Integer videoId) {
        SvideoVideo svideoVideoDb = svideoVideoModel.getSvideoVideoByVideoId(videoId);
        AssertUtil.notNull(svideoVideoDb, "视频不存在");

        SvideoVideo svideoVideo = new SvideoVideo();
        svideoVideo.setVideoId(videoId);
        svideoVideo.setShareNum(svideoVideoDb.getShareNum() + 1);
        svideoVideo.setUpdateTime(new Date());
        svideoVideoModel.updateSvideoVideo(svideoVideo);
        return SldResponse.success();
    }

    @ApiOperation("更新视频点击量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query")
    })
    @PostMapping("updateClickNum")
    public JsonResult updateClickNum(HttpServletRequest request, Integer videoId) {
        SvideoVideo svideoVideoDb = svideoVideoModel.getSvideoVideoByVideoId(videoId);
        AssertUtil.notNull(svideoVideoDb, "视频不存在");

        SvideoVideo svideoVideo = new SvideoVideo();
        svideoVideo.setVideoId(videoId);
        svideoVideo.setClickNum(svideoVideoDb.getClickNum() + 1);
        svideoVideo.setUpdateTime(new Date());
        svideoVideoModel.updateSvideoVideo(svideoVideo);
        return SldResponse.success();
    }

    @ApiOperation("更新视频商品点击量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true),
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true)
    })
    @PostMapping("updateGoodsClickNum")
    public JsonResult updateGoodsClickNum(HttpServletRequest request, Integer videoId, Long goodsId) {
        VideoGoodsExample example = new VideoGoodsExample();
        example.setVideoId(videoId);
        example.setGoodsId(goodsId);
        List<VideoGoods> videoGoodsList = videoGoodsModel.getVideoGoodsList(example, null);
        AssertUtil.notEmpty(videoGoodsList, "视频商品不存在");
        VideoGoods videoGoodsDb = videoGoodsList.get(0);

        VideoGoods videoGoods = new VideoGoods();
        videoGoods.setVideoGoodsId(videoGoodsDb.getVideoGoodsId());
        videoGoods.setClickNum(videoGoodsDb.getClickNum() + 1);
        videoGoodsModel.updateVideoGoods(videoGoods);
        return SldResponse.success();
    }

    @ApiOperation("删除视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query")
    })
    @PostMapping("delVideo")
    public JsonResult delVideo(HttpServletRequest request, Integer videoId) {
        svideoVideoModel.deleteSvideoVideo(videoId);
        return SldResponse.success("删除成功");
    }

    @ApiOperation("发布视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "短视频类型:1-视频;2-图文", defaultValue = "1"),
            @ApiImplicitParam(name = "videoPath", value = "视频地址(短视频必传)"),
            @ApiImplicitParam(name = "videoName", value = "视频名称", required = true),
            @ApiImplicitParam(name = "videoImage", value = "封面(图片)", required = true),
            @ApiImplicitParam(name = "videoThemeImage", value = "主题封面(短视频必传)"),
            @ApiImplicitParam(name = "introduction", value = "视频简介", required = true),
            @ApiImplicitParam(name = "content", value = "正文(图文必传)"),
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true),
            @ApiImplicitParam(name = "imageInfo", value = "图片信息(json存储)", required = true),
            @ApiImplicitParam(name = "width", value = "宽度", required = true),
            @ApiImplicitParam(name = "height", value = "高度", required = true),
            @ApiImplicitParam(name = "goodsIds", value = "商品id集合，用逗号隔开")
    })
    @PostMapping("releaseVideo")
    public JsonResult releaseVideo(HttpServletRequest request, String videoPath, String videoName, String videoImage,
                                   String videoThemeImage, String introduction, Integer labelId, String goodsIds,
                                   Integer type, String content, Integer width, Integer height, String imageInfo) {
        VideoSetting setting = videoSettingModel.getVideoSettingBySettingName("video_switch");
        if (setting.getValue().equals(VideoConst.VIDEO_SWITCH_0)
                && !StringUtil.isNullOrZero(type) && type == VideoConst.SVIDEO_TYPE_1) {
            return SldResponse.moduleDisabled("短视频模块未开启");
        }

        Member member = UserUtil.getUser(request, Member.class);
        //查询作者信息
        Author author = authorModel.getAuthorByMemberId(member.getMemberId());
        //查询配置是否开启发布权限审核开关
        if ("1".equals(videoSettingModel.getVideoSettingBySettingName("member_review_switch").getValue())
                && author.getRoleType() == VideoConst.ROLE_TYPE_1) {
            AssertUtil.isTrue(author.getPermissionState() == VideoConst.PERMISSION_STATE_0, "您无权操作，请申请发布权限");
            AssertUtil.isTrue(author.getPermissionState() == VideoConst.PERMISSION_STATE_1, "审核中，请耐心等待");
            AssertUtil.isTrue(author.getPermissionState() == VideoConst.PERMISSION_STATE_3, "审核失败，" + author.getRemark());
        }
        if (author.getPermissionState() == VideoConst.PERMISSION_STATE_4) {
            return SldResponse.fail("您已被禁止发布作品：" + author.getRemark() + "，请联系平台管理员");
        }

        SvideoVideo svideoVideo = new SvideoVideo();
        svideoVideo.setVideoName(videoName);
        svideoVideo.setVideoImage(videoImage);
        if (!StringUtil.isNullOrZero(type) && type == VideoConst.SVIDEO_TYPE_2) {
            AssertUtil.notEmpty(videoImage, "图片不能为空");
            AssertUtil.notEmpty(content, "正文不能为空");

            svideoVideo.setVideoThemeImage(videoImage.split(",")[0]);
            svideoVideo.setVideoContent(content);
            svideoVideo.setVideoType(VideoConst.SVIDEO_TYPE_2);
        } else {
            AssertUtil.notEmpty(videoPath, "视频不能为空");
            AssertUtil.notEmpty(videoThemeImage, "主题封面不能为空");

            svideoVideo.setVideoPath(videoPath);
            svideoVideo.setVideoThemeImage(videoThemeImage);
            svideoVideo.setVideoType(VideoConst.SVIDEO_TYPE_1);
        }
        svideoVideo.setIntroduction(introduction);
        svideoVideo.setLabelId(labelId);
        svideoVideo.setImageInfo(imageInfo);
        svideoVideo.setWidth(width);
        svideoVideo.setHeight(height);
        svideoVideo.setCreateTime(new Date());
        //查询配置是否开启短视频审核开关,没有的话直接审核通过
        if ("0".equals(videoSettingModel.getVideoSettingBySettingName("video_audit_switch").getValue())) {
            svideoVideo.setState(VideoConst.STATE_2);
        }
        svideoVideoModel.saveSvideoVideo(svideoVideo, author, goodsIds);
        return SldResponse.success("发布成功");
    }

    @ApiOperation("重新编辑视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true),
            @ApiImplicitParam(name = "videoPath", value = "视频地址(短视频必传)"),
            @ApiImplicitParam(name = "videoName", value = "视频名称", required = true),
            @ApiImplicitParam(name = "videoImage", value = "封面(图片)", required = true),
            @ApiImplicitParam(name = "videoThemeImage", value = "主题封面(短视频必传)"),
            @ApiImplicitParam(name = "introduction", value = "视频简介", required = true),
            @ApiImplicitParam(name = "content", value = "正文(图文必传)"),
            @ApiImplicitParam(name = "labelId", value = "标签id", required = true),
            @ApiImplicitParam(name = "imageInfo", value = "图片信息(json存储)", required = true),
            @ApiImplicitParam(name = "width", value = "宽度", required = true),
            @ApiImplicitParam(name = "height", value = "高度", required = true),
            @ApiImplicitParam(name = "goodsIds", value = "商品id集合，用逗号隔开")
    })
    @PostMapping("editVideo")
    public JsonResult editVideo(HttpServletRequest request, Integer videoId, String videoPath, String videoName,
                                String videoImage, String videoThemeImage, String introduction, Integer labelId,
                                String goodsIds, String content, Integer width, Integer height, String imageInfo) {
        Member member = UserUtil.getUser(request, Member.class);
        //查询作者信息
        Author author = authorModel.getAuthorByMemberId(member.getMemberId());
        if (author.getPermissionState() == VideoConst.PERMISSION_STATE_4) {
            return SldResponse.fail("您已被禁止发布作品：" + author.getRemark() + "，请联系平台管理员");
        }

        SvideoVideo svideoVideo = new SvideoVideo();
        svideoVideo.setVideoId(videoId);
        svideoVideo.setVideoPath(videoPath);
        svideoVideo.setVideoName(videoName);
        svideoVideo.setVideoImage(videoImage);
        svideoVideo.setVideoThemeImage(videoThemeImage);
        svideoVideo.setIntroduction(introduction);
        svideoVideo.setLabelId(labelId);
        svideoVideo.setImageInfo(imageInfo);
        svideoVideo.setWidth(width);
        svideoVideo.setHeight(height);
        svideoVideo.setState(VideoConst.STATE_1);
        svideoVideo.setRemark("");
        svideoVideo.setCreateTime(new Date());
        svideoVideo.setVideoContent(content);
        svideoVideoModel.updateSvideoVideo(svideoVideo, author, goodsIds);
        return SldResponse.success("编辑成功");
    }

    @ApiOperation("短视频详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true)
    })
    @GetMapping("detail")
    public JsonResult<SVideoDetailVO> detail(HttpServletRequest request, Integer videoId) {
        AssertUtil.notNullOrZero(videoId, "视频id不能为空");
        //查询短视频信息
        SvideoVideo svideoVideo = svideoVideoModel.getSvideoVideoByVideoId(videoId);
        AssertUtil.notNull(svideoVideo, "短视频不存在");

        SVideoDetailVO detailVO = new SVideoDetailVO(svideoVideo);
        VideoGoodsExample example = new VideoGoodsExample();
        example.setVideoId(videoId);
        example.setVideoType(VideoConst.VIDEO_TYPE_1);
        example.setOrderBy("video_goods_id ASC");
        List<VideoGoods> list = videoGoodsModel.getVideoGoodsList(example, null);
        List<VideoGoodsVO> goodsList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(videoGoods -> {
                goodsList.add(new VideoGoodsVO(videoGoods));
            });
        }
        detailVO.setGoodsList(goodsList);
        return SldResponse.success(detailVO);
    }

    @ApiOperation("短视频标签列表")
    @GetMapping("labelList")
    public JsonResult<PageVO<VideoLabelVO>> labelList(HttpServletRequest request) {
        SvideoLabelExample example = new SvideoLabelExample();
        example.setIsShow(VideoConst.IS_SHOW_1);
        example.setOrderBy("sort asc, create_time desc");
        List<SvideoLabel> list = svideoLabelModel.getSvideoLabelList(example, null);
        List<VideoLabelVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(label -> {
                vos.add(new VideoLabelVO(label));
            });
        }
        return SldResponse.success(new PageVO<>(vos, null));
    }

    @ApiOperation("短视频列表(上下滑动使用)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "标签id", paramType = "query"),
            @ApiImplicitParam(name = "videoId", value = "视频id", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("videoList")
    public JsonResult<PageVO<VideoListVO>> videoList(HttpServletRequest request, Integer labelId, Integer videoId) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoVideoExample example = new SvideoVideoExample();
        example.setLabelId(labelId);
        example.setVideoIdNotEquals(videoId);
        example.setVideoType(VideoConst.SVIDEO_TYPE_1);
        example.setState(VideoConst.STATE_2);
        example.setOrderBy("create_time desc ");
        List<SvideoVideo> list = svideoVideoModel.getSvideoVideoList(example, pager);
        List<VideoListVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(video -> {
                vos.add(new VideoListVO(video));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }
}
