package com.prolog.rdc.tms.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.prolog.common.util.DateUtils;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.rdc.tms.model.dto.BookingExportDTO;
import com.prolog.rdc.tms.model.dto.BookingGetShipbillDTO;
import com.prolog.rdc.tms.model.dto.BookingItemDetailDTO;
import com.prolog.rdc.tms.model.dto.BookingPackageDetailDTO;
import com.prolog.rdc.tms.model.dto.BookingQueryDTO;
import com.prolog.rdc.tms.model.dto.BookingViewByOneDTO;
import com.prolog.rdc.tms.model.vo.BookingAdvancedQueryVO;
import com.prolog.rdc.tms.model.vo.BookingByIDVO;
import com.prolog.rdc.tms.model.vo.BookingByShipbillPopVO;
import com.prolog.rdc.tms.model.vo.BookingGetShipbillPopDTO;
import com.prolog.rdc.tms.model.vo.BookingGetShipbillVO;
import com.prolog.rdc.tms.model.vo.BookingItemDetailVO;
import com.prolog.rdc.tms.model.vo.BookingPackageDetailVO;
import com.prolog.rdc.tms.model.vo.BookingQueryExportAllVO;
import com.prolog.rdc.tms.model.vo.BookingQueryExportPageVO;
import com.prolog.rdc.tms.model.vo.BookingQueryVO;
import com.prolog.rdc.tms.model.vo.BookingUpdateBatchVO;
import com.prolog.rdc.tms.model.vo.BookingUpdateOneVO;
import com.prolog.rdc.tms.service.BookingService;
import com.prolog.wms.base.util.ExcelUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 园区管理-预约管理控制层
 * <p>
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/7/5 10:28
 */
@RestController
@Api(tags = "园区管理-预约管理控制层")
@RequestMapping("/booking")
public class BookingController {

	@Autowired
	private BookingService bookingService;

