package com.bjtdba.userCenter.controller;

import com.bjtdba.entity.order.User;
import com.bjtdba.entity.userCenter.UserVideo;
import com.bjtdba.entity.userCenter.UserVideoBrowse;
import com.bjtdba.entity.userCenter.UserVideoLike;
import com.bjtdba.userCenter.service.UserService;
import com.bjtdba.userCenter.service.UserVideoBrowseService;
import com.bjtdba.userCenter.service.UserVideoLikeService;
import com.bjtdba.userCenter.service.UserVideoService;
import com.bjtdba.utils.ResponseUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户视频表(UserVideo)表控制层
 *
 * @author 何海强
 * @since 2022-11-12 09:33:20
 */
@RestController
@RequestMapping("userCenter_service/userVideo")
public class UserVideoController {
    /**
     * 服务对象
     */
    @Resource
    private UserVideoService userVideoService;

    @Autowired
    private UserVideoLikeService userVideoLikeService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserVideoBrowseService userVideoBrowseService;

    /**
     * 视频列表
     *

     * @return 查询结果
     */
    @GetMapping("/lst")
    public Object queryByPage(@RequestHeader Map<String,String> hmap,@RequestParam Map<String,String> map) {

        if (!map.containsKey("page") || !map.containsKey("limit")) {
            return ResponseUtil.fail("参数错误");
        }
        int page = Integer.parseInt(map.get("page") + "");
        int limit = Integer.parseInt(map.get("limit"));

        Integer vid=0;
        if(map.containsKey("vid")){
           vid=Integer.parseInt(map.get("vid"));
        }
        Map<String,Object> data=new HashMap<String,Object>();
        PageHelper.startPage(page, limit);
        List<UserVideo> userVideoList=userVideoService.queryList(null);
        List<UserVideo> userVideoListData=new ArrayList<UserVideo>();
        if(vid!=0){
            UserVideo userVideo=userVideoService.queryById(vid);
            userVideoListData.add(userVideo);
            for (int i=0;i<limit-1;i++){
                userVideoListData.add(userVideoList.get(i));
            }
            data.put("list",userVideoListData);
        }else{
            data.put("list",userVideoList);
        }

        return ResponseUtil.ok(data);
    }

    /**
     * 我发布的视频列表
     *

     * @return 查询结果
     */
    @GetMapping("/mylst")
    public Object querymylst(@RequestHeader Map<String,String> hmap,@RequestParam Map<String,String> map) {
        if(!hmap.containsKey("uid")){
            ResponseUtil.fail("uid 参数错误");
        }
        if (!map.containsKey("page") || !map.containsKey("limit")) {
            return ResponseUtil.fail("参数错误");
        }
        int page = Integer.parseInt(map.get("page") + "");
        int limit = Integer.parseInt(map.get("limit"));

        Integer uid=Integer.parseInt(hmap.get("uid"));

        Map<String,Object> data=new HashMap<String,Object>();
        UserVideo queryParm=new UserVideo();
        queryParm.setUid(uid);
        PageHelper.startPage(page, limit);
        List<UserVideo> userVideoList=userVideoService.queryList(queryParm);

        data.put("list",userVideoList);

        return ResponseUtil.ok(data);
    }

    /**
     * 我点过赞的视频列表
     *

     * @return 查询结果
     */
    @GetMapping("/mylikelst")
    public Object mylikelst(@RequestHeader Map<String,String> hmap,@RequestParam Map<String,String> map) {
        if(!hmap.containsKey("uid")){
            ResponseUtil.fail("uid 参数错误");
        }
        if (!map.containsKey("page") || !map.containsKey("limit")) {
            return ResponseUtil.fail("参数错误");
        }
        int page = Integer.parseInt(map.get("page") + "");
        int limit = Integer.parseInt(map.get("limit"));

        Integer uid=Integer.parseInt(hmap.get("uid"));

        Map<String,Object> data=new HashMap<String,Object>();
        UserVideo queryParm=new UserVideo();
        queryParm.setUid(uid);
        PageHelper.startPage(page, limit);
        List<UserVideo> userVideoList=userVideoService.queryAllByLike(queryParm);

        data.put("list",userVideoList);

        return ResponseUtil.ok(data);
    }

    /**
     * 视频详情
     * @return 单条数据
     */
    @GetMapping("/detail/{id}")
    public Object queryById(@RequestHeader Map<String,String> hmap,@PathVariable("id") Integer id) {
        if(!hmap.containsKey("uid")){
            ResponseUtil.fail("uid 参数错误");
        }
        Integer uid=Integer.parseInt(hmap.get("uid"));
        UserVideo userVideo=userVideoService.queryById(id);
        if(userVideo==null){
            return  ResponseUtil.fail("视频不存在");
        }
        UserVideoLike querlike=new UserVideoLike();
        User user=userService.queryByid(Integer.parseInt(userVideo.getUid()+""));
        querlike.setVideoId(userVideo.getId());
        querlike.setUid(uid);
        long  countlike=userVideoLikeService.count(querlike);
        Map<String,Object> data=new HashMap<String,Object>();
        userVideo.setAvatar(user.getAvatar());
        data.put("detail",userVideo);
        data.put("likeid=",countlike);

        //添加浏览量
        UserVideoBrowse userVideoBrowse=new UserVideoBrowse();
        userVideoBrowse.setVideoId(userVideo.getId());
        userVideoBrowse.setAddtime(new Date());
        userVideoBrowse.setUid(uid);
        userVideoBrowseService.insert(userVideoBrowse);


       return ResponseUtil.ok(data);
    }

    /**
     * 添加视频
     *
     * @return 新增结果
     */
    @PostMapping("/add")
    public Object add(@RequestHeader Map<String,String> Hmap,@RequestBody Map<String,String> map) {
        if(!Hmap.containsKey("uid")){
            return ResponseUtil.fail("uid 参数错误");
        }
        if(!map.containsKey("context")||map.containsKey("videourl")){
            return ResponseUtil.fail("参数错误");
        }
        Integer uid = Integer.parseInt(Hmap.get("uid"));
        UserVideo parmUsrvideo=new UserVideo();
        parmUsrvideo.setAddtime(new Date());
        parmUsrvideo.setUid(uid);
        parmUsrvideo.setContext(map.get("context"));
        parmUsrvideo.setVideo_url(map.get("videourl"));
        parmUsrvideo.setBrowse_num(0l);
        parmUsrvideo.setShare_num(0);
        parmUsrvideo.setIs_del(1);
        UserVideo uv=this.userVideoService.insert(parmUsrvideo);
        uv.getId();
        if(uv!=null&&uv.getId()>0){
            return ResponseUtil.ok(uv);
        }else{
            return ResponseUtil.fail("添加失败");
        }


    }


    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Object deleteById(@RequestHeader Map<String,String> Hmap,@RequestParam Map<String,String> map) {
        if(!Hmap.containsKey("uid")){
            return ResponseUtil.fail("uid 参数错误");
        }
        if(!map.containsKey("ids")){
            return ResponseUtil.fail("参数错误");
        }
        String[] ids=map.get("ids").split(",");
        boolean i=this.userVideoService.deleteById(ids);
        if(i){
            return ResponseUtil.ok();
        }else{
            return ResponseUtil.fail("删除失败");
        }

    }

}

