package com.prolog.rdc.tms.controller;

import java.util.List;

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

import com.prolog.rdc.tms.model.dto.*;
import com.prolog.rdc.tms.model.vo.*;
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.service.AllShipbillService;
import com.prolog.wms.base.util.ExcelUtils;

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

/**
 * 运输管理——所有运单控制层
 * 
 * @author xyf
 * @since 2022/4/11 12:23
 */
@RestController
@Api(tags = "运输管理—所有运单控制层")
@RequestMapping("/allShipbill")
public class AllShipbillController {

	@Autowired
	private AllShipbillService service;

	@UserLog("运单分页查询")
	@ApiOperation(value = "运单分页查询", notes = "运单分页查询")
	@PostMapping("/queryAllShipbillPage")
	public RestMessage<Page<AllShipbillPageDTO>> queryAllShipbillPage(@RequestBody @Valid AllShipbillQueryVO vo) {
		Page<AllShipbillPageDTO> page = service.queryAllShipbillPage(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("运单高级查询")
	@ApiOperation(value = "运单高级查询", notes = "运单高级查询")
	@PostMapping("/advanced")
	public RestMessage<Page<AllShipbillPageDTO>> pageAdvancedExecute(@RequestBody AllShipbillAdsQueryVO vo) {
		Page<AllShipbillPageDTO> page = service.pageAdvanced(vo);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("运单商品查询")
	@ApiOperation(value = "运单商品查询", notes = "运单商品查询")
	@PostMapping("/listItemDetailData")
	public RestMessage<Page<AllShipbillItemDTO>> listItemDetailData(@RequestBody @Valid AllShipbillItemQueryVO allShipbillItemQueryVO) {
		Page<AllShipbillItemDTO> page = service.listItemDetailData(allShipbillItemQueryVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("运单包裹查询")
	@ApiOperation(value = "运单包裹查询", notes = "运单包裹查询")
	@PostMapping("/listPackageData")
	public RestMessage<Page<AllShipbillPackageDTO>> listPackageData(@RequestBody @Valid AllShipbillPackageQueryVO allShipbillPackageQueryVO) {
		Page<AllShipbillPackageDTO> page = service.listPackageData(allShipbillPackageQueryVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("客户反馈")
	@ApiOperation(value = "客户反馈", notes = "客户反馈")
	@PostMapping("/feedBack")
	public RestMessage<Object> feedBack(@RequestBody @Valid FeedBackVO vo) {
		service.feedBack(vo);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	@UserLog("异常登记")
	@ApiOperation(value = "异常登记", notes = "异常登记")
	@PostMapping("/abnormalRegister")
	public RestMessage<Object> abnormalRegister(@RequestBody @Valid AbnormalRegisterVO vo) {
		service.abnormalRegister(vo);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	@UserLog("运单修改")
	@ApiOperation(value = "运单修改", notes = "运单修改")
	@PostMapping("/update")
	public RestMessage<Object> update(@RequestBody AllShipbillUpdateVO vo) throws Exception {
		service.update(vo);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	@UserLog("所有运单导出")
	@ApiOperation(value = "所有运单导出", notes = "所有运单导出")
	@PostMapping("/export")
	public RestMessage<Object> export(@RequestBody AllShipbillQueryVO vo, HttpServletResponse response) throws Exception {
		List<AllShipbillExportDTO> list = service.export(vo);
		ExcelUtils.export(response, AllShipbillExportDTO.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 AllShipbillQueryVO vo, HttpServletResponse response) throws Exception {
		List<AllShipbillExportDTO> list = service.exportPage(vo);
		ExcelUtils.export(response, AllShipbillExportDTO.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<AllShipbillExportDTO> list = service.exportSelected(ids);
		ExcelUtils.export(response, AllShipbillExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	// STD-DEVELOP20220823 lhb 运单件重体调整 20220913 start
	@UserLog("调整运单")
	@ApiOperation(value = "调整运单", notes = "调整运单")
	@PostMapping("/adjust")
	public RestMessage<String> shipbillUpdate(@RequestBody ShipbillAdjustVO adjustVO) {
		try {
			service.shipbillUpdate(adjustVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
	// STD-DEVELOP20220823 lhb 运单件重体调整 20220913 end

	// STD-DEVELOP20220823 lhb 销售单模式 20220919 start
	@UserLog("销售单模式查询")
	@ApiOperation(value = "销售单模式查询", notes = "销售单模式查询")
	@PostMapping("/query_sell")
	public RestMessage<Page<AllShipbillPageDTO>> querySellAllShipbillPage(@RequestBody @Valid AllShipbillQueryVO vo) {
		try {
			Page<AllShipbillPageDTO> page = service.querySellAllShipbillPage(vo);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("发货单信息查询")
	@ApiOperation(value = "发货单信息查询", notes = "发货单信息查询")
	@PostMapping("/query_ship")
	public RestMessage<Page<AllShipbillPageDTO>> queryShipShipbill(@RequestBody @Valid AllShipbillSellQueryVO vo) {
		try {
			Page<AllShipbillPageDTO> page = service.queryShipShipbill(vo);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("备货等待信息查询")
	@ApiOperation(value = "备货等待信息查询", notes = "备货等待信息查询")
	@PostMapping("/query_stocking_waiting")
	public RestMessage<Page<AllShipbillPageDTO>> queryStockingWaitingShipbill(@RequestBody @Valid AllShipbillSellQueryVO vo) {
		try {
			Page<AllShipbillPageDTO> page = service.queryStockingWaitingShipbill(vo);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("关闭信息查询")
	@ApiOperation(value = "关闭信息查询", notes = "关闭信息查询")
	@PostMapping("/query_closing")
	public RestMessage<Page<AllShipbillPageDTO>> queryClosingShipbill(@RequestBody @Valid AllShipbillSellQueryVO vo) {
		try {
			Page<AllShipbillPageDTO> page = service.queryClosingShipbill(vo);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("关闭运单")
	@ApiOperation(value = "关闭运单", notes = "关闭运单")
	@PostMapping("/close")
	public RestMessage<Object> closeShipbill(@RequestBody ShipbillCloseVO vo) {
		try {
			service.closeShipbill(vo);
			return RestMessage.newInstance(true, "关闭运单成功！", null);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	// STD-DEVELOP20220823 lhb 销售单模式 20220919 end

	@UserLog("关联运单")
	@ApiOperation(value = "关联运单", notes = "关联运单")
	@PostMapping("/related_shipbill")
	public RestMessage<List<RelatedShipbillDTO>> queryRelatedShipbill(@RequestBody RelatedShipbillQueryVO relatedShipbillVO) {
		try {
			List<RelatedShipbillDTO> relatedShipbillDTOS = service.queryRelatedShipbill(relatedShipbillVO);
			return RestMessage.newInstance(true, "关联运单取得成功", relatedShipbillDTOS);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("评价信息查询")
	@ApiOperation(value = "评价信息查询", notes = "评价信息查询")
	@PostMapping("/query_evaluation")
	public RestMessage<Page<ShipbillEvaluationDTO>> queryShipbillEvaluation(@RequestBody @Valid AllShipbillSellQueryVO vo) {
		try {
			Page<ShipbillEvaluationDTO> page = service.queryShipbillEvaluation(vo);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单温度轨迹信息查询")
	@ApiOperation(value = "运单温度轨迹信息查询", notes = "运单温度轨迹信息查询")
	@PostMapping("/query_shipbill_temperature")
	public RestMessage<ShipBillTemperatureTrackDTO> queryShipbillTemperature(@RequestBody ShipbillViewVO vo) {
		try {
			ShipBillTemperatureTrackDTO dto = service.queryShipbillTemperature(vo);
			return RestMessage.newInstance(true, "查询成功！", dto);
		} catch (Exception e) {
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
