package com.cyksj.web.controller.payment;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.ShoppingCartDon;
import com.cyksj.model.request.UnifiedOrderPayReq;
import com.cyksj.model.views.ShoppingCartDonPayDetailView;
import com.cyksj.model.views.ShoppingCartDonView;
import com.cyksj.service.order.ShoppingCartDonService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目名: yhlxj
 * 文件名: OrderShopCartController
 * 创建者: JavaZou
 * 创建时间:2023/10/12 11:05
 */
@RestController
@RequestMapping("/applet/order/shopCart")
@RequiredArgsConstructor
public class OrderShopCartController {
	private final ShoppingCartDonService shoppingCartDonService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	/**
	 * 将商品添加至购物车
	 */
	@PostMapping("/add")
	public Result<String> addGoodsShoppingCart(@RequestBody ShoppingCartDon goods) {
		long userId = StpUserUtil.getLoginIdAsLong();
		goods.setUserId(userId);
		shoppingCartDonService.addGoodsShoppingCart(goods);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 将商品 移除购物车
	 */
	@DeleteMapping("/remove/{id}")
	public Result<String> removeShoppingCartGoodsById(@PathVariable Long id) {
		long userId = StpUserUtil.getLoginIdAsLong();
		shoppingCartDonService.remove(Wrappers.lambdaQuery(ShoppingCartDon.class)
				.eq(ShoppingCartDon::getUserId, userId)
				.eq(ShoppingCartDon::getId, id));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 一键清空 购物车
	 */
	@DeleteMapping("/clean")
	public Result<String> cleanShoppingCart() {
		long userId = StpUserUtil.getLoginIdAsLong();
		shoppingCartDonService.remove(Wrappers.lambdaQuery(ShoppingCartDon.class)
				.eq(ShoppingCartDon::getUserId, userId));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 个人 购物车列表
	 */
	@DeleteMapping("/get")
	public Result<SearchResult<ShoppingCartDonView>> getShoppingCartGoods() {
		long userId = StpUserUtil.getLoginIdAsLong();
		MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
		builder.put("uid", String.format(" where user_id = %s", userId));
		SearchResult<ShoppingCartDonView> search = beanSearcher.search(ShoppingCartDonView.class, builder.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}


	/**
	 * 购物车 下单
	 */
	@PostMapping("/unified/pay")
	public Result<ShoppingCartDonPayDetailView> unifiedPayShoppingCartGoods(@RequestBody UnifiedOrderPayReq unifiedOrderPayReq) {
		long userId = StpUserUtil.getLoginIdAsLong();
		unifiedOrderPayReq.setUserId(userId);
		ShoppingCartDonPayDetailView payDetailView = shoppingCartDonService.unifiedPayShoppingCartGoods(unifiedOrderPayReq);
		return GatewayResponse.SUCCESS.newBuilder().toResult(payDetailView);
	}


}
