package com.cyksj.service.nano.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.constant.Constant;
import com.cyksj.mapper.GroupsMapper;
import com.cyksj.mapper.GroupsRelationMapper;
import com.cyksj.mapper.NanoUserQuotaMapper;
import com.cyksj.mapper.vip.VipUserMapper;
import com.cyksj.model.entity.GroupsRelation;
import com.cyksj.model.entity.GroupsTrips;
import com.cyksj.model.entity.NanoUserQuota;
import com.cyksj.model.entity.VipUser;
import com.cyksj.model.manage.views.GroupsRelationView;
import com.cyksj.model.views.NanoUserQuotaInfoView;
import com.cyksj.service.nano.NanoService;
import com.cyksj.service.user.UserBindRelationService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Nano服务实现
 */
@Service
@RequiredArgsConstructor
public class NanoServiceImpl implements NanoService {
	private final UserBindRelationService userBindRelationService;

	private final NanoUserQuotaMapper nanoUserQuotaMapper;

	private final GroupsRelationMapper groupsRelationMapper;

	private final GroupsMapper groupsMapper;

	private final VipUserMapper vipUserMapper;

	private final BeanSearcher beanSearcher;
	@Override
	public NanoUserQuotaInfoView getUserSubsInfo(long userId) {
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
		VipUser vipUser = vipUserMapper.selectOne(Wrappers.lambdaQuery(VipUser.class).in(VipUser::getUserId, userIdList).gt(VipUser::getExpiryTime, DateTime.now()));
		Boolean isVip = vipUser != null;
		NanoUserQuota nanoUserQuota = null;
		if (isVip) {
			GroupsRelationView groupsRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
					.field(GroupsRelationView::getIsVip, Boolean.TRUE)
					.field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
					.field(GroupsRelationView::getGoodsId, Constant.NANO_GOODS_ID)
					.build());
			if (groupsRelationView != null) {
				nanoUserQuota = nanoUserQuotaMapper.selectOne(Wrappers.lambdaQuery(NanoUserQuota.class).eq(NanoUserQuota::getRelationId, groupsRelationView.getId()).last("limit 1"));
			}
		}
		if (nanoUserQuota == null) {
			nanoUserQuota = nanoUserQuotaMapper.selectOne(Wrappers.lambdaQuery(NanoUserQuota.class).in(NanoUserQuota::getUid, userIdList).orderByDesc(NanoUserQuota::getTotalQuota).orderByDesc(NanoUserQuota::getRemainingQuota).last("limit 1"));
		}
		if (nanoUserQuota != null) {
			NanoUserQuotaInfoView nanoUserQuotaInfoView = new NanoUserQuotaInfoView();
			nanoUserQuotaInfoView.setIsVip(vipUser != null);
			GroupsRelation relation = groupsRelationMapper.selectById(nanoUserQuota.getRelationId());
			GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
			BeanUtil.copyProperties(nanoUserQuota, nanoUserQuotaInfoView);
			nanoUserQuotaInfoView.setSkuId(groupsTrips.getSkuId());
			return nanoUserQuotaInfoView;
		}
		return null;
	}

	@Override
	public Long getUserTokenByNanoToken(String usertoken) {
		NanoUserQuota nanoUserQuota = nanoUserQuotaMapper.selectOne(Wrappers.lambdaQuery(NanoUserQuota.class).eq(NanoUserQuota::getUserToken, usertoken).last("limit 1"));
		if (nanoUserQuota != null) {
			return nanoUserQuota.getUid();
		}
		return null;
	}
}