<?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.dcsoft.system.mapper.SysSyncRuleMapper">

    <resultMap type="SysSyncRule" id="SysSyncRuleResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="guid"    column="guid"    />
        <result property="allDeptUrl"    column="all_dept_url"    />
        <result property="addDeptUrl"    column="add_dept_url"    />
        <result property="allPeopleUrl"    column="all_people_url"    />
        <result property="addPeopleUrl"    column="add_people_url"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSysSyncRuleVo">
        select id, name, guid, all_dept_url, add_dept_url, all_people_url, add_people_url, remark, create_by, create_time, update_by, update_time from sys_sync_rule
    </sql>

    <select id="selectSysSyncRuleList" parameterType="SysSyncRule" resultMap="SysSyncRuleResult">
        <include refid="selectSysSyncRuleVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
        </where>
    </select>

    <select id="selectSysSyncRuleById" parameterType="Long" resultMap="SysSyncRuleResult">
        <include refid="selectSysSyncRuleVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysSyncRule" parameterType="SysSyncRule" useGeneratedKeys="true" keyProperty="id">
        insert into sys_sync_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="guid != null and guid != ''">guid,</if>
            <if test="allDeptUrl != null and allDeptUrl != ''">all_dept_url,</if>
            <if test="addDeptUrl != null and addDeptUrl != ''">add_dept_url,</if>
            <if test="allPeopleUrl != null and allPeopleUrl != ''">all_people_url,</if>
            <if test="addPeopleUrl != null and addPeopleUrl != ''">add_people_url,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="guid != null and guid != ''">#{guid},</if>
            <if test="allDeptUrl != null and allDeptUrl != ''">#{allDeptUrl},</if>
            <if test="addDeptUrl != null and addDeptUrl != ''">#{addDeptUrl},</if>
            <if test="allPeopleUrl != null and allPeopleUrl != ''">#{allPeopleUrl},</if>
            <if test="addPeopleUrl != null and addPeopleUrl != ''">#{addPeopleUrl},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSysSyncRule" parameterType="SysSyncRule">
        update sys_sync_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="guid != null and guid != ''">guid = #{guid},</if>
            <if test="allDeptUrl != null and allDeptUrl != ''">all_dept_url = #{allDeptUrl},</if>
            <if test="addDeptUrl != null and addDeptUrl != ''">add_dept_url = #{addDeptUrl},</if>
            <if test="allPeopleUrl != null and allPeopleUrl != ''">all_people_url = #{allPeopleUrl},</if>
            <if test="addPeopleUrl != null and addPeopleUrl != ''">add_people_url = #{addPeopleUrl},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysSyncRuleById" parameterType="Long">
        delete from sys_sync_rule where id = #{id}
    </delete>

    <delete id="deleteSysSyncRuleByIds" parameterType="String">
        delete from sys_sync_rule where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insetSysSyncRuleItem" parameterType="SysSyncItem" >
        insert into sys_sync_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dept != null and dept != ''">dept,</if>
            <if test="ruleId != null and ruleId != ''">rule_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dept != null and dept != ''">#{dept},</if>
            <if test="ruleId != null and ruleId != ''">#{ruleId},</if>
        </trim>
    </insert>

    <delete id="deleteSysSyncRuleItem" parameterType="Long">
        delete from sys_sync_item where rule_id = #{id}
    </delete>

    <select id="selectSysSyncRuleItem" parameterType="Long" resultMap="SysSyncItemResult">
        <include refid="selectSysSyncItemVo"/>
        where rule_id = #{id}
    </select>

    <resultMap type="SysSyncItem" id="SysSyncItemResult">
        <result property="id"    column="id"    />
        <result property="dept"    column="dept"    />
        <result property="ruleId"    column="rule_id"    />
    </resultMap>

    <sql id="selectSysSyncItemVo">
        select id, dept,rule_id from sys_sync_item
    </sql>

    <select id="selectSysSyncItemRuleList" parameterType="SysSyncRule" resultMap="SysSyncRuleResult">
        <include refid="selectSysSyncRulesVo"/>
        <where>
            and ssi.dept=#{branchId}
        </where>
    </select>
    <select id="queryParkDept" resultType="com.dcsoft.system.domain.SysSyncItem">
        select ssi.dept
        from sys_sync_rule ssr
                 left join sys_sync_item ssi on ssr.id = ssi.rule_id
        where guid = #{tdGuid}
    </select>

    <sql id="selectSysSyncRulesVo">
        select ssr.id, ssr.name,ssr.guid, ssr.all_dept_url, ssr.add_dept_url, ssr.all_people_url, ssr.add_people_url, ssr.remark, ssr.create_by, ssr.create_time, ssr.update_by, ssr.update_time
        from sys_sync_rule ssr
        left join sys_sync_item ssi on ssr.id=ssi.rule_id
    </sql>

</mapper>
