package com.cyksj.service.order.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.OrderCommentMapper;
import com.cyksj.mapper.OrderCommentRewardConfigMapper;
import com.cyksj.model.entity.OrderComment;
import com.cyksj.model.entity.OrderCommentRewardConfig;
import com.cyksj.model.entity.UserBalanceSourceRecord;
import com.cyksj.service.order.OrderDonCommentService;
import com.cyksj.service.user.UserBenefitsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/*
 *项目名: netflix
 *文件名: OrderDonCommentServiceImpl
 *创建者: JavaZou
 *创建时间:2023/7/28 14:41
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OrderDonCommentServiceImpl implements OrderDonCommentService {
	private final OrderCommentMapper orderCommentMapper;

	private final OrderCommentRewardConfigMapper orderCommentRewardConfigMapper;

	private final UserBenefitsService userBenefitsService;

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void verifyCommentStatus(Long id, OrderComment.VerifyStatus verifyStatus) {
		if (id == null || verifyStatus == null) {
			throw BusinessRuntimeException.getInstance("审核参数错误");
		}
		OrderComment orderComment = orderCommentMapper.selectById(id);
		if (orderComment == null) throw BusinessRuntimeException.getInstance("评价不存在");
		OrderComment.VerifyStatus dbVerifyStatus = orderComment.getVerifyStatus();

		if (OrderComment.VerifyStatus.verifying != orderComment.getVerifyStatus()) {
			throw BusinessRuntimeException.getInstance("该评价已审核过");
		}
		if (verifyStatus == OrderComment.VerifyStatus.verifying) {
			throw BusinessRuntimeException.getInstance("审核状态错误");
		}

		int update = orderCommentMapper.update(null, Wrappers.lambdaUpdate(OrderComment.class)
				.set(OrderComment::getVerifyStatus, verifyStatus)
				.eq(OrderComment::getId, id)
				.eq(OrderComment::getVerifyStatus, dbVerifyStatus));
		if (update == 1) {
			//审核成功 是否配置对应余额奖励
			if (OrderComment.VerifyStatus.success == verifyStatus) {
				Long goodsId = orderComment.getGoodsId();
				OrderCommentRewardConfig orderCommentRewardConfig = orderCommentRewardConfigMapper.selectOne(Wrappers.lambdaQuery(OrderCommentRewardConfig.class)
						.eq(OrderCommentRewardConfig::getGoodsId, goodsId).last("limit 1"));
				if (orderCommentRewardConfig != null) {
					BigDecimal balance = orderCommentRewardConfig.getBalance();
					userBenefitsService.addUserBalance(orderComment.getUserId(), balance, UserBalanceSourceRecord.Source.rd_comment, 0l, orderComment.getOrderId(), UserBalanceSourceRecord.Source.rd_comment.getDesc(), true);
					log.info("审核订单:{}评价成功,发放余额:{}", orderComment.getOrderId(), balance.divide(BigDecimal.valueOf(100)));
					orderCommentMapper.update(null, Wrappers.lambdaUpdate(OrderComment.class)
							.set(OrderComment::getBalance, balance)
							.eq(OrderComment::getId, id));
				}
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void saveOrUpdateOrderCommentConfig(List<OrderCommentRewardConfig> configs) {
		List<Long> exists_goods_ids = new LinkedList<>();
		configs.forEach(data->{
			Long goodsId = data.getGoodsId();
			if (goodsId == null || goodsId < 0) {
				throw BusinessRuntimeException.getInstance("平台id为空");
			}
			BigDecimal balance = data.getBalance();
			if (balance == null || balance.compareTo(BigDecimal.ZERO) < 0) {
				throw BusinessRuntimeException.getInstance("请输入正确的奖励余额");
			}
			exists_goods_ids.add(goodsId);
		});
		orderCommentRewardConfigMapper.delete(Wrappers.lambdaQuery(OrderCommentRewardConfig.class)
				.notIn(exists_goods_ids.size() > 0, OrderCommentRewardConfig::getGoodsId, exists_goods_ids));
		if (CollUtil.isNotEmpty(configs)) {
			orderCommentRewardConfigMapper.batchInsertConfigs(configs);
		}
	}
}
