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

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

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

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.ExpensePopDTO;
import com.prolog.rdc.tms.model.dto.SettlementTypeDTO;
import com.prolog.rdc.tms.model.dto.basicData.BaseFeeDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierApprovalInfoDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierByIdPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarrierPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.CarriersByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.UserCarrierPopupDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.ContainerDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeliverymanPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeviceDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.PaperDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePagingQueryDTO;
import com.prolog.rdc.tms.model.vo.AccountSaveInfoVO;
import com.prolog.rdc.tms.model.vo.FeeSaveInfoVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierAddVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalInfoVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalInvalidVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalRetractVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalSubmitVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierApprovalVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierByIdPageVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierCopyVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierModifySysFlagVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierPageVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.CarrierRelationInfoVO;
import com.prolog.rdc.tms.model.vo.basicData.CarriersByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.SettlementTypeVO;
import com.prolog.rdc.tms.model.vo.basicData.UserCarrierPopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.CarrierPaperLinkVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.CarrierPaperWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.CarriersImportStdVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.basicData.CarriersService;

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

/**
 *
 *
 * 基础数据-承运商管理控制层
 *
 * @className :TmsBaseCarriersController
 * @author :zsj
 * @creationTime :2022/03/21
 * @description :
 *
 * @modifier :zsj
 * @modifyTime :2022/03/21
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@RestController
@Api(tags = "基础数据-承运商管理控制层")
@RequestMapping("/carrier")
public class CarriersController {

	@Autowired
	private CarriersService service;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("承运商分页查询")
	@ApiOperation(value = "承运商分页查询", notes = "承运商分页查询")
	@PostMapping("/page")
	public RestMessage<Page<CarrierPageDTO>> pageCarrierExecute(@RequestBody CarrierPageVO carrierPageVO) {
		Page<CarrierPageDTO> carrierPageDTOs = service.pageCarrierData(carrierPageVO);
		return RestMessage.newInstance(true, "查询成功", carrierPageDTOs);
	}

	@UserLog("承运商弹框查询")
	@ApiOperation(value = "承运商弹框查询", notes = "承运商弹框查询")
	@PostMapping("/popup")
	public RestMessage<Page<CarrierPopupDTO>> popupCarrierExecute(@RequestBody CarrierPopupVO carrierPopupVO) {
		Page<CarrierPopupDTO> carrierPopupDTOs = service.popupCarrierData(carrierPopupVO);
		return RestMessage.newInstance(true, "查询成功", carrierPopupDTOs);
	}

	@UserLog("用户承运商弹框")
	@ApiOperation(value = "用户承运商弹框", notes = "用户承运商弹框")
	@PostMapping("/user/popup")
	public RestMessage<Page<UserCarrierPopupDTO>> popupUserCarrierExecute(@RequestBody UserCarrierPopupVO userCarrierPopupVO) {
		Page<UserCarrierPopupDTO> userCarrierPopupDTOs = service.popupUserCarrierData(userCarrierPopupVO);
		return RestMessage.newInstance(true, "查询成功", userCarrierPopupDTOs);
	}

	@UserLog("承运商查看")
	@ApiOperation(value = "承运商查看", notes = "承运商查看")
	@PostMapping("/check")
	public RestMessage<CarrierCheckDTO> getCarrierExecute(@RequestParam(value = "id") Long id) throws Exception{
		CarrierCheckDTO carrierCheckDTO = service.getCarrierData(id);
		return RestMessage.newInstance(true, "查询成功", carrierCheckDTO);
	}

	@UserLog("承运商新增")
	@ApiOperation(value = "承运商新增", notes = "承运商新增")
	@PostMapping("/add")
	public RestMessage<Object> addCarrierExecute(@RequestBody CarrierAddVO carrierAddVO) {
		service.addCarrierData(carrierAddVO);
		return RestMessage.newInstance(true, "新增成功", null);
	}

	@UserLog("承运商复制")
	@ApiOperation(value = "承运商复制", notes = "承运商复制")
	@PostMapping("/copy")
	public RestMessage<Object> copyCarrierExecute(@RequestBody CarrierCopyVO carrierCopyVO) {
		service.copyCarrierData(carrierCopyVO);
		return RestMessage.newInstance(true, "复制成功", null);
	}

	@UserLog("承运商修改")
	@ApiOperation(value = "承运商修改", notes = "承运商修改")
	@PostMapping("/modify")
	public RestMessage<Object> modifyCarrierExecute(@RequestBody CarrierModifyVO carrierModifyVO) {
		service.modifyCarrierData(carrierModifyVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("承运商系统标志修改")
	@ApiOperation(value = "承运商系统标志修改", notes = "承运商系统标志修改")
	@PostMapping("/modify_sys_flag")
	public RestMessage<Object> modifyCarrierSysFlagExecute(@RequestBody CarrierModifySysFlagVO carrierModifyVO) {
		service.modifyCarrierSysFlagData(carrierModifyVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("承运商停用")
	@ApiOperation(value = "承运商停用", notes = "承运商停用")
	@PostMapping("/disable")
	public RestMessage<Object> disableCarrierExecute(@RequestBody List<String> ids) {
		service.disableCarrierData(ids);
		return RestMessage.newInstance(true, "停用成功", null);
	}

	@UserLog("承运商启用")
	@ApiOperation(value = "承运商启用", notes = "承运商启用")
	@PostMapping("/enable")
	public RestMessage<Object> enableCarrierExecute(@RequestBody List<String> ids) {
		service.enableCarrierData(ids);
		return RestMessage.newInstance(true, "启用成功", null);
	}

	@UserLog("承运商删除")
	@ApiOperation(value = "承运商删除", notes = "承运商删除")
	@PostMapping("/delete")
	public RestMessage<Object> deleteCarrierExecute(@RequestBody List<CarrierDeleteVO> carrierDeleteVOs) {
		service.deleteCarrierData(carrierDeleteVOs);
		return RestMessage.newInstance(true, "删除成功", null);
	}

	@UserLog("承运商根据ID分页查询")
	@ApiOperation(value = "承运商根据ID分页查询", notes = "承运商根据ID分页查询")
	@PostMapping("/page_by_id")
	public RestMessage<Page<CarrierByIdPageDTO>> PageCarrierByIdExecute(@RequestBody CarrierByIdPageVO carrierByIdPageVO) {
		Page<CarrierByIdPageDTO> carrierByIdPageDTOs = service.PageCarrierByIdData(carrierByIdPageVO);
		return RestMessage.newInstance(true, "查询成功", carrierByIdPageDTOs);
	}

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

	@UserLog("承运商模板")
	@ApiOperation(value = "承运商模板", notes = "承运商模板")
	@PostMapping("/download_template")
	public RestMessage<Object> downloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, CarriersImportStdVO.class, "承运商模板", null, null, null, true);
		return null;
	}

	@UserLog("承运商审核信息查询")
	@ApiOperation(value = "承运商审核信息查询", notes = "承运商审核信息查询")
	@PostMapping("/get_approval_info")
	public RestMessage<CarrierApprovalInfoDTO> getApprovalInfoExecute(@RequestBody CarrierApprovalInfoVO carrierApprovalInfoVO) {
		try {
			CarrierApprovalInfoDTO carrierApprovalInfoDTO = service.getApprovalInfoData(carrierApprovalInfoVO);
			return RestMessage.newInstance(true, "查询成功", carrierApprovalInfoDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运商审批作废")
	@ApiOperation(value = "承运商审批作废", notes = "承运商审批作废")
	@PostMapping("/carrier_approval_invalid")
	public RestMessage<Object> carrierApprovalInvalidExecute(@RequestBody CarrierApprovalInvalidVO carrierApprovalInvalidVO) {
		try {
			service.carrierApprovalInvalidData(carrierApprovalInvalidVO);
			return RestMessage.newInstance(true, "作废成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运商审批撤回")
	@ApiOperation(value = "承运商审批撤回", notes = "承运商审批撤回")
	@PostMapping("/carrier_approval_retract")
	public RestMessage<Object> carrierApprovalRetractExecute(@RequestBody CarrierApprovalRetractVO carrierApprovalRetractVO) {
		try {
			service.carrierApprovalRetractData(carrierApprovalRetractVO);
			return RestMessage.newInstance(true, "撤回成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运商审批提交")
	@ApiOperation(value = "承运商审批提交", notes = "承运商审批提交")
	@PostMapping("/carrier_approval_submit")
	public RestMessage<Object> carrierApprovalSubmitExecute(@RequestBody CarrierApprovalSubmitVO carrierApprovalSubmitVO) {
		try {
			service.carrierApprovalSubmitData(carrierApprovalSubmitVO);
			return RestMessage.newInstance(true, "提交成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运商审核")
	@ApiOperation(value = "承运商审核", notes = "承运商审核")
	@PostMapping("/carrier_approval")
	public RestMessage<Object> carrierApprovalExecute(@RequestBody CarrierApprovalVO carrierApprovalVO) {
		try {
			service.carrierApprovalData(carrierApprovalVO);
			return RestMessage.newInstance(true, "审核成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("基础费用保存")
	@ApiOperation(value = "基础费用保存", notes = "基础费用保存")
	@PostMapping("/feeSave")
	public RestMessage<Object> feeSave(@RequestBody FeeSaveInfoVO feeSaveInfoVO) {
		service.feeSave(feeSaveInfoVO);
		return RestMessage.newInstance(true, "查询成功", null);
	}

	@UserLog("结算费用保存")
	@ApiOperation(value = "结算费用保存", notes = "结算费用保存")
	@PostMapping("/accountSave")
	public RestMessage<Object> accountSave(@RequestBody AccountSaveInfoVO accountSaveInfoVO) {
		service.accountSave(accountSaveInfoVO);
		return RestMessage.newInstance(true, "查询成功", accountSaveInfoVO);
	}

	@UserLog("通过编码查询费用项")
	@ApiOperation(value = "通过编码查询费用项", notes = "结算费用保存")
	@PostMapping("/getSettlement")
	public RestMessage<List<SettlementTypeDTO>> getSettlement(@RequestBody SettlementTypeVO settlementTypeVO) {
		List<SettlementTypeDTO> list = service.getSettlementByCode(settlementTypeVO);
		return RestMessage.newInstance(true, "查询成功", list);
	}

	@UserLog("通过编码查询结算项")
	@ApiOperation(value = "通过编码查询结算项", notes = "通过编码查询结算项")
	@PostMapping("/feeCode")
	public RestMessage<List<BaseFeeDTO>> feeCode(@RequestBody SettlementTypeVO settlementTypeVO) {
		List<BaseFeeDTO> list = service.feeCode(settlementTypeVO);
		return RestMessage.newInstance(true, "查询成功", list);
	}

	@UserLog("通过编码查询结算项")
	@ApiOperation(value = "通过编码查询结算项", notes = "通过编码查询结算项")
	@PostMapping("/findBaseFeePage")
	public RestMessage<Page<BaseFeeDTO>> findBaseFeePage(@RequestBody ExpensePopDTO dto) {
		Page<BaseFeeDTO> page = service.findBaseFeePage(dto);
		return RestMessage.newInstance(true, "查询成功", page);
	}

	@UserLog("承运商司机分页查询")
	@ApiOperation(value = "承运商司机分页查询", notes = "承运商司机分页查询")
	@PostMapping("/pageCarrierDriver")
	public RestMessage<Page<DriverPagingQueryDTO>> pageCarrierDriver(@Valid @RequestBody CarrierRelationInfoVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.pageCarrierDriver(vo));
	}

	@UserLog("承运商送货员分页查询")
	@ApiOperation(value = "承运商送货员分页查询", notes = "承运商送货员分页查询")
	@PostMapping("/pageCarrierDeliveryman")
	public RestMessage<Page<DeliverymanPagingQueryDTO>> pageCarrierDeliveryman(@Valid @RequestBody CarrierRelationInfoVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.pageCarrierDeliveryman(vo));
	}

	@UserLog("承运商车辆分页查询")
	@ApiOperation(value = "承运商车辆分页查询", notes = "承运商车辆分页查询")
	@PostMapping("/pageCarrierVehicle")
	public RestMessage<Page<VehiclePagingQueryDTO>> pageCarrierVehicle(@Valid @RequestBody CarrierRelationInfoVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.pageCarrierVehicle(vo));
	}

	@UserLog("承运商容器分页查询")
	@ApiOperation(value = "承运商容器分页查询", notes = "承运商容器分页查询")
	@PostMapping("/pageCarrierContainer")
	public RestMessage<Page<ContainerDTO>> pageCarrierContainer(@Valid @RequestBody CarrierRelationInfoVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.pageCarrierContainer(vo));
	}

	@UserLog("承运商设备分页查询")
	@ApiOperation(value = "承运商设备分页查询", notes = "承运商设备分页查询")
	@PostMapping("/pageCarrierDevice")
	public RestMessage<Page<DeviceDTO>> pageCarrierDevice(@Valid @RequestBody CarrierRelationInfoVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.pageCarrierDevice(vo));
	}

	@UserLog("承运商证件分页查询")
	@ApiOperation(value = "承运商证件分页查询", notes = "承运商证件分页查询")
	@PostMapping("/pageCarrierPaper")
	public RestMessage<Page<PaperDTO>> pageCarrierPaper(@Valid @RequestBody CarrierRelationInfoVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.pageCarrierPaper(vo));
	}

	@UserLog("承运商证件资料新增")
	@ApiOperation(value = "承运商证件资料新增", notes = "承运商证件资料新增")
	@PostMapping("/addCarrierPaper")
	public RestMessage addCarrierPaper(@Valid @RequestBody CarrierPaperLinkVO vo) {
		service.addCarrierPaper(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("承运商证件资料修改")
	@ApiOperation(value = "承运商证件资料修改", notes = "承运商证件资料修改")
	@PostMapping("/updateCarrierPaper")
	public RestMessage updateCarrierPaper(@Valid @RequestBody CarrierPaperLinkVO vo) {
		service.updateCarrierPaper(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("承运商证件资料是否启用")
	@ApiOperation(value = "承运商证件资料是否启用", notes = "承运商证件资料是否启用")
	@PostMapping("/whetherEnableCarrierPaper")
	public RestMessage whetherEnableCarrierPaper(@Valid @RequestBody CarrierPaperWhetherEnableVO vo) {
		service.whetherEnableCarrierPaper(vo);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("承运商证件资料删除")
	@ApiOperation(value = "承运商证件资料删除", notes = "承运商证件资料删除")
	@PostMapping("/deleteCarrierPaper")
	public RestMessage deleteCarrierPaper(@RequestBody List<Long> paperIdList) {
		service.deleteCarrierPaper(paperIdList);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("根据承运商编码名称查询承运商")
	@ApiOperation(value = "根据承运商编码名称查询承运商", notes = "根据承运商编码名称查询承运商")
	@PostMapping("/byEncodingName")
	public RestMessage<CarriersByEncodingNameDTO> getCarriersByEncodingNameExecute(@RequestBody CarriersByEncodingNameVO carriersByEncodingNameVO) {
		try {
			CarriersByEncodingNameDTO carriersByEncodingNameDTO = service.getCarriersByEncodingNameData(carriersByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", carriersByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
