package org.springblade.gateway.goods_gateway.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.goods_gateway.service.AppHelpGoodsService;
import org.springblade.gateway.goods_gateway.util.HelpGoodsUtil;
import org.springblade.payment.cmcc.request.CmccDectOrderRequest;
import org.springblade.payment.cmcc.request.CmccPlaceOrderRequest;
import org.springblade.payment.cmcc.util.CmccUtil;
import org.springblade.sing.active.entity.ActiveRecord;
import org.springblade.sing.active.service.IActiveRecordService;
import org.springblade.sing.goods.entity.HelpGoods;
import org.springblade.sing.goods.service.IHelpGoodsService;
import org.springblade.sing.goods.vo.HelpGoodsVO;
import org.springblade.sing.goods.wrapper.HelpGoodsWrapper;
import org.springblade.sing.point.entity.CmccPointRecord;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 14:12 2021/11/10
 * @Modified By:
 */
@RestController
@AllArgsConstructor
@RequestMapping("app/help/goods")
@Api(value = "助力商场", tags = "助力商场接口")
@Slf4j
public class AppHelpGoodsController {
	private final AppHelpGoodsService appHelpGoodsService;
	private final IHelpGoodsService helpGoodsService;
	private final IActiveRecordService activeRecordService;


	/**
	 * 兑换道具
	 */
	@ApiLog("兑换道具")
	@PostMapping("/exchangeProps")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "兑换道具", notes = "传入pointRecord")
	public R<String> exchangeProps(@ApiParam(value = "道具ID", required = true) @RequestParam Long helpGoodsId,
								   @ApiParam(value = "用户ID", required = true) @RequestParam Long userId,
								   @ApiParam(value = "购买数量", required = true) @RequestParam BigDecimal num,
								   @ApiParam(value = "作品ID", required = true) @RequestParam Long productId,
								   CmccDectOrderRequest cmccDectOrderRequest) {
		try {
			CmccPointRecord cmccPointRecord = new CmccPointRecord();
			cmccPointRecord.setHelpGoodsId(helpGoodsId);
			cmccPointRecord.setUserId(userId);
			cmccPointRecord.setNum(num);
			//加锁
			synchronized (cmccPointRecord.getUserId().toString().intern()) {
				appHelpGoodsService.exchangeProps(cmccPointRecord, cmccDectOrderRequest, productId);
			}
			return R.success("兑换成功");
		} catch (Exception e) {
			log.error(e.getMessage());
			return R.fail(e.getMessage());
		}
	}

	/**
	 * 创建道具订单
	 */
	@ApiLog("创建道具订单")
	@PostMapping("/createPropsOrder")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "创建道具订单", notes = "传入cmccPointRecord")
	public R<String> createPropsOrder(@ApiParam(value = "道具ID", required = true) @RequestParam Long helpGoodsId,
									  @ApiParam(value = "用户ID", required = true) @RequestParam Long userId,
									  @ApiParam(value = "手机号码", required = true) @RequestParam String phone,
									  @ApiParam(value = "购买数量", required = true) @RequestParam BigDecimal num,
									  @ApiParam(value = "同盾设备指纹", required = true) @RequestParam String fingerprint,
									  @ApiParam(value = "通付盾参数", required = true) @RequestParam String sessionId) {
		try {
			CmccPlaceOrderRequest cmccPlaceOrderRequest = CmccPlaceOrderRequest
				.builder()
				.fingerprint(fingerprint)
				.sessionId(sessionId)
				.build();

			CmccPointRecord cmccPointRecord = new CmccPointRecord();
			cmccPointRecord.setHelpGoodsId(helpGoodsId);
			cmccPointRecord.setNum(num);
			cmccPointRecord.setPhone(phone);
			cmccPointRecord.setUserId(userId);
			//创建随机单号
			appHelpGoodsService.createPropsOrder(cmccPointRecord, cmccPlaceOrderRequest);
			return R.data(cmccPlaceOrderRequest.getOutOrderId());
		} catch (Exception e) {
			log.error(e.getMessage());
			return R.fail(e.getMessage());
		}
	}

	/**
	 * 发送验证码
	 */
	@ApiLog("发送验证码")
	@PostMapping("/sendCmccSms")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "发送验证码", notes = "传入cmccPlaceOrderRequest")
	public R<String> sendCmccSms(@ApiParam(value = "手机号码", required = true) @RequestParam String mobile,
								 @ApiParam(value = "订单号ID", required = true) @RequestParam String outOrderId) {
		try {
			CmccPlaceOrderRequest cmccPlaceOrderRequest = CmccPlaceOrderRequest
				.builder()
				.mobile(mobile)
				.outOrderId(outOrderId)
				.build();
			CmccUtil.sendCmccSms(cmccPlaceOrderRequest);
			return R.data(cmccPlaceOrderRequest.getOutOrderId());
		} catch (Exception e) {
			log.error(e.getMessage());
			return R.fail(e.getMessage());
		}
	}

	/**
	 * 初始化道具缓存(预热)
	 */
	@PostConstruct
	public void init() {
		for (ActiveRecord activeRecord : activeRecordService.list(
			Wrappers.<ActiveRecord>lambdaQuery().select(ActiveRecord::getId))) {
			HelpGoodsUtil.setHelpGoodsListCache(activeRecord.getId(),
				helpGoodsService.list(Wrappers.<HelpGoods>lambdaQuery().eq(HelpGoods::getActiveId, activeRecord.getId())));
		}
	}

	/**
	 * 获取道具助力列表
	 */
	@GetMapping("/getHelpGoodsList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取道具助力列表", notes = "传入helpGoods")
	public R<List<HelpGoodsVO>> getHelpGoodsList(@ApiParam(value = "活动ID", required = true) @RequestParam Long activeId,
												 @ApiParam(value = "用户ID", required = true) @RequestParam Long userId,
												 @ApiParam(value = "作品ID", required = true) @RequestParam Long productId) {
		return R.data(HelpGoodsWrapper.build(userId, productId)
			.listVO(HelpGoodsUtil.getHelpGoodsList(activeId)));
	}
}
