<?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.cftech.mp.wechat.dao.QrcodeMapper">

    <resultMap id="resultMap" type="com.cftech.mp.wechat.model.Qrcode">
        <id column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="ticket" property="ticket"/>
        <result column="reply_type" property="replyType"/>
        <result column="content_source" property="contentSource"/>
        <result column="action_name" property="actionName"/>
        <result column="expire_seconds" property="expireSeconds"/>
        <result column="shortUrl" property="shortUrl"/>
        <result column="accountsid" property="accountsid"/>
        <result column="del_flag" property="delFlag"/>
        <result column="es_endtime" property="esEndtime"/>
        <result column="sence" property="sence"/>
    </resultMap>

    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
		id, create_time, update_time, create_by, update_by, ticket, reply_type, content_source, action_name, expire_seconds, shortUrl, accountsid, del_flag, es_endtime,sence
	</sql>

    <insert id="save" parameterType="com.cftech.mp.wechat.model.Qrcode" keyProperty="id">
		insert into wx_mp_wechat_qrcode
		(
		id, create_time, update_time, create_by, update_by, ticket, reply_type, content_source, action_name, expire_seconds, shortUrl, accountsid, del_flag, es_endtime,sence
		)
		values
		(
		#{id, jdbcType=INTEGER}, #{createTime, jdbcType=TIMESTAMP},#{updateTime, jdbcType=TIMESTAMP},  #{createBy, jdbcType=BIGINT},  
		#{updateBy, jdbcType=BIGINT},  #{ticket, jdbcType=VARCHAR},  #{replyType, jdbcType=VARCHAR},  #{contentSource, jdbcType=VARCHAR},  
		#{actionName, jdbcType=VARCHAR},  #{expireSeconds, jdbcType=INTEGER},  #{shortUrl, jdbcType=VARCHAR},  #{accountsid, jdbcType=BIGINT},  
		#{delFlag, jdbcType=CHAR},#{esEndtime, jdbcType=TIMESTAMP},#{sence,jdbcType=VARCHAR}
		)
	</insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_wechat_qrcode t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_mp_wechat_qrcode
        <include refid="sqlWhere"/>
    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_wechat_qrcode t
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="fetchSearchByPageStore" parameterType="java.util.Map" resultType="com.cftech.mp.wechat.model.QrcodeDto">
        SELECT
        t.id, t.create_time, t.update_time, t.create_by, t.update_by, t.ticket, t.reply_type, t.content_source,
        t.action_name, t.expire_seconds, t.shortUrl, t.accountsid, t.del_flag, t.es_endtime,t.sence,
        s.name storeName
        FROM wx_mp_wechat_qrcode t
        left join aoformal.t_ao_store s on s.id = t.sence
        <include refid="sqlWhere"/>
        and (es_endtime > now() or es_endtime is null)
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>


    <select id="countStore" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_mp_wechat_qrcode t
        left join aoformal.t_ao_store s on s.id = t.sence
        <include refid="sqlWhere"/>
        and (es_endtime > now() or es_endtime is null)
    </select>

    <update id="update" parameterType="com.cftech.mp.wechat.model.Qrcode">
        update wx_mp_wechat_qrcode
        <set>
            <if test="id != null">
                id = #{id, jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time=now(),
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
            <if test="ticket != null">
                ticket = #{ticket, jdbcType=VARCHAR},
            </if>
            <if test="replyType != null">
                reply_type = #{replyType, jdbcType=VARCHAR},
            </if>
            <if test="contentSource != null">
                content_source = #{contentSource, jdbcType=VARCHAR},
            </if>
            <if test="actionName != null">
                action_name = #{actionName, jdbcType=VARCHAR},
            </if>
            <if test="expireSeconds != null">
                expire_seconds = #{expireSeconds, jdbcType=INTEGER},
            </if>
            <if test="shortUrl != null">
                shortUrl = #{shortUrl, jdbcType=VARCHAR},
            </if>
            <if test="accountsid != null">
                accountsid = #{accountsid, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=CHAR},
            </if>
            <if test="esEndtime != null">
                es_endtime = #{esEndtime, jdbcType=TIMESTAMP},
            </if>
            <if test="sence != null">
                sence = #{sence, jdbcType=VARCHAR},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
		update wx_mp_wechat_qrcode set del_flag=1 where id=#{id,jdbcType=BIGINT}
	</update>

</mapper>