<?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.bonus.material.purchase.mapper.PurchaseNoticePersonMapper">
    <resultMap type="com.bonus.material.purchase.domain.PurchaseNoticePerson" id="PurchaseNoticePersonResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="nickName"    column="nick_name"    />
        <result property="telphone"    column="telphone"    />
        <result property="roleName"    column="role_name"    />
        <result property="deptName"    column="dept_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectPurchaseNoticePersonVo">
        select id, user_id, user_name, telphone, create_time, update_time from purchase_notice_person
    </sql>

    <select id="selectPurchaseNoticePersonList" parameterType="com.bonus.material.purchase.domain.PurchaseNoticePerson" resultMap="PurchaseNoticePersonResult">
        SELECT
        pnp.id,
        pnp.user_id,
        pnp.user_name,
        u.nick_name,
        telphone,
        GROUP_CONCAT(r.role_name SEPARATOR  '/') as role_name,
        concat( d2.dept_name, '/', d1.dept_name, '/', d.dept_name ) deptName
        FROM
        purchase_notice_person pnp
        LEFT JOIN sys_user u ON pnp.user_id = u.user_id
        LEFT JOIN sys_user_role sur ON u.user_id = sur.user_id
        LEFT JOIN sys_role r ON sur.role_id = r.role_id
        LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
        LEFT JOIN sys_dept d1 ON d.parent_id = d1.dept_id
        LEFT JOIN sys_dept d2 ON d1.parent_id = d2.dept_id
        WHERE
        u.STATUS = '0'
        GROUP BY pnp.user_id
    </select>

    <select id="getUnSelectedUserList" resultType="com.bonus.material.purchase.domain.PurchaseNoticePerson">
        SELECT
            u.user_id userId,
            u.dept_id deptId,
            u.nick_name nickName,
            u.user_name userName,
            u.avatar,
            u.phonenumber telphone,
            concat( d2.dept_name, '/', d1.dept_name, '/', d.dept_name ) deptName,
            GROUP_CONCAT(r.role_name SEPARATOR  '/') as roleName
        FROM
            sys_user u
                LEFT JOIN sys_user_role sur ON u.user_id = sur.user_id
                LEFT JOIN sys_role r ON sur.role_id = r.role_id
                LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
                LEFT JOIN sys_dept d1 ON d.parent_id = d1.dept_id
                LEFT JOIN sys_dept d2 ON d1.parent_id = d2.dept_id
        WHERE
            u.STATUS = '0'
          AND u.del_flag = '0'
          AND u.user_id NOT IN (
            SELECT
                user_id
            FROM
                purchase_notice_person)
        GROUP BY u.user_id
    </select>
    
    <select id="selectPurchaseNoticePersonById" parameterType="Long" resultMap="PurchaseNoticePersonResult">
        <include refid="selectPurchaseNoticePersonVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertPurchaseNoticePerson" parameterType="com.bonus.material.purchase.domain.PurchaseNoticePerson" useGeneratedKeys="true" keyProperty="id">
        insert into purchase_notice_person
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="telphone != null">telphone,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="telphone != null">#{telphone},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updatePurchaseNoticePerson" parameterType="com.bonus.material.purchase.domain.PurchaseNoticePerson">
        update purchase_notice_person
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="telphone != null">telphone = #{telphone},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePurchaseNoticePersonById" parameterType="Long">
        delete from purchase_notice_person where id = #{id}
    </delete>

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

    <insert id="insertBatchPurchaseNoticePerson">
        insert into purchase_notice_person(user_id,user_name,telphone,create_time) values
        <foreach collection="list" item="item" separator=",">
            (#{item.userId},#{item.userName},#{item.telphone},now())
        </foreach>
    </insert>
</mapper>