package com.cyksj.web.controller.shop;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.dto.YhShopInfo;
import com.cyksj.model.entity.YhShop;
import com.cyksj.service.shop.YhShopFrontService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/*
 * server/小店店铺/开店申请
 *项目名: yhlxj
 *文件名: OpenShopController
 *创建者: JavaZou
 *创建时间:2023/9/14 12:26
 */
@RestController
@RequestMapping("/applet/shop")
@RequiredArgsConstructor
public class OpenShopController {
	private final YhShopFrontService yhShopFrontService;

	private final BeanSearcher beanSearcher;

	/**
	 * 开店申请
	 */
	@PostMapping("/apply")
	public Result<String> applyOpenShop(@RequestBody YhShop shopInfo) {
		long userId = StpUserUtil.getLoginIdAsLong();
		shopInfo.setUserId(userId);
		yhShopFrontService.applyOpenShop(shopInfo);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 店铺审核失败 重新申请
	 */
	@PutMapping("/reApply")
	public Result<String> reApplyOpenShop(@RequestBody YhShop shopInfo) {
		long userId = StpUserUtil.getLoginIdAsLong();
		shopInfo.setUserId(userId);
		yhShopFrontService.reApplyOpenShop(shopInfo);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 个人店铺审核详情
	 */
	@GetMapping("/get/detail")
	public Result<YhShopInfo> getYhShopDetail() {
		long userId = StpUserUtil.getLoginIdAsLong();
		YhShopInfo yhShop = beanSearcher.searchFirst(YhShopInfo.class, MapUtils.builder()
				.put("uid", String.format("user_id = %s", userId)).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(yhShop);
	}

}
