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 javax.validation.Valid;

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.basicData.AppointTimeQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.BranchByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.BranchByNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.BranchConsigneeAndShipperPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ImportMatchingDataDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseBranchApTimeDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseBranchDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCarportDTO;
import com.prolog.rdc.tms.model.entity.line.TmsBaseBranchModel;
import com.prolog.rdc.tms.model.vo.AppointTimeQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.BranchByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.BranchByNameVO;
import com.prolog.rdc.tms.model.vo.basicData.BranchConsigneeAndShipperPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ImportMatchingDataVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseBranchQueryPopVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseBranchQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseBranchSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseBranchUpdateVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCarportSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.TmsBaseCarportUpdateVO;
import com.prolog.rdc.tms.model.vo.xlsimport.BranchImportStdVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CarportImportStdVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.basicData.BranchService;

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

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

	@Autowired
	private BranchService service;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("仓库分页查询")
	@ApiOperation(value = "仓库分页查询", notes = "仓库分页查询")
	@PostMapping("/queryBranch")
	public RestMessage<Page<TmsBaseBranchDTO>> queryBranch(@RequestBody TmsBaseBranchQueryVO vo) throws Exception {
		Page<TmsBaseBranchDTO> list = service.queryBranch(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("仓库弹出框分页查询")
	@ApiOperation(value = "仓库弹出框分页查询", notes = "仓库弹出框分页查询")
	@PostMapping("/queryBranchPopup")
	public RestMessage<Page<TmsBaseBranchDTO>> queryBranchPopup(@RequestBody TmsBaseBranchQueryVO vo) throws Exception {
		Page<TmsBaseBranchDTO> list = service.queryBranchPopup(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("仓库收发货方弹窗")
	@ApiOperation(value = "仓库收发货方弹窗", notes = "仓库收发货方弹窗")
	@PostMapping("/consigneeAndShipper")
	public RestMessage<Page<BranchConsigneeAndShipperPopupDTO>> popupBranchConsigneeAndShipperExecute(@RequestBody BranchConsigneeAndShipperPopupVO branchConsigneeAndShipperPopupVO) {
		try {
			Page<BranchConsigneeAndShipperPopupDTO> branchConsigneeAndShipperPopupDTOs = service.popupBranchConsigneeAndShipperData(branchConsigneeAndShipperPopupVO);
			return RestMessage.newInstance(true, "查询成功", branchConsigneeAndShipperPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

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

	@UserLog("仓库新增")
	@ApiOperation(value = "仓库新增", notes = "仓库新增")
	@PostMapping("/saveBranch")
	public RestMessage<Object> saveBranch(@RequestBody @Valid TmsBaseBranchSaveVO vo) throws Exception {
		service.saveBranch(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

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

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

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

	@UserLog("仓库批量删除")
	@ApiOperation(value = "仓库批量删除", notes = "仓库批量删除")
	@PostMapping("/batch_delete")
	public RestMessage<String> batchDeleteBranchExecute(@RequestBody List<Long> ids) {
		try {
			service.batchDeleteBranchData(ids);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

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

	@UserLog("车位查询")
	@ApiOperation(value = "车位查询", notes = "车位查询")
	@PostMapping("/queryCarport")
	public RestMessage<List<TmsBaseCarportDTO>> queryCarport(@RequestParam(value = "branchid") Long branchid, @RequestParam(value = "userId") String userId) throws Exception {
		List<TmsBaseCarportDTO> list = service.queryCarport(branchid, userId);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("车位列表查询")
	@ApiOperation(value = "车位列表查询", notes = "车位列表查询")
	@PostMapping("/queryCarport_list")
	public RestMessage<List<TmsBaseCarportDTO>> queryCarportList(@RequestParam(value = "branchid") Long branchid) throws Exception {
		List<TmsBaseCarportDTO> list = service.queryCarportList(branchid);
		return RestMessage.newInstance(true, "操作成功", list);
	}

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

	@UserLog("车位新增")
	@ApiOperation(value = "车位新增", notes = "车位新增")
	@PostMapping("/saveCarport")
	public RestMessage<Object> saveCarport(@RequestBody TmsBaseCarportSaveVO vo) throws Exception {
		service.saveCarport(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

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

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

	@UserLog("车位批量删除")
	@ApiOperation(value = "车位批量删除", notes = "车位批量删除")
	@PostMapping("/batch_delete_carport")
	public RestMessage<String> batchDeleteCarportExecute(@RequestBody List<Long> ids) {
		try {
			service.batchDeleteCarportData(ids);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

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

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

	@UserLog("地址解析")
	@ApiOperation(value = "地址解析", notes = "地址解析")
	@PostMapping("/parse_address")
	public RestMessage<String> parseBranchAddress(@RequestParam Long id, BigDecimal longitude, BigDecimal latitude, String address) {
		try {
			service.parseBranchAddress(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("/carport_download_template")
	public RestMessage<Object> carportDownloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, CarportImportStdVO.class, "仓库车位模板", null, null, null, true);
		return null;
	}

	@UserLog("仓库模板")
	@ApiOperation(value = "仓库模板", notes = "仓库模板")
	@PostMapping("/branch_download_template")
	public RestMessage<Object> branchDownloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, BranchImportStdVO.class, "仓库模板", null, null, null, true);
		return null;
	}

	@UserLog("仓库导入")
	@ApiOperation(value = "仓库导入", notes = "仓库导入")
	@PostMapping("/import")
	public RestMessage<String> batchImportBranchExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
		service.batchImportBranchData(file, importBatchNumber);
		return RestMessage.newInstance(true, "导入成功", null);
	}

	@UserLog("仓库车位导入")
	@ApiOperation(value = "仓库车位导入", notes = "仓库车位导入")
	@PostMapping("/parking_space_import")
	public RestMessage<String> batchImportCarportExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
		service.batchImportCarportData(file, importBatchNumber);
		return RestMessage.newInstance(true, "导入成功", null);
	}

	@UserLog("根据仓库编码名称查询仓库资料")
	@ApiOperation(value = "根据仓库编码名称查询仓库资料", notes = "根据仓库编码名称查询仓库资料")
	@PostMapping("/by_encoding_name")
	public RestMessage<BranchByEncodingNameDTO> getBranchByEncodingNameExecute(@RequestBody BranchByEncodingNameVO branchByEncodingNameVO) {
		try {
			BranchByEncodingNameDTO branchByEncodingNameDTO = service.getBranchByEncodingNameData(branchByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", branchByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据仓库编码名称查询仓库资料")
	@ApiOperation(value = "根据仓库编码名称查询仓库资料", notes = "根据仓库编码名称查询仓库资料")
	@PostMapping("/by_encoding_name_intf")
	public RestMessage<BranchByEncodingNameDTO> getBranchByEncodingNameDataIntf(@RequestBody BranchByEncodingNameVO branchByEncodingNameVO) {
		try {
			BranchByEncodingNameDTO branchByEncodingNameDTO = service.getBranchByEncodingNameDataIntf(branchByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", branchByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据仓库名称查询仓库资料")
	@ApiOperation(value = "根据仓库名称查询仓库资料", notes = "根据仓库名称查询仓库资料")
	@PostMapping("/by_name")
	public RestMessage<BranchByNameDTO> getBranchByNameExecute(@RequestBody BranchByNameVO branchByNameVO) {
		try {
			BranchByNameDTO branchByNameDTO = service.getBranchByNameData(branchByNameVO);
			return RestMessage.newInstance(true, "查询成功", branchByNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("查询仓库可预约时间段")
	@ApiOperation(value = "查询仓库可预约时间段", notes = "查询仓库可预约时间段")
	@PostMapping("/queryAppointTime")
	public RestMessage<List<TmsBaseBranchApTimeDTO>> queryAppointTime(@RequestBody @Valid AppointTimeQueryVO vo) throws Exception {
		List<TmsBaseBranchApTimeDTO> list = service.queryAppointTime(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("仓库弹出框分页查询(预约管理)")
	@ApiOperation(value = "仓库弹出框分页查询(预约管理)", notes = "仓库弹出框分页查询(预约管理)")
	@PostMapping("/queryBranchByBooking")
	public RestMessage<Page<TmsBaseBranchModel>> queryBranchByBooking(@RequestBody TmsBaseBranchQueryPopVO vo) throws Exception {
		Page<TmsBaseBranchModel> list = service.queryBranchByBooking(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("查询仓库可预约时间段(拼接)")
	@ApiOperation(value = "查询仓库可预约时间段(拼接)", notes = "查询仓库可预约时间段(拼接)")
	@PostMapping("/queryBranchTime")
	public RestMessage<List<AppointTimeQueryDTO>> queryBranchTime(@RequestBody @Valid AppointTimeQueryVO vo) throws Exception {
		List<AppointTimeQueryDTO> list = service.queryBranchTime(vo);
		return RestMessage.newInstance(true, "操作成功", list);
	}

	@UserLog("通过仓库ID查询仓库")
	@ApiOperation(value = "通过仓库ID查询仓库", notes = "通过仓库ID查询仓库")
	@PostMapping("/getOneBranch")
	public RestMessage<TmsBaseBranchDTO> getOneBranch(@RequestParam(value = "branchId") Long branchId) throws Exception {
		try {
			TmsBaseBranchDTO dto = service.getOneBranch(branchId);
			return RestMessage.newInstance(true, "操作成功", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单导入时匹配仓资料")
	@ApiOperation(value = "运单导入时匹配仓资料", notes = "运单导入时匹配仓资料")
	@PostMapping("/shipbill/matching")
	public RestMessage<ImportMatchingDataDTO> importMatchingBranchDataExecute(@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("/updateBranchLatitudeAndLongitude")
	public RestMessage<Object> updateBranchLatitudeAndLongitude(@RequestBody UpdateLatitudeAndLongitudeVO vo) {
		service.updateBranchLatitudeAndLongitude(vo.getIdList(), vo.getLatitude(), vo.getLongitude());
		return RestMessage.newInstance(true, "操作成功", null);
	}
}
