<?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.qianhe.mapper.SjZdscMapper">
    
    <resultMap type="SjZdsc" id="SjZdscResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="lx"    column="lx"    />
        <result property="zdmc"    column="zdmc"    />
        <result property="bb"    column="bb"    />
        <result property="fjms"    column="fjms"    />
        <result property="zt"    column="zt"    />
        <result property="xzcs"    column="xzcs"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="deptName"    column="dept_name"    />
        <result property="gxqx"    column="gxqx"    />
    </resultMap>

    <sql id="selectSjZdscVo">
        select a.id, a.dept_id, a.lx, a.zdmc, a.bb, a.fjms, a.zt, a.xzcs, a.create_by, a.create_time,
               a.update_by, a.update_time, a.remark, a.gxqx,
                CONCAT(p.dept_name,'/',d.dept_name) dept_name
          from sj_zdsc a
          left join sys_dept d on a.dept_id=d.dept_id
            LEFT JOIN  sys_dept p on d.parent_id = p.dept_id
    </sql>
    <select id="selectSjZdscList" parameterType="SjZdsc" resultMap="SjZdscResult">
        <include refid="selectSjZdscVo"/>
        <where>
            <if test="deptId != null ">and (a.dept_id = #{deptId} or  find_in_set(#{deptId},d.ancestors))</if>
            <if test="lx != null  and lx != ''"> and a.lx = #{lx}</if>
            <if test="zdmc != null  and zdmc != ''"> and a.zdmc like  concat('%', #{zdmc}, '%')</if>
            <if test="bb != null  and bb != ''"> and a.bb = #{bb}</if>
            <if test="fjms != null  and fjms != ''"> and a.fjms = #{fjms}</if>
            <if test="zt != null  and zt != ''"> and a.zt = #{zt}</if>
            <if test="xzcs != null  and xzcs != ''"> and a.xzcs = #{xzcs}</if>
            <if test="gxqx != null  and gxqx != ''"> and a.gxqx = #{gxqx}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>
    <select id="selectSjZdscById" parameterType="Long" resultMap="SjZdscResult">
        <include refid="selectSjZdscVo"/>
        where a.id = #{id}
    </select>
        
    <insert id="insertSjZdsc" parameterType="SjZdsc" useGeneratedKeys="true" keyProperty="id">
        insert into sj_zdsc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="lx != null">lx,</if>
            <if test="zdmc != null">zdmc,</if>
            <if test="bb != null">bb,</if>
            <if test="fjms != null">fjms,</if>
            <if test="zt != null">zt,</if>
            <if test="xzcs != null">xzcs,</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>
            <if test="remark != null">remark,</if>
            <if test="gxqx != null">gxqx,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="lx != null">#{lx},</if>
            <if test="zdmc != null">#{zdmc},</if>
            <if test="bb != null">#{bb},</if>
            <if test="fjms != null">#{fjms},</if>
            <if test="zt != null">#{zt},</if>
            <if test="xzcs != null">#{xzcs},</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>
            <if test="remark != null">#{remark},</if>
            <if test="gxqx != null">#{gxqx},</if>
         </trim>
    </insert>
    <update id="updateSjZdsc" parameterType="SjZdsc">
        update sj_zdsc
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="lx != null">lx = #{lx},</if>
            <if test="zdmc != null">zdmc = #{zdmc},</if>
            <if test="bb != null">bb = #{bb},</if>
            <if test="fjms != null">fjms = #{fjms},</if>
            <if test="zt != null">zt = #{zt},</if>
            <if test="xzcs != null">xzcs = #{xzcs},</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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="gxqx != null">gxqx = #{gxqx},</if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="plxg" parameterType="SjBzxx">
        update sj_zdsc
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="lx != null">lx = #{lx},</if>
            <if test="zdmc != null">zdmc = #{zdmc},</if>
            <if test="bb != null">bb = #{bb},</if>
            <if test="fjms != null">fjms = #{fjms},</if>
            <if test="zt != null">zt = #{zt},</if>
            <if test="xzcs != null">xzcs = #{xzcs},</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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="gxqx != null">gxqx = #{gxqx},</if>
        </trim>
        where find_in_set(id, #{ids})
    </update>




    <select id="gxList" parameterType="SjZdsc" resultMap="SjZdscResult">
        select * from (
        <include refid="selectSjZdscVo"/>
        where 1=1 and zt = '1' and gxqx = '仅自己' and a.create_by = #{nowUserName}
        <if test="deptId != null ">and (a.dept_id = #{deptId} or  find_in_set(#{deptId}, d.ancestors))</if>
        <if test="lx != null  and lx != ''"> and a.lx = #{lx}</if>
        <if test="zdmc != null  and zdmc != ''"> and a.zdmc like concat('%', #{zdmc}, '%')</if>
        <if test="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>

        union
        <include refid="selectSjZdscVo"/>
        where 1=1 and zt = '1' and gxqx = '本单位' and a.dept_id = #{nowUserDeptId}
        <if test="deptId != null ">and (a.dept_id = #{deptId} or  find_in_set(#{deptId}, d.ancestors))</if>
        <if test="lx != null  and lx != ''"> and a.lx = #{lx}</if>
        <if test="zdmc != null  and zdmc != ''"> and a.zdmc like concat('%', #{zdmc}, '%')</if>
        <if test="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>

        union
        <include refid="selectSjZdscVo"/>
        where 1=1 and zt = '1' and gxqx = '仅上级' and (a.dept_id = #{nowUserDeptId} or find_in_set(#{nowUserDeptId}, d.ancestors))
        <if test="deptId != null ">and (a.dept_id = #{deptId} or  find_in_set(#{deptId}, d.ancestors))</if>
        <if test="lx != null  and lx != ''"> and a.lx = #{lx}</if>
        <if test="zdmc != null  and zdmc != ''"> and a.zdmc like concat('%', #{zdmc}, '%')</if>
        <if test="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>

        union
        <include refid="selectSjZdscVo"/>
        where 1=1 and zt = '1' and gxqx = '公开'
        <if test="deptId != null ">and (a.dept_id = #{deptId} or  find_in_set(#{deptId}, d.ancestors))</if>
        <if test="lx != null  and lx != ''"> and a.lx = #{lx}</if>
        <if test="zdmc != null  and zdmc != ''"> and a.zdmc like concat('%', #{zdmc}, '%')</if>
        <if test="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>

        ) t
    </select>
    <select id="gxgkList" parameterType="SjZdsc" resultMap="SjZdscResult">
        <include refid="selectSjZdscVo"/>
        where 1=1 and zt = '1' and gxqx = '公开'
    </select>


</mapper>