package com.cyksj.service.order.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.constant.Constant;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.OrderCommentMapper;
import com.cyksj.mapper.OrderDonMapper;
import com.cyksj.mapper.UserMapper;
import com.cyksj.model.entity.OrderComment;
import com.cyksj.model.entity.User;
import com.cyksj.model.manage.views.OrderDonView;
import com.cyksj.model.request.OrderCommentReq;
import com.cyksj.redis.RedisService;
import com.cyksj.service.order.OrderCommentFrontService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *项目名: netflix
 *文件名: OrderCommentFrontServiceImpl
 *创建者: JavaZou
 *创建时间:2023/7/28 13:46
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderCommentFrontServiceImpl implements OrderCommentFrontService {
	private final OrderCommentMapper orderCommentMapper;

	private final UserMapper userMapper;

	private final OrderDonMapper orderDonMapper;

	private final BeanSearcher beanSearcher;

	private final RedisService redisService;

	@Override
	public void saveComment(OrderCommentReq request) {
		Long orderId = request.getOrderId();
		Long userId = request.getUserId();
		List<Long> userIds = request.getUserIds();
		//是否已评价
		OrderComment orderComment = orderCommentMapper.selectOne(Wrappers.lambdaQuery(OrderComment.class)
				.in(OrderComment::getUserId, userIds)
				.eq(OrderComment::getOrderId, orderId).last("limit 1"));
		if (orderComment != null) throw BusinessRuntimeException.getInstance("您已完成该订单评价");

		OrderDonView orderDonView = beanSearcher.searchFirst(OrderDonView.class, MapUtils.builder()
				.field(OrderDonView::getId, orderId)
				.field(OrderDonView::getUserId, userIds).op(Operator.InList)
				.build());
		if (orderDonView == null) throw BusinessRuntimeException.getInstance("不好意思，您当前未购买设备产品，无法进行评价");
		if (request.getTitle().length() > 200) {
			throw BusinessRuntimeException.getInstance("标题过长");
		}
		if (request.getContent().length() > 1000) {
			throw BusinessRuntimeException.getInstance("评论内容过多");
		}
		if (StrUtil.isNotBlank(request.getPicture()) && request.getPicture().length() > 2000) {
			throw BusinessRuntimeException.getInstance("图片过多");
		}
		if (Constant.noOrderStatus.contains(orderDonView.getStatus().name())) {
			throw BusinessRuntimeException.getInstance("您该订单未完成");
		}
		if (orderDonView.getGoodsDonType() != 2) {
			throw BusinessRuntimeException.getInstance("暂只支持实物设备订单评价");
		}
		User user = userMapper.selectById(userId);
		orderComment = new OrderComment();
		BeanUtil.copyProperties(request, orderComment);
		orderComment.setGoodsId(orderDonView.getGoodsId());
		orderComment.setSkuId(orderDonView.getSkuId());
		orderComment.setNickname(user.getNickname());
		orderComment.setHeadImgUrl(user.getHeadimgurl());
		orderComment.setVerifyStatus(OrderComment.VerifyStatus.verifying);
		orderComment.setCommentTime(DateTime.now().toString());
		if (redisService.setNx(RedisService.key.ORDER_COMMENT_KEY.getName() + orderId, userId, RedisService.key.ORDER_COMMENT_KEY.getTimeout())) {
			orderCommentMapper.insert(orderComment);
		}
	}
}
