package org.springblade.modules.martial.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.martial.pojo.entity.MartialAthlete;
import org.springblade.modules.martial.pojo.entity.MartialCompetition;
import org.springblade.modules.martial.service.IMartialAthleteService;
import org.springblade.modules.martial.mapper.MartialAthleteMapper;
import org.springblade.modules.martial.service.IMartialCompetitionService;
import org.springblade.modules.system.pojo.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 赛事信息 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/competition")
@Tag(name = "赛事管理", description = "赛事信息管理接口")
public class MartialCompetitionController extends BladeController {

	private final IMartialCompetitionService competitionService;
	private final IMartialAthleteService martialAthleteService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入ID")
	public R<MartialCompetition> detail(@RequestParam Long id) {
		MartialCompetition detail = competitionService.getById(id);
		if (detail != null) {
			// Count distinct participants by id_card
			Long cnt = ((MartialAthleteMapper) martialAthleteService.getBaseMapper()).countDistinctParticipants(detail.getId());
			detail.setTotalParticipants(cnt != null ? cnt.intValue() : 0);
		}
		return R.data(detail);
	}

	/**
	 * 分页列表
	 */
	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "分页查询")
	public R<IPage<MartialCompetition>> list(MartialCompetition competition, Query query) {
		IPage<MartialCompetition> pages = competitionService.page(Condition.getPage(query), Condition.getQueryWrapper(competition));
		List<MartialCompetition> pagelist = pages.getRecords();
		for (MartialCompetition martialCompetition : pagelist) {
			// Count distinct participants by id_card
			Long cnt = ((MartialAthleteMapper) martialAthleteService.getBaseMapper()).countDistinctParticipants(martialCompetition.getId());
			martialCompetition.setTotalParticipants(cnt != null ? cnt.intValue() : 0);
		}
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入实体")
	public R<MartialCompetition> submit(@RequestBody MartialCompetition competition) {
		competitionService.saveOrUpdate(competition);
		return R.data(competition);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@Operation(summary = "删除", description = "传入ID")
	public R remove(@RequestParam String ids) {
		return R.status(competitionService.removeByIds(Func.toLongList(ids)));
	}

}
