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

import com.slodon.b2b2c.aop.OperationLogger;
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.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.video.AuthorModel;
import com.slodon.b2b2c.model.video.SvideoVideoModel;
import com.slodon.b2b2c.model.video.VideoGoodsModel;
import com.slodon.b2b2c.model.video.VideoSettingModel;
import com.slodon.b2b2c.video.example.SvideoVideoExample;
import com.slodon.b2b2c.video.example.VideoGoodsExample;
import com.slodon.b2b2c.video.pojo.Author;
import com.slodon.b2b2c.video.pojo.SvideoVideo;
import com.slodon.b2b2c.video.pojo.VideoGoods;
import com.slodon.b2b2c.vo.video.VideoGoodsVO;
import com.slodon.b2b2c.vo.video.VideoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 = "admin-视频管理")
@RestController
@RequestMapping("v3/video/admin/video")
public class AdminVideoController extends BaseController {

    @Resource
    private SvideoVideoModel svideoVideoModel;
    @Resource
    private VideoGoodsModel videoGoodsModel;
    @Resource
    private VideoSettingModel videoSettingModel;
    @Resource
    private AuthorModel authorModel;
    @Resource
    private GoodsModel goodsModel;

    @ApiOperation("视频列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "视频类型:audit-视频审核,默认不传", paramType = "query"),
            @ApiImplicitParam(name = "videoName", value = "视频名称", paramType = "query"),
            @ApiImplicitParam(name = "memberName", value = "会员名称", paramType = "query"),
            @ApiImplicitParam(name = "memberNickname", value = "会员昵称", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "发布开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "发布结束时间", paramType = "query"),
            @ApiImplicitParam(name = "labelId", value = "标签id", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:1待审核，2正常，3拒绝，4禁止", paramType = "query"),
            @ApiImplicitParam(name = "videoType", value = "视频类型:1-视频;2-图文", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<VideoVO>> list(HttpServletRequest request, String videoName, String memberName, String memberNickname,
                                            Date startTime, Date endTime, Integer state, String type, Integer labelId, Integer videoType) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoVideoExample example = new SvideoVideoExample();
        example.setVideoNameLike(videoName);
        example.setMemberNameLike(memberName);
        example.setMemberNicknameLike(memberNickname);
        example.setCreateTimeAfter(startTime);
        example.setCreateTimeBefore(endTime);
        example.setLabelId(labelId);
        example.setVideoType(videoType);
        if (!StringUtil.isEmpty(type) && "audit".equals(type)) {
            if (!StringUtil.isNullOrZero(state)) {
                example.setState(state);
            } else {
                example.setStateIn(VideoConst.STATE_1 + "," + VideoConst.STATE_3);
            }
        } else {
            if (!StringUtil.isNullOrZero(state)) {
                example.setState(state);
            } else {
                example.setStateIn(VideoConst.STATE_2 + "," + VideoConst.STATE_4);
            }
        }
        example.setIsDelete(VideoConst.IS_DELETE_0);
        List<SvideoVideo> list = svideoVideoModel.getSvideoVideoList(example, pager);
        List<VideoVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            //key-作者id  value-作者信息
            Map<Integer, Author> map = new HashMap<>();
            list.forEach(svideoVideo -> {
                //设置默认头像
                if (StringUtil.isEmpty(svideoVideo.getMemberAvatar())) {
                    svideoVideo.setMemberAvatar(videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue());
                }
                Author videoAuthor = null;
                if (!map.containsKey(svideoVideo.getAuthorId())) {
                    //查询作者信息
                    videoAuthor = authorModel.getAuthorByAuthorId(svideoVideo.getAuthorId());
                    AssertUtil.notNull(videoAuthor, "作者不存在");

                    map.put(svideoVideo.getAuthorId(), videoAuthor);
                } else {
                    videoAuthor = map.get(svideoVideo.getAuthorId());
                }
                //无昵称,用会员账号
                if (StringUtil.isEmpty(videoAuthor.getMemberNickname())) {
                    svideoVideo.setMemberNickname(videoAuthor.getMemberName());
                } else {
                    svideoVideo.setMemberNickname(videoAuthor.getMemberNickname());
                }
                svideoVideo.setMemberName(videoAuthor.getMemberName());
                vos.add(new VideoVO(svideoVideo));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("审核")
    @OperationLogger(option = "审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true),
            @ApiImplicitParam(name = "isPass", value = "是否审核通过: true-审核通过，false-审核拒绝", required = true),
            @ApiImplicitParam(name = "remark", value = "拒绝理由")
    })
    @PostMapping("audit")
    public JsonResult audit(HttpServletRequest request, Integer videoId, boolean isPass, String remark) {
        SvideoVideo video = new SvideoVideo();
        video.setVideoId(videoId);
        if (isPass) {
            video.setState(VideoConst.STATE_2);
            video.setRemark("");
        } else {
            AssertUtil.notEmpty(remark, "请填写拒绝理由!");
            video.setState(VideoConst.STATE_3);
            video.setRemark(remark);
        }
        video.setUpdateTime(new Date());
        svideoVideoModel.updateSvideoVideo(video);
        return SldResponse.success("审核成功");
    }

    @ApiOperation("禁止显示或解除禁止视频")
    @OperationLogger(option = "禁止显示或解除禁止视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true),
            @ApiImplicitParam(name = "isShow", value = "是否显示: true-解除禁止，false-禁止显示", required = true),
            @ApiImplicitParam(name = "remark", value = "禁止理由")
    })
    @PostMapping("isShow")
    public JsonResult isShow(HttpServletRequest request, Integer videoId, boolean isShow, String remark) {
        SvideoVideo video = new SvideoVideo();
        video.setVideoId(videoId);
        if (isShow) {
            video.setState(VideoConst.STATE_2);
            video.setRemark("");
        } else {
            AssertUtil.notEmpty(remark, "请填写禁止理由!");
            video.setState(VideoConst.STATE_4);
            video.setRemark(remark);
        }
        video.setUpdateTime(new Date());
        svideoVideoModel.updateSvideoVideo(video);
        return SldResponse.success("操作成功");
    }

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

    @ApiOperation("查看商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "videoType", value = "视频类型：1-短视频；2-直播", required = true, paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("goodsList")
    public JsonResult<PageVO<VideoGoodsVO>> goodsList(HttpServletRequest request, Integer videoId, Integer videoType, String goodsName) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        VideoGoodsExample example = new VideoGoodsExample();
        example.setVideoId(videoId);
        example.setVideoType(videoType);
        example.setGoodsNameLike(goodsName);
        List<VideoGoods> list = videoGoodsModel.getVideoGoodsList(example, pager);
        List<VideoGoodsVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(videoGoods -> {
                VideoGoodsVO vo = new VideoGoodsVO(videoGoods);
                Goods goods = goodsModel.getGoodsByGoodsId(videoGoods.getGoodsId());
                AssertUtil.notNull(goods, "商品不存在");
                vo.setStoreId(goods.getStoreId());
                vo.setStoreName(goods.getStoreName());
                vo.setGoodsStock(goods.getGoodsStock());
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }
}
