package com.cyksj.service.ticket.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.*;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.entity.MidjourneyUser;
import com.cyksj.model.entity.UserReceiveTrialTicketRecord;
import com.cyksj.service.groups.GroupsFuncService;
import com.cyksj.service.midjourney.MidjourneyAccountService;
import com.cyksj.service.relation.GroupRelationFrontService;
import com.cyksj.service.ticket.UserReceiveTrialTicketRecordService;
import com.cyksj.service.user.UserBindRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目名: yhlxj
 * 文件名: UserReceiveTrialTicketRecordServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/6/3 17:55
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserReceiveTrialTicketRecordServiceImpl implements UserReceiveTrialTicketRecordService {
	private final UserBindRelationService userBindRelationService;

	private final UserReceiveTrialTicketRecordMapper userReceiveTrialTicketRecordMapper;

	private final GroupsMapper groupsMapper;

	private final GroupsRelationMapper groupsRelationMapper;

	private final GoodsDonSkuMapper goodsDonSkuMapper;

	private final GroupsFuncService groupsFuncService;

	private final OrderDonMapper orderDonMapper;

	private final MidjourneyAccountService midjourneyAccountService;

	private final GroupRelationFrontService groupRelationFrontService;

	@Override
	public void sendTicketAndRecord(Long userId, Long trialSkuId) {
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		GoodsDonSku sku = goodsDonSkuMapper.selectById(trialSkuId);
		if (sku == null) {
			throw BusinessRuntimeException.getInstance("系统异常,请联系客服..");
		}
		Long goodsId = sku.getGoodsId();
		//是否购买过mj镜像
		Integer count = orderDonMapper.checkUserHasPaidMjMirror(userIdList);
		if (count > 0) {
			throw BusinessRuntimeException.getInstance("您不符合领取条件");
		}

		Integer selectCount = userReceiveTrialTicketRecordMapper.selectCount(Wrappers.lambdaQuery(UserReceiveTrialTicketRecord.class)
				.in(UserReceiveTrialTicketRecord::getUserId, userIdList)
				.eq(UserReceiveTrialTicketRecord::getSkuId, trialSkuId));
		if (selectCount > 0) {
			throw BusinessRuntimeException.getInstance("您已领取该试用车票");
		}

		try {
			UserReceiveTrialTicketRecord userReceiveTrialTicketRecord = new UserReceiveTrialTicketRecord();
			userReceiveTrialTicketRecord.setUserId(userId);
			userReceiveTrialTicketRecord.setGoodsId(goodsId);
			userReceiveTrialTicketRecord.setSkuId(trialSkuId);

			userReceiveTrialTicketRecordMapper.insert(userReceiveTrialTicketRecord);

			Long relationId = groupRelationFrontService.getTrialTicketByUserIdAndGoodsId(userId, trialSkuId, 2);
			userReceiveTrialTicketRecord.setRelationId(relationId);
			try {
				//mj user
				MidjourneyUser midjourneyUser = midjourneyAccountService.getMidjourneyUserToken(userId, relationId);
				userReceiveTrialTicketRecord.setMjUserId(midjourneyUser.getId());
			} catch (Exception e) {
			}
			userReceiveTrialTicketRecordMapper.updateById(userReceiveTrialTicketRecord);

		} catch (DuplicateKeyException e) {
			throw BusinessRuntimeException.getInstance("您已领取该试用车票");
		}
	}

	@Override
	public Boolean isReceiveTrialTicket(long userId, Long trialSkuId) {
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		//是否购买过mj镜像
		Integer count = orderDonMapper.checkUserHasPaidMjMirror(userIdList);
		if (count > 0) {
			return true;
		}
		Integer selectCount = userReceiveTrialTicketRecordMapper.selectCount(Wrappers.lambdaQuery(UserReceiveTrialTicketRecord.class)
				.in(UserReceiveTrialTicketRecord::getUserId, userIdList)
				.eq(UserReceiveTrialTicketRecord::getSkuId, trialSkuId));
		return selectCount > 0 ? true : false;
	}
}
