package com.cyksj.service.click.impl;

import cn.hutool.core.util.StrUtil;
import com.cyksj.dto.UserSharedDto;
import com.cyksj.mapper.DistributePopularizeClickRecordMapper;
import com.cyksj.mapper.UserDistributePosterRecordMapper;
import com.cyksj.model.entity.DistributePopularizeClickRecord;
import com.cyksj.model.entity.UserDistributePosterRecord;
import com.cyksj.model.request.DistributePopularizeClickReq;
import com.cyksj.service.click.UserClickRecordService;
import com.cyksj.service.user.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 项目名: yhlxj
 * 文件名: UserClickRecordServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/8/14 18:53
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserClickRecordServiceImpl implements UserClickRecordService {

	private final DistributePopularizeClickRecordMapper distributePopularizeClickRecordMapper;

	private final UserService userService;

	private final UserDistributePosterRecordMapper userDistributePosterRecordMapper;


	@Override
	public void recordDistributePopularize(Long userId, DistributePopularizeClickReq clickReq) {
		String showId = clickReq.getDsCode();
		log.info("点击用户分销链接:{} 进入官网", showId);
		if (StrUtil.isEmpty(showId) || showId.length() > 10) {
			return;
		}
		Long rid = clickReq.getRid();
		DistributePopularizeClickRecord clickRecord = new DistributePopularizeClickRecord();
		if (rid != null) {
			log.info("点击用户推广海报：{}进入官网", rid);
			UserDistributePosterRecord distributePosterRecord = userDistributePosterRecordMapper.selectById(rid);
			if (distributePosterRecord != null) {
				clickRecord.setSharedId(distributePosterRecord.getUserId());
			}
		}
		//不存在推广海报
		if (rid == null) {
			UserSharedDto popularizeDto = userService.getUserShredDtoByDsCode(showId);
			if (popularizeDto != null) {
				clickRecord.setSharedId(popularizeDto.getSharedId());
				clickRecord.setPopularizeId(popularizeDto.getDsPopularizeId());
			}
		}
		clickRecord.setUserId(userId);
		clickRecord.setShowId(showId);
		clickRecord.setRid(rid);
		distributePopularizeClickRecordMapper.insert(clickRecord);
	}
}
