<?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.fs.qw.mapper.QwDrainageLinkMapper">
    
    <resultMap type="QwDrainageLink" id="QwDrainageLinkResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="wayId"    column="way_id"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="linkUrl"    column="link_url"    />
        <result property="title"    column="title"    />
        <result property="nickName"    column="nick_name"    />
        <result property="phone"    column="phone"    />
        <result property="companyName"    column="company_name"    />
        <result property="avatarUrl"    column="avatar_url"    />
        <result property="content"    column="content"    />
        <result property="clickNum"    column="click_num"    />
        <result property="addNum"    column="add_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="companyId"    column="company_id"    />
        <result property="type"    column="type"    />
        <result property="corpId"    column="corp_id"    />
    </resultMap>

    <sql id="selectQwDrainageLinkVo">
        select id, name, way_id,type, qr_code, link_url, title, nick_name, phone, company_name, avatar_url, content, click_num, add_num, create_time, create_by, company_id ,corp_id from qw_drainage_link
    </sql>

    <select id="selectQwDrainageLinkList" parameterType="QwDrainageLink" resultMap="QwDrainageLinkResult">
        <include refid="selectQwDrainageLinkVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="wayId != null "> and way_id = #{wayId}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="type != null "> and `type` = #{type}</if>
            <if test="corpId != null "> and corp_id = #{corpId}</if>
        </where>
    </select>
    
    <select id="selectQwDrainageLinkById" parameterType="Long" resultMap="QwDrainageLinkResult">
        <include refid="selectQwDrainageLinkVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertQwDrainageLink" parameterType="QwDrainageLink" useGeneratedKeys="true" keyProperty="id">
        insert into qw_drainage_link
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="wayId != null">way_id,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="linkUrl != null">link_url,</if>
            <if test="title != null">title,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="phone != null">phone,</if>
            <if test="companyName != null">company_name,</if>
            <if test="avatarUrl != null">avatar_url,</if>
            <if test="content != null">content,</if>
            <if test="clickNum != null">click_num,</if>
            <if test="addNum != null">add_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="companyId != null">company_id,</if>
            <if test="type != null">type,</if>
            <if test="corpId != null">corp_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="wayId != null">#{wayId},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="linkUrl != null">#{linkUrl},</if>
            <if test="title != null">#{title},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="avatarUrl != null">#{avatarUrl},</if>
            <if test="content != null">#{content},</if>
            <if test="clickNum != null">#{clickNum},</if>
            <if test="addNum != null">#{addNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="type != null">#{type},</if>
            <if test="corpId != null">#{corpId},</if>
         </trim>
    </insert>

    <update id="updateQwDrainageLink" parameterType="QwDrainageLink">
        update qw_drainage_link
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="wayId != null">way_id = #{wayId},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="linkUrl != null">link_url = #{linkUrl},</if>
            <if test="title != null">title = #{title},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="content != null">content = #{content},</if>
            <if test="clickNum != null">click_num = #{clickNum},</if>
            <if test="addNum != null">add_num = #{addNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwDrainageLinkById" parameterType="Long">
        delete from qw_drainage_link where id = #{id}
    </delete>
    <select id="selectQwDrainageLinkStatisticsCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  count( IF ( t.tt =2,1, null ) ) AS addNum,count( IF ( t.tt =1,1, NULL ) ) AS clickNum ,t.type
        FROM
            (
                SELECT DATE_FORMAT( l.create_time, #{sqlDateFormat} ) AS type, l.type as tt
                FROM
                    qw_drainage_link_logs  AS l
                WHERE
                    (DATE_FORMAT(l.create_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
                  AND l.link_id=#{companyId} )t
        GROUP BY t.type
    </select>
    <delete id="deleteQwDrainageLinkByIds" parameterType="String">
        delete from qw_drainage_link where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>