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

import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.GoodsDonMapper;
import com.cyksj.model.entity.GiftCardGoodsSku;
import com.cyksj.model.request.GiftCardGoodsSkuReq;
import com.cyksj.model.views.GiftCardGoodsSkuView;
import com.cyksj.model.views.GiftCardUserPayView;
import com.cyksj.service.giftcard.GiftCardGoodsSkuService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/*
 *项目名: netflix
 *文件名: GiftCardController
 *创建者: JavaZou
 *创建时间:2023/8/29 18:15
 */
@RequestMapping("/manage/giftCard")
@RestController
@RequiredArgsConstructor
public class GiftCardController {
	private final GiftCardGoodsSkuService giftCardGoodsSkuService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final GoodsDonMapper goodsDonMapper;

	/**
	 * 设置礼品卡 规格
	 */
	@PostMapping("/set")
	public Result<String> setGiftCardSku(@RequestBody GiftCardGoodsSkuReq req) {
		giftCardGoodsSkuService.setGiftCardSku(req);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 修改礼品卡 规格
	 */
	@PutMapping("/put")
	public Result<String> updateGiftCardSku(@RequestBody GiftCardGoodsSku giftCardSku) {
		if (giftCardSku.getId() == null) {
			throw BusinessRuntimeException.getInstance("请指定要修改的礼品卡");
		}
		giftCardGoodsSkuService.setOrUpdateGiftCardSku(giftCardSku);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 获取礼品卡规格 配置
	 */
	@GetMapping("/get")
	public Result<SearchResult<GiftCardGoodsSkuView>> getGiftCardGoodsSku() {
		SearchResult<GiftCardGoodsSkuView> list = beanSearcher.search(GiftCardGoodsSkuView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(list);
	}

	/**
	 * 礼品卡用户购买成功 记录
	 */
	@GetMapping("/get/pay")
	public Result<SearchResult<GiftCardUserPayView>> getGiftCardPayRecord() {
		SearchResult<GiftCardUserPayView> search = beanSearcher.search(GiftCardUserPayView.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(GiftCardUserPayView::getIsPay, true)
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 礼品卡上下架
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		GiftCardGoodsSku byId = giftCardGoodsSkuService.getById(id);
		if (byId == null) {
			return GatewayResponse.SUCCESS.newBuilder().toResult();
		}
//		if (!byId.getStatus()) {
//			//校验礼品卡已上架多少个 最多上架三个
//			int count = giftCardGoodsSkuService.count(Wrappers.lambdaQuery(GiftCardGoodsSku.class)
//					.eq(GiftCardGoodsSku::getStatus, true)
//					.eq(GiftCardGoodsSku::getDeleted, true)
//					.eq(GiftCardGoodsSku::getGcType, byId.getGcType()));
//			if (count > 2) {
//				throw BusinessRuntimeException.getInstance(String.format("%s最多上架三个", byId.getGcType() == 1 ? "现金卡" : "虚拟卡"));
//			}
//		}
		byId.setStatus(!byId.getStatus());
		giftCardGoodsSkuService.updateById(byId);
		//设置对应礼品卡平台 最低价格
		giftCardGoodsSkuService.setGiftCardGoodsStat(goodsDonMapper.selectById(byId.getGoodsId()));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除礼品卡
	 */
	@DeleteMapping("/del/{id}")
	public Result<String> delById(@PathVariable Long id) {
		GiftCardGoodsSku byId = giftCardGoodsSkuService.getById(id);
		if (byId == null) {
			return GatewayResponse.SUCCESS.newBuilder().toResult();
		}
		byId.setDeleted(false);
		giftCardGoodsSkuService.updateById(byId);
		giftCardGoodsSkuService.setGiftCardGoodsStat(goodsDonMapper.selectById(byId.getGoodsId()));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
