/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
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.ProjectApplyExcel;
import org.springblade.transport.pojo.entity.ProjectApply;
import org.springblade.transport.pojo.vo.ProjectApplyVO;
import org.springblade.transport.service.IProjectApplyService;
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 = "project_apply")
@RequestMapping("/project-apply")
@Tag(name = "项目立项", description = "项目立项")
public class ProjectApplyController extends BladeController {

	private final IProjectApplyService projectApplyService;

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

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

	@PostMapping("/save-draft")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "保存草稿", description = "传入projectApply")
	public R saveDraft(@RequestBody ProjectApply projectApply) {
		return R.status(projectApplyService.saveDraft(projectApply));
	}

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

	@PostMapping("/submit-approval")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "提交审批", description = "传入id")
	public R submitApproval(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(projectApplyService.submitApproval(id));
	}

	@PostMapping("/approve")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "审批通过", description = "传入id")
	public R approve(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(projectApplyService.approve(id));
	}

	@PostMapping("/reject")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "审批驳回", description = "传入id")
	public R reject(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(projectApplyService.reject(id));
	}

	@PostMapping("/withdraw")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "撤回审批", description = "传入id")
	public R withdraw(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(projectApplyService.withdraw(id));
	}

	@PostMapping("/void")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "作废", description = "传入id和reason")
	public R voidProject(@Parameter(description = "主键", required = true) @RequestParam Long id, @RequestParam(required = false) String reason) {
		return R.status(projectApplyService.voidProject(id, reason));
	}

	@PostMapping("/save-change")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "保存变更", description = "传入projectApply")
	public R saveChange(@RequestBody ProjectApplyVO projectApply) {
		return R.status(projectApplyService.saveChange(projectApply));
	}

	@PostMapping("/submit-change")
	@ApiOperationSupport(order = 11)
	@Operation(summary = "提交变更", description = "传入projectApply")
	public R submitChange(@RequestBody ProjectApplyVO projectApply) {
		return R.status(projectApplyService.submitChange(projectApply));
	}

	@PostMapping("/start-change")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "发起变更", description = "传入id、changeContent和changeReason")
	public R startChange(@Parameter(description = "主键", required = true) @RequestParam Long id,
						 @RequestParam String changeContent,
						 @RequestParam String changeReason) {
		return R.status(projectApplyService.startChange(id, changeContent, changeReason));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 13)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(projectApplyService.removeDraft(ids));
	}

	@GetMapping("/export-project-apply")
	@ApiOperationSupport(order = 14)
	@Operation(summary = "导出项目立项")
	public void exportProjectApply(ProjectApplyVO projectApply, @RequestParam(required = false) String ids, HttpServletResponse response) {
		List<ProjectApplyExcel> list = projectApplyService.exportProjectApply(projectApply, ids);
		ExcelUtil.export(response, "项目立项" + DateUtil.time(), "项目立项", list, ProjectApplyExcel.class);
	}

}
