package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.CarInformation;
import com.muyu.domain.CarType;
import com.muyu.domain.WarnStrategy;
import com.muyu.domain.req.WarnStrategyReq;
import com.muyu.enterprise.cache.warn.VehicleCacheWarnService;
import com.muyu.server.mapper.WarnStrategyMapper;
import com.muyu.server.service.CarInformationService;
import com.muyu.server.service.WarnStrategyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 预警策略业务实现层
 * @Author：蓬叁
 * @Package：com.muyu.warn.controller
 * @Project：cloud-server-8
 * @name：WarnLogsController
 * @Date：2024/9/20 下午7:29
 */
@Service
@AllArgsConstructor
public class WarnStrategyServiceImpl
        extends ServiceImpl<WarnStrategyMapper, WarnStrategy>
        implements WarnStrategyService {

    private final CarInformationService carInformationService;
    private final VehicleCacheWarnService vehicleCacheWarnService;

    /**
     * 精确查询预警策略
     *
     * @param id 预警策略主键
     * @return 预警策略
     */
    @Override
    public WarnStrategy selectWarnStrategyById(Long id)
    {
        LambdaQueryWrapper<WarnStrategy> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(id, "id不可为空");
        queryWrapper.eq(WarnStrategy::getId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 查询预警策略列表
     *
     * @param warnStrategy 预警策略
     * @return 预警策略
     */
    @Override
    public List<WarnStrategy> selectWarnStrategyList(WarnStrategy warnStrategy)
    {
        LambdaQueryWrapper<WarnStrategy> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(warnStrategy.getCarTypeId())){
            queryWrapper.eq(WarnStrategy::getCarTypeId, warnStrategy.getCarTypeId());
        }
        if (StringUtils.isNotEmpty(warnStrategy.getStrategyName())){
            queryWrapper.like(WarnStrategy::getStrategyName, warnStrategy.getStrategyName());
        }
        if (StringUtils.isNotNull(warnStrategy.getMsgId())){
            queryWrapper.eq(WarnStrategy::getMsgId, warnStrategy.getMsgId());
        }
        List<WarnStrategy> list = this.list(queryWrapper);
        list.forEach(warnStrategy1 -> {
            List<CarInformation> list1 = carInformationService
                    .list(new LambdaQueryWrapper<CarInformation>()
                    .eq(CarInformation::getCarInformationType, warnStrategy1.getCarTypeId()));
            list1.forEach(carInformation -> {
                vehicleCacheWarnService.put(vehicleCacheWarnService.keyPre()+carInformation.getCarInformationVIN(),list);
            });
        });
        return list;
    }

    /**
    * 唯一 判断
    * @param warnStrategy 预警策略
     * @return 预警策略
    */
    @Override
    public Boolean checkIdUnique(WarnStrategy warnStrategy) {
        LambdaQueryWrapper<WarnStrategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarnStrategy::getId, warnStrategy.getId());
        return this.count(queryWrapper) > 0;
    }
    /**
     * 查找车辆类型
     * @return
     */
    @Override
    public List<CarType> findByCarType() {
        List<WarnStrategy> list = this.list();
        List<CarType> carTypes = new ArrayList<>();
        list.forEach(warnStrategy -> {
            CarType carType = new CarType();
            carType.setCarTypeId(warnStrategy.getCarTypeId());
            carType.setCarTypeName(warnStrategy.getStrategyName());
            carType.setCarTypeRules(warnStrategy.getCarTypeId());
            carTypes.add(carType);
        });
        return carTypes;
    }

    /**
     * 根据Id查找车辆信息
     * @param ids
     * @return
     */
    @Override
    public List<WarnStrategyReq> selectStrategyType(Long ids) {
        List<WarnStrategy> warnStrategyReqs = this.list(new LambdaQueryWrapper<WarnStrategy>()
                .eq(WarnStrategy::getCarTypeId,ids))
                .stream().toList();
        List<WarnStrategyReq> carTypes = new ArrayList<>();
        WarnStrategyReq warnStrategyReq1 = new WarnStrategyReq();
        warnStrategyReqs.forEach(warnStrategyReq -> {
            warnStrategyReq1.setId(warnStrategyReq.getId());
            warnStrategyReq1.setStrategyName(warnStrategyReq.getStrategyName());
            carTypes.add(warnStrategyReq1);
        });
        return carTypes;
    }

}
