package com.prolog.rdc.tms.controller.basicData;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.prolog.rdc.tms.model.vo.UpdateLatitudeAndLongitudeVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderGeocodingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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 org.springframework.web.multipart.MultipartFile;

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.product.core.easyexcel.util.ExcelUtils;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.dto.basicData.ImportMatchingDataDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderByCargoOwnerPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderByIdsDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderConsigneeAndShipperPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderQueryPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderShipperCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderShipperPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ProviderShipperPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ShipperByIdsDTO;
import com.prolog.rdc.tms.model.dto.basicData.UserProviderPopupDTO;
import com.prolog.rdc.tms.model.vo.IntfProviderSaveVO;
import com.prolog.rdc.tms.model.vo.IntfProviderShipperSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.ImportMatchingDataVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderAddVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderByCargoOwnerPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderByIdsVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderCheckVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderConsigneeAndShipperPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderCopyVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperAddVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperCheckVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.basicData.ShipperByIdsVO;
import com.prolog.rdc.tms.model.vo.basicData.UserProviderPopupVO;
import com.prolog.rdc.tms.model.vo.xlsimport.ProviderImportStdVO;
import com.prolog.rdc.tms.model.vo.xlsimport.ShipperImportStdVO;
import com.prolog.rdc.tms.service.basicData.ProviderService;

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

/**
 * 基础资料-供应商管理控制层
 *
 * @author Eton Woo
 * @date 2022/03/25
 */
@Api(tags = "基础资料-供应商管理控制层")
@RestController
@RequestMapping("/provider")
public class ProviderController {

	@Autowired
	private ProviderService providerService;

