package com.cyksj.service.vip.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.GroupsRelationMapper;
import com.cyksj.mapper.manage.cms.CmsUserMapper;
import com.cyksj.mapper.vip.VipGoodsSkuMapper;
import com.cyksj.mapper.vip.VipUserMapper;
import com.cyksj.mapper.vip.VipUserOperateLogMapper;
import com.cyksj.model.entity.*;
import com.cyksj.model.request.OpenVipReq;
import com.cyksj.service.relation.GroupRelationFrontService;
import com.cyksj.service.user.UserBindRelationService;
import com.cyksj.service.vip.CmsVipUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目名: yhlxj2
 * 文件名: CmsVipUserServiceImpl
 * 创建者: JavaZou
 * 创建时间:2025/6/10 10:56
 */
@Service
@RequiredArgsConstructor
public class CmsVipUserServiceImpl implements CmsVipUserService {
	private final VipUserMapper vipUserMapper;

	private final UserBindRelationService userBindRelationService;

	private final CmsUserMapper cmsUserMapper;

	private final VipGoodsSkuMapper vipGoodsSkuMapper;

	private final GroupRelationFrontService groupRelationFrontService;

	private final VipUserOperateLogMapper vipUserOperateLogMapper;

	private final GroupsRelationMapper groupsRelationMapper;

	@Override
	public void openVip(OpenVipReq openVipReq) {
		Long userId = openVipReq.getUserId();
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		VipUser vipUser = vipUserMapper.selectOne(Wrappers.lambdaQuery(VipUser.class)
				.in(VipUser::getUserId, userIdList).last("limit 1"));
		CmsUser cmsUser = cmsUserMapper.selectById(openVipReq.getAdminId());
		if (vipUser != null) {
			if (vipUser.getExpiryTime().after(DateTime.now())) {
				throw BusinessRuntimeException.getInstance("相关联账号已开通会员");
			}
		}
		if (vipUser == null) {
			vipUser = new VipUser();
		}
		DateTime now = DateTime.now();
		Integer days = openVipReq.getDays();
		vipUser.setUserId(userId);
		vipUser.setStartTime(now);
		vipUser.setExpiryTime(DateUtil.offsetDay(now, days));
		if (vipUser.getId() == null) {
			vipUserMapper.insert(vipUser);
		} else {
			vipUserMapper.updateById(vipUser);
		}
		//发送车票
		List<VipGoodsSku> skus = vipGoodsSkuMapper.selectList(null);
		for (VipGoodsSku sku : skus) {
			//未有抵扣 直接发送会员车票
			groupRelationFrontService.getTicket(userId, sku.getSkuId(), Boolean.TRUE);
		}
		//同步时间
		groupsRelationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
				.set(GroupsRelation::getExpiryTime, vipUser.getExpiryTime())
				.in(GroupsRelation::getUserId, userIdList)
				.eq(GroupsRelation::getIsVip, Boolean.TRUE));
		//操作记录
		VipUserOperateLog vipUserOperateLog = new VipUserOperateLog();
		vipUserOperateLog.setUserId(userId);
		vipUserOperateLog.setOperator(cmsUser != null ? cmsUser.getNickname() : StrUtil.EMPTY);
		vipUserOperateLog.setDays(days);
		vipUserOperateLogMapper.insert(vipUserOperateLog);
	}
}
