package com.prolog.rdc.tms.controller;

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.*;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.service.OutsourceTrackService;
import com.prolog.wms.base.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 运输管理——外包跟踪控制层
 *
 *
 * @author xyf
 * @since 2022/4/11 12:23
 */
@RestController
@Api(tags = "运输管理-外包跟踪控制层")
@RequestMapping("/outsource_track")
public class OutsourceTrackController {

	@Autowired
	private OutsourceTrackService service;

	@UserLog("主界面分页查询")
	@ApiOperation(value = "主界面分页查询", notes = "主界面分页查询")
	@PostMapping("/page")
	public RestMessage<Page<OutsourceTrackPageDTO>> queryOutsourceTrackPage(@RequestBody @Valid OutsourceTrackQueryVO vo) throws Exception {
		Page<OutsourceTrackPageDTO> page = service.queryOutsourceTrackPage(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("主界面高级查询")
	@ApiOperation(value = "主界面高级查询", notes = "主界面高级查询")
	@PostMapping("/advanced")
	public RestMessage<Page<OutsourceTrackPageDTO>> advancedQueryPage(@RequestBody @Valid OutsourceTrackAdvancedQueryVO vo) throws Exception {
		Page<OutsourceTrackPageDTO> page = service.advancedQueryPage(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("运单查询")
	@ApiOperation(value = "运单查询", notes = "运单查询")
	@PostMapping("/shipbill")
	public RestMessage<Page<OutsourceTrackShipbillDTO>> queryOutsourceTrackShipbill(@RequestBody @Valid OutsourceTrackShipbillQueryVO vo) throws Exception {
		Page<OutsourceTrackShipbillDTO> page = service.queryOutsourceTrackShipbill(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("包裹查询")
	@ApiOperation(value = "包裹查询", notes = "包裹查询")
	@PostMapping("/package")
	public RestMessage<Page<OutsourceTrackPackageDTO>> queryOutsourceTrackPackage(@RequestBody @Valid OutsourceTrackPackageQueryVO vo) throws Exception {
		Page<OutsourceTrackPackageDTO> page = service.queryOutsourceTrackPackage(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("商品查询")
	@ApiOperation(value = "商品查询", notes = "商品查询")
	@PostMapping("/item")
	public RestMessage<Page<OutsourceTrackItemDTO>> queryOutsourceTrackItem(@RequestBody @Valid OutsourceTrackItemQueryVO vo) throws Exception {
		Page<OutsourceTrackItemDTO> page = service.queryOutsourceTrackItem(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("托运交接")
	@ApiOperation(value = "托运交接", notes = "托运交接")
	@PostMapping("/handover")
	public RestMessage<Object> consignmentHandover(@RequestBody @Valid ConsignmentHandoverVO vo) throws Exception {
		service.consignmentHandover(vo);
		return RestMessage.newInstance(true, "托运交接成功！", null);
	}

	@UserLog("正本回单确认")
	@ApiOperation(value = "正本回单确认", notes = "正本回单确认")
	@PostMapping("/confirm")
	public RestMessage<Object> returnOrderConfirm(@RequestBody @Valid ReturnOrderConfirmVO vo) throws Exception {
		service.returnOrderConfirm(vo);
		return RestMessage.newInstance(true, "正本回单确认操作成功！", null);
	}

	@UserLog("所有运单导出")
	@ApiOperation(value = "所有运单导出", notes = "所有运单导出")
	@PostMapping("/export_all_shipbill")
	public RestMessage<Object> export(@RequestBody OutsourceTrackExportVO vo, HttpServletResponse response) throws Exception {
		List<OutsourceTrackExportDTO> list = service.exportAllOutsourceTrack(vo);
		ExcelUtils.export(response, OutsourceTrackExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	@UserLog("所有运单分页导出")
	@ApiOperation(value = "所有运单分页导出", notes = "所有运单分页导出")
	@PostMapping("/exportPage")
	public RestMessage<Object> exportPage(@RequestBody OutsourceTrackExportVO vo, HttpServletResponse response) throws Exception {
		List<OutsourceTrackExportDTO> list = service.exportPageOutsourceTrack(vo);
		ExcelUtils.export(response, OutsourceTrackExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	@UserLog("所有运单选中行导出")
	@ApiOperation(value = "所有运单选中行导出", notes = "所有运单选中行导出")
	@PostMapping("/exportSelected")
	public RestMessage<Object> exportSelected(@RequestBody List<String> ids, HttpServletResponse response) throws Exception {
		List<OutsourceTrackExportDTO> list = service.exportSelected(ids);
		ExcelUtils.export(response, OutsourceTrackExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

}
