package com.qs.mp.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.channel.domain.ChannelUserRel;
import com.qs.mp.channel.mapper.ChannelUserRelMapper;
import com.qs.mp.channel.service.IChannelUserRelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 经销商的用户 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-02
 */
@Service
public class ChannelUserRelServiceImpl extends ServiceImpl<ChannelUserRelMapper, ChannelUserRel> implements IChannelUserRelService {

	@Override
	public int getChannelTotalUserCnt(String channelNo) {
		return getBaseMapper().getChannelTotalUserCnt(channelNo);
	}

	@Override
	public int getChannelNewUserCnt(String channelNo, int days) {
		return getBaseMapper().getChannelNewUserCnt(channelNo, days == 1?0:days);
	}

	@Override
	public void bindUser(Long userId, Long channelId) {
		// 经销商id为空则不进行绑定
		if (Objects.isNull(channelId) || channelId == 0) {
			return;
		}
		// 线下票如果用户没被绑定，则保存经销商用户绑定关系
		ChannelUserRel channelUserRel = this.getOne(
				new LambdaQueryWrapper<ChannelUserRel>()
						.eq(ChannelUserRel::getUserId, userId));
		if (null == channelUserRel) {
			channelUserRel = new ChannelUserRel();
			channelUserRel.setChannelId(channelId);
			channelUserRel.setUserId(userId);
			this.save(channelUserRel);
		}
	}
}
