VehiclePermitMapper.xml 6.42 KB
<?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="cn.com.glsx.admin.modules.mapper.VehiclePermitMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.admin.modules.entity.VehiclePermit">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="content_id" jdbcType="INTEGER" property="contentId"/>
        <result column="accorder_id" jdbcType="INTEGER" property="accorderId"/>
        <result column="receiver_id" jdbcType="INTEGER" property="receiverId"/>
        <result column="content_type" jdbcType="TINYINT" property="contentType"/>
        <result column="permit_type" jdbcType="VARCHAR" property="permitType"/>
        <result column="permit_days" jdbcType="INTEGER" property="permitDays"/>
        <result column="permit_effective_date" jdbcType="TIMESTAMP" property="permitEffectiveDate"/>
        <result column="permit_expired_date" jdbcType="TIMESTAMP" property="permitExpiredDate"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, content_id, accorder_id, receiver_id, content_type, permit_type, permit_days,
        permit_effective_date, permit_expired_date, del_flag, created_date, created_by, updated_date, updated_by
    </sql>

    <select id="selectByReceiverIdAndContentType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_vehicle_permit
        where del_flag = 0
        and receiver_id = #{receiverId}
        and content_type = #{contentType}
        and permit_effective_date &lt;= now()
        and permit_expired_date &gt; now()
    </select>

    <select id="selectAllByReceiverIdAndContentType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_vehicle_permit
        where del_flag = 0
          and receiver_id = #{receiverId}
          and content_type = #{contentType}
    </select>

    <select id="selectPermitVehicleByReceiverId" resultType="cn.com.glsx.admin.modules.model.view.VehiclePermitDTO">
        select concat('v', tvp.content_id) as id,
               tvp.accorder_id                accorderId,
               tv.id                          vehicleId,
               tv.vin,
               tv.owner_name                  ownerName,
               concat('u', tv.created_by)  as pid,
               tvp.permit_days                permitDays,
               tvp.permit_effective_date      permitEffectiveDate,
               tvp.permit_expired_date        permitExpiredDate,
               case
                   when tvp.permit_days = 0 then '长期'
                   else concat(tvp.permit_days, '天')
               end                            validTime,
               case
                   when tvp.permit_days = 0 then null
                   else greatest(ceil(timestampdiff(second, now(), tvp.permit_expired_date) / 86400), 0)
               end                            remainingDays
        from t_vehicle_permit tvp
                 left join t_vehicle tv on tvp.content_id = tv.id
        where tvp.del_flag = 0
          and tv.del_flag = 0
          and tv.created_by is not null
          and tvp.receiver_id = #{receiverId}
          and tvp.content_type = #{contentType}
          and tvp.permit_effective_date &lt;= now()
          and tvp.permit_expired_date &gt; now()
    </select>

    <insert id="insertOrUpdatePermits">
        INSERT INTO t_vehicle_permit (content_id, accorder_id, receiver_id, content_type, permit_type,
                                      permit_days, permit_effective_date, permit_expired_date,
                                      del_flag, created_date, created_by)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.contentId},
            #{item.accorderId},
            #{item.receiverId},
            #{item.contentType},
            #{item.permitType},
            #{item.permitDays},
            #{item.permitEffectiveDate},
            #{item.permitExpiredDate},
            0,
            sysdate(),
            #{item.createdBy}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
            accorder_id = VALUES(accorder_id),
            permit_type = VALUES(permit_type),
            permit_days = VALUES(permit_days),
            permit_effective_date = VALUES(permit_effective_date),
            permit_expired_date = VALUES(permit_expired_date),
            del_flag = 0,
            updated_date = sysdate(),
            updated_by = VALUES(created_by)
    </insert>

    <insert id="insertLongTermPermitsIfAbsent">
        INSERT IGNORE INTO t_vehicle_permit (content_id, accorder_id, receiver_id, content_type, permit_type,
                                             permit_days, permit_effective_date, permit_expired_date,
                                             del_flag, created_date, created_by)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.contentId},
            #{item.accorderId},
            #{item.receiverId},
            #{item.contentType},
            #{item.permitType},
            #{item.permitDays},
            #{item.permitEffectiveDate},
            #{item.permitExpiredDate},
            0,
            sysdate(),
            #{item.createdBy}
            )
        </foreach>
    </insert>

    <delete id="deleteExistedPermit">
        delete from t_vehicle_permit
        where del_flag = 0
        and content_type = #{contentType}
        and receiver_id = #{receiverId}
        and content_id in
        <foreach collection="contentIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </delete>

    <delete id="deletePermitByReceiverId">
        delete
        from t_vehicle_permit
        where del_flag = 0
          and content_type = #{contentType}
          and receiver_id = #{receiverId}
    </delete>
</mapper>