package com.emergency.avenue.storage.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emergency.avenue.common.core.util.R;
import com.emergency.avenue.common.log.annotation.SysLog;
import com.emergency.avenue.storage.api.entity.InTransitVehicleInfo;
import com.emergency.avenue.storage.api.request.EyeCommonReq;
import com.emergency.avenue.storage.service.InTransitVehicleInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/inTransitVehicle")
@Tag(description = "inTransitVehicle", name = "在途车辆信息管理")
public class InTransitVehicleInfoController {

	private final InTransitVehicleInfoService inTransitVehicleInfoService;

	/**
	 * 获取在途车辆信息管理列表
	 * @return 在途车辆信息管理列表
	 */
	@GetMapping("/list")
	public R<List<InTransitVehicleInfo>> listPosts() {
		return R.ok(inTransitVehicleInfoService.list(Wrappers.emptyWrapper()));
	}

	/**
	 * 分页查询-出库凭证列表
	 * @param page 分页对象
	 * @param inTransitVehicleInfo 在途车辆信息管理表
	 * @return
	 */
	@Operation(description = "出库凭证列表", summary = "出库凭证列表")
	@GetMapping("/pageOrder")
	public R getInTransitVehicleInfoPageOrder(Page page, InTransitVehicleInfo inTransitVehicleInfo) {
		return R.ok(inTransitVehicleInfoService.getInTransitVehicleInfoPageOrder(page,inTransitVehicleInfo));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param inTransitVehicleInfo 在途车辆信息管理表
	 * @return
	 */
	@Operation(description = "出库凭证下车辆列表", summary = "出库凭证下车辆列表")
	@GetMapping("/page")
	public R getInTransitVehicleInfoPage(Page page, InTransitVehicleInfo inTransitVehicleInfo) {
		if(StrUtil.isBlank(inTransitVehicleInfo.getOrderName())){
			return R.failed("请输入出库凭证");
		}
		page.setCurrent(1);
		page.setSize(100);
		Page<InTransitVehicleInfo> page1 = getTransitVehicleInfoPage(page, inTransitVehicleInfo);
		return R.ok(page1);
	}

	private Page<InTransitVehicleInfo> getTransitVehicleInfoPage(Page page, InTransitVehicleInfo inTransitVehicleInfo) {
		QueryWrapper<InTransitVehicleInfo> query = Wrappers.query(inTransitVehicleInfo);
//		String[] deliveryTime = inTransitVehicleInfo.getDeliveryTime();
//		if (deliveryTime != null && deliveryTime.length > 0) {
//			query.ge("start_delivery_time", deliveryTime[0]);
//			if (deliveryTime.length > 1) {
//				query.le("start_delivery_time", deliveryTime[1]);
//			}
//		}
//		query.orderByDesc("id");
		Page<InTransitVehicleInfo> page1 = inTransitVehicleInfoService.page(page, query);
		if(page1.getTotal()>0){
			List<InTransitVehicleInfo> records = page1.getRecords();
			records.forEach(item -> {
				if(item.getEstimatedArrivalTime() == null){
					item.setEstimatedArrivalTime(new Timestamp(System.currentTimeMillis()));
				}
				inTransitVehicleInfoService.getEyeString(item);
			});
		}
		return page1;
	}

	/**
	 * 通过id查询在途车辆信息管理表
	 * @param vehicleId id
	 * @return R
	 */
	@Operation(description = "通过id查询", summary = "通过id查询")
	@GetMapping("/{vehicleId}")
	public R getById(@PathVariable("vehicleId") Long vehicleId) {
		return R.ok(inTransitVehicleInfoService.getById(vehicleId));
	}

	/**
	 * 新增在途车辆信息管理表
	 * @param inTransitVehicleInfos 在途车辆信息管理表
	 * @return R
	 */
	@Operation(description = "新增在途车辆信息管理表", summary = "新增在途车辆信息管理表")
	@SysLog("新增在途车辆信息管理表")
	@PostMapping
	public R save(@RequestBody List<InTransitVehicleInfo> inTransitVehicleInfos) {
		if(inTransitVehicleInfos == null || inTransitVehicleInfos.isEmpty()){
			return R.failed("请输入出库凭证");
		}
		InTransitVehicleInfo inTransitVehicleInfo = inTransitVehicleInfos.get(0);
		if(StrUtil.isBlank(inTransitVehicleInfo.getOrderName())){
			return R.failed("请输入出库凭证");
		}
		boolean b = inTransitVehicleInfos.stream().allMatch(item -> item.getOrderName().equals(inTransitVehicleInfo.getOrderName()));
		if(!b){
			return R.failed("请输入相同出库凭证");
		}
		Page<InTransitVehicleInfo> page = new Page<>(1, 1);
		InTransitVehicleInfo param = new InTransitVehicleInfo();
		param.setOrderName(inTransitVehicleInfo.getOrderName());
		Page<InTransitVehicleInfo> page1 = getTransitVehicleInfoPage(page, param);
		if(page1.getTotal() > 0){
			return R.failed("该出库凭证已存在");
		}
		return R.ok(inTransitVehicleInfoService.saveBatch(inTransitVehicleInfos));
	}

	/**
	 * 修改在途车辆信息管理表
	 * @param inTransitVehicleInfos 在途车辆信息管理表
	 * @return R
	 */
	@Operation(description = "修改在途车辆信息管理表", summary = "修改在途车辆信息管理表")
	@SysLog("修改在途车辆信息管理表")
	@PutMapping
	@Transactional(rollbackFor = Exception.class)
	public R updateById(@RequestBody List<InTransitVehicleInfo> inTransitVehicleInfos) {

		if(inTransitVehicleInfos == null || inTransitVehicleInfos.isEmpty()){
			return R.failed("请输入出库凭证");
		}
		InTransitVehicleInfo inTransitVehicleInfo = inTransitVehicleInfos.get(0);
		if(StrUtil.isBlank(inTransitVehicleInfo.getOrderName())){
			return R.failed("请输入出库凭证");
		}
		boolean b = inTransitVehicleInfos.stream().allMatch(item -> item.getOrderName().equals(inTransitVehicleInfo.getOrderName()));
		if(!b){
			return R.failed("请输入相同出库凭证");
		}

		InTransitVehicleInfo param = new InTransitVehicleInfo();
		param.setOrderName(inTransitVehicleInfo.getOrderName());
		QueryWrapper<InTransitVehicleInfo> query = Wrappers.query(param);
		inTransitVehicleInfoService.remove(query);

		inTransitVehicleInfos.forEach(item -> item.setId(null));

		return R.ok(inTransitVehicleInfoService.saveBatch(inTransitVehicleInfos));
	}

	/**
	 * 通过id删除在途车辆信息管理表
	 * @param vehicleId id
	 * @return R
	 */
	@Operation(description = "通过id删除在途车辆信息管理表", summary = "通过id删除在途车辆信息管理表")
	@SysLog("通过id删除在途车辆信息管理表")
	@DeleteMapping("/{vehicleId}")
	public R removeById(@PathVariable Long vehicleId) {
		return R.ok(inTransitVehicleInfoService.removeById(vehicleId));
	}

	/**
	 * 通过出库凭证删除在途车辆信息管理表
	 * @param inTransitVehicleInfo .orderName 出库凭证
	 * @return R
	 */
	@Operation(description = "通过出库凭证删除在途车辆信息管理表", summary = "通过id删除在途车辆信息管理表")
	@SysLog("通过出库凭证删除在途车辆信息管理表")
	@PostMapping("delete")
	public R removeById(@RequestBody InTransitVehicleInfo inTransitVehicleInfo) {
		if(StrUtil.isBlank(inTransitVehicleInfo.getOrderName())){
			return R.failed("请输入出库凭证");
		}
		QueryWrapper<InTransitVehicleInfo> query = Wrappers.query(inTransitVehicleInfo);
		return R.ok(inTransitVehicleInfoService.remove(query));
	}


	@Operation(description = "查看轨迹返回参数", summary = "查看轨迹返回参数")
	@GetMapping("/trajectory/{vehicleId}")
	public R trajectoryById(@PathVariable Long vehicleId) throws Exception {
		EyeCommonReq eyeCommonReq = inTransitVehicleInfoService.trajectorybyId(vehicleId);
		return R.ok(eyeCommonReq);
	}


	@Operation(description = "查看多个车辆定位", summary = "查看多个车辆定位")
	@PostMapping("/trajectorybyOrdierName")
	public R trajectoryById(@RequestBody InTransitVehicleInfo inTransitVehicleInfo) throws Exception {
		if(StrUtil.isBlank(inTransitVehicleInfo.getOrderName()) && inTransitVehicleInfo.getId() == null ){
			return R.failed("请选择出库凭证或者车辆 id");
		}
		Page <InTransitVehicleInfo> page = new Page<>(1, 1000);
		Page<InTransitVehicleInfo> page1 = getTransitVehicleInfoPage(page, inTransitVehicleInfo);
		if(page1.getTotal()>0){
			EyeCommonReq eyeCommonReq = inTransitVehicleInfoService.trajectorybyOrdierName(page1.getRecords());
			return R.ok(eyeCommonReq);
		}
		return R.ok();
	}
}
