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.MartialDeductionItem;
import org.springblade.modules.martial.pojo.entity.MartialProject;
import org.springblade.modules.martial.service.IMartialDeductionItemService;
import org.springblade.modules.martial.service.IMartialProjectService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 扣分项配置 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blade-martial/deductionItem")
@Tag(name = "扣分项配置管理", description = "扣分项配置接口")
public class MartialDeductionItemController extends BladeController {

	private final IMartialDeductionItemService deductionItemService;

	private final IMartialProjectService martialProjectService;

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

	/**
	 * 分页列表
	 */
	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "分页查询")
	public R<IPage<MartialDeductionItem>> list(MartialDeductionItem deductionItem, Query query) {
		IPage<MartialDeductionItem> pages = deductionItemService.page(Condition.getPage(query), Condition.getQueryWrapper(deductionItem));
		List<MartialDeductionItem> deductionItems =  pages.getRecords();
		for (MartialDeductionItem item : deductionItems) {
			MartialProject project = martialProjectService.getById(item.getProjectId());
			item.setProjectName(project.getProjectName());
		}
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入实体")
	public R submit(@RequestBody MartialDeductionItem deductionItem) {
		return R.status(deductionItemService.saveOrUpdate(deductionItem));
	}

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

	/**
	 * 更新排序
	 */
	@PostMapping("/update-order")
	@Operation(summary = "更新排序", description = "传入排序数据")
	public R updateOrder(@RequestBody List<MartialDeductionItem> sortData) {
		return R.status(deductionItemService.updateOrder(sortData));
	}

}
