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

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

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

import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.service.SysCodeengineService;
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.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.fleetManagement.DeliverymanCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeliverymanPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeliverymanPaperCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeliverymanPaperLinkCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeliverymanPopupDTO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPaperAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPaperCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPaperDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPaperLinkCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPaperModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPaperWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanPopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeliverymanWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.DeliverymanImportStdVO;
import com.prolog.rdc.tms.service.fleetManagement.DeliverymanService;

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

/**
 *
 *
 * 车队管理-送货员资料控制层
 *
 * @className :DeliverymanController
 * @author :fanzw
 * @creationTime :2022/03/16
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/03/16
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Api(tags = "车队管理-送货员资料控制层")
@RestController
@RequestMapping("/deliveryman")
public class DeliverymanController {

	@Autowired
	private DeliverymanService deliverymanService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("送货员资料分页查询")
	@ApiOperation(value = "送货员资料分页查询", notes = "送货员资料分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<DeliverymanPagingQueryDTO>> pageDeliverymanExecute(@RequestBody DeliverymanPagingQueryVO deliverymanPagingQueryVO) {
		try {
			Page<DeliverymanPagingQueryDTO> deliverymanPagingQueryDTOs = deliverymanService.pageDeliverymanData(deliverymanPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", deliverymanPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员资料弹窗")
	@ApiOperation(value = "送货员资料弹窗", notes = "送货员资料弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<DeliverymanPopupDTO>> popupDeliverymanExecute(@RequestBody DeliverymanPopupVO deliverymanPopupVO) {
		try {
			Page<DeliverymanPopupDTO> deliverymanPopupDTOs = deliverymanService.popupDeliverymanData(deliverymanPopupVO);
			return RestMessage.newInstance(true, "查询成功", deliverymanPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员资料查看")
	@ApiOperation(value = "送货员资料查看", notes = "送货员资料查看")
	@PostMapping("/check")
	public RestMessage<DeliverymanCheckDTO> getDeliverymanExecute(@RequestBody DeliverymanCheckVO deliverymanCheckVO) {
		try {
			DeliverymanCheckDTO deliverymanCheckDTO = deliverymanService.getDeliverymanData(deliverymanCheckVO);
			return RestMessage.newInstance(true, "查询成功", deliverymanCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员资料是否启用")
	@ApiOperation(value = "送货员资料是否启用", notes = "送货员资料是否启用")
	@PostMapping("/whether_to_enable")
	public RestMessage<String> whetherEnableDeliverymanExecute(@RequestBody DeliverymanWhetherEnableVO deliverymanWhetherEnableVO) {
		try {
			deliverymanService.whetherEnableDeliverymanData(deliverymanWhetherEnableVO);
			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> addDeliverymanExecute(@RequestBody @Valid DeliverymanAddVO deliverymanAddVO) {
		try {
			String deliverymanId = deliverymanService.addDeliverymanData(deliverymanAddVO);
			return RestMessage.newInstance(true, "新增成功", deliverymanId);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员资料修改")
	@ApiOperation(value = "送货员资料修改", notes = "送货员资料修改")
	@PostMapping("/modify")
	public RestMessage<String> modifyDeliverymanExecute(@RequestBody @Valid DeliverymanModifyVO deliverymanModifyVO) {
		try {
			deliverymanService.modifyDeliverymanData(deliverymanModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员资料删除")
	@ApiOperation(value = "送货员资料删除", notes = "送货员资料删除")
	@PostMapping("/delete")
	public RestMessage<String> deleteDeliverymanExecute(@RequestBody List<DeliverymanDeleteVO> list) {
		try {
			deliverymanService.deleteDeliverymanData(list);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员证件关联查看")
	@ApiOperation(value = "送货员证件关联查看", notes = "送货员证件关联查看")
	@PostMapping("/paper_link_check")
	public RestMessage<List<DeliverymanPaperLinkCheckDTO>> listDeliverymanPaperLinkCheckExecute(@RequestBody DeliverymanPaperLinkCheckVO deliverymanPaperLinkCheckVO) {
		try {
			List<DeliverymanPaperLinkCheckDTO> deliverymanPaperLinkCheckDTOs = deliverymanService.listDeliverymanPaperLinkCheckData(deliverymanPaperLinkCheckVO);
			return RestMessage.newInstance(true, "查询成功", deliverymanPaperLinkCheckDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员证件资料查看")
	@ApiOperation(value = "送货员证件资料查看", notes = "送货员证件资料查看")
	@PostMapping("/paper_check")
	public RestMessage<DeliverymanPaperCheckDTO> getDeliverymanPaperCheckExecute(@RequestBody DeliverymanPaperCheckVO deliverymanPaperCheckVO) {
		try {
			DeliverymanPaperCheckDTO deliverymanPaperCheckDTO = deliverymanService.getDeliverymanPaperCheckData(deliverymanPaperCheckVO);
			return RestMessage.newInstance(true, "查询成功", deliverymanPaperCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员证件资料新增")
	@ApiOperation(value = "送货员证件资料新增", notes = "送货员证件资料新增")
	@PostMapping("/paper_add")
	public RestMessage<String> addDeliverymanPaperExecute(@RequestBody DeliverymanPaperAddVO deliverymanPaperAddVO) {
		try {
			deliverymanService.addDeliverymanPaperData(deliverymanPaperAddVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员证件资料修改")
	@ApiOperation(value = "送货员证件资料修改", notes = "送货员证件资料修改")
	@PostMapping("/paper_modify")
	public RestMessage<String> modifyDeliverymanPaperExecute(@RequestBody DeliverymanPaperModifyVO deliverymanPaperModifyVO) {
		try {
			deliverymanService.modifyDeliverymanPaperData(deliverymanPaperModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员证件资料是否启用")
	@ApiOperation(value = "送货员证件资料是否启用", notes = "送货员证件资料是否启用")
	@PostMapping("/paper_whether_to_enable")
	public RestMessage<String> whetherEnableDeliverymanPaperExecute(@RequestBody DeliverymanPaperWhetherEnableVO deliverymanPaperWhetherEnableVO) {
		try {
			deliverymanService.whetherEnableDeliverymanPaperData(deliverymanPaperWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员证件资料删除")
	@ApiOperation(value = "送货员证件资料删除", notes = "送货员证件资料删除")
	@PostMapping("/paper_delete")
	public RestMessage<String> deleteDeliverymanPaperExecute(@RequestBody List<DeliverymanPaperDeleteVO> deliverymanPaperDeleteVOs) {
		try {
			deliverymanService.deleteDeliverymanPaperData(deliverymanPaperDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("送货员批量导入")
	@ApiOperation(value = "送货员批量导入", notes = "送货员批量导入")
	@PostMapping("/batchImportDeliveryman")
	public RestMessage<Object> batchImportDeliverymanExcel(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception {
		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
		deliverymanService.batchImportDeliverymanData(file,importBatchNumber);
		return RestMessage.newInstance(true, "导入成功", null);
	}

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