package com.qs.mp.web.controller.api.user;

import com.alibaba.fastjson.JSONObject;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.domain.AjaxResult.Type;
import com.qs.mp.framework.service.IWxUrlLinkService;
import com.qs.mp.user.domain.vo.UserShareVO;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @auther zhongcp
 * @create 2022-02-28 16:17:48
 * @describe 用户分享前端控制器
 */
@Api(tags = "用户分享调用API")
@RestController
@RequestMapping("/api/v1/mp/user/share")
@Component
public class UserShareController extends BaseApiController {

	@Autowired
	private IWxUrlLinkService wxUrlLinkService;

	@Value("${wx-user.appId}")
	private String userAppId;

	/**
	 * 生成盲票详情小程序码
	 *
	 * @return
	 */
	@PostMapping("/code/generate")
	public AjaxResult generateInviteCode(@RequestBody  JSONObject jsonObject) {
		String boxId = jsonObject.getString("boxId"); // 票组ID
		String type = jsonObject.getString("type"); // 分享类型，1/2
		Long userId = SecurityUtils.getUserId();
		if (UserShareVO.SHARE_TYPE_SITE.equals(type) && StringUtils.isBlank(boxId)) {
			// 经销商分享，默认到盲票小程序首页
			String rst = wxUrlLinkService.generateUnlimitCode("", userId + "&" + type, userAppId);
			return new AjaxResult(Type.SUCCESS, "", rst);
		}
		String rst = wxUrlLinkService.generateUnlimitCode("pages/ticketBox/detail", boxId + "&" + userId + "&" + type, userAppId);
		return new AjaxResult(Type.SUCCESS, "", rst);
	}
}
