/**
 * 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.core.tool.utils.Func;
import org.springblade.common.excel.ImportFailureExcelUtil;
import org.springblade.transport.excel.TransportPlanExcel;
import org.springblade.transport.excel.TransportPlanImportExcel;
import org.springblade.transport.pojo.dto.TransportPlanDispatchRequest;
import org.springblade.transport.pojo.entity.TransportPlan;
import org.springblade.transport.pojo.vo.BusinessRemoveResultVO;
import org.springblade.transport.pojo.vo.TransportPlanVO;
import org.springblade.transport.service.ITransportPlanService;
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 org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 运输计划 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@PreAuth(menu = "transport_plan")
@RequestMapping("/transport-plan")
@Tag(name = "运输计划", description = "运输计划")
public class TransportPlanController extends BladeController {

	private final ITransportPlanService transportPlanService;

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

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

	@PostMapping("/submit")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "新增或修改", description = "传入transportPlan")
	public R submit(@RequestBody TransportPlan transportPlan) {
		return R.status(transportPlanService.submit(transportPlan));
	}

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

	@GetMapping("/export-transport-plan")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "导出运输计划")
	public void exportTransportPlan(TransportPlanVO transportPlan, @RequestParam(required = false) String ids, HttpServletResponse response) {
		List<TransportPlanExcel> list = transportPlanService.exportTransportPlan(transportPlan, ids);
		ExcelUtil.export(response, "运输计划" + DateUtil.time(), "运输计划", list, TransportPlanExcel.class);
	}

	@GetMapping("/export-template")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导出运输计划模板")
	public void exportTemplate(HttpServletResponse response) {
		TransportPlanImportExcel template = new TransportPlanImportExcel();
		template.setPlanName("示例运输计划");
		template.setTransportType("公路运输");
		template.setPlanStartDate(LocalDate.now().toString());
		template.setPlanEndDate(LocalDate.now().plusDays(1).toString());
		template.setCargoName("示例货物");
		template.setCargoType("示例货物类型");
		template.setQuantity(new BigDecimal("10"));
		template.setQuantityUnit("吨");
		template.setPackageType("袋装");
		template.setDepartureAddress("示例发货地址");
		template.setArrivalAddress("示例收货地址");
		ExcelUtil.export(response, "运输计划模板", "运输计划导入模板", List.of(template), TransportPlanImportExcel.class);
	}

	@PostMapping("/import-transport-plan")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导入运输计划", description = "传入 Excel、项目和客户合同")
	public R importTransportPlan(MultipartFile file, @RequestParam Long projectId, @RequestParam String projectName,
		@RequestParam Long contractId, @RequestParam String contractName, @RequestParam String customerName,
		HttpServletResponse response) {
		List<TransportPlanImportExcel> failureList = transportPlanService.importTransportPlan(
			ExcelUtil.read(file, TransportPlanImportExcel.class), projectId, projectName, contractId, contractName, customerName);
		if (Func.isNotEmpty(failureList)) {
			ImportFailureExcelUtil.export(response, "运输计划导入失败明细" + DateUtil.time(), "导入失败明细", failureList, TransportPlanImportExcel.class);
			return null;
		}
		return R.success("导入数据成功");
	}

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

	@PostMapping("/dispatch")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "计划调度并生成运单")
	public R<Integer> dispatch(@RequestBody TransportPlanDispatchRequest request) {
		return R.data(transportPlanService.dispatch(request));
	}

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

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

}
