package com.cyksj.service.home.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.GoodsClickUserRecordMapper;
import com.cyksj.mapper.GoodsDonCategoryMapper;
import com.cyksj.mapper.GoodsDonMapper;
import com.cyksj.mapper.InformationClickUserRecordMapper;
import com.cyksj.mapper.manage.home.HomeManagementConfigMapper;
import com.cyksj.mapper.manage.home.HomeManagementConfigUserClickRecordMapper;
import com.cyksj.model.entity.*;
import com.cyksj.model.request.ClickRecordReq;
import com.cyksj.service.home.ClickRecordFrontService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/*
 *项目名: netflix
 *文件名: ClickRecordFrontServiceImpl
 *创建者: JavaZou
 *创建时间:2023/8/17 18:08
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ClickRecordFrontServiceImpl implements ClickRecordFrontService {

	private final HomeManagementConfigMapper homeManagementConfigMapper;

	private final HomeManagementConfigUserClickRecordMapper homeManagementConfigUserClickRecordMapper;

	private final GoodsDonMapper goodsDonMapper;

	private final GoodsDonCategoryMapper goodsDonCategoryMapper;

	private final GoodsClickUserRecordMapper goodsClickUserRecordMapper;

	private final InformationClickUserRecordMapper informationClickUserRecordMapper;


	@Override
	public void click(Long userId, ClickRecordReq request) {
		Assert.notNull(request.getType(), "参数错误");
		if (request.getType() == 1) {
			clickHomePage(userId, request.getConfigId());
			return;
		}
		if (request.getType() == 2) {
			clickGoodsCategoryOrGoods(userId, request.getGoodsId(), request.getCategory(), request.getEnvType());
			return;
		}
		if (request.getType() == 3) {
			clickInformation(userId,request.getGoodsId(),request.getInfoId());
		}
	}

	public void clickHomePage(Long userId, Long configId) {
		Assert.notNull(configId, "参数错误");
		HomeManagementConfig homeManagementConfig = homeManagementConfigMapper.selectById(configId);
		if (homeManagementConfig == null) {
			return;
		}
		HomeManagementConfigUserClickRecord clickRecord = new HomeManagementConfigUserClickRecord();
		clickRecord.setConfigId(configId);
		clickRecord.setUserId(userId);
		homeManagementConfigUserClickRecordMapper.insert(clickRecord);
	}

	public void clickGoodsCategoryOrGoods(Long userId, Long goodsId, Long category, GoodsClickUserRecord.EnvType envType) {
		if (goodsId == null && category == null) {
			throw BusinessRuntimeException.getInstance("参数错误");
		}
		GoodsClickUserRecord goodsClickUserRecord;
		if (goodsId != null) {
			GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
			if (goodsDon != null) {
				goodsClickUserRecord = new GoodsClickUserRecord();
				goodsClickUserRecord.setUserId(userId);
				goodsClickUserRecord.setGoodsId(goodsId);
				goodsClickUserRecord.setEnvType(envType);
				goodsClickUserRecordMapper.insert(goodsClickUserRecord);
				return;
			}
		}

		if (category != null) {
			GoodsDonCategory goodsDonCategory = goodsDonCategoryMapper.selectById(category);
			if (goodsDonCategory != null) {
				goodsClickUserRecord = new GoodsClickUserRecord();
				goodsClickUserRecord.setUserId(userId);
				goodsClickUserRecord.setCategory(category);
				goodsClickUserRecordMapper.insert(goodsClickUserRecord);
				return;
			}
		}
	}
	public void clickInformation(Long userId,Long goodsId,Long infoId){
		if (infoId == null && goodsId == null) {
			throw BusinessRuntimeException.getInstance("参数错误");
		}
		InformationClickUserRecord record = new InformationClickUserRecord();
		record.setUserId(userId);
		record.setGoodsId(goodsId);
		record.setInfoId(infoId);
		informationClickUserRecordMapper.insert(record);
	}
}
