package com.cyksj.service.relation.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.mapper.GroupsRelationExpiryRecordMapper;
import com.cyksj.model.entity.GoodsDon;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.entity.GroupsRelationExpiryRecord;
import com.cyksj.model.entity.OrderDon;
import com.cyksj.service.relation.GroupsRelationExpiryRecordService;
import com.cyksj.service.user.UserBindRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/*
 *项目名: netflix
 *文件名: GroupsRelationExpiryRecordServiceImpl
 *创建者: JavaZou
 *创建时间:2023/9/5 11:47
 */
@Service
@RequiredArgsConstructor
public class GroupsRelationExpiryRecordServiceImpl extends ServiceImpl<GroupsRelationExpiryRecordMapper, GroupsRelationExpiryRecord> implements GroupsRelationExpiryRecordService {
	private final UserBindRelationService userBindRelationService;

	@Override
	public void syncGroupsRelationExpiryRecord(OrderDon orderDon, GoodsDon goodsDon, GoodsDonSku sku) {
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(orderDon.getUserId(), null);
		GroupsRelationExpiryRecord groupsRelationExpiryRecord = this.getOne(Wrappers.lambdaQuery(GroupsRelationExpiryRecord.class)
				.in(GroupsRelationExpiryRecord::getUserId, userIdList)
				.eq(GroupsRelationExpiryRecord::getIsReBuy, false)
				.eq(GroupsRelationExpiryRecord::getRelationId, orderDon.getRelationId()).last("limit 1"));
		if (groupsRelationExpiryRecord == null) {
			List<GroupsRelationExpiryRecord> groupsRelationExpiryRecords = this.list(Wrappers.lambdaQuery(GroupsRelationExpiryRecord.class)
					.in(GroupsRelationExpiryRecord::getUserId, userIdList)
					.eq(GroupsRelationExpiryRecord::getIsReBuy, false)
					.eq(GroupsRelationExpiryRecord::getGoodsId, orderDon.getGoodsId()));
			if (CollUtil.isNotEmpty(groupsRelationExpiryRecords)) {
				groupsRelationExpiryRecords.forEach(data -> {
					data.setReBuyGoodsId(goodsDon.getId());
					data.setReBuyGoodsTitle(goodsDon.getTitle());
					data.setReBuySkuId(sku.getId());
					data.setReBuySpecVal(sku.getSpecVal());
					data.setReBuyOrderId(orderDon.getId());
					data.setReBuyTime(Optional.ofNullable(orderDon.getPayTime()).orElse(orderDon.getCreatedTime()));
					data.setIsReBuy(true);
				});
				this.saveOrUpdateBatch(groupsRelationExpiryRecords);
			}
		}
		if (groupsRelationExpiryRecord != null) { 
			groupsRelationExpiryRecord.setReBuyGoodsId(goodsDon.getId());
			groupsRelationExpiryRecord.setReBuyGoodsTitle(goodsDon.getTitle());
			groupsRelationExpiryRecord.setReBuySkuId(sku.getId());
			groupsRelationExpiryRecord.setReBuySpecVal(sku.getSpecVal());
			groupsRelationExpiryRecord.setReBuyOrderId(orderDon.getId());
			groupsRelationExpiryRecord.setReBuyTime(Optional.ofNullable(orderDon.getPayTime()).orElse(orderDon.getCreatedTime()));
			groupsRelationExpiryRecord.setIsReBuy(true);
			this.saveOrUpdate(groupsRelationExpiryRecord);
		}
	}
}
