package com.bjtdba.userCenter.service.impl;

import com.bjtdba.entity.userCenter.UserVideo;
import com.bjtdba.entity.userCenter.UserVideoBrowse;
import com.bjtdba.userCenter.dao.UserVideoBrowseDao;
import com.bjtdba.userCenter.dao.UserVideoDao;
import com.bjtdba.userCenter.service.UserVideoBrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户浏览记录表(UserVideoBrowse)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 14:14:10
 */
@Service("userVideoBrowseService")
public class UserVideoBrowseServiceImpl implements UserVideoBrowseService {
    @Resource
    private UserVideoBrowseDao userVideoBrowseDao;

    @Autowired
    private UserVideoDao userVideoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param bid 主键
     * @return 实例对象
     */
    @Override
    public UserVideoBrowse queryById(Integer bid) {
        return this.userVideoBrowseDao.queryById(bid);
    }

    /**
     * 分页查询
     *
     * @param userVideoBrowse 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<UserVideoBrowse> queryByPage(UserVideoBrowse userVideoBrowse, PageRequest pageRequest) {
        long total = this.userVideoBrowseDao.count(userVideoBrowse);
        return new PageImpl<>(this.userVideoBrowseDao.queryAllByLimit(userVideoBrowse, pageRequest), pageRequest, total);
    }

    public  long counts(UserVideoBrowse userVideoBrowse){
        return this.userVideoBrowseDao.count(userVideoBrowse);
    }
    /**
     * 新增数据
     *
     * @param userVideoBrowse 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(UserVideoBrowse userVideoBrowse) {
        this.userVideoBrowseDao.insert(userVideoBrowse);
        UserVideoBrowse queryB=new UserVideoBrowse();
        queryB.setVideoId(userVideoBrowse.getVideoId());
        long b_num=userVideoBrowseDao.count(queryB);
        UserVideo upUV=new UserVideo();
        upUV.setId(userVideoBrowse.getVideoId());
        upUV.setBrowse_num(b_num);
        userVideoDao.update(upUV);

    }

    /**
     * 修改数据
     *
     * @param userVideoBrowse 实例对象
     * @return 实例对象
     */
    @Override
    public UserVideoBrowse update(UserVideoBrowse userVideoBrowse) {
        this.userVideoBrowseDao.update(userVideoBrowse);
        return this.queryById(userVideoBrowse.getBid());
    }

    /**
     * 通过主键删除数据
     *
     * @param bid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer bid) {
        return this.userVideoBrowseDao.deleteById(bid) > 0;
    }
}
