/**
 * 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.CommonRouteExcel;
import org.springblade.transport.excel.CommonRouteImportExcel;
import org.springblade.transport.pojo.entity.CommonRoute;
import org.springblade.transport.pojo.vo.BusinessRemoveResultVO;
import org.springblade.transport.pojo.vo.CommonRouteVO;
import org.springblade.transport.service.ICommonRouteService;
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.util.ArrayList;
import java.util.List;

/**
 * 常用线路 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@PreAuth(menu = "common_route")
@RequestMapping("/common-route")
@Tag(name = "常用线路", description = "常用线路")
public class CommonRouteController extends BladeController {

	private final ICommonRouteService commonRouteService;

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

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

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

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

	@GetMapping("/export-common-route")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "导出常用线路")
	public void exportCommonRoute(CommonRouteVO commonRoute, @RequestParam(required = false) String ids, HttpServletResponse response) {
		List<CommonRouteExcel> list = commonRouteService.exportCommonRoute(commonRoute, ids);
		ExcelUtil.export(response, "常用线路" + DateUtil.time(), "常用线路", list, CommonRouteExcel.class);
	}

	@PostMapping("/import-common-route")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "导入常用线路", description = "传入excel")
	public R importCommonRoute(MultipartFile file, HttpServletResponse response) {
		List<CommonRouteImportExcel> failureList = commonRouteService.importCommonRoute(
			ExcelUtil.read(file, CommonRouteImportExcel.class)
		);
		if (Func.isNotEmpty(failureList)) {
			ImportFailureExcelUtil.export(
				response,
				"常用线路导入失败明细" + DateUtil.time(),
				"导入失败明细",
				failureList,
				CommonRouteImportExcel.class
			);
			return null;
		}
		return R.success("导入数据成功");
	}

	@GetMapping("/export-template")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "导出导入模板")
	public void exportTemplate(HttpServletResponse response) {
		ExcelUtil.export(response, "常用线路模板", "常用线路导入模板", new ArrayList<CommonRouteImportExcel>(), CommonRouteImportExcel.class);
	}

}
