package com.cyksj.web.controller.mini;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserMapper;
import com.cyksj.mapper.UserStudentCertRecordMapper;
import com.cyksj.model.entity.User;
import com.cyksj.model.entity.UserStudentCertRecord;
import com.cyksj.model.views.CouponView;
import com.cyksj.model.views.UserActivityView;
import com.cyksj.service.mini.MiniService;
import com.cyksj.service.user.UserActivityService;
import com.cyksj.service.user.UserBindRelationService;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目名: yhlxj
 * 文件名: UserCertController
 * 创建者: JavaZou
 * 创建时间:2024/4/16 11:34
 */
@RestController
@RequestMapping("/applets/mini/identity")
@RequiredArgsConstructor
@Slf4j
public class UserStudentCertController {
	private final UserMapper userMapper;

	private final MiniService miniService;

	private final UserActivityService userActivityService;

	private final UserBindRelationService userBindRelationService;

	private final BeanSearcher beanSearcher;

	private final UserStudentCertRecordMapper userStudentCertRecordMapper;

	/**
	 * 获取学生身份接口
	 */
	@GetMapping("/get/student")
	public Result<String> getStudentIdentity(String school, String code, String dsCode) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		User user = userMapper.selectById(userId);
		if (user == null || StrUtil.isEmpty(user.getOpenId())) {
			throw BusinessRuntimeException.getInstance("用户未授权");
		}
		//是否认证过
		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, user);
		Integer selectCount = userStudentCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserStudentCertRecord.class)
				.in(UserStudentCertRecord::getUserId, userIdList));
		if (selectCount > 0) {
			throw BusinessRuntimeException.getInstance("已认证大学生身份");
		}
		miniService.getStudentIdentity(user, code);
		//大学生认证活动 若有sid则发送对应用户
		userActivityService.sendStudentCertActivity(user, dsCode, school);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 大学生认证活动详情
	 */
	@GetMapping("/get/activity/info")
	public Result<UserActivityView> getIdentityAcInfo() throws Exception {
		UserActivityView userActivityView = beanSearcher.searchFirst(UserActivityView.class, MapUtils.builder().field(UserActivityView::getId, 1).build());
		if (userActivityView == null) {
			return GatewayResponse.SUCCESS.newBuilder().toResult();
		}
		if (StrUtil.isNotBlank(userActivityView.getCouponIds())) {
			List<Long> couponIds = Jsons.parseList(userActivityView.getCouponIds(), Long.class);
			userActivityView.setCouponViews(beanSearcher.searchAll(CouponView.class, MapUtils.builder().field(CouponView::getId, couponIds).op(Operator.InList).build()));
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult(userActivityView);
	}

}
