package com.bjtdba.userCenter.controller;

import com.bjtdba.entity.userCenter.UserVideoComment;
import com.bjtdba.userCenter.service.UserVideoCommentService;
import com.bjtdba.utils.ResponseUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 视频评论表(UserVideoComment)表控制层
 *
 * @author 何海强
 * @since 2022-11-16 10:42:03
 */
@RestController
@RequestMapping("userCenter_service/userVideoComment")
public class UserVideoCommentController {
    /**
     * 服务对象
     */
    @Resource
    private UserVideoCommentService userVideoCommentService;

    @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("comment_text")||!map.containsKey("vid")){
            return ResponseUtil.fail("参数错误");
        }
        String text=map.get("comment_text");
        Integer uid=Integer.parseInt(Hmap.get("uid"));
        Integer vid=Integer.parseInt(map.get("vid"));
        UserVideoComment userVideoComment =new UserVideoComment();
        userVideoComment.setCommentText(text);
        userVideoComment.setUid(uid);
        userVideoComment.setAddtime(new Date());
        userVideoComment.setVid(vid);
        UserVideoComment res=userVideoCommentService.insert(userVideoComment);
        if(res!=null&&res.getUid()>0){
            return ResponseUtil.ok();
        }else{
            return ResponseUtil.fail("添加错误");
        }
    }


    @PostMapping("lst")
    public Object lst(@RequestHeader Map<String,String> Hmap, @RequestBody Map<String,String> map){
        if(!map.containsKey("vid")){
            return ResponseUtil.fail("参数错误");
        }
        Integer vid=Integer.parseInt(map.get("vid"));
        UserVideoComment querParm=new UserVideoComment();
        querParm.setVid(vid);
        long counts=userVideoCommentService.counts(querParm);
        if (!map.containsKey("page") || !map.containsKey("limit")) {
            return ResponseUtil.fail("参数错误");
        }
        int page = Integer.parseInt(map.get("page") + "");
        int limit = Integer.parseInt(map.get("limit"));

        PageHelper.startPage(page, limit);
        List<UserVideoComment> userVideoCommentList=userVideoCommentService.quertList(querParm);

        Map<String,Object> data=new HashMap<String,Object>();
        data.put("list",userVideoCommentList);
        data.put("counts",counts);
        return ResponseUtil.ok(data);
    }


}

