<?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.dgtly.wxportal.mapper.WxMagnetMapper">

    <resultMap type="com.dgtly.wxportal.domain.WxMagnet" id="WxMagnetResult">
        <id     property="magnetId"       column="magnet_id"        />
        <result property="magnetName"     column="magnet_name"      />
        <result property="fatherMagnet"     column="father_magnet"      />
        <result property="orderNum"       column="order_num"      />
        <result property="url"            column="url"            />
        <result property="visible"        column="visible"        />
        <result property="magnetSize"     column="magnet_size"          />
        <result property="bgUrl"          column="bg_url"           />
        <result property="createBy"       column="create_by"      />
        <result property="createTime"     column="create_time"    />
        <result property="updateTime"     column="update_time"    />
        <result property="updateBy"       column="update_by"      />
        <result property="remark"         column="remark"         />
        <result property="magnetType"     column="magnet_type"         />
        <result property="appletQrUrl"    column="applet_qr_url"         />
        <result property="isEncry"        column="is_encry"   />

    </resultMap>

    <sql id="selectMagnetVo">
        select magnet_id, magnet_name, father_magnet,order_num, url,  visible, magnet_size, bg_url ,remark, magnet_type, applet_qr_url, create_by, create_time
		from wx_magnet
    </sql>

    <select id="selectTileMagnetList" parameterType="com.dgtly.wxportal.domain.WxMagnet" resultMap="WxMagnetResult">
    select distinct m.magnet_id, m.father_magnet, m.magnet_name, m.url, m.visible, m.magnet_size
    , m.bg_url, m.order_num, m.create_time, m.magnet_type, m.applet_qr_url
    from wx_magnet m where  m.visible =0
    </select>

    <select id="selectMagnetList" parameterType="com.dgtly.wxportal.domain.WxMagnet" resultMap="WxMagnetResult">
        select distinct m.magnet_id, m.father_magnet, m.magnet_name, m.url, m.visible, m.magnet_size
        , m.bg_url, m.order_num, m.create_time, m.magnet_type, m.applet_qr_url
        from wx_magnet m
        <where>
            <if test="magnetName != null and magnetName != ''">
                AND m.magnet_name like concat('%', #{magnetName}, '%')
            </if>
            <if test="visible != null and visible != ''">
                AND m.visible = #{visible}
            </if>
            <if test="magnetSize != null and magnetSize != ''">
                AND m.magnet_size = #{magnetSize}
            </if>
            <if test="magnetType != null and magnetType != ''">
                AND m.magnet_type = #{magnetType}
            </if>
            ${params.dataScope}
        </where>
        order by  order_num
    </select>


    <select id="selectCountRoleMagnetByMagnetId" resultType="Integer">
	    select count(1) from wx_role_magnet where magnet_id=#{magnetId}
	</select>

    <delete id="deleteMagnetById" parameterType="Long">
	    delete from wx_magnet where magnet_id = #{magnetId}
	</delete>

    <select id="selectMagnetById" parameterType="Long" resultMap="WxMagnetResult">
        <include refid="selectMagnetVo"/>
        where magnet_id = #{magnetId}
    </select>

    <select id="checkMagnetNameUnique"  resultMap="WxMagnetResult">
        <include refid="selectMagnetVo"/>
        where magnet_name=#{magnetName}
    </select>

    <insert id="insertMagnet" parameterType="com.dgtly.wxportal.domain.WxMagnet">
        insert into wx_magnet(
        <if test="magnetId != null and magnetId != 0">magnet_id,</if>
        <if test="magnetName != null and magnetName != ''">magnet_name,</if>
        <if test="fatherMagnet != null and fatherMagnet != ''">father_magnet,</if>
        <if test="orderNum != null and orderNum != ''">order_num,</if>
        <if test="url != null and url != ''">url,</if>
        <if test="visible != null">visible,</if>
        <if test="magnetSize !=null and magnetSize != ''">magnet_size,</if>
        <if test="bgUrl != null and bgUrl != ''">bg_url,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="magnetType != null and magnetType != ''">magnet_type,</if>
        <if test="appletQrUrl != null and appletQrUrl != ''">applet_qr_url,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="magnetId != null and magnetId != 0">#{magnetId},</if>
        <if test="magnetName != null and magnetName != ''">#{magnetName},</if>
        <if test="fatherMagnet != null and fatherMagnet != ''">#{fatherMagnet},</if>
        <if test="orderNum != null and orderNum != ''">#{orderNum},</if>
        <if test="url != null and url != ''">#{url},</if>
        <if test="visible != null">#{visible},</if>
        <if test="magnetSize !=null and magnetSize != ''">#{magnetSize},</if>
        <if test="bgUrl != null and bgUrl != ''">#{bgUrl},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="magnetType != null and magnetType != ''">#{magnetType},</if>
        <if test="appletQrUrl != null and appletQrUrl != ''">#{appletQrUrl},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <update id="updateMagnet" parameterType="com.dgtly.wxportal.domain.WxMagnet">
        update wx_magnet
        <set>
            <if test="magnetName != null and magnetName != ''">magnet_name = #{magnetName},</if>
            <if test="fatherMagnet != null and fatherMagnet != ''">father_magnet =#{fatherMagnet},</if>
            <if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
            <if test="url != null and url != ''">url = #{url},</if>
            <if test="visible != null">visible = #{visible},</if>
            <if test="magnetSize !=null">magnet_size = #{magnetSize},</if>
            <if test="bgUrl !=null and bgUrl != ''">bg_url = #{bgUrl},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="magnetType != null and magnetType != ''">magnet_type = #{magnetType},</if>
            <if test="appletQrUrl != null">applet_qr_url = #{appletQrUrl},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where magnet_id = #{magnetId}
    </update>

    <select id="selectMagnetAll" resultMap="WxMagnetResult">
        <include refid="selectMagnetVo"/>
        order by order_num
    </select>

    <select id="selectMagnetAllByUserId"  resultMap="WxMagnetResult">
        select distinct m.magnet_id,  m.magnet_name,m.father_magnet, m.url, m.visible, m.magnet_size
        , m.bg_url, m.order_num, m.create_time, m.magnet_type, m.applet_qr_url
        from wx_magnet m
        left join wx_role_magnet rm on m.magnet_id = rm.magnet_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        LEFT JOIN sys_role ro on ur.role_id = ro.role_id
        <where>
            ur.user_id = #{userId}
            <if test="wxMagnet.magnetName != null and wxMagnet.magnetName != ''">
                AND m.magnet_name like concat('%', #{wxMagnet.magnetName}, '%')
            </if>
            <if test="wxMagnet.visible != null and wxMagnet.visible != ''">
                AND m.visible = #{wxMagnet.visible}
            </if>
            <if test="wxMagnet.magnetSize != null and wxMagnet.magnetSize != ''">
                AND m.magnetSize = #{wxMagnet.magnet_size}
            </if>
            ${wxMagnet.params.dataScope}
        </where>
        order by  m.order_num
    </select>

    <select id="selectMagnetIdByRoleId" parameterType="Long" resultType="String">
		select m.magnet_id
		from wx_magnet m
		left join wx_role_magnet rm on m.magnet_id = rm.magnet_id
		where rm.role_id = #{roleId}

		order by  m.order_num
	</select>

    <delete id="deleteRoleMagnetByRoleId" parameterType="String">

        delete from wx_role_magnet where role_id = #{roleId}
    </delete>

    <insert id="batchRoleMagnet">
        insert into wx_role_magnet(role_id, magnet_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.roleId},#{item.magnetId})
        </foreach>
    </insert>

    <select id="selectMagnetByUserId" parameterType="String" resultMap="WxMagnetResult">
        select distinct m.magnet_id,  m.magnet_name,m.father_magnet, m.url, m.visible, m.magnet_size
        , m.bg_url, m.order_num, m.create_time, m.magnet_type, m.applet_qr_url,m.is_encry
        from wx_magnet m
        left join wx_role_magnet rm on m.magnet_id = rm.magnet_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        LEFT JOIN sys_role ro on ur.role_id = ro.role_id
        <where>
            ur.user_id = #{userId}
            and m.visible = "0"
            <if test="loginType != null and loginType != ''">
                AND ur.shop_type = #{loginType}
            </if>

            <if test="loginType == null or loginType == ''.toString()">
                AND ur.shop_type is null
            </if>

        </where>
        order by  m.order_num
    </select>

    <select id="selectWxMagnetRoleByUserId" parameterType="String" resultMap="WxMagnetResult">
        select distinct m.magnet_id,  m.magnet_name,m.father_magnet, m.url, m.visible, m.magnet_size
        , m.bg_url, m.order_num, m.create_time, m.magnet_type, m.applet_qr_url,m.is_encry
        from wx_magnet m
        left join wx_role_magnet rm on m.magnet_id = rm.magnet_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        LEFT JOIN sys_role ro on ur.role_id = ro.role_id
        <where>
            ur.user_id = #{userId}
             and father_magnet =#{magnetName}
        </where>
        order by  m.order_num asc
    </select>

    <select id="selectMagnetByCw" parameterType="String" resultMap="WxMagnetResult">
        select distinct m.magnet_id,  m.magnet_name,m.father_magnet, m.url, m.visible, m.magnet_size
        , m.bg_url, m.order_num, m.create_time, m.magnet_type, m.applet_qr_url,m.is_encry
        from wx_magnet m
        left join wx_role_magnet rm on m.magnet_id = rm.magnet_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        LEFT JOIN sys_role ro on ur.role_id = ro.role_id
        <where>
            ur.user_id = #{userId}
            and m.visible = "0"
            and ro.role_id= #{roleId}
        </where>
        order by  m.order_num
    </select>
    <select id="selectSapEmploeeIdByUserId" resultType="java.lang.String">
        select sap_employee_id from sys_user_ext where user_id = #{userId}
    </select>


</mapper>
