<?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.glt.mapper.GltUserTicketReleaseMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*, u.nickname, u.avatar, u.phone
        FROM glt_user_ticket_release a
        LEFT JOIN gxwebsoft_core.sys_user u ON a.user_id = u.user_id
          <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.userTicketId != null">
                AND a.user_ticket_id LIKE CONCAT('%', #{param.userTicketId}, '%')
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.periodNo != null">
                AND a.period_no = #{param.periodNo}
            </if>
            <if test="param.releaseQty != null">
                AND a.release_qty = #{param.releaseQty}
            </if>
            <if test="param.releaseTime != null">
                AND a.release_time LIKE CONCAT('%', #{param.releaseTime}, '%')
            </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.user_ticket_id = #{param.keywords}
                  OR u.user_id = #{param.keywords}
                )
              </if>
        </where>
    </sql>

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

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.glt.entity.GltUserTicketRelease">
        <include refid="selectSql"></include>
    </select>
    <select id="getThisMonthReleaseList" resultType="com.gxwebsoft.glt.entity.GltUserTicketRelease">
        SELECT
            id,
            user_ticket_id,
            user_id,
            release_qty,
            tenant_id
        FROM
            glt_user_ticket_release
        WHERE
            STATUS = 0
          AND deleted = 0
          AND release_qty > 0
          AND DATE_FORMAT(release_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')
        ORDER BY
            release_time ASC
            LIMIT #{limitNum}
    </select>

</mapper>
