/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 */
package org.springblade.transport.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.transport.excel.LoadingManageExcel;
import org.springblade.transport.pojo.entity.LoadingManage;
import org.springblade.transport.pojo.vo.BusinessRemoveResultVO;
import org.springblade.transport.pojo.vo.LoadingManageVO;
import org.springblade.transport.service.ILoadingManageService;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 配载管理 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@PreAuth(menu = "loading_manage")
@RequestMapping("/loading-manage")
@Tag(name = "配载管理", description = "配载管理")
public class LoadingManageController extends BladeController {

	private final ILoadingManageService loadingManageService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入id")
	public R<LoadingManageVO> detail(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.data(loadingManageService.detail(id));
	}

	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入loadingManage")
	public R<IPage<LoadingManageVO>> list(LoadingManageVO loadingManage, Query query) {
		return R.data(loadingManageService.selectLoadingManagePage(Condition.getPage(query), loadingManage));
	}

	@PostMapping("/save-draft")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "保存草稿", description = "传入loadingManage")
	public R<LoadingManageVO> saveDraft(@RequestBody LoadingManage loadingManage) {
		loadingManageService.saveDraft(loadingManage);
		return R.data(loadingManageService.detail(loadingManage.getId()));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增或修改", description = "传入loadingManage")
	public R submit(@RequestBody LoadingManage loadingManage) {
		return R.status(loadingManageService.submit(loadingManage));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R<BusinessRemoveResultVO> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.data(loadingManageService.removeLoadingManage(ids));
	}

	@GetMapping("/export-loading-manage")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导出配载管理")
	public void exportLoadingManage(LoadingManageVO loadingManage, @RequestParam(required = false) String ids, HttpServletResponse response) {
		List<LoadingManageExcel> list = loadingManageService.exportLoadingManage(loadingManage, ids);
		ExcelUtil.export(response, "配载管理" + DateUtil.time(), "配载管理", list, LoadingManageExcel.class);
	}

	@PostMapping("/copy")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "复制", description = "传入id")
	public R<LoadingManageVO> copy(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.data(loadingManageService.copy(id));
	}

	@PostMapping("/reassign")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "重新派单", description = "传入loadingManage")
	public R reassign(@RequestBody LoadingManage loadingManage) {
		return R.status(loadingManageService.reassign(loadingManage));
	}

	@PostMapping("/cancel")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "取消", description = "传入id")
	public R cancel(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(loadingManageService.cancel(id));
	}

	@PostMapping("/complete")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "完成", description = "传入id")
	public R complete(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(loadingManageService.complete(id));
	}

	@PostMapping("/batch-complete")
	@ApiOperationSupport(order = 11)
	@Operation(summary = "批量完成", description = "传入ids")
	public R<BusinessRemoveResultVO> batchComplete(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.data(loadingManageService.batchComplete(ids));
	}

}
