package com.cyksj.web.controller.payment;

import com.cyksj.common.annotation.NoSubmit;
import com.cyksj.common.constant.Constant;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.request.OrderCommentReq;
import com.cyksj.model.views.OrderDonUserCommentView;
import com.cyksj.service.order.OrderCommentFrontService;
import com.cyksj.service.user.UserService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 *项目名: netflix
 *文件名: OrderCommentFrontController
 *创建者: JavaZou
 *创建时间:2023/7/28 13:42
 */
@Slf4j
@RequestMapping("/applets/order/comment")
@RestController
@RequiredArgsConstructor
public class OrderCommentFrontController {
	private final OrderCommentFrontService orderCommentFrontService;

	private final BeanSearcher beanSearcher;

	private final UserService userService;

	/**
	 * 新增评价
	 */
	@PostMapping("/post")
	@NoSubmit
	public Result<String> post(@RequestBody @Validated OrderCommentReq request) {
		long userId = StpUserUtil.getLoginIdAsLong();
		request.setUserId(userId);
		request.setUserIds(userService.getRelationUserIdList(userId, null));
		orderCommentFrontService.saveComment(request);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 可评论的订单
	 */
	@GetMapping("/get/orders")
	public Result<List<OrderDonUserCommentView>> getOrders(@RequestParam(defaultValue = "2") Integer type) {
		long userId = StpUserUtil.getLoginIdAsLong();
		List<Long> userIds = userService.getRelationUserIdList(userId, null);
		String userIdsStr = userIds.toString().replace("[", "(")
				.replace("]", ")");
		String conditionSql = "where status not in ('close','noPayment','refund')";
		if (Constant.realGoodsType.contains(type)) {
			conditionSql = "where status = 'transport'";
		}
		conditionSql += String.format(" and user_id in %s", userIdsStr);
		List<OrderDonUserCommentView> OrderDonUserCommentViews = beanSearcher.searchAll(OrderDonUserCommentView.class, MapUtils.builder()
				.put("condition", conditionSql)
				.field(OrderDonUserCommentView::getGoodsType, type).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(OrderDonUserCommentViews);
	}
}