	@UserLog("主界面分页查询")
	@ApiOperation(value = "主界面分页查询", notes = "主界面分页查询")
	@PostMapping("/page")
	public RestMessage<Page<BookingQueryDTO>> queryPageExecute(@RequestBody @Valid BookingQueryVO bookingQueryVO) {
		Page<BookingQueryDTO> page = bookingService.queryPageData(bookingQueryVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("主界面高级查询")
	@ApiOperation(value = "主界面高级查询", notes = "主界面高级查询")
	@PostMapping("/advanced")
	public RestMessage<Page<BookingQueryDTO>> advancedQueryPageExecute(@RequestBody @Valid BookingAdvancedQueryVO bookingAdvancedQueryVO) {
		Page<BookingQueryDTO> page = bookingService.advancedQueryPageData(bookingAdvancedQueryVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("配载运单查询")
	@ApiOperation(value = "配载运单查询", notes = "配载运单查询")
	@PostMapping("/get_shipbill")
	public RestMessage<Page<BookingGetShipbillDTO>> getShipbillExecute(@RequestBody @Valid BookingGetShipbillVO bookingGetShipbillVO) {
		Page<BookingGetShipbillDTO> page = bookingService.getShipbillData(bookingGetShipbillVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("配载商品查询")
	@ApiOperation(value = "配载商品查询", notes = "配载商品查询")
	@PostMapping("/item_detail")
	public RestMessage<Page<BookingItemDetailDTO>> itemDetailExecute(@RequestBody @Valid BookingItemDetailVO bookingItemDetailVO) {
		Page<BookingItemDetailDTO> page = bookingService.itemDetailData(bookingItemDetailVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("配载包裹查询")
	@ApiOperation(value = "配载包裹查询", notes = "配载包裹查询")
	@PostMapping("/package_detail")
	public RestMessage<Page<BookingPackageDetailDTO>> packageDetailExecute(@RequestBody @Valid BookingPackageDetailVO bookingPackageDetailVO) {
		Page<BookingPackageDetailDTO> page = bookingService.packageDetailData(bookingPackageDetailVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("预约单审核")
	@ApiOperation(value = "预约单审核", notes = "预约单审核")
	@PostMapping("/review")
	public RestMessage<Object> reviewExecute(@RequestBody List<String> appointmentIds) {
		try {
			bookingService.reviewData(appointmentIds);
			return RestMessage.newInstance(true, "操作成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约单取消")
	@ApiOperation(value = "预约单取消", notes = "预约单取消")
	@PostMapping("/cancel")
	public RestMessage<Object> cancelExecute(@RequestBody List<String> appointmentIds) {
		try {
			bookingService.cancelData(appointmentIds);
			return RestMessage.newInstance(true, "操作成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约管理导出所有数据")
	@ApiOperation(value = "预约管理导出所有数据", notes = "预约管理导出所有数据")
	@PostMapping("/export_all")
	public RestMessage<Object> exportAllExecute(@RequestBody @Valid BookingQueryExportAllVO bookingQueryExportAllVO, HttpServletResponse response) {
		try {
			List<BookingExportDTO> list = bookingService.exportAllData(bookingQueryExportAllVO);
			ExcelUtils.export(response, BookingExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
			return RestMessage.newInstance(true, "导出成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约管理导出当前页数据")
	@ApiOperation(value = "预约管理导出当前页数据", notes = "预约管理导出当前页数据")
	@PostMapping("/export_page")
	public RestMessage<Object> exportPageExecute(@RequestBody @Valid BookingQueryExportPageVO bookingQueryExportPageVO, HttpServletResponse response) {
		try {
			List<BookingExportDTO> list = bookingService.exportPageData(bookingQueryExportPageVO);
			ExcelUtils.export(response, BookingExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
			return RestMessage.newInstance(true, "导出成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约管理导出选择行数据")
	@ApiOperation(value = "预约管理导出选择行数据", notes = "预约管理导出选择行数据")
	@PostMapping("/export_select_line")
	public RestMessage<Object> exportSelectLineExecute(@RequestBody List<String> appointmentIds, HttpServletResponse response) {
		try {
			List<BookingExportDTO> list = bookingService.exportSelectLineData(appointmentIds);
			ExcelUtils.export(response, BookingExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
			return RestMessage.newInstance(true, "导出成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("添加运单弹窗")
	@ApiOperation(value = "添加运单弹窗", notes = "添加运单弹窗")
	@PostMapping("/shipbill_pop")
	public RestMessage<Page<BookingGetShipbillPopDTO>> shipbillPopExecute(@RequestBody @Valid BookingByShipbillPopVO bookingByShipbillPopVO) {
		try {
			Page<BookingGetShipbillPopDTO> page = bookingService.shipbillPopData(bookingByShipbillPopVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();

			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约调整查询")
	@ApiOperation(value = "预约调整查询", notes = "预约调整查询")
	@PostMapping("/view_one")
	public RestMessage<BookingViewByOneDTO> viewOneExecute(@RequestBody @Valid BookingByIDVO bookingByIDVO) {
		try {
			BookingViewByOneDTO dto = bookingService.viewOneData(bookingByIDVO);
			return RestMessage.newInstance(true, "查询成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约调整确认")
	@ApiOperation(value = "预约调整确认", notes = "预约调整确认")
	@PostMapping("/update_one")
	public RestMessage<Object> updateOneExecute(@RequestBody @Valid BookingUpdateOneVO bookingUpdateOneVO) {
		try {
			bookingService.updateOneData(bookingUpdateOneVO);
			return RestMessage.newInstance(true, "操作成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("预约调整(批量)确认")
	@ApiOperation(value = "预约调整(批量)确认", notes = "预约调整(批量)确认")
	@PostMapping("/update_batch")
	public RestMessage<Object> updateBatchExecute(@RequestBody @Valid BookingUpdateBatchVO bookingUpdateBatchVO) {
		try {
			bookingService.updateBatchData(bookingUpdateBatchVO);
			return RestMessage.newInstance(true, "操作成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
