package com.emergency.avenue.storage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emergency.avenue.admin.exception.MyCustomException;
import com.emergency.avenue.common.core.util.R;
import com.emergency.avenue.common.log.annotation.SysLog;
import com.emergency.avenue.storage.api.entity.OutboundInfo;
import com.emergency.avenue.storage.api.entity.TransferOrder;
import com.emergency.avenue.storage.api.vo.TransferOrderVo;
import com.emergency.avenue.storage.service.TransferOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@AllArgsConstructor
@RequestMapping("/order")
@Tag(description = "order", name = "调令下发管理")
public class TransferOrderController {

	private final TransferOrderService transferOrderService;


	@GetMapping("/page")
	@Operation(description = "调令信息分页", summary = "调令信息分页")
	public R<Page<TransferOrderVo>> getTransferOrderPage(Page<TransferOrderVo> page, TransferOrderVo transferOrder) {
		return transferOrderService.getTransferOrderPage(page, transferOrder);
	}

	@GetMapping("/export")
	@Operation(description = "调令信息-导出excel", summary = "调令信息-导出excel")
	public void exportcjxxxf(TransferOrderVo transferOrder,
							 HttpServletResponse response) {
		Page<TransferOrderVo> page = new Page<TransferOrderVo>(-1,-1);
		R<Page<TransferOrderVo>> page1 = getTransferOrderPage(page, transferOrder);

		String fileName = "export.xlsx";
		String templateFile = "调令下发-导出模板.xlsx";
		try {
			transferOrderService.export(fileName,templateFile, page1.getData().getRecords(),response);
		} catch (MyCustomException e) {
			throw new RuntimeException("导出失败");
		}
	}
	/**
	 * 通过ID调令信息
	 * @param id ID
	 * @return 调令信息
	 */
	@GetMapping("/{id}")
	@Operation(description = "通过ID查询调令信息", summary = "通过ID查询调令信息")
	public R<TransferOrder> getById(@PathVariable Integer id) {
		return R.ok(transferOrderService.getByIdAndTransferOrder(id));
	}

	@SysLog("添加and修改调令信息")
	@PostMapping
	@Operation(description = "添加and修改调令信息", summary = "添加and修改调令信息")
	public R save(@RequestBody TransferOrder transferOrder) {
		return transferOrderService.saveTransferOrderAndMaterial(transferOrder);
	}

	@SysLog("删除调令信息")
	@DeleteMapping("/{id}")
	@Operation(description = "删除调令信息", summary = "删除调令信息")
	public R<Boolean> removeById(@PathVariable Long id) {
		return R.ok(transferOrderService.removeByIdAndTransferOrder(id));
	}

}
