package com.huimv.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.common.utils.R;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;

import com.huimv.management.dao.UserAttentionDao;
import com.huimv.management.entity.UserAttentionEntity;
import com.huimv.management.service.UserAttentionService;

/**
 * 
 *
 * @author yinhao
 * @version ${version}
 * @date 2021-07-13 15:48:41
 */
@Service("userAttentionService")
public class UserAttentionServiceImpl extends ServiceImpl<UserAttentionDao, UserAttentionEntity> implements UserAttentionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserAttentionEntity> page = this.page(
                new Query<UserAttentionEntity>().getPage(params),
                new QueryWrapper<UserAttentionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R updateAttention(Integer cameraId, Long userId) {
        LambdaQueryWrapper<UserAttentionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserAttentionEntity::getCameraId,cameraId);
        wrapper.eq(UserAttentionEntity::getUserId,userId);
        UserAttentionEntity userAttentionEntity = baseMapper.selectOne(wrapper);
        if (userAttentionEntity == null){
            userAttentionEntity = new UserAttentionEntity();
            userAttentionEntity.setCameraId(cameraId);
            userAttentionEntity.setUserId(userId);
            baseMapper.insert(userAttentionEntity);
            return R.ok("关注成功");
        }
        baseMapper.deleteById(userAttentionEntity.getId());


        return R.ok("取消关注成功");
    }
}