package com.emergency.avenue.storage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.common.security.annotation.Inner;
import com.emergency.avenue.storage.api.entity.DriverInfo;
import com.emergency.avenue.storage.service.DriverInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/driver")
@Tag(description = "driver", name = "司机信息管理")
public class DriverInfoController {

	private final DriverInfoService driverInfoService;

	/**
	 * 获取司机列表
	 * @return 司机列表
	 */
	@GetMapping("/list")
	@Inner(value = false)
	public R<List<DriverInfo>> listPosts() {
		return R.ok(driverInfoService.list(Wrappers.emptyWrapper()));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param driverInfo 司机信息表
	 * @return
	 */
	@Operation(description = "分页查询", summary = "分页查询")
	@GetMapping("/page")
	public R getDriverInfoPage(Page page, DriverInfo driverInfo) {
		return R.ok(driverInfoService.page(page, Wrappers.query(driverInfo)));
	}

	/**
	 * 通过id查询司机信息表
	 * @param driverId id
	 * @return R
	 */
	@Operation(description = "通过id查询", summary = "通过id查询")
	@GetMapping("/{driverId}")
	public R getById(@PathVariable("driverId") Long driverId) {
		return R.ok(driverInfoService.getById(driverId));
	}

	/**
	 * 新增司机信息表
	 * @param driverInfo 司机信息表
	 * @return R
	 */
	@Operation(description = "新增司机信息表", summary = "新增司机信息表")
	@SysLog("新增司机信息表")
	@PostMapping
	public R save(@RequestBody DriverInfo driverInfo) {
		validateRe(DriverInfo.builder().idCard(driverInfo.getIdCard()).build());
		validateRe(DriverInfo.builder().idCard(driverInfo.getDrivingLicense()).build());
		return R.ok(driverInfoService.save(driverInfo));
	}

	private boolean validateRe(DriverInfo driverInfo){
		String mag="";
		QueryWrapper<DriverInfo> driverInfoQueryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotEmpty(driverInfo.getIdCard())){
			driverInfoQueryWrapper.lambda().eq(DriverInfo::getIdCard,driverInfo.getIdCard());
			mag="身份证号重复,请重新输入";
		}
		if (StringUtils.isNotEmpty(driverInfo.getDrivingLicense())){
			driverInfoQueryWrapper.lambda().eq(DriverInfo::getDrivingLicense,driverInfo.getDrivingLicense());
			mag="驾驶证号重复,请重新输入";
		}
		if (driverInfo.getDriverId()!=null){
			driverInfoQueryWrapper.lambda().ne(DriverInfo::getDriverId,driverInfo.getDriverId());
		}
		long count = driverInfoService.count(driverInfoQueryWrapper);
		if (count>0){
			throw new IllegalStateException(mag);
		}
		return false;
	}

	/**
	 * 修改司机信息表
	 * @param driverInfo 司机信息表
	 * @return R
	 */
	@Operation(description = "修改司机信息表", summary = "修改司机信息表")
	@SysLog("修改司机信息表")
	@PutMapping
	public R updateById(@RequestBody DriverInfo driverInfo) {
		validateRe(DriverInfo.builder().idCard(driverInfo.getIdCard()).driverId(driverInfo.getDriverId()).build());
		validateRe(DriverInfo.builder().idCard(driverInfo.getDrivingLicense()).driverId(driverInfo.getDriverId()).build());
		return R.ok(driverInfoService.updateById(driverInfo));
	}

	/**
	 * 通过id删除司机信息表
	 * @param driverId id
	 * @return R
	 */
	@Operation(description = "通过id删除司机信息表", summary = "通过id删除司机信息表")
	@SysLog("通过id删除司机信息表")
	@DeleteMapping("/{driverId}")
	public R removeById(@PathVariable Long driverId) {
		return R.ok(driverInfoService.removeById(driverId));
	}
}
