<?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.yanzhu.jh.project.mapper.SurProjectUnitInfoMapper">
    
    <resultMap type="SurProjectUnitInfo" id="SurProjectUnitInfoResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="projectId"    />
        <result property="unitId"    column="unitId"    />
        <result property="deptId"    column="deptId"    />
        <result property="unitType"    column="unitType"    />
        <result property="unitTypeName"    column="unitTypeName"    />
        <result property="unitName"    column="unitName"    />
        <result property="deptName"    column="deptName"    />
        <result property="unitPersonName"    column="unitPersonName"    />
        <result property="unitPersonPhone"    column="unitPersonPhone"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectSurProjectUnitInfoVo">
        select id, projectId, unitId, unitId as deptId, unitType, unitName, unitName as deptName,unitPersonName, unitPersonPhone, createUser, createTime, del_flag from sur_project_unit_info
    </sql>

    <select id="selectSurProjectUnitInfoList" parameterType="SurProjectUnitInfo" resultMap="SurProjectUnitInfoResult">
        select spui.id, spui.projectId, spui.unitId, spui.unitId as deptId, spui.unitType, sdd.dict_label as unitTypeName, spui.unitName, spui.unitName as deptName, spui.unitPersonName, spui.unitPersonPhone, spui.createUser, spui.createTime, spui.del_flag from sur_project_unit_info spui
        left join sys_dict_data sdd on sdd.dict_type='sys_dept_type' and dict_value=spui.unitType
        <where>
            and del_flag=0
            <if test="projectId != null "> and spui.projectId = #{projectId}</if>
            <if test="unitId != null "> and spui.unitId = #{unitId}</if>
            <if test="unitType != null "> and spui.unitType = #{unitType}</if>
            <if test="unitName != null  and unitName != ''"> and spui.unitName like concat('%', #{unitName}, '%')</if>
            <if test="unitPersonName != null  and unitPersonName != ''"> and spui.unitPersonName like concat('%', #{unitPersonName}, '%')</if>
            <if test="unitPersonPhone != null  and unitPersonPhone != ''"> and spui.unitPersonPhone = #{unitPersonPhone}</if>
            <if test="createUser != null  and createUser != ''"> and spui.createUser = #{createUser}</if>
            <if test="createTime != null "> and spui.createTime = #{createTime}</if>
            <if test="nowDept != null  and nowDept != ''">
                <if test='nowRole == "2" or nowRole == "3" or nowRole == "4"'>
                    and spui.unitType != 11
                </if>
                <if test='nowRole != "2" and nowRole != "3" or nowRole != "4"'>
                    and spui.unitId != #{nowDept}
                </if>
            </if>
            <if test="unitTypes != null ">
                and spui.unitType in
                <foreach item="type" collection="unitTypes" open="(" separator="," close=")">
                    #{type}
                </foreach>
            </if>
        </where>
        order by sdd.dict_sort
    </select>
    
    <select id="selectSurProjectUnitInfoById" parameterType="Long" resultMap="SurProjectUnitInfoResult">
        <include refid="selectSurProjectUnitInfoVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSurProjectUnitInfo" parameterType="SurProjectUnitInfo" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_unit_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">projectId,</if>
            <if test="unitId != null">unitId,</if>
            <if test="unitType != null">unitType,</if>
            <if test="unitName != null">unitName,</if>
            <if test="unitPersonName != null">unitPersonName,</if>
            <if test="unitPersonPhone != null">unitPersonPhone,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="unitType != null">#{unitType},</if>
            <if test="unitName != null">#{unitName},</if>
            <if test="unitPersonName != null">#{unitPersonName},</if>
            <if test="unitPersonPhone != null">#{unitPersonPhone},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateSurProjectUnitInfo" parameterType="SurProjectUnitInfo">
        update sur_project_unit_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">projectId = #{projectId},</if>
            <if test="unitId != null">unitId = #{unitId},</if>
            <if test="unitType != null">unitType = #{unitType},</if>
            <if test="unitName != null">unitName = #{unitName},</if>
            <if test="unitPersonName != null">unitPersonName = #{unitPersonName},</if>
            <if test="unitPersonPhone != null">unitPersonPhone = #{unitPersonPhone},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectUnitInfoById" parameterType="Long">
        delete from sur_project_unit_info where id = #{id}
    </delete>

    <delete id="deleteSurProjectUnitInfoByProjectId" parameterType="Long">
        delete from sur_project_unit_info where projectId = #{projectId} and unitId not in (select dept_id from sur_project_userinfo where project_id = #{projectId} and is_del=0)
    </delete>

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