package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.proprietorCar.ProprietorCar;
import com.ruoyi.system.mapper.ProprietorCarMapper;
import com.ruoyi.system.service.IProprietorCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 业主车辆管理Service业务层处理
 *
 * @author boman
 * @date 2025-02-20
 */
@Service
public class ProprietorCarServiceImpl implements IProprietorCarService {
    @Autowired
    private ProprietorCarMapper proprietorCarMapper;

    /**
     * 查询业主车辆管理
     *
     * @param carId 业主车辆管理主键
     * @return 业主车辆管理
     */
    @Override
    public ProprietorCar selectProprietorCarByCarId(Long carId) {
        return proprietorCarMapper.selectProprietorCarByCarId(carId);
    }

    /**
     * 查询业主车辆管理列表
     *
     * @param proprietorCar 业主车辆管理
     * @return 业主车辆管理
     */
    @Override
    public List<ProprietorCar> selectProprietorCarList(ProprietorCar proprietorCar) {
        return proprietorCarMapper.selectProprietorCarList(proprietorCar);
    }

    /**
     * 新增业主车辆管理
     *
     * @param proprietorCar 业主车辆管理
     * @return 结果
     */
    @Override
    public int insertProprietorCar(ProprietorCar proprietorCar) {
        //根据车牌号判断是否是新能源
        proprietorCar.setCreateTime(DateUtils.getNowDate());
        String plateNumber = proprietorCar.getPlateNumber();
        if (!checkPlateNumber(plateNumber)){
            return 0;
        }
/*        String vehicleEnergy = "燃油车";
        if (isNewEnergyVehicle(plateNumber)) {
            vehicleEnergy = "新能源";
        }
        proprietorCar.setVehicleEnergy(vehicleEnergy);*/
        //proprietorCar.setUserId(SecurityUtils.getUserId());
        return proprietorCarMapper.insertProprietorCar(proprietorCar);
    }

    /**
     * 判断是否是新能源
     *
     * @param licensePlate
     * @return
     */
    public static boolean isNewEnergyVehicle(String licensePlate) {
        if (licensePlate == null || licensePlate.isEmpty()) {
            return false;
        }

        // 去除所有非汉字、字母和数字的字符
        String cleaned = licensePlate.replaceAll("[^\\u4e00-\\u9fa5A-Z0-9]", "");

        // 检查总长度是否为7或8位
        int length = cleaned.length();
        if (length != 7 && length != 8) {
            return false;
        }

        // 正则表达式验证结构
        String regex = "^[\u4e00-\u9fa5]" +         // 省份汉字
                "[A-HJ-NP-Z]" +              // 发牌机关（排除I/O）
                "[DFABCEGHJKLMNP]" +                     // 新能源标识（D/F）
                "[A-HJ-NP-Z0-9]{4,5}$";      // 后续字符（排除I/O）

        return cleaned.matches(regex);
    }
    /**
     * 判断车牌号是否合规
     *
     * @param plateNumber
     * @return
     */
    public static boolean checkPlateNumber(String plateNumber) {
        String regEx = "^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$";
        // 编译正则表达式
        //Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);

        Matcher matcher = pat.matcher(plateNumber);
        // 字符串是否与正则表达式相匹配
        return matcher.matches();
    }
    /**
     * 修改业主车辆管理
     *
     * @param proprietorCar 业主车辆管理
     * @return 结果
     */
    @Override
    public int updateProprietorCar(ProprietorCar proprietorCar) {
        proprietorCar.setUpdateTime(DateUtils.getNowDate());
        String plateNumber = proprietorCar.getPlateNumber();
        if (!checkPlateNumber(plateNumber)){
            return 0;
        }
        String vehicleType = "燃油车";
        if (isNewEnergyVehicle(plateNumber)) {
            vehicleType = "新能源";
        }
        proprietorCar.setVehicleType(vehicleType);
        return proprietorCarMapper.updateProprietorCar(proprietorCar);
    }

    /**
     * 批量删除业主车辆管理
     *
     * @param carIds 需要删除的业主车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteProprietorCarByCarIds(Long[] carIds) {
        return proprietorCarMapper.deleteProprietorCarByCarIds(carIds);
    }

    /**
     * 删除业主车辆管理信息
     *
     * @param carId 业主车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteProprietorCarByCarId(Long carId) {
        return proprietorCarMapper.deleteProprietorCarByCarId(carId);
    }
}
