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 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 org.springframework.web.multipart.MultipartFile;

import com.prolog.common.util.ToolKits;
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.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerCarrierDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneeAndShipperPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneeByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.CustomerConsigneePopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ImportMatchingDataDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerPopDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerReceiverDTO;
import com.prolog.rdc.tms.model.dto.basicData.UserCustomerPopupDTO;
import com.prolog.rdc.tms.model.vo.IntfCustomerConsigneeSaveVO;
import com.prolog.rdc.tms.model.vo.IntfCustomerSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.ConsigneeAddressVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerCarrierVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerConsigneeAndShipperPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerConsigneeByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerConsigneePopupVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerReceiverSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.CustomerReceiverUpdateVO;
import com.prolog.rdc.tms.model.vo.basicData.ImportMatchingDataVO;
import com.prolog.rdc.tms.model.vo.basicData.QueryCustomerAndReceiverVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerReceiverQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCustomerUpdateVO;
import com.prolog.rdc.tms.model.vo.basicData.UserCustomerPopupVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CustomerImportStdVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CustomerReceiverImportStdVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.basicData.CustomerService;

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

/**
 *
 *
 * 基础数据-客户管理控制层
 *
 * @className :CustomerController
 * @author :xyf
 * @creationTime :2022/03/21
 * @description :
 *
 * @modifier :xyf
 * @modifyTime :2022/03/21
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@RestController
@Api(tags = "基础数据-客户管理控制层")
@RequestMapping("/tmsBaseCustomer")
public class CustomerController {

	@Autowired
	private CustomerService service;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("客户分页查询")
	@ApiOperation(value = "客户分页查询", notes = "客户分页查询")
	@PostMapping("/queryCustomer")
	public RestMessage<Page<TmsBaseCustomerDTO>> queryCustomer(@RequestBody TmsBaseCustomerQueryVO vo) throws Exception {
		Page<TmsBaseCustomerDTO> list = service.queryCustomer(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("客户弹出框分页查询")
	@ApiOperation(value = "客户弹出框分页查询", notes = "客户弹出框分页查询")
	@PostMapping("/queryCustomerPopup")
	public RestMessage<Page<TmsBaseCustomerDTO>> queryCustomerPopup(@RequestBody TmsBaseCustomerQueryVO vo) throws Exception {
		Page<TmsBaseCustomerDTO> list = service.queryTmsBaseCustomerPopup(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("客户收发货方弹窗")
	@ApiOperation(value = "客户收发货方弹窗", notes = "客户收发货方弹窗")
	@PostMapping("/consigneeAndShipper")
	public RestMessage<Page<CustomerConsigneeAndShipperPopupDTO>> popupCustomerConsigneeAndShipperExecute(@RequestBody CustomerConsigneeAndShipperPopupVO customerConsigneeAndShipperPopupVO) {
		try {
			Page<CustomerConsigneeAndShipperPopupDTO> customerConsigneeAndShipperPopupDTOs = service.popupCustomerConsigneeAndShipperData(customerConsigneeAndShipperPopupVO);
			return RestMessage.newInstance(true, "查询成功", customerConsigneeAndShipperPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("用户客户弹窗")
	@ApiOperation(value = "用户客户弹窗", notes = "用户客户弹窗")
	@PostMapping("/user/popup")
	public RestMessage<Page<UserCustomerPopupDTO>> popupUserCustomerExecute(@RequestBody UserCustomerPopupVO userCustomerPopupVO) {
		Page<UserCustomerPopupDTO> userCustomerPopupDTOs = service.popupUserCustomerData(userCustomerPopupVO);
		return RestMessage.newInstance(true, "查询成功", userCustomerPopupDTOs);
	}

	@UserLog("客户查看")
	@ApiOperation(value = "客户查看", notes = "客户查看")
	@PostMapping("/viewCustomer")
	public RestMessage<TmsBaseCustomerDTO> viewCustomer(@RequestParam(value = "id") Long id) throws Exception {
		TmsBaseCustomerDTO dto = service.viewCustomer(id);
		return RestMessage.newInstance(true, "操作成功", dto);
	}

	@UserLog("客户资料新增")
	@ApiOperation(value = "客户资料新增", notes = "客户资料新增")
	@PostMapping("/saveCustomer")
	public RestMessage<Object> saveCustomer(@RequestBody TmsBaseCustomerSaveVO vo) throws Exception {
		service.saveCustomer(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("客户复制")
	@ApiOperation(value = "客户复制", notes = "客户复制")
	@PostMapping("/copyCustomer")
	public RestMessage<Object> copyCustomer(@RequestBody TmsBaseCustomerSaveVO vo) throws Exception {
		service.copyCustomer(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

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

	@UserLog("客户删除(停用)")
	@ApiOperation(value = "客户删除(停用)", notes = "客户删除(停用)")
	@PostMapping("/deleteCustomer")
	public RestMessage<Object> deleteCustomer(@RequestBody List<String> ids) throws Exception {
		service.deleteCustomer(ids);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("客户启用")
	@ApiOperation(value = "客户启用", notes = "客户启用")
	@PostMapping("/activeCustomer")
	public RestMessage<Object> activeCustomer(@RequestBody List<String> ids) throws Exception {
		service.activeCustomer(ids);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("客户物理删除")
	@ApiOperation(value = "客户物理删除", notes = "客户物理删除")
	@PostMapping("/neverDelete")
	public RestMessage<Object> neverDelete(@RequestBody List<TmsBaseCustomerDeleteVO> list) throws Exception {
		service.neverDelete(list);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("收货方物理删除")
	@ApiOperation(value = "收货方物理删除", notes = "收货方物理删除")
	@PostMapping("/neverDeleteCustomerReceiver")
	public RestMessage<Object> neverDeleteCustomerReceiver(@RequestBody List<TmsBaseCustomerDeleteVO> list) throws Exception {
		service.neverDeleteCustomerReceiver(list);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("收货方查询")
	@ApiOperation(value = "收货方查询", notes = "收货方查询")
	@PostMapping("/queryCustomerReceiver")
	public RestMessage<List<TmsBaseCustomerReceiverDTO>> queryCustomerReceiver(@RequestParam(value = "customerId") Long customerId) throws Exception {
		List<TmsBaseCustomerReceiverDTO> list = service.queryCustomerReceiver(customerId);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("收货方查看")
	@ApiOperation(value = "收货方查看", notes = "收货方查看")
	@PostMapping("/viewCustomerReceiver")
	public RestMessage<TmsBaseCustomerReceiverDTO> viewCustomerReceiver(@RequestParam(value = "id") Long id) throws Exception {
		TmsBaseCustomerReceiverDTO dto = service.viewCustomerReceiver(id);
		return RestMessage.newInstance(true, "操作成功", dto);
	}

	@UserLog("收货方新增")
	@ApiOperation(value = "收货方新增", notes = "收货方新增")
	@PostMapping("/saveCustomerReceiver")
	public RestMessage<Object> saveCustomerReceiver(@RequestBody CustomerReceiverSaveVO customerReceiverSaveVO) {
		service.saveCustomerReceiver(customerReceiverSaveVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("客户收货方修改")
	@ApiOperation(value = "客户收货方修改", notes = "客户收货方修改")
	@PostMapping("/updateCustomerReceiver")
	public RestMessage<Object> updateCustomerReceiver(@RequestBody CustomerReceiverUpdateVO customerReceiverUpdateVO) {
		service.updateCustomerReceiver(customerReceiverUpdateVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("收货方停用")
	@ApiOperation(value = "收货方删除(停用)", notes = "收货方删除(停用)")
	@PostMapping("/deleteCustomerReceiver")
	public RestMessage<Object> deleteCustomerReceiver(@RequestBody List<String> ids) throws Exception {
		service.deleteCustomerReceiver(ids);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("收货方启用")
	@ApiOperation(value = "收货方启用", notes = "收货方启用")
	@PostMapping("/activeCustomerReceiver")
	public RestMessage<Object> activeCustomerReceiver(@RequestBody List<String> ids) throws Exception {
		service.activeCustomerReceiver(ids);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("地址解析专用客户分页查询")
	@ApiOperation(value = "地址解析专用客户分页查询", notes = "地址解析专用客户分页查询")
	@PostMapping("/queryCustomerForAddress")
	public RestMessage<Page<TmsBaseCustomerDTO>> queryCustomerForAddress(@RequestBody TmsBaseCustomerQueryVO vo) throws Exception {
		Page<TmsBaseCustomerDTO> list = service.queryCustomerForAddress(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("地址解析专用收货方分页查询")
	@ApiOperation(value = "地址解析专用收货方分页查询", notes = "地址解析专用收货方分页查询")
	@PostMapping("/queryCustomerReceiverForAddress")
	public RestMessage<Page<TmsBaseCustomerReceiverDTO>> queryCustomerReceiverForAddress(@RequestBody TmsBaseCustomerReceiverQueryVO vo) throws Exception {
		Page<TmsBaseCustomerReceiverDTO> list = service.queryCustomerReceiverForAddress(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("客户-收货方查询")
	@ApiOperation(value = "收货方查询", notes = "收货方查询")
	@PostMapping("/queryCustomerAndReceiver")
	public RestMessage<Page<TmsBaseCustomerPopDTO>> queryCustomerAndReceiver(@RequestBody QueryCustomerAndReceiverVO vo) throws Exception {
		Page<TmsBaseCustomerPopDTO> list = service.queryCustomerAndReceiver(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("客户批量导入")
	@ApiOperation(value = "客户批量导入", notes = "客户批量导入")
	@PostMapping("/batchImportCustomer")
	public RestMessage<Object> batchImportCustomerExcel(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception {
		try {
			// 导入批次
			String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
			ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
			service.batchImportCustomerData(file, importBatchNumber);
			return RestMessage.newInstance(true, "导入成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("收货方批量导入")
	@ApiOperation(value = "收货方批量导入", notes = "收货方批量导入")
	@PostMapping("/batchImportCustomerReceiver")
	public RestMessage<Object> batchImportCustomerReceiverExcel(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception {
		try {
			// 导入批次
			String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
			ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
			service.batchImportCustomerReceiverData(file, importBatchNumber);
			return RestMessage.newInstance(true, "导入成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("收货方地址解析")
	@ApiOperation(value = "收货方地址解析", notes = "收货方地址解析")
	@PostMapping("/parse_address")
	public RestMessage<String> parseCustomerConsigneeAddress(@RequestParam Long id, BigDecimal longitude, BigDecimal latitude, String address) {
		try {
			service.parseCustomerConsigneeAddress(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("/consignee/popup")
	public RestMessage<Page<CustomerConsigneePopupDTO>> popupCustomerConsigneeExecute(@RequestBody CustomerConsigneePopupVO customerConsigneePopupVO) {
		Page<CustomerConsigneePopupDTO> customerConsigneePopupDTOs = service.popupCustomerConsigneeData(customerConsigneePopupVO);
		return RestMessage.newInstance(true, "查询成功", customerConsigneePopupDTOs);
	}

	@UserLog("客户收货方模板")
	@ApiOperation(value = "客户收货方模板", notes = "客户收货方模板")
	@PostMapping("/consignee_download_template")
	public RestMessage<Object> consigneeDownloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, CustomerReceiverImportStdVO.class, "客户收货方模板", null, null, null, true);
		return null;
	}

	@UserLog("客户模板")
	@ApiOperation(value = "客户模板", notes = "客户模板")
	@PostMapping("/customer_download_template")
	public RestMessage<Object> customerDownloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, CustomerImportStdVO.class, "客户模板", null, null, null, true);
		return null;
	}

	@UserLog("根据客户编码名称查询客户资料")
	@ApiOperation(value = "根据客户编码名称查询客户资料", notes = "根据客户编码名称查询客户资料")
	@PostMapping("/by_encoding_name")
	public RestMessage<CustomerByEncodingNameDTO> getCustomerByEncodingNameExecute(@RequestBody CustomerByEncodingNameVO customerByEncodingNameVO) {
		try {
			CustomerByEncodingNameDTO customerByEncodingNameDTO = service.getCustomerByEncodingNameData(customerByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", customerByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据客户收货方编码名称查询客户收货方资料")
	@ApiOperation(value = "根据客户收货方编码名称查询客户收货方资料", notes = "根据客户收货方编码名称查询客户收货方资料")
	@PostMapping("/consignee_by_encoding_name")
	public RestMessage<CustomerConsigneeByEncodingNameDTO> getCustomerConsigneeByEncodingNameExecute(@RequestBody CustomerConsigneeByEncodingNameVO customerConsigneeByEncodingNameVO) {
		try {
			CustomerConsigneeByEncodingNameDTO customerConsigneeByEncodingNameDTO = service.getCustomerConsigneeByEncodingNameData(customerConsigneeByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", customerConsigneeByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("客户收货方匹配唯一承运方")
	@ApiOperation(value = "客户收货方匹配唯一承运方", notes = "客户收货方匹配唯一承运方")
	@PostMapping("/customer_carrier")
	public RestMessage<CustomerCarrierDTO> customerCarrierExecute(@RequestBody CustomerCarrierVO carrierVO) {
		CustomerCarrierDTO customerCarrier = service.customerCarrier(carrierVO);
		return RestMessage.newInstance(true, "查询成功", customerCarrier);
	}

	@UserLog("客户接口转TMS")
	@ApiOperation(value = "客户接口转TMS", notes = "客户接口转TMS")
	@PostMapping("/save/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveCustomerInterfaceExecute(@RequestBody List<IntfCustomerSaveVO> intfCustomerSaveVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = service.saveCustomerInterfaceData(intfCustomerSaveVOs);
			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("/saveConsignee/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveCustomerConsigneeInterfaceExecute(@RequestBody List<IntfCustomerConsigneeSaveVO> vos) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = service.saveCustomerConsigneeInterfaceData(vos);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

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

	@UserLog("临时客户收货方保存")
	@ApiOperation(value = "临时客户收货方保存", notes = "临时客户收货方保存")
	@PostMapping("/temp/consignee/save")
	public RestMessage<Object> tempConsigneeSave(@RequestBody ConsigneeAddressVO consigneeAddressVO) {
		service.tempConsigneeSave(consigneeAddressVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("临时客户收货方更新")
	@ApiOperation(value = "临时客户收货方更新", notes = "临时客户收货方更新")
	@PostMapping("/temp/consignee/update")
	public RestMessage<Object> tempConsigneeUpdate(@RequestBody ConsigneeAddressVO consigneeAddressVO) {
		service.tempConsigneeUpdate(consigneeAddressVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("临时客户收货方删除")
	@ApiOperation(value = "临时客户收货方删除", notes = "临时客户收货方删除")
	@PostMapping("/temp/consignee/delete")
	public RestMessage<Object> tempConsigneeDelete(@RequestBody List<Long> consigneeIds) {
		service.tempConsigneeDelete(consigneeIds);
		return RestMessage.newInstance(true, "操作成功", null);
	}

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

	@UserLog("根据客户名称查询客户资料")
	@ApiOperation(value = "根据客户名称查询客户资料", notes = "根据客户名称查询客户资料")
	@PostMapping("/by_name")
	public RestMessage<CustomerByEncodingNameDTO> getCustomerByNameExecute(@RequestBody CustomerByEncodingNameVO customerByEncodingNameVO) {
		try {
			CustomerByEncodingNameDTO customerByEncodingNameDTO = service.getCustomerByNameData(customerByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", customerByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
