package com.muyu.enterpise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.domain.SysCar;
import com.muyu.domain.req.SysCarReq;
import com.muyu.domain.resp.SysCarResp;
import com.muyu.enterprise.cache.SysCarCacheService;
import com.muyu.enterpise.mapper.SysCarMapper;
import com.muyu.enterpise.service.SysCarService;
import com.muyu.enterprise.cache.domain.InformationData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 车辆基础信息Service业务层处理
 * @author LiHD
 * @date 2024-09-18
 */
@Service
public class SysCarServiceImpl
        extends ServiceImpl<SysCarMapper, SysCar>
        implements SysCarService {

    @Autowired
    private SysCarMapper sysCarMapper;

    @Autowired
    private SysCarCacheService sysCarCacheService;

    /**
     * 精确查询车辆基础信息
     * @param id 车辆基础信息主键
     * @return 车辆基础信息
     */
    @Override
    public SysCar selectSysCarById(Long id) {
        LambdaQueryWrapper<SysCar> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(id, "id不可为空");
        queryWrapper.eq(SysCar::getId, id);


        return this.getOne(queryWrapper);
    }


    /**
     * 查询车辆基础信息列表
     * @param sysCarReq 车辆基础信息
     * @return 车辆基础信息
     */
    @Override
    public List<SysCarResp> selectSysCarList(SysCarReq sysCarReq) {
        LambdaQueryWrapper<SysCar> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(sysCarReq.getCarVin())) {
            queryWrapper.eq(SysCar::getCarVin,sysCarReq.getCarVin());
        }
        if (StringUtils.isNotEmpty(sysCarReq.getCarPlate())) {
            queryWrapper.like(SysCar::getCarPlate, sysCarReq.getCarPlate());
        }
        if (StringUtils.isNotEmpty(sysCarReq.getCarBrand())) {
            queryWrapper.eq(SysCar::getCarBrand, sysCarReq.getCarBrand());
        }
        if (StringUtils.isNotEmpty(sysCarReq.getCarModel())) {
            queryWrapper.eq(SysCar::getCarModel, sysCarReq.getCarModel());
        }
        if (null != sysCarReq.getCarType()) {
            queryWrapper.eq(SysCar::getCarType, sysCarReq.getCarType());
        }
        if (null != sysCarReq.getState()) {
            queryWrapper.eq(SysCar::getState, sysCarReq.getState());
        }
        //就是下面两个判断有问题 还有问题吗 没有我就下了
        List<SysCar> list = this.list(queryWrapper);
        for (SysCar sysCar : list) {
            String carVin = sysCar.getCarVin();
            //MQTTX是啥  跟MQ差不多 这样就行吧 OK  这个缓存 是老懂教我们的

            sysCarCacheService.put(carVin, sysCar);
            System.out.println("s够不够水电费健康发动机刚发打卡机这咋广发卡缴纳:"+carVin);
            SysCar sysCar1 = sysCarCacheService.get(carVin);
            System.out.println("asfih"+sysCar1);
        }
        return list.stream()
                .map(SysCarResp::reverseResp)
                .toList();
    }


    /**
    * 唯一 判断
    * @param sysCar 车辆基础信息
     * @return 车辆基础信息
    */
    @Override
    public Boolean checkIdUnique(SysCar sysCar) {
        LambdaQueryWrapper<SysCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCar::getId, sysCar.getId());
        return this.count(queryWrapper) > 0;
    }

    @Override
    public InformationData selectSysCarInfoByVin(String vin) {
        LambdaQueryWrapper<SysCar> car = new LambdaQueryWrapper<>();
        car.eq(SysCar::getCarVin, vin);
        SysCar sysCar = this.sysCarMapper.selectOne(car);
        return InformationData.builder()
                .sysCar(sysCar)

                .build();
    }

}
