package com.cyksj.web.controller.fission;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.UserMirrorFissionRecord;
import com.cyksj.model.response.UserMirrorFissionReceiveResp;
import com.cyksj.model.views.UserMirrorFissionReceiveRecordView;
import com.cyksj.service.fission.UserMirrorFissionFrontService;
import com.cyksj.web.util.StpUserUtil;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * server/镜像裂变
 * 项目名: yhlxj2
 * 文件名: UserMirrorFissionController
 * 创建者: JavaZou
 * 创建时间:2024/11/21 18:10
 */
@Slf4j
@RestController
@RequestMapping("/applets/mirror/fission")
@RequiredArgsConstructor
public class UserMirrorFissionController {
	private final UserMirrorFissionFrontService userMirrorFissionFrontService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;


	/**
	 * 是否购买过镜像车票
	 */
	@GetMapping("/get/purchased/mirror/status")
	public Result<Boolean> getPurchasedMirrorStatus(@RequestParam(defaultValue = "18") Long goodsId) {
		long userId = StpUserUtil.getLoginIdAsLong();
		Boolean flag = userMirrorFissionFrontService.getPurchasedMirrorStatus(userId, goodsId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(flag);
	}

	/**
	 * 领取镜像试用车票
	 */
	@PostMapping("/receive/trial/mirror/ticket/{code}")
	public Result<String> receiveTrialMirrorTicket(@PathVariable String code) {
		long userId = StpUserUtil.getLoginIdAsLong();
		UserMirrorFissionReceiveResp resp = userMirrorFissionFrontService.receiveTrialMirrorTicket(userId, code);
		return GatewayResponse.SUCCESS.newBuilder().toResult(resp.getStatus().name());
	}

	/**
	 * 镜像分裂车票
	 */
	@GetMapping("/get/present/ticket")
	public Result<UserMirrorFissionRecord> getPresentFissionTicket() {
		long userId = StpUserUtil.getLoginIdAsLong();
		UserMirrorFissionRecord userMirrorFissionRecord = userMirrorFissionFrontService.getPresentFissionTicket(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(userMirrorFissionRecord);
	}

	/**
	 * 镜像分裂车票记录
	 */
	@PostMapping("/record/{relationId}")
	public Result<UserMirrorFissionRecord> recordUserMirrorFission(@PathVariable Long relationId) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		UserMirrorFissionRecord userMirrorFissionRecord = userMirrorFissionFrontService.recordUserMirrorFission(userId, relationId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(userMirrorFissionRecord);
	}

	/**
	 * 镜像分裂车票 赠送记录
	 */
	@GetMapping("/get/receive/record/{code}")
	public Result<SearchResult<UserMirrorFissionReceiveRecordView>> getFissionReceiveRecord(@PathVariable String code) {
		SearchResult<UserMirrorFissionReceiveRecordView> search = beanSearcher.search(UserMirrorFissionReceiveRecordView.class, MapUtils.flatBuilder(request.getParameterMap()).field(UserMirrorFissionReceiveRecordView::getCode, code).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 是否可进行裂变活动
	 */
	@GetMapping("/get/permit/status")
	public Result<Boolean> getPermitStatus(@RequestParam(defaultValue = "18") Long goodsId) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		Boolean flag = userMirrorFissionFrontService.getPermitStatus(userId, goodsId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(flag);
	}
}
