package com.bjtdba.userCenter.controller;

import com.bjtdba.entity.userCenter.UserVideo;
import com.bjtdba.entity.userCenter.UserVideoShare;
import com.bjtdba.userCenter.service.UserVideoService;
import com.bjtdba.userCenter.service.UserVideoShareService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 用户分享记录表(UserVideoShare)表控制层
 *
 * @author 何海强
 * @since 2022-11-16 17:20:05
 */
@RestController
@RequestMapping("userCenter_service/userVideoShare")
public class UserVideoShareController {
    /**
     * 服务对象
     */
    @Resource
    private UserVideoShareService userVideoShareService;
    @Autowired
    private UserVideoService userVideoService;


    @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("vid")){
            return ResponseUtil.fail("参数错误");
        }
        Integer vid=Integer.parseInt(map.get("vid"));
        Integer  uid=Integer.parseInt(Hmap.get("uid"));

        UserVideoShare queryparm=new UserVideoShare();
        queryparm.setAddtime(new Date());
        queryparm.setVideoId(vid);
        queryparm.setUid(uid);
        UserVideoShare res=userVideoShareService.insert(queryparm);
        if(res!=null&&res.getSid()>0){
            UserVideo userVideo=userVideoService.queryById(res.getVideoId());
            UserVideo objsave=new UserVideo();
            objsave.setId(userVideo.getId());
            objsave.setShare_num(userVideo.getShare_num()+1);
            userVideoService.update(objsave);
        }
        return ResponseUtil.ok();
    }

}

