package com.xhpc.user.service.impl;

import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.domain.*;
import com.xhpc.user.mapper.XhpcMotorcadeMapper;
import com.xhpc.user.mapper.XhpcUserVehicleMapper;
import com.xhpc.user.service.IMechanismService;
import com.xhpc.user.service.IXhpcMotorcadeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author yuyang
 * @Date 2026-01-15 14:15
 */
@Service
public class XhpcMotorcadeServiceImpl extends BaseService implements IXhpcMotorcadeService {

    @Resource
    private XhpcMotorcadeMapper xhpcMotorcadeMapper;
    @Resource
    IMechanismService mechanismService;
    @Resource
    TokenService tokenService;
    @Resource
    XhpcUserVehicleMapper xhpcUserVehicleMapper;

    @Override
    public List<Map<String, Object>> userList(HttpServletRequest request, String name) {
        Long userId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(userId !=null){
            String userType = loginUser.getSysUser().getUserType();
            if(UserTypeUtil.SYS_USER_TYPE_ZERO.equals(userType)){
                list = xhpcMotorcadeMapper.tree(name, loginUser.getTenantId());
            }
        }

        Iterator<Map<String, Object>> iterator = list.iterator();
        while (iterator.hasNext()){
            Map<String, Object> commMap = iterator.next();
            List<Map<String, Object>> userMapList = xhpcMotorcadeMapper.motorcadePersonnelTree(commMap.get("id").toString().replaceAll("COMM_", ""));
            if (userMapList.size() < 1){
                iterator.remove();
            } else {
                commMap.put("children", userMapList);
            }
        }
        return list;
    }

    @Override
    public Map<String, Object> getOrderMotorcade(String mark) {
        return xhpcMotorcadeMapper.getOrderMotorcade(mark);
    }

    @Override
    public List<Map<String, Object>> list(HttpServletRequest request, String name) {
        Long userId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(userId !=null){
            startPage();
            String userType = loginUser.getSysUser().getUserType();
            if(UserTypeUtil.SYS_USER_TYPE_ZERO.equals(userType)){
                list = xhpcMotorcadeMapper.list(name,loginUser.getTenantId());
            }
        }
        return list;
    }

