package com.prolog.rdc.tms.controller;

import java.util.List;

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.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.basicConfigure.CarrierConfigPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.CarrierConfigViewDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.ShipbillMatchCarrierConfigDTO;
import com.prolog.rdc.tms.model.vo.basicConfigure.CarrierConfigPageVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.CarrierConfigSaveVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.CarrierConfigUpdateVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.CarrierConfigViewVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.ShipbillMatchCarrierVO;
import com.prolog.rdc.tms.service.CarrierConfigService;

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

/**
 * 承运方配置业务控制层
 *
 * @author :lhb
 * @version :1.0.0
 * @className :CarrierConfigController
 * @creationTime :2022/05/07
 * @description :
 * @modifier :
 * @modifyTime :
 * @reviseRemarks:
 */
@RestController
@Api(tags = "策略中心-承运方配置业务控制层")
@RequestMapping("/carrier_config")
public class CarrierConfigController {

	@Autowired
	private CarrierConfigService carrierConfigService;

	@UserLog("承运方配置分页查询")
	@ApiOperation(httpMethod = "POST", value = "承运方配置分页查询")
	@PostMapping(value = "/page")
	public RestMessage<Page<CarrierConfigPageDTO>> pageQueryExecute(@RequestBody CarrierConfigPageVO pageVO) {
		try {
			Page<CarrierConfigPageDTO> pageDTOs = carrierConfigService.pageQuery(pageVO);
			return RestMessage.newInstance(true, "查询成功", pageDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运方配置保存")
	@ApiOperation(httpMethod = "POST", value = "承运方配置保存")
	@PostMapping(value = "/save")
	public RestMessage<String> saveExecute(@RequestBody CarrierConfigSaveVO carrierConfigSaveVO) {
		try {
			carrierConfigService.save(carrierConfigSaveVO);
			return RestMessage.newInstance(true, "保存成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运方配置修改")
	@ApiOperation(httpMethod = "POST", value = "承运方配置修改")
	@PostMapping(value = "/update")
	public RestMessage<String> updateExecute(@RequestBody CarrierConfigUpdateVO carrierConfigUpdateVO) {
		try {
			carrierConfigService.update(carrierConfigUpdateVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运方配置停用")
	@ApiOperation(httpMethod = "POST", value = "承运方配置停用")
	@PostMapping(value = "/stop")
	public RestMessage<String> stopExecute(@RequestBody List<Long> ids) {
		try {
			carrierConfigService.stop(ids);
			return RestMessage.newInstance(true, "停用成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运方配置启用")
	@ApiOperation(httpMethod = "POST", value = "承运方配置启用")
	@PostMapping(value = "/enable")
	public RestMessage<String> enableExecute(@RequestBody List<Long> ids) {
		try {
			carrierConfigService.enable(ids);
			return RestMessage.newInstance(true, "启用成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运方配置查看")
	@ApiOperation(httpMethod = "POST", value = "承运方配置查看")
	@PostMapping(value = "/view")
	public RestMessage<CarrierConfigViewDTO> viewExecute(@RequestBody CarrierConfigViewVO viewVO) {
		try {
			CarrierConfigViewDTO viewDTO = carrierConfigService.view(viewVO);
			return RestMessage.newInstance(true, "查看成功", viewDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据运单适配匹配承运方配置")
	@ApiOperation(httpMethod = "POST", value = "根据运单适配匹配承运方配置")
	@PostMapping(value = "/shipbill_match_carrier_config")
	public RestMessage<ShipbillMatchCarrierConfigDTO> shipbillMatchCarrierExecute(@RequestBody ShipbillMatchCarrierVO matchCarrierVO) {
		try {
			ShipbillMatchCarrierConfigDTO carrierDTO = carrierConfigService.shipbillMatchCarrier(matchCarrierVO);
			return RestMessage.newInstance(true, "承运方配置匹配成功", carrierDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