	@UserLog("供应商资料分页查询")
	@ApiOperation(value = "供应商资料分页查询", notes = "供应商资料分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<ProviderPagingQueryDTO>> pageProviderExecute(@RequestBody ProviderPagingQueryVO providerPagingQueryVO) {
		try {
			Page<ProviderPagingQueryDTO> providerPagingQueryDTOs = providerService.pageProviderData(providerPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", providerPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商根据货主查询弹窗")
	@ApiOperation(value = "供应商根据货主查询弹窗", notes = "供应商根据货主查询弹窗")
	@PostMapping("/ByCargoOwner/popup")
	public RestMessage<Page<ProviderByCargoOwnerPopupDTO>> popupProviderByCargoOwnerExecute(@RequestBody ProviderByCargoOwnerPopupVO providerByCargoOwnerPopupVO) {
		try {
			Page<ProviderByCargoOwnerPopupDTO> providerByCargoOwnerPopupDTOs = providerService.popupProviderByCargoOwnerData(providerByCargoOwnerPopupVO);
			return RestMessage.newInstance(true, "查询成功", providerByCargoOwnerPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商收发货方弹窗")
	@ApiOperation(value = "供应商收发货方弹窗", notes = "供应商收发货方弹窗")
	@PostMapping("/consigneeAndShipper")
	public RestMessage<Page<ProviderConsigneeAndShipperPopupDTO>> popupProviderConsigneeAndShipperExecute(@RequestBody ProviderConsigneeAndShipperPopupVO providerConsigneeAndShipperPopupVO) {
		try {
			Page<ProviderConsigneeAndShipperPopupDTO> providerConsigneeAndShipperPopupDTOs = providerService.popupProviderConsigneeAndShipperData(providerConsigneeAndShipperPopupVO);
			return RestMessage.newInstance(true, "查询成功", providerConsigneeAndShipperPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商查询弹窗")
	@ApiOperation(value = "供应商查询弹窗", notes = "供应商查询弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<ProviderQueryPopupDTO>> popupProviderExecute(@RequestBody ProviderPopupVO providerPopupVO) {
		try {
			Page<ProviderQueryPopupDTO> providerQueryPopupDTOs = providerService.popupProviderData(providerPopupVO);
			return RestMessage.newInstance(true, "查询成功", providerQueryPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("用户供应商弹窗")
	@ApiOperation(value = "用户供应商弹窗", notes = "用户供应商弹窗")
	@PostMapping("/user/popup")
	public RestMessage<Page<UserProviderPopupDTO>> popupUserProviderExecute(@RequestBody UserProviderPopupVO userProviderPopupVO) {
		try {
			Page<UserProviderPopupDTO> userProviderPopupDTOs = providerService.popupUserProviderData(userProviderPopupVO);
			return RestMessage.newInstance(true, "查询成功", userProviderPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方资料分页查询")
	@ApiOperation(value = "供应商发货方资料分页查询", notes = "供应商发货方资料分页查询")
	@PostMapping("/paging_query_shipper")
	public RestMessage<Page<ProviderShipperPagingQueryDTO>> pageProviderShipperExecute(@RequestBody ProviderShipperPagingQueryVO providerShipperPagingQueryVO) {
		try {
			Page<ProviderShipperPagingQueryDTO> providerShipperPagingQueryDTOs = providerService.pageProviderShipperData(providerShipperPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", providerShipperPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方弹窗")
	@ApiOperation(value = "供应商发货方弹窗", notes = "供应商发货方弹窗")
	@PostMapping("/shipper/popup")
	public RestMessage<Page<ProviderShipperPopupDTO>> popupProviderShipperExecute(@RequestBody ProviderShipperPopupVO providerShipperPopupVO) {
		try {
			Page<ProviderShipperPopupDTO> providerShipperPopupDTOs = providerService.popupProviderShipperData(providerShipperPopupVO);
			return RestMessage.newInstance(true, "查询成功", providerShipperPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料批量删除")
	@ApiOperation(value = "供应商资料批量删除", notes = "供应商资料批量删除")
	@PostMapping("/batch_delete")
	public RestMessage<String> batchDeleteProviderExecute(@RequestBody List<Long> ids) {
		try {
			providerService.batchDeleteProviderData(ids);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料是否启用")
	@ApiOperation(value = "供应商资料是否启用", notes = "供应商资料是否启用")
	@PostMapping("/whether_to_enable")
	public RestMessage<String> whetherEnableProviderExecute(@RequestBody ProviderWhetherEnableVO providerWhetherEnableVO) {
		try {
			providerService.whetherEnableProviderData(providerWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料新增")
	@ApiOperation(value = "供应商资料新增", notes = "供应商资料新增")
	@PostMapping("/add")
	public RestMessage<String> addProviderExecute(@RequestBody ProviderAddVO providerAddVO) {
		try {
			return RestMessage.newInstance(true, "新增成功", providerService.addProviderData(providerAddVO));
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料查看")
	@ApiOperation(value = "供应商资料查看", notes = "供应商资料查看")
	@PostMapping("/check")
	public RestMessage<ProviderCheckDTO> getProviderExecute(@RequestBody ProviderCheckVO providerCheckVO) {
		try {
			ProviderCheckDTO providerCheckDTO = providerService.getProviderData(providerCheckVO);
			return RestMessage.newInstance(true, "查询成功", providerCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料复制")
	@ApiOperation(value = "供应商资料复制", notes = "供应商资料复制")
	@PostMapping("/copy")
	public RestMessage<String> copyProviderExecute(@RequestBody ProviderCopyVO providerCopyVO) {
		try {
			providerService.copyProviderData(providerCopyVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料修改")
	@ApiOperation(value = "供应商资料修改", notes = "供应商资料修改")
	@PostMapping("/modify")
	public RestMessage<String> modifyProviderExecute(@RequestBody ProviderModifyVO providerModifyVO) {
		try {
			providerService.modifyProviderData(providerModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方资料新增")
	@ApiOperation(value = "供应商发货方资料新增", notes = "供应商发货方资料新增")
	@PostMapping("/add_shipper")
	public RestMessage<String> addShipperExecute(@RequestBody ProviderShipperAddVO providerShipperAddVO) {
		try {
			return RestMessage.newInstance(true, "新增成功", providerService.addShipperData(providerShipperAddVO));
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方资料是否启用")
	@ApiOperation(value = "供应商发货方资料是否启用", notes = "供应商发货方资料是否启用")
	@PostMapping("/whether_to_enable_shipper")
	public RestMessage<String> whetherEnableShipperExecute(@RequestBody ProviderShipperWhetherEnableVO providerShipperWhetherEnableVO) {
		try {
			providerService.whetherEnableShipperData(providerShipperWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方资料批量删除")
	@ApiOperation(value = "供应商发货方资料批量删除", notes = "供应商发货方资料批量删除")
	@PostMapping("/batch_delete_shipper")
	public RestMessage<String> batchDeleteShipperExecute(@RequestBody List<Long> ids) {
		try {
			providerService.batchDeleteShipperData(ids);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方资料查看")
	@ApiOperation(value = "供应商发货方资料查看", notes = "供应商发货方资料查看")
	@PostMapping("/check_shipper")
	public RestMessage<ProviderShipperCheckDTO> getShipperExecute(@RequestBody ProviderShipperCheckVO providerShipperCheckVO) {
		try {
			ProviderShipperCheckDTO providerShipperCheckDTO = providerService.getShipperData(providerShipperCheckVO);
			return RestMessage.newInstance(true, "查询成功", providerShipperCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方资料修改")
	@ApiOperation(value = "供应商发货方资料修改", notes = "供应商发货方资料修改")
	@PostMapping("/modify_shipper")
	public RestMessage<String> modifyShipperExecute(@RequestBody ProviderShipperModifyVO providerShipperModifyVO) {
		try {
			providerService.modifyShipperData(providerShipperModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方批量导入")
	@ApiOperation(value = "供应商发货方批量导入", notes = "供应商发货方批量导入")
	@PostMapping("/batch_import_shipper")
	public RestMessage<String> batchImportShipperExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
		providerService.batchInsertProviderShipperData(file);
		return RestMessage.newInstance(true, "导入成功", null);
	}

	@UserLog("供应商发货方导入撤销")
	@ApiOperation(value = "供应商发货方导入撤销", notes = "供应商发货方导入撤销")
	@PostMapping("/rollback_shipper/{batchno}")
	public RestMessage<String> rollbackShipperExcel(@PathVariable("batchno") String batchno) throws Exception {
		try {
			providerService.rollbackShipperData(batchno);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商批量导入")
	@ApiOperation(value = "供应商批量导入", notes = "供应商批量导入")
	@PostMapping("/batch_import")
	public RestMessage<String> batchImportExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
		providerService.batchInsertProviderData(file);
		return RestMessage.newInstance(true, "导入成功", null);
	}

	@UserLog("供应商导入撤销")
	@ApiOperation(value = "供应商导入撤销", notes = "供应商导入撤销")
	@PostMapping("/rollback/{batchno}")
	public RestMessage<String> rollbackExcel(@PathVariable("batchno") String batchno) throws Exception {
		try {
			providerService.rollbackData(batchno);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据供应商ID集合分页查询")
	@ApiOperation(value = "根据供应商ID集合分页查询", notes = "根据供应商ID集合分页查询")
	@PostMapping("/byIds")
	public RestMessage<Page<ProviderByIdsDTO>> pageProviderByIdsExecute(@RequestBody ProviderByIdsVO providerByIdsVO) {
		try {
			Page<ProviderByIdsDTO> providerByIdsDTOs = providerService.pageProviderByIdsData(providerByIdsVO);
			return RestMessage.newInstance(true, "查询成功", providerByIdsDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据供应商发货方ID集合分页查询")
	@ApiOperation(value = "根据供应商发货方ID集合分页查询", notes = "根据供应商发货方ID集合分页查询")
	@PostMapping("/byShipperIds")
	public RestMessage<Page<ShipperByIdsDTO>> pageShipperByIdsExecute(@RequestBody ShipperByIdsVO shipperByIdsVO) {
		try {
			Page<ShipperByIdsDTO> shipperByIdsDTOs = providerService.pageShipperByIdsData(shipperByIdsVO);
			return RestMessage.newInstance(true, "查询成功", shipperByIdsDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("发货方地址解析")
	@ApiOperation(value = "发货方地址解析", notes = "发货方地址解析")
	@PostMapping("/parse_address_shipper")
	public RestMessage<String> parseCustomerConsigneeAddress(@RequestParam Long id, BigDecimal longitude, BigDecimal latitude, String address) {
		try {
			providerService.parseProviderShipperAddress(id, longitude, latitude, address);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方模板")
	@ApiOperation(value = "供应商发货方模板", notes = "供应商发货方模板")
	@PostMapping("/shipper_download_template")
	public RestMessage<Object> shipperDownloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, ShipperImportStdVO.class, "供应商发货方模板", null, null, null, true);
		return null;
	}

	@UserLog("供应商模板")
	@ApiOperation(value = "供应商模板", notes = "供应商模板")
	@PostMapping("/provider_download_template")
	public RestMessage<Object> providerDownloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, ProviderImportStdVO.class, "供应商模板", null, null, null, true);
		return null;
	}

	@UserLog("运单导入时匹配供应商资料")
	@ApiOperation(value = "运单导入时匹配供应商资料", notes = "运单导入时匹配供应商资料")
	@PostMapping("/shipbill/matching")
	public RestMessage<ImportMatchingDataDTO> importMatchingProviderDataExecute(@RequestBody ImportMatchingDataVO importMatchingDataVO) {
		try {
			ImportMatchingDataDTO importMatchingDataDTO = providerService.importMatchingData(importMatchingDataVO);
			return RestMessage.newInstance(true, "查询成功", importMatchingDataDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商接口转TMS")
	@ApiOperation(value = "供应商接口转TMS", notes = "供应商接口转TMS")
	@PostMapping("/save/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveProviderInterfaceExecute(@RequestBody List<IntfProviderSaveVO> intfProviderSaveVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = providerService.saveProviderInterfaceData(intfProviderSaveVOs);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商发货方接口转TMS")
	@ApiOperation(value = "供应商发货方接口转TMS", notes = "供应商发货方接口转TMS")
	@PostMapping("/save/shipper/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveProviderShipperInterfaceExecute(@RequestBody List<IntfProviderShipperSaveVO> intfProviderShipperSaveVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = providerService.saveProviderShipperInterfaceData(intfProviderShipperSaveVOs);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("供应商资料地址解析")
	@ApiOperation(value = "供应商资料地址解析", notes = "供应商资料地址解析")
	@PostMapping("/geocoding")
	public RestMessage<String> geocodingExecute(@RequestBody ProviderGeocodingVO vo) {
		providerService.geocodingData(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("更新发货方经纬度")
	@ApiOperation(value = "更新发货方经纬度", notes = "更新发货方经纬度")
	@PostMapping("/updateShipperLatitudeAndLongitude")
	public RestMessage<Object> updateShipperLatitudeAndLongitude(@RequestBody UpdateLatitudeAndLongitudeVO vo) {
		providerService.updateShipperLatitudeAndLongitude(vo.getIdList(), vo.getLatitude(), vo.getLongitude());
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("根据名称查询供应商资料")
	@ApiOperation(value = "根据名称查询供应商资料", notes = "根据名称查询供应商资料")
	@PostMapping("/by_name")
	public RestMessage<ProviderCheckDTO> getProviderByNameExecute(@RequestBody ProviderCheckVO providerCheckVO) {
		try {
			ProviderCheckDTO providerCheckDTO = providerService.getProviderByName(providerCheckVO);
			return RestMessage.newInstance(true, "查询成功", providerCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
}