    @Override
    public AjaxResult addMotorcade(HttpServletRequest request, XhpcMotorcade xhpcMotorcade) {
        R r= judgeXhpcCommunity(xhpcMotorcade);
        if(r.getCode() !=200){
            return AjaxResult.error(r.getMsg());
        }
        if(xhpcMotorcade.getMotorcadeId() !=null){
            return updateMotorcade(xhpcMotorcade);
        }
        //名称重复不能入库
        Map<String, Object> communityById = xhpcMotorcadeMapper.getMotorcadeById(null, xhpcMotorcade.getName(), null);
        if(communityById !=null){
            return AjaxResult.error("车队名称重复");
        }

        String chargingStationIds = xhpcMotorcade.getChargingStationIds();
        if("".equals(chargingStationIds) || chargingStationIds ==null){
            xhpcMotorcade.setStatus(0);
        }else{
            xhpcMotorcade.setStatus(1);
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser !=null){
            SysUser sysUser = loginUser.getSysUser();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) || UserTypeUtil.SYS_USER_TYPE_THREE.equals(sysUser.getUserType())){
                xhpcMotorcade.setType(2);
                xhpcMotorcade.setOperatorId(sysUser.getOperatorId());
            }else{
                xhpcMotorcade.setType(1);
            }
        }else{
            return AjaxResult.error("请重新登录");
        }
        xhpcMotorcade.setRemainingAmount(xhpcMotorcade.getTotalAmount());
        xhpcMotorcade.setConsumptionAmount(xhpcMotorcade.getTotalAmount());
        xhpcMotorcade.setAllocatedAmount(xhpcMotorcade.getTotalAmount());
        xhpcMotorcade.setCreateTime(new Date());
        xhpcMotorcade.setMark(generateRandomString(10));
        xhpcMotorcadeMapper.addMotorcade(xhpcMotorcade);

        if(!"".equals(xhpcMotorcade.getVinNumber()) && xhpcMotorcade.getVinNumber().length()==17){
            //先删除VIN，其他端的VIN码，在添加车队VIN码
            xhpcUserVehicleMapper.updateVehicleNameUserId(xhpcMotorcade.getMotorcadeId(),xhpcMotorcade.getVinNumber());

            XhpcUserVehicle xhpcUserVehicle =new XhpcUserVehicle();
            int i = xhpcUserVehicleMapper.xhpcUserVehicleInformation(xhpcMotorcade.getMotorcadeId(), UserTypeUtil.MOTORCADE_TYPE, 0);
            if(i==0){
                xhpcUserVehicle.setType(1);
            }
            xhpcUserVehicle.setSource(4);
            xhpcUserVehicle.setStatus(1);
            xhpcUserVehicle.setVinSpecCode(xhpcMotorcade.getVinNumber());
            xhpcUserVehicle.setVehicleName(xhpcMotorcade.getVehicleName());
            xhpcUserVehicle.setAppUserId(xhpcMotorcade.getMotorcadeId());
            xhpcUserVehicleMapper.addUserVehicle(xhpcUserVehicle);
        }


        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            mechanismService.addMechanism(Arrays.asList(chargingStationIds.split(",")),xhpcMotorcade.getMotorcadeId(),3);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteMotorcade(XhpcMotorcade xhpcMotorcade) {
        if(xhpcMotorcade !=null && xhpcMotorcade.getMotorcadeId() !=null){
            int i = xhpcMotorcadeMapper.countMotorcadePersonnel(xhpcMotorcade.getMotorcadeId());
            if(i==0){
                xhpcMotorcadeMapper.deleteMotorcade(xhpcMotorcade.getMotorcadeId());
                return AjaxResult.success();
            }else{
                return AjaxResult.error("该车队组下有用户不能删除");
            }
        }else{
            return AjaxResult.error("请传车队组id");
        }
    }

    @Override
    public AjaxResult updateMotorcade(XhpcMotorcade xhpcMotorcade) {
        R r = judgeXhpcCommunity(xhpcMotorcade);
        if(r.getCode()!=200){
            return AjaxResult.error(r.getMsg());
        }
        //名称重复不能入库
        Map<String, Object> communityById = xhpcMotorcadeMapper.getMotorcadeById(xhpcMotorcade.getMotorcadeId(), xhpcMotorcade.getName(), 2);
        if(communityById !=null){
            return AjaxResult.error("车队名称重复");
        }

        String chargingStationIds = xhpcMotorcade.getChargingStationIds();
        if("".equals(chargingStationIds) || chargingStationIds ==null){
            xhpcMotorcade.setStatus(0);
        }else{
            xhpcMotorcade.setStatus(1);
        }
        xhpcMotorcadeMapper.updateMotorcade(xhpcMotorcade);
        mechanismService.updateMechanism(xhpcMotorcade.getMotorcadeId(),3);
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            mechanismService.addMechanism(Arrays.asList(chargingStationIds.split(",")),xhpcMotorcade.getMotorcadeId(),3);
        }
        if(!"".equals(xhpcMotorcade.getVinNumber()) && xhpcMotorcade.getVinNumber().length()==17){
            //先删除VIN，其他端的VIN码，在添加车队VIN码
            xhpcUserVehicleMapper.updateVehicleNameUserId(xhpcMotorcade.getMotorcadeId(),xhpcMotorcade.getVinNumber());

            XhpcUserVehicle xhpcUserVehicle =new XhpcUserVehicle();
            int i = xhpcUserVehicleMapper.xhpcUserVehicleInformation(xhpcMotorcade.getMotorcadeId(), UserTypeUtil.MOTORCADE_TYPE, 0);
            if(i==0){
                xhpcUserVehicle.setType(1);
            }
            xhpcUserVehicle.setSource(4);
            xhpcUserVehicle.setStatus(1);
            xhpcUserVehicle.setVinSpecCode(xhpcMotorcade.getVinNumber());
            xhpcUserVehicle.setVehicleName(xhpcMotorcade.getVehicleName());
            xhpcUserVehicle.setAppUserId(xhpcMotorcade.getMotorcadeId());
            xhpcUserVehicleMapper.addUserVehicle(xhpcUserVehicle);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateMotorcadeMoney(XhpcMotorcade xhpcMotorcade) {
        Long motorcadeId = xhpcMotorcade.getMotorcadeId();
        Map<String, Object> motorcadeById = xhpcMotorcadeMapper.getMotorcadeById(motorcadeId, null, 1);

        //增加充值记录
        if(motorcadeById !=null && new BigDecimal(0).compareTo(xhpcMotorcade.getAddAmount())==-1){
            BigDecimal totalAmount = xhpcMotorcade.getAddAmount();
            BigDecimal totalAmountYu = new BigDecimal(motorcadeById.get("totalAmount").toString());
            BigDecimal remainingAmount = new BigDecimal(motorcadeById.get("remainingAmount").toString());
            Long rechargeOrderId =mechanismService.addRechargeOrder(motorcadeId,UserTypeUtil.MOTORCADE_TYPE,totalAmount,totalAmount,UserTypeUtil.RECHARGE_PT);
            //增加一条充值记录
            mechanismService.insertUserAccount(motorcadeId,totalAmount,totalAmount.add(totalAmountYu),null,rechargeOrderId,null,UserTypeUtil.INSERT_BALANCE,xhpcMotorcade.getTenantId(),UserTypeUtil.MOTORCADE_TYPE);
            xhpcMotorcade.setTotalAmount(totalAmount.add(totalAmountYu));
            xhpcMotorcade.setRemainingAmount(totalAmount.add(remainingAmount));
        }else{
            return AjaxResult.error("金额大于零");
        }
        xhpcMotorcadeMapper.updateMotorcade(xhpcMotorcade);
        //mechanismService.updateMechanism(xhpcMotorcade.getMotorcadeId(),3);
        return AjaxResult.success();
    }

    @Override
    public Map<String, Object> getMotorcadeById(XhpcMotorcade xhpcMotorcade) {
        return xhpcMotorcadeMapper.getMotorcadeById(xhpcMotorcade.getMotorcadeId(),null,1);

    }

    @Override
    public List<Map<String, Object>> motorcadePersonnelList(Long motorcadeId, String account, String phone, Integer status) {
        return xhpcMotorcadeMapper.motorcadePersonnelList(motorcadeId, account, phone, status);

    }

    @Override
    public AjaxResult addMotorcadePersonnel(HttpServletRequest request, XhpcMotorcadePersonnel xhpcMotorcadePersonnel) {
        R r =  judgeXhpcCommunityPersonnel(xhpcMotorcadePersonnel);
        if(r.getCode()!=200){
            return AjaxResult.error(r.getMsg());
        }
        String phone = xhpcMotorcadePersonnel.getPhone();
        String account = xhpcMotorcadePersonnel.getAccount();
        Map<String, Object> communityPersonnelById = xhpcMotorcadeMapper.getMotorcadePersonnelById(null, phone, account, 3);
        if(communityPersonnelById !=null){
            if(phone.equals(communityPersonnelById.get("phone").toString())){
                return AjaxResult.error("手机号重复");
            }
            if(account.equals(communityPersonnelById.get("account").toString())){
                return AjaxResult.error("账号重复");
            }
        }
        BigDecimal rechargeMoney = xhpcMotorcadePersonnel.getRechargeMoney();
        if(new BigDecimal(0).compareTo(rechargeMoney)==-1){
            //增加社区用户的剩余金额
            xhpcMotorcadePersonnel.setSurplusMoney(rechargeMoney);
            //减去车队总金额
            Map<String, Object> motorcadeById = xhpcMotorcadeMapper.getMotorcadeById(xhpcMotorcadePersonnel.getMotorcadeId(), null, 1);
            BigDecimal remainingAmount = new BigDecimal(motorcadeById.get("remainingAmount").toString());
            BigDecimal allocatedAmount = new BigDecimal(motorcadeById.get("allocatedAmount").toString());
            if(new BigDecimal(0).compareTo(remainingAmount)==-1 && rechargeMoney.compareTo(remainingAmount)==-1){
                XhpcMotorcade xhpcMotorcade =new XhpcMotorcade();
                Long motorcadeId = xhpcMotorcadePersonnel.getMotorcadeId();
                xhpcMotorcade.setMotorcadeId(motorcadeId);
                xhpcMotorcade.setRemainingAmount(remainingAmount.subtract(rechargeMoney));
                xhpcMotorcade.setAllocatedAmount(allocatedAmount.add(rechargeMoney));
                xhpcMotorcadeMapper.updateMotorcade(xhpcMotorcade);

                //增加用户流水记录
                mechanismService.insertUserAccount(motorcadeId, rechargeMoney, remainingAmount.subtract(rechargeMoney),null,null,null,8,xhpcMotorcadePersonnel.getTenantId(),UserTypeUtil.MOTORCADE_TYPE);
            }else{
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("车队总金额不足");
            }
        }
        Long userId = SecurityUtils.getUserId();
        if(userId !=null){
            LoginUser loginUser = tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getSysUser();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) || UserTypeUtil.SYS_USER_TYPE_THREE.equals(sysUser.getUserType())){
                xhpcMotorcadePersonnel.setCreateType(2);
                xhpcMotorcadePersonnel.setCreateId(sysUser.getOperatorId());
            }else{
                xhpcMotorcadePersonnel.setCreateType(1);
            }
        }else{
            return AjaxResult.error("请重新登录");
        }
        xhpcMotorcadePersonnel.setCreateTime(new Date());
        xhpcMotorcadePersonnel.setSunchaSwitch(0);
        xhpcMotorcadeMapper.addMotorcadePersonnel(xhpcMotorcadePersonnel);

        if(!"".equals(xhpcMotorcadePersonnel.getVinNumber()) && xhpcMotorcadePersonnel.getVinNumber().length()==17){
            //先删除VIN，其他端的VIN码，在添加车队VIN码
            xhpcUserVehicleMapper.updateVehicleNameUserId(xhpcMotorcadePersonnel.getMotorcadePersonnelId(),xhpcMotorcadePersonnel.getVinNumber());

            XhpcUserVehicle xhpcUserVehicle =new XhpcUserVehicle();
            int i = xhpcUserVehicleMapper.xhpcUserVehicleInformation(xhpcMotorcadePersonnel.getMotorcadePersonnelId(), UserTypeUtil.MOTORCADE_USER_TYPE, 0);
            if(i==0){
                xhpcUserVehicle.setType(1);
            }
            xhpcUserVehicle.setSource(4);
            xhpcUserVehicle.setStatus(1);
            xhpcUserVehicle.setVinSpecCode(xhpcMotorcadePersonnel.getVinNumber());
            xhpcUserVehicle.setVehicleName(xhpcMotorcadePersonnel.getVehicleName());
            xhpcUserVehicle.setAppUserId(xhpcMotorcadePersonnel.getMotorcadePersonnelId());
            xhpcUserVehicleMapper.addUserVehicle(xhpcUserVehicle);
        }
        //增加用户流水记录
        if(new BigDecimal(0).compareTo(rechargeMoney)==-1){
            mechanismService.insertUserAccount(xhpcMotorcadePersonnel.getMotorcadePersonnelId(), rechargeMoney, rechargeMoney,null,null,null,1,xhpcMotorcadePersonnel.getTenantId(),UserTypeUtil.MOTORCADE_USER_TYPE);
        }
        return AjaxResult.success();
    }

    @Override
    public Map<String, Object> getMotorcadePersonnelById(XhpcMotorcadePersonnel xhpcMotorcadePersonnel) {
        return xhpcMotorcadeMapper.getMotorcadePersonnelById(xhpcMotorcadePersonnel.getMotorcadePersonnelId(), null, null, 1);
    }

    @Override
    public AjaxResult updateMotorcadePersonnel(XhpcMotorcadePersonnel xhpcMotorcadePersonnel) {
        R r = judgeXhpcCommunityPersonnel(xhpcMotorcadePersonnel);
        if(r.getCode()!=200){
            return AjaxResult.error(r.getMsg());
        }
        Long motorcadePersonnelId = xhpcMotorcadePersonnel.getMotorcadePersonnelId();
        String phone = xhpcMotorcadePersonnel.getPhone();
        String account = xhpcMotorcadePersonnel.getAccount();
        BigDecimal rechargeMoney = xhpcMotorcadePersonnel.getRechargeMoney();
        BigDecimal surplusMoney = xhpcMotorcadePersonnel.getSurplusMoney();
        if( rechargeMoney!=null){
            return AjaxResult.error("充值金额不能填写");
        }
        if( surplusMoney!=null){
            return AjaxResult.error("剩余金额不能填写");
        }
        Map<String, Object> communityPersonnelById = xhpcMotorcadeMapper.getMotorcadePersonnelById(motorcadePersonnelId, phone, account, 2);
        if(communityPersonnelById !=null){
            if(phone.equals(communityPersonnelById.get("phone").toString())){
                return AjaxResult.error("手机号重复");
            }
            if(account.equals(communityPersonnelById.get("account").toString())){
                return AjaxResult.error("账号重复");
            }
        }
        if(!"".equals(xhpcMotorcadePersonnel.getVinNumber()) && xhpcMotorcadePersonnel.getVinNumber().length()==17){
            //先删除VIN，其他端的VIN码，在添加车队VIN码
            xhpcUserVehicleMapper.updateVehicleNameUserId(xhpcMotorcadePersonnel.getMotorcadePersonnelId(),xhpcMotorcadePersonnel.getVinNumber());

            XhpcUserVehicle xhpcUserVehicle =new XhpcUserVehicle();
            int i = xhpcUserVehicleMapper.xhpcUserVehicleInformation(xhpcMotorcadePersonnel.getMotorcadePersonnelId(), UserTypeUtil.MOTORCADE_USER_TYPE, 0);
            if(i==0){
                xhpcUserVehicle.setType(1);
            }
            xhpcUserVehicle.setSource(4);
            xhpcUserVehicle.setStatus(1);
            xhpcUserVehicle.setVinSpecCode(xhpcMotorcadePersonnel.getVinNumber());
            xhpcUserVehicle.setVehicleName(xhpcMotorcadePersonnel.getVehicleName());
            xhpcUserVehicle.setAppUserId(xhpcMotorcadePersonnel.getMotorcadePersonnelId());
            xhpcUserVehicleMapper.addUserVehicle(xhpcUserVehicle);
        }
        xhpcMotorcadeMapper.updateMotorcadePersonnel(xhpcMotorcadePersonnel);
        return AjaxResult.success();
    }

    @Override
    public void deleteMotorcadePersonnel(XhpcMotorcadePersonnel xhpcMotorcadePersonnel) {
        xhpcMotorcadeMapper.deleteMotorcadePersonnel(xhpcMotorcadePersonnel.getMotorcadePersonnelId());

    }

    @Override
    public void updateMotorcadePersonnelStatus(XhpcMotorcadePersonnel xhpcMotorcadePersonnel) {
        xhpcMotorcadeMapper.updateMotorcadePersonnelStatus(xhpcMotorcadePersonnel.getMotorcadePersonnelId(),xhpcMotorcadePersonnel.getStatus());

    }

    @Override
    public AjaxResult addRecharge(XhpcMotorcadePersonnel xhpcMotorcadePersonnel) {
        //充值金额
        BigDecimal rechargeMoney = xhpcMotorcadePersonnel.getRechargeMoney();
        Long motorcadePersonnelId = xhpcMotorcadePersonnel.getMotorcadePersonnelId();
        if(rechargeMoney !=null &&new BigDecimal(0).compareTo(rechargeMoney)==-1){
            //增加社区用户的剩余金额
            Map<String, Object> communityPersonnelById = xhpcMotorcadeMapper.getMotorcadePersonnelById(motorcadePersonnelId, null, null, 1);
            if(communityPersonnelById !=null){
                BigDecimal surplusMoney = new BigDecimal(communityPersonnelById.get("surplusMoney").toString());
                //原本充值金额
                BigDecimal rechargeMoney1 = new BigDecimal(communityPersonnelById.get("rechargeMoney").toString());
                XhpcMotorcadePersonnel xhpcMotorcadePersonnel1=new XhpcMotorcadePersonnel();
                xhpcMotorcadePersonnel1.setMotorcadePersonnelId(motorcadePersonnelId);
                BigDecimal remainingSum = surplusMoney.add(rechargeMoney);
                xhpcMotorcadePersonnel1.setSurplusMoney(remainingSum);
                xhpcMotorcadePersonnel1.setRechargeMoney(rechargeMoney1.add(rechargeMoney));
                xhpcMotorcadeMapper.updateMotorcadePersonnel(xhpcMotorcadePersonnel1);

//                Long rechargeOrderId=null;
//                if(UserTypeUtil.RECHARGE_PT.equals(xhpcMotorcadePersonnel.getRechargeType())){
//                    //增加充值记录
//                    rechargeOrderId = mechanismService.addRechargeOrder(motorcadePersonnelId, UserTypeUtil.MOTORCADE_USER_TYPE, rechargeMoney, remainingSum, xhpcMotorcadePersonnel.getRechargeType());
//                }else{
//                    rechargeOrderId=xhpcMotorcadePersonnel.getMoneyOrderId();
//                }
                //增加用户流水
                mechanismService.insertUserAccount(motorcadePersonnelId,rechargeMoney,remainingSum,null,null,null,UserTypeUtil.INSERT_BALANCE,xhpcMotorcadePersonnel1.getTenantId(),UserTypeUtil.MOTORCADE_USER_TYPE);
                //减去车队总金额
                Map<String, Object> motorcadeById = xhpcMotorcadeMapper.getMotorcadeById(xhpcMotorcadePersonnel.getMotorcadeId(), null, 1);
                BigDecimal remainingAmount = new BigDecimal(motorcadeById.get("remainingAmount").toString());
                BigDecimal allocatedAmount = new BigDecimal(motorcadeById.get("allocatedAmount").toString());

                if(new BigDecimal(0).compareTo(remainingAmount)==-1 && rechargeMoney.compareTo(remainingAmount)==-1){
                    XhpcMotorcade xhpcMotorcade =new XhpcMotorcade();
                    Long motorcadeId = xhpcMotorcadePersonnel.getMotorcadeId();
                    xhpcMotorcade.setMotorcadeId(motorcadeId);
                    xhpcMotorcade.setRemainingAmount(remainingAmount.subtract(rechargeMoney));
                    xhpcMotorcade.setAllocatedAmount(allocatedAmount.add(rechargeMoney));
                    xhpcMotorcadeMapper.updateMotorcade(xhpcMotorcade);
                    //增加用户流水记录
                    mechanismService.insertUserAccount(motorcadeId, rechargeMoney, remainingAmount.subtract(rechargeMoney),null,null,null,8,xhpcMotorcadePersonnel.getTenantId(),UserTypeUtil.MOTORCADE_TYPE);
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("车队总金额不足");
                }

                return AjaxResult.success();
            }
        }
        return AjaxResult.error("充值金额必需大于0");
    }

    @Override
    public Map<String, Object> getMotorcadePersonnelUserId(Long userId, String tenantId, Long chargingStationId) {
        return xhpcMotorcadeMapper.getMotorcadePersonnelUserId(userId, tenantId,chargingStationId);
    }

    public R judgeXhpcCommunity(XhpcMotorcade xhpcMotorcade){

        if(xhpcMotorcade.getName()==null || "".equals(xhpcMotorcade.getName())){
            return R.fail("名称必填");
        }
        if(xhpcMotorcade.getContactName()==null || "".equals(xhpcMotorcade.getContactName())){
            return R.fail("联系人必填");
        }
        if(xhpcMotorcade.getContactPhone()==null || "".equals(xhpcMotorcade.getContactPhone())){
            return R.fail("联系人电话必填");
        }
        if(xhpcMotorcade.getAddress()==null || "".equals(xhpcMotorcade.getAddress())){
            return R.fail("地址必填");
        }
        if(xhpcMotorcade.getIlk()==null ){
            return R.fail("付款类型必填");
        }
        if(xhpcMotorcade.getAccount()==null || "".equals(xhpcMotorcade.getAccount())){
            return R.fail("账号必填");
        }
        return R.ok();
    }


    public R judgeXhpcCommunityPersonnel(XhpcMotorcadePersonnel xhpcMotorcadePersonnel){

        if(xhpcMotorcadePersonnel.getName()==null || "".equals(xhpcMotorcadePersonnel.getName())){
            return R.fail("名称必填");
        }
        if(xhpcMotorcadePersonnel.getAccount()==null || "".equals(xhpcMotorcadePersonnel.getAccount()) || !"D".equals(xhpcMotorcadePersonnel.getAccount().substring(0,1))){
            return R.fail("账号必填且D开头12位");
        }
        if(xhpcMotorcadePersonnel.getPhone()==null || "".equals(xhpcMotorcadePersonnel.getPhone()) || xhpcMotorcadePersonnel.getPhone().length()!=11){
            return R.fail("绑定手机号必填");
        }

        return R.ok();
    }

    public  String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }

        return sb.toString();
    }
}
