package com.prolog.rdc.tms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

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.RequestParam;
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.DispatchAdsQueryDTO;
import com.prolog.rdc.tms.model.dto.DispatcheItemByPackagePageDTO;
import com.prolog.rdc.tms.model.dto.DispatcheItemPageDTO;
import com.prolog.rdc.tms.model.dto.DispatchePackageByItemPageDTO;
import com.prolog.rdc.tms.model.dto.DispatchePackagePageDTO;
import com.prolog.rdc.tms.model.dto.DispatchePageDTO;
import com.prolog.rdc.tms.model.dto.DispatcheViewDTO;
import com.prolog.rdc.tms.model.dto.ExportDispatchDTO;
import com.prolog.rdc.tms.model.vo.DetailItemByPackagePageVO;
import com.prolog.rdc.tms.model.vo.DetailItemPageVO;
import com.prolog.rdc.tms.model.vo.DetailPackageByItemPageVO;
import com.prolog.rdc.tms.model.vo.DetailPackagePageVO;
import com.prolog.rdc.tms.model.vo.DispatchAdvanceQueryVO;
import com.prolog.rdc.tms.model.vo.DispatchExportAllVO;
import com.prolog.rdc.tms.model.vo.DispatchExportPageVO;
import com.prolog.rdc.tms.model.vo.DispatchPageVO;
import com.prolog.rdc.tms.model.vo.DispatcheViewVO;
import com.prolog.rdc.tms.service.DispatchService;
import com.prolog.wms.base.util.ExcelUtils;

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

/**
 * 运单调度- 已调度控制层
 *
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/4/11 12:23
 */
@RestController
@Api(tags = "运单调度-已调度控制层")
@RequestMapping("/dispatch")
public class DispatchController {

	@Autowired
	private DispatchService service;

	@UserLog("分页查询")
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping("/page")
	public RestMessage<Page<DispatchePageDTO>> pageDispatchExecute(@RequestBody DispatchPageVO dispatchPageVO) {
		try {
			Page<DispatchePageDTO> page = service.pageDispatchData(dispatchPageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已调度高级查询")
	@ApiOperation(value = "已调度高级查询", notes = "已调度高级查询")
	@PostMapping("/advanced")
	public RestMessage<Page<DispatchAdsQueryDTO>> pageAdvancedExecute(@RequestBody DispatchAdvanceQueryVO advancedQueryVO) {
		try {
			Page<DispatchAdsQueryDTO> page = service.pageAdvanced(advancedQueryVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("查询商品")
	@ApiOperation(value = "查询商品", notes = "查询商品")
	@PostMapping("/page_detail_item")
	public RestMessage<Page<DispatcheItemPageDTO>> pageDetailItemExecute(@RequestBody DetailItemPageVO detailItemPageVO) {
		try {
			Page<DispatcheItemPageDTO> page = service.pageDetailItemData(detailItemPageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("查询包裹")
	@ApiOperation(value = "查询包裹", notes = "查询包裹")
	@PostMapping("/page_detail_package")
	public RestMessage<Page<DispatchePackagePageDTO>> pageDetailPackageExecute(@RequestBody DetailPackagePageVO detailPackagePageVO) {
		try {
			Page<DispatchePackagePageDTO> page = service.pageDetailPackageData(detailPackagePageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据商品查询包裹")
	@ApiOperation(value = "根据商品查询包裹", notes = "根据商品查询包裹")
	@PostMapping("/page_package_item")
	public RestMessage<Page<DispatchePackageByItemPageDTO>> pagePackageByItemExecute(@RequestBody DetailPackageByItemPageVO detailPackageByItemPageVO) {
		try {
			Page<DispatchePackageByItemPageDTO> page = service.pagePackageByItemData(detailPackageByItemPageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据包裹查询商品")
	@ApiOperation(value = "根据包裹查询商品", notes = "根据包裹查询商品")
	@PostMapping("/page_item_package")
	public RestMessage<Page<DispatcheItemByPackagePageDTO>> pageItemByPackageExecute(@RequestBody DetailItemByPackagePageVO detailItemByPackagePageVO) {
		try {
			Page<DispatcheItemByPackagePageDTO> page = service.pageItemByPackageData(detailItemByPackagePageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("导出所有数据")
	@ApiOperation(value = "导出所有数据", notes = "导出所有数据")
	@PostMapping("/export_all_dispatch")
	public RestMessage<Object> exportAllDispatchExecute(HttpServletResponse response, @RequestBody DispatchExportAllVO dispatchExportAllVO) {
		try {
			List<ExportDispatchDTO> list = service.exportAllDispatchData(dispatchExportAllVO);
			ExcelUtils.export(response, ExportDispatchDTO.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_dispatch")
	public RestMessage<Object> exportPageDispatchExecute(HttpServletResponse response, @RequestBody DispatchExportPageVO dispatchExportPageVO) {
		try {
			List<ExportDispatchDTO> list = service.exportPageDispatchData(dispatchExportPageVO);
			ExcelUtils.export(response, ExportDispatchDTO.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_choose_dispatch")
	public RestMessage<Object> exportChooseDispatchExecute(HttpServletResponse response, @RequestBody List<String> ids) {
		try {
			List<ExportDispatchDTO> list = service.exportChooseDispatchData(ids);
			ExcelUtils.export(response, ExportDispatchDTO.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("/print_detail")
	public RestMessage<Map<String, Object>> printDispatchExecute(@RequestParam("id") String id) {
		try {
			Map<String, Object> map = service.printDispatchData(id);
			return RestMessage.newInstance(true, "打印成功！", map);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已调度运单查看")
	@ApiOperation(value = "已调度运单查看", notes = "已调度运单查看")
	@PostMapping("/view")
	public RestMessage<DispatcheViewDTO> viewDispatchExecute(@RequestBody DispatcheViewVO dispatcheViewVO) {
		try {
			DispatcheViewDTO dto = service.viewDispatchData(dispatcheViewVO);
			return RestMessage.newInstance(true, "操作成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已调度运单撤销")
	@ApiOperation(value = "已调度运单撤销", notes = "已调度运单撤销")
	@PostMapping("/undo_Dispatch")
	public RestMessage<Object> undoDispatchExecute(@RequestBody List<String> ids) {
		try {
			service.undoDispatchData(ids,null);
			return RestMessage.newInstance(true, "撤销成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
}
