/**
 * 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>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.zhaocai.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.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
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.secure.annotation.PreAuth;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.zhaocai.pojo.entity.ProcurementPlan;
import org.springblade.zhaocai.pojo.vo.ProcurementPlanVO;
import org.springblade.zhaocai.service.IBillCodeService;
import org.springblade.zhaocai.service.IProcurementPlanService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 年度招采计划控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@RequestMapping("procurement-plan")
@AllArgsConstructor
@PreAuth(menu = "procurement-plan")
@Tag(name = "年度招采计划", description = "年度招采计划接口")
public class ProcurementPlanController extends BladeController {

	private final IProcurementPlanService procurementPlanService;
	private final IBillCodeService billCodeService;

	/**
	 * 生成单据号
	 */
	@GetMapping("/bill-no")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "生成单据号", description = "生成唯一单据号")
	public R<String> generateBillNo() {
		return R.data(billCodeService.generateBillCode("PROCUREMENT_PLAN","NDZC"));
	}

	/**
	 * 详情（含明细）
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "详情", description = "传入id")
	public R<ProcurementPlanVO> detail(@Parameter(name = "id", description = "主键", in = ParameterIn.QUERY, schema = @Schema(type = "integer")) @RequestParam Long id) {
		ProcurementPlanVO detail = procurementPlanService.detail(id);
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@Parameters({
		@Parameter(name = "applicant", description = "申请人", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
		@Parameter(name = "billNo", description = "单据号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
		@Parameter(name = "year", description = "年度", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
	})
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入procurementPlan")
	public R<IPage<ProcurementPlan>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> procurementPlan, Query query) {
		IPage<ProcurementPlan> pages = procurementPlanService.page(Condition.getPage(query), Condition.getQueryWrapper(procurementPlan, ProcurementPlan.class));
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入procurementPlan")
	public R save(@RequestBody ProcurementPlanVO procurementPlanVO) {
		return R.status(procurementPlanService.submit(procurementPlanVO));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入procurementPlan")
	public R update(@RequestBody ProcurementPlanVO procurementPlanVO) {
		return R.status(procurementPlanService.submit(procurementPlanVO));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入procurementPlan")
	public R submit(@RequestBody ProcurementPlanVO procurementPlanVO) {
		return R.status(procurementPlanService.submit(procurementPlanVO));
	}

	/**
	 * 保存草稿（不触发流程引擎）
	 */
	@PostMapping("/draft/save")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "保存草稿", description = "传入procurementPlan，不触发流程引擎")
	public R<Long> saveDraft(@RequestBody ProcurementPlanVO procurementPlanVO) {
		boolean result = procurementPlanService.saveDraft(procurementPlanVO);
		if (result) {
			return R.data(procurementPlanVO.getId());
		}
		return R.fail("保存草稿失败");
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(name = "ids", description = "主键集合", in = ParameterIn.QUERY, schema = @Schema(type = "string")) @RequestParam String ids) {
		boolean temp = procurementPlanService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}

}
