package org.springblade.modules.martial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.MartialCompetitionAttachment;
import org.springblade.modules.martial.service.IMartialCompetitionAttachmentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

	private final IMartialCompetitionAttachmentService attachmentService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入ID")
	public R<MartialCompetitionAttachment> detail(@RequestParam Long id) {
		MartialCompetitionAttachment detail = attachmentService.getById(id);
		return R.data(detail);
	}

	/**
	 * 分页列表
	 */
	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "分页查询")
	public R<IPage<MartialCompetitionAttachment>> list(MartialCompetitionAttachment attachment, Query query) {
		IPage<MartialCompetitionAttachment> pages = attachmentService.page(Condition.getPage(query), Condition.getQueryWrapper(attachment));
		return R.data(pages);
	}

	/**
	 * 根据赛事ID和类型获取附件列表
	 */
	@GetMapping("/getByType")
	@Operation(summary = "根据赛事ID和类型获取附件列表", description = "传入赛事ID和附件类型")
	public R<List<MartialCompetitionAttachment>> getByType(
		@RequestParam Long competitionId,
		@RequestParam String attachmentType) {
		List<MartialCompetitionAttachment> list = attachmentService.getByCompetitionIdAndType(competitionId, attachmentType);
		return R.data(list);
	}

	/**
	 * 根据赛事ID获取所有附件
	 */
	@GetMapping("/getByCompetition")
	@Operation(summary = "根据赛事ID获取所有附件", description = "传入赛事ID")
	public R<List<MartialCompetitionAttachment>> getByCompetition(@RequestParam Long competitionId) {
		List<MartialCompetitionAttachment> list = attachmentService.getByCompetitionId(competitionId);
		return R.data(list);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入实体")
	public R submit(@RequestBody MartialCompetitionAttachment attachment) {
		// 设置默认状态为启用
		if (attachment.getStatus() == null) {
			attachment.setStatus(1);
		}
		// 设置默认排序
		if (attachment.getOrderNum() == null) {
			attachment.setOrderNum(0);
		}
		return R.status(attachmentService.saveOrUpdate(attachment));
	}

	/**
	 * 批量保存附件
	 */
	@PostMapping("/batchSubmit")
	@Operation(summary = "批量保存附件", description = "传入附件列表")
	public R batchSubmit(@RequestBody List<MartialCompetitionAttachment> attachments) {
		for (MartialCompetitionAttachment attachment : attachments) {
			if (attachment.getStatus() == null) {
				attachment.setStatus(1);
			}
			if (attachment.getOrderNum() == null) {
				attachment.setOrderNum(0);
			}
		}
		return R.status(attachmentService.saveOrUpdateBatch(attachments));
	}

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

	/**
	 * 删除赛事的指定类型附件
	 */
	@PostMapping("/removeByType")
	@Operation(summary = "删除赛事的指定类型附件", description = "传入赛事ID和附件类型")
	public R removeByType(
		@RequestParam Long competitionId,
		@RequestParam String attachmentType) {
		return R.status(attachmentService.removeByCompetitionIdAndType(competitionId, attachmentType));
	}

}
