package com.cyksj.service.mange.ticket.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.mapper.TicketAdvertiseConfigClickRecordMapper;
import com.cyksj.mapper.TicketAdvertiseConfigMapper;
import com.cyksj.model.entity.TicketAdvertiseConfig;
import com.cyksj.model.entity.TicketAdvertiseConfigClickRecord;
import com.cyksj.service.mange.ticket.TicketAdvertiseConfigClickRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/*
 *项目名: netflix
 *文件名: TicketAdvertiseConfigClickRecordServiceImpl
 *创建者: JavaZou
 *创建时间:2023/7/12 14:01
 */
@Service
@RequiredArgsConstructor
public class TicketAdvertiseConfigClickRecordServiceImpl extends ServiceImpl<TicketAdvertiseConfigClickRecordMapper, TicketAdvertiseConfigClickRecord> implements TicketAdvertiseConfigClickRecordService {
	private final TicketAdvertiseConfigMapper ticketAdvertiseConfigMapper;

	@Override
	public void recordUserClickTicketAdvertise(long userId, Long configId) {
		TicketAdvertiseConfig ticketAdvertiseConfig = ticketAdvertiseConfigMapper.selectById(configId);
		if (ticketAdvertiseConfig == null) return;
		TicketAdvertiseConfigClickRecord record = new TicketAdvertiseConfigClickRecord();
		record.setUserId(userId);
		record.setConfigId(configId);
		this.save(record);

		updateAdvertiseClickNum(ticketAdvertiseConfig.getId(), ticketAdvertiseConfig.getClick());
	}

	public void updateAdvertiseClickNum(Long configId, Integer o_click) {
		int update = 0;
		while (update != 1) {
			TicketAdvertiseConfig ticketAdvertiseConfig = ticketAdvertiseConfigMapper.selectById(configId);
			if (ticketAdvertiseConfig == null) break;
			o_click = ticketAdvertiseConfig.getClick();
			update = ticketAdvertiseConfigMapper.update(null, Wrappers.lambdaUpdate(TicketAdvertiseConfig.class)
					.set(TicketAdvertiseConfig::getClick, o_click + 1)
					.eq(TicketAdvertiseConfig::getId, configId)
					.eq(TicketAdvertiseConfig::getClick, o_click));
		}
	}
}
