/**
 * 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.ContractManageExcel;
import org.springblade.transport.pojo.entity.ContractManage;
import org.springblade.transport.pojo.vo.ContractManageVO;
import org.springblade.transport.service.IContractManageService;
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;
import java.util.Map;

/**
 * 合同管理 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@PreAuth(menu = "contract_manage")
@RequestMapping("/contract-manage")
@Tag(name = "合同管理", description = "合同管理")
public class ContractManageController extends BladeController {

	private final IContractManageService contractManageService;

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

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

	@PostMapping("/save-draft")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "保存草稿", description = "传入contractManage")
	public R<ContractManage> saveDraft(@RequestBody ContractManage contractManage) {
		return contractManageService.saveDraft(contractManage) ? R.data(contractManage) : R.fail("保存失败");
	}

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

	@PostMapping("/to-temporary")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "转临时合同", description = "传入id")
	public R toTemporary(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(contractManageService.toTemporary(id));
	}

	@PostMapping("/submit-formal")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "提交正式合同", description = "传入id")
	public R submitFormal(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(contractManageService.submitFormal(id));
	}

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

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

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

	@PostMapping("/start-change")
	@ApiOperationSupport(order = 10)
	@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(contractManageService.startChange(id, changeContent, changeReason));
	}

	@PostMapping("/terminate")
	@ApiOperationSupport(order = 11)
	@Operation(summary = "终止合同", description = "传入id和reason")
	public R terminate(@Parameter(description = "主键", required = true) @RequestParam Long id, @RequestParam(required = false) String reason) {
		return R.status(contractManageService.terminate(id, reason));
	}

	@PostMapping("/update-attachments")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "更新合同附件", description = "传入合同id及附件信息")
	public R updateAttachments(@RequestBody ContractManage contractManage) {
		return R.status(contractManageService.updateAttachments(contractManage));
	}

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

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

	@GetMapping("/expire-stats")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "到期统计", description = "传入contractManage")
	public R<Map<String, Long>> expireStats(ContractManageVO contractManage) {
		return R.data(contractManageService.expireStats(contractManage));
	}

	@GetMapping("/export-contract-manage")
	@ApiOperationSupport(order = 16)
	@Operation(summary = "导出合同管理")
	public void exportContractManage(ContractManageVO contractManage, @RequestParam(required = false) String ids, HttpServletResponse response) {
		List<ContractManageExcel> list = contractManageService.exportContractManage(contractManage, ids);
		ExcelUtil.export(response, "合同管理" + DateUtil.time(), "合同管理", list, ContractManageExcel.class);
	}

}
