package cn.pluss.platform.user.impl;

import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserPushId;
import cn.pluss.platform.mapper.UserPushIdMapper;
import cn.pluss.platform.user.UserPushIdService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author djh
 */
@Service
public class UserPushIdServiceImpl extends ServiceImpl<UserPushIdMapper, UserPushId> implements UserPushIdService {

    @Autowired
    private UserAppService userAppService;

    @Override
    public void setCid(Long userId, String cid) {
        LambdaQueryWrapper<UserPushId> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(UserPushId::getUserId, userId);
        UserPushId entity = baseMapper.selectOne(qWrapper);

        if (entity != null) {
            baseMapper.deleteById(entity.getId());
        }

        qWrapper.clear();
        qWrapper.eq(UserPushId::getGetuiClientId, cid);
        entity = baseMapper.selectOne(qWrapper);
        if (entity != null) {
            baseMapper.deleteById(entity.getId());
        }

        entity = new UserPushId();
        entity.setUserId(userId);
        entity.setGetuiClientId(cid);

        baseMapper.insert(entity);
    }

    @Override
    public void apiSetCid(String cid) {
        UserApp tokenUa = userAppService.checkApiToken();
        setCid(tokenUa.getUserId(), cid);
    }
}
