package com.cyksj.web.controller.manage.vip;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.OrderDonGalaxyCoinRecordMapper;
import com.cyksj.mapper.channel.ChannelPopularizeMapper;
import com.cyksj.model.entity.OrderDonGalaxyCoinRecord;
import com.cyksj.model.entity.VipGoodsSku;
import com.cyksj.model.request.OrderRefundReq;
import com.cyksj.model.views.CmsVipOrderDonView;
import com.cyksj.model.views.VipGoodsSkuView;
import com.cyksj.service.mange.vip.CmsVipOrderService;
import com.cyksj.service.mange.vip.VipCommonService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * 项目名: yhlxj2
 * 文件名: CmsVipController
 * 创建者: JavaZou
 * 创建时间:2025/5/13 15:31
 */
@RestController
@RequestMapping("/manage/vip")
@RequiredArgsConstructor
@Slf4j
public class CmsVipController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final ChannelPopularizeMapper channelPopularizeMapper;

	private final CmsVipOrderService cmsVipOrderService;

	private final VipCommonService vipCommonService;

	private final OrderDonGalaxyCoinRecordMapper orderDonGalaxyCoinRecordMapper;

	/**
	 * 会员订单列表
	 */
	@GetMapping("/get/orders")
	public Result<SearchResult<CmsVipOrderDonView>> getOrders() {
		SearchResult<CmsVipOrderDonView> search = beanSearcher.search(CmsVipOrderDonView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		search.getDataList().forEach(data->{
			if (data.getPopularizeId() != null) {
				Optional.ofNullable(channelPopularizeMapper.selectChannelByPid(data.getPopularizeId()))
						.ifPresent(channelName -> data.setChannel(channelName));
			}
			OrderDonGalaxyCoinRecord orderDonGalaxyCoinRecord = orderDonGalaxyCoinRecordMapper.selectOne(Wrappers.lambdaQuery(OrderDonGalaxyCoinRecord.class)
					.eq(OrderDonGalaxyCoinRecord::getOrderId, data.getId())
					.last("limit 1"));
			if (orderDonGalaxyCoinRecord != null) {
				data.setGalaxyCoin(orderDonGalaxyCoinRecord.getGalaxyCoin());
				data.setGalaxyCoinDeductMoney(orderDonGalaxyCoinRecord.getDeductMoney());
			}
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 会员订单退款
	 */
	@PostMapping("/refund/order")
	public Result<String> refundOrders(@RequestBody @Validated OrderRefundReq refundReq) throws Exception {
		long adminId = StpUtil.getLoginIdAsLong();
		refundReq.setAdminId(adminId);
		cmsVipOrderService.refundOrders(refundReq);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	//==================================================会员配置======================================================================

	/**
	 * 会员规格配置列表
	 */
	@GetMapping("/get/skus")
	public Result<List<VipGoodsSkuView>> getVipGoodsSku() {
		List<VipGoodsSkuView> vipGoodsSkuViews = beanSearcher.searchAll(VipGoodsSkuView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(vipGoodsSkuViews);
	}

	/**
	 * 设置会员规格
	 */
	@PostMapping("/post/skus")
	public Result<String> setVipGoodsSku(@RequestBody List<VipGoodsSku> vipGoodsSkus) {
		vipCommonService.setVipGoodsSku(vipGoodsSkus);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
