<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gxwebsoft.hjm.mapper.HjmCarMapper">

  <!-- 关联查询sql -->
  <sql id="selectSql">
    SELECT a.*,b.organization_name as organization,e.name as fence, f.organization_name as parentOrganization,
    f.comments as
    parentOrganizationAdmin, u.real_name as driver,u.phone as driverPhone
    FROM hjm_car a
    LEFT JOIN gxwebsoft_core.sys_organization b ON a.organization_id = b.organization_id
    LEFT JOIN gxwebsoft_core.sys_organization f ON a.organization_parent_id = f.organization_id
    LEFT JOIN hjm_fence e ON a.fence_id = e.id
    LEFT JOIN gxwebsoft_core.sys_user u ON a.driver_id = u.user_id
    <where>
      <if test="param.id != null">
        AND a.id = #{param.id}
      </if>
      <if test="param.name != null">
        AND a.name LIKE CONCAT('%', #{param.name}, '%')
      </if>
      <if test="param.image != null">
        AND a.image LIKE CONCAT('%', #{param.image}, '%')
      </if>
      <if test="param.type != null">
        AND a.type = #{param.type}
      </if>
      <if test="param.organizationId != null">
        AND a.organization_id = #{param.organizationId}
      </if>
      <if test="param.organizationParentId != null">
        AND a.organization_parent_id = #{param.organizationParentId}
      </if>
      <if test="param.driverId != null">
        AND a.driver_id = #{param.driverId}
      </if>
      <if test="param.kuaidi != null">
        AND a.kuaidi LIKE CONCAT('%', #{param.kuaidi}, '%')
      </if>
      <if test="param.kuaidiAdmin != null">
        AND a.kuaidi_admin LIKE CONCAT('%', #{param.kuaidiAdmin}, '%')
      </if>
      <if test="param.code != null">
        AND a.code LIKE CONCAT('%', #{param.code}, '%')
      </if>
      <if test="param.driver != null">
        AND a.driver = #{param.driver}
      </if>
      <if test="param.insuranceStatus != null">
        AND a.insurance_status = #{param.insuranceStatus}
      </if>
      <if test="param.gpsNo != null">
        AND a.gps_no LIKE CONCAT('%', #{param.gpsNo}, '%')
      </if>
      <if test="param.fence != null">
        AND a.fence LIKE CONCAT('%', #{param.fence}, '%')
      </if>
      <if test="param.district != null">
        AND a.district LIKE CONCAT('%', #{param.district}, '%')
      </if>
      <if test="param.claim != null">
        AND a.claim = #{param.claim}
      </if>
      <if test="param.installerId != null">
        AND a.installer_id = #{param.installerId}
      </if>
      <if test="param.userId != null">
        AND a.user_id = #{param.userId}
      </if>
      <if test="param.sortNumber != null">
        AND a.sort_number = #{param.sortNumber}
      </if>
      <if test="param.comments != null">
        AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
      </if>
      <if test="param.status != null">
        AND a.status = #{param.status}
      </if>
      <if test="param.deleted != null">
        AND a.deleted = #{param.deleted}
      </if>
      <if test="param.deleted == null">
        AND a.deleted = 0
      </if>
      <if test="param.createTimeStart != null">
        AND a.create_time &gt;= #{param.createTimeStart}
      </if>
      <if test="param.createTimeEnd != null">
        AND a.create_time &lt;= #{param.createTimeEnd}
      </if>
      <if test="param.keywords != null">
        AND (a.code LIKE CONCAT('%', #{param.keywords}, '%')
        OR a.comments LIKE CONCAT('%', #{param.keywords}, '%')
        OR a.gps_no = #{param.keywords}
        OR a.driver_name = #{param.keywords}
        OR u.phone = #{param.keywords}
        )
      </if>
    </where>
  </sql>

  <!-- 分页查询 -->
  <select id="selectPageRel" resultType="com.gxwebsoft.hjm.entity.HjmCar">
    <include refid="selectSql"></include>
  </select>

  <!-- 查询全部 -->
  <select id="selectListRel" resultType="com.gxwebsoft.hjm.entity.HjmCar">
    <include refid="selectSql"></include>
  </select>

  <select id="getByGpsNo" resultType="com.gxwebsoft.hjm.entity.HjmCar">
    SELECT a.*
    FROM hjm_car a
    WHERE a.gps_no = #{gpsNo} AND a.deleted = 0
  </select>

  <select id="getByCode" resultType="com.gxwebsoft.hjm.entity.HjmCar">
    SELECT a.*,b.organization_name as organization, b.comments as kuaidiAdmin,e.name as fence, f.organization_name as parentOrganization,
    f.comments as
    parentOrganizationAdmin, u.real_name as driver,u.phone as driverPhone
    FROM hjm_car a
    LEFT JOIN gxwebsoft_core.sys_organization b ON a.organization_id = b.organization_id
    LEFT JOIN gxwebsoft_core.sys_organization f ON a.organization_parent_id = f.organization_id
    LEFT JOIN hjm_fence e ON a.fence_id = e.id
    LEFT JOIN gxwebsoft_core.sys_user u ON a.driver_id = u.user_id
    <where>
      <if test="code != null">
        AND a.code = #{code}
      </if>
      AND a.deleted = 0
    </where>
  </select>

  <update id="updateByGpsNo" parameterType="com.gxwebsoft.hjm.entity.HjmCar">
    UPDATE hjm_car
    <set>
      <if test="param.longitude != null">
        longitude = #{param.longitude},
      </if>
      <if test="param.latitude != null">
        latitude = #{param.latitude},
      </if>
      <if test="param.speed != null">
        speed = #{param.speed},
      </if>
    </set>
    <where>
      gps_no = #{param.gpsNo}
    </where>
  </update>

</mapper>
