package com.muyu.enterprise.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.FenceGroup;
import com.muyu.domain.Vehicle;
import com.muyu.domain.VehicleType;
import com.muyu.domain.req.BoundFenceGroupReq;
import com.muyu.domain.req.VehicleManageReq;
import com.muyu.domain.resp.VehicleManageResp;
import com.muyu.enterprise.cache.AllVehicleCacheService;
import com.muyu.enterprise.mapper.VehicleMapper;
import com.muyu.enterprise.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Author: LiDongJia
 * @Package: com.muyu.car.service.impl
 * @Project: 2112-car-cloud-server
 * @name: VehicleServiceImpl
 * @Date: 2024/9/17  17:41
 * @Description: 车辆管理业务层
 */
@Service
public class VehicleServiceImpl
        extends ServiceImpl<VehicleMapper, Vehicle>
        implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询车辆管理列表
     * @param vehicleManageReq
     * @return
     */
    @Override
    public List<VehicleManageResp> getVehicleList(VehicleManageReq vehicleManageReq) {
        MPJLambdaWrapper<Vehicle> wrapper = new MPJLambdaWrapper<>();

        wrapper.selectAll(Vehicle.class)
                .selectAs(VehicleType::getVehicleTypeName, VehicleManageResp::getVehicleTypeName)
                .leftJoin(VehicleType.class, VehicleType::getVehicleTypeId, Vehicle::getVehicleTypeId)
                .like(
                        StringUtils.isNotEmpty(vehicleManageReq.getLicenceNumber()),
                        Vehicle::getLicenceNumber, vehicleManageReq.getLicenceNumber()
                )
                .like(
                        StringUtils.isNotEmpty(vehicleManageReq.getVehicleVin()),
                        Vehicle::getVehicleVin, vehicleManageReq.getVehicleVin()
                )
                .eq(
                        StringUtils.isNotBlank(vehicleManageReq.getVehicleStatus()),
                        Vehicle::getVehicleStatus, vehicleManageReq.getVehicleStatus()
                );
        List<VehicleManageResp> list = vehicleMapper.selectJoinList(VehicleManageResp.class, wrapper);
        return list;
    }

    /**
     * 根据车辆vin查询模板id
     * @param vehicleVin
     * @return
     */
    @Override
    public Long findByVehicleVin(String vehicleVin) {
        Long templateId = vehicleMapper.findByVehicleVin(vehicleVin);
        return templateId;
    }

    @Override
    public void boundFenceGroup(BoundFenceGroupReq boundFenceGroupReq) {
        vehicleMapper.boundFenceGroup(boundFenceGroupReq);
    }

    @Override
    public List<FenceGroup> showBoundFenceGroup(Long fenceGroupId) {
        List<FenceGroup> fenceGroups = vehicleMapper.showBoundFenceGroup(fenceGroupId);
        return fenceGroups;
    }


}
