<?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.gs.sub.evaluate.evaluate.dao.PersonDao">
    <insert id="addPersonInfo" keyProperty="userId" useGeneratedKeys="true">
        insert into sys_user(username, password, nickname, id_card, phone, sex, status, org_id)
        values (#{name},
                #{password},
                #{name},
                #{idCard},
                #{phone},
                #{sex},
                '1',
                #{deptId})
    </insert>
    <insert id="addPersonRole">
        insert into sys_role_user(userId, roleId) values
        <foreach collection="list" item="item" separator=",">
            (#{item.userId}, #{item.roleId})
        </foreach>
    </insert>
    <insert id="addPersonRoleById">
        insert into sys_role_user(userId, roleId) values (#{id}, #{roleId})
    </insert>
    <update id="updatePerson">
        update sys_user
           set username = #{name},
               id_card = #{idCard},
               phone = #{phone},
               nickname = #{name},
               sex =#{sex},
               org_id = #{deptId}
            where id = #{id}
    </update>
    <update id="updateOldUser">
        <foreach collection="list" item="item" separator=";">
            update gs_exam.pm_user
            set evaluate_pass_word = #{item.evaluatePassWord}
            where TELPHONE = #{item.phone}
        </foreach>
    </update>
    <delete id="deletePersonRole">
        delete from sys_role_user where userId = #{id}
    </delete>
    <delete id="deletePersonById">
        delete from sys_user where id = #{id}
    </delete>
    <select id="getDeptTree" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.NodeBean">
        select ID        as id,
               PARENT_ID as parentId,
               NAME      as title,
               NAME      as name
          from gs_exam.pm_dept
    </select>
    <select id="getUserList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.PersonBean">
        select
        pu.ID as id,
        pu.NAME as name,
        pu.DEPT_ID as deptId,
        pd.NAME as deptName,
        GROUP_CONCAT(pnr.NAME) as roleName,
        pu.TELPHONE as phone,
        pu.LOGIN_NAME as idCard,
        if(pu.SEX = '1', '男','女') as sex,
        pu.CREATE_TIME as createTime
        from gs_exam.pm_user pu
        left join gs_exam.pm_dept pd on pu.DEPT_ID = pd.ID and pd.IS_ACTIVE = '1'
        left join gs_exam.pm_new_role_user pnru on pu.ID = pnru.user_id and pnru.del_flag = '0'
        left join gs_exam.pm_new_role pnr on pnru.ROLE_ID = pnr.id and pnr.del_flag = '0'
        where pu.IS_ACTIVE = '1' and pu.if_active = '1'
          <if test="bean.type == 'team'">
              and pu.LOGIN_NAME not in (select id_card from sys_user where status = '1' and id_card is not null)
          </if>
        <if test="bean.type == 'evaluate'">
            and pu.LOGIN_NAME not in (select id_card from pm_org_info where status = '1' and id_card is not null)
        </if>
        and pu.id != 1
        <if test="bean.id != null and bean.id != ''">
            and pu.dept_id in
            <foreach collection="arr" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="bean.keyWord != null and bean.keyWord != ''">
            and (
            pu.NAME like concat('%',#{bean.keyWord},'%') or
            pu.TELPHONE like concat('%',#{bean.keyWord},'%') or
            pu.LOGIN_NAME like concat('%',#{bean.keyWord},'%')
            )
        </if>
        GROUP BY pu.ID
    </select>
    <select id="getRoleSelect" resultType="com.bonus.gs.sub.evaluate.manager.model.Role">
        select id   as id,
               NAME as name
          from sys_role
    </select>
    <select id="getDeptSelect" resultType="com.bonus.gs.sub.evaluate.manager.model.Role">
        select ID   as id,
               NAME as name
          from pm_org_info
         where status = '1'
           and level = '2'
    </select>
    <select id="getPersonList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.PersonBean">
        select
            su.id,
            su.username as name,
            poi.name as deptName,
            su.phone,
            if(su.sex = '1', '男','女') as sex,
            su.id_card as idCard,
            sr.name as roleName,
            su.org_id as deptId,
            sr.id as roleId
        from sys_user su
        left join pm_org_info poi on su.org_id = poi.id
        left join sys_role_user sru on su.id = sru.userId
        left join sys_role sr on sru.roleId = sr.id
        where su.status = '1'
        <if test="keyWord != null and keyWord != ''">
            and (
            su.username like concat('%',#{keyWord},'%') or
            su.phone like concat('%',#{keyWord},'%') or
            su.id_card like concat('%',#{keyWord},'%')
            )
        </if>
    </select>
</mapper>
