package com.muyu.enterprise.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.muyu.domain.FenceGroup;
import com.muyu.domain.Vehicle;
import com.muyu.domain.req.BoundFenceGroupReq;
import com.muyu.domain.req.VehicleManageReq;
import com.muyu.domain.resp.VehicleManageResp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: LiDongJia
 * @Package: com.muyu.car.mapper
 * @Project: 2112-car-cloud-server
 * @name: VehicleMapper
 * @Date: 2024/9/17  17:43
 * @Description: 车辆管理持久层
 */
@Mapper
public interface VehicleMapper extends MPJBaseMapper<Vehicle> {

    /**
     * 查询车辆管理列表
     * @param vehicleManageReq
     * @return
     */
    List<VehicleManageResp> findAll(VehicleManageReq vehicleManageReq);

    /**
     * 根据车辆vin查询模版id
     * @param vehicleVin
     * @return
     */
    @Select("SELECT t.message_template_id FROM vehicle v LEFT JOIN vehicle_type t ON v.vehicle_type_id = t.vehicle_type_id WHERE v.vehicle_vin = #{vehicleVin}")
    Long findByVehicleVin(String vehicleVin);

    /**
     * 车辆绑定围栏组
     * @param boundFenceGroupReq
     */
//    @Insert("INSERT INTO `vehicle-basic`.`bound_middle` (`fence_group_id`,`vehicle_id`) VALUES <foreach collection=\"fenceGroupIds\" item=\"id\" separator=\",\">  (#{id},#{vehicleId}) </foreach>")
    @Insert({
            "<script> INSERT INTO `vehicle-basic`.`bound_middle` (fence_group_id,vehicle_id) VALUES <foreach collection='fenceGroupIds' item='id' separator=','> (#{id}, #{vehicleId}) </foreach> </script>"
//   "INSERT INTO `vehicle-basic`.`bound_middle` (fence_group_id, vehicle_id) VALUES <foreach collection=\"fenceGroupIds\" item=\"id\" separator=\",\"> (#{id}, #{vehicleId}) </foreach> "
    })
    void boundFenceGroup(BoundFenceGroupReq boundFenceGroupReq);

    @Select("SELECT * FROM bound_middle bm LEFT JOIN vehicle v ON bm.vehicle_id=v.vehicle_id WHERE bm.fence_group_id = #{fenceGroupId}")
    List<FenceGroup> showBoundFenceGroup(@Param("fenceGroupId") Long fenceGroupId);


}
