<?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.OrganizationalDao">
    <insert id="addData">
        insert into pm_org_info(name, level, parent_id, user_name, user_id, id_card,
                                sex, user_phone, status, org_type, lk_id, table_source)
        values (#{name}, #{level}, #{parentId}, #{userName}, #{userId}, #{idCard},
                #{sex}, #{userPhone}, 1, #{orgType}, #{lkId}, #{tableSource})
    </insert>
    <!--查询机构数据 -->
    <insert id="addDepartData" useGeneratedKeys="true" keyProperty="addId">
        insert into gs_exam.pm_dept(NAME, PARENT_ID, IS_ACTIVE, create_time, data_source)
        values (#{name}, #{parentId}, 1, now(), 1)
    </insert>

    <insert id="addUserData" useGeneratedKeys="true" keyProperty="addId">
        insert into gs_exam.pm_user(NAME, id_card, dept_id, LOGIN_NAME, SEX, TELPHONE, data_source, CREATE_TIME,
                                       IS_ACTIVE, SALT, PASSWD)
        values (#{userName}, #{idCard}, #{addId}, #{idCard}, #{sex}, #{userPhone}, 1, now(), 1, #{salt}, #{password})
    </insert>


    <update id="updateUserInfo">
        update gs_exam.pm_user
           set NAME=#{userName},
               SEX=#{sex},
               TELPHONE=#{userPhone},
               UPDATE_TIME=now(),
               id_card=#{idCard}
         where LOGIN_NAME = #{idCard}
    </update>
    <insert id="insertDeptLink">
        insert into pt_dept_lk(dep_id, sub_id)
        values (#{parentId}, #{tableId})
    </insert>
    <update id="updateData">
        update pm_org_info
           set user_name=#{userName},
               user_phone=#{userPhone},
               sex=#{sex},
               id_card=#{idCard}
         WHERE id = #{id}
    </update>
    <!--更新 退场-->
    <update id="userOutData">
        update pm_org_info
           set status=2
         where id = #{id}
    </update>
    <update id="updateDept">
        update gs_exam.pm_user
           set dept_id=#{deptId}
         where LOGIN_NAME = #{idCard}

    </update>
    <update id="theTeamExits">
        UPDATE gs_exam.pm_dept
           SET IS_ACTIVE=0
         where id = #{deptId}
    </update>
    <update id="updateUserOutStatus">
        update pm_org_info
           set status = 1
         where id = (select team_id from pt_team_exit where id = #{id})
    </update>

    <delete id="deleteUserOut">
        delete
          from pm_user_outsource
         where LOGIN_NAME = #{idCard}
    </delete>
    <delete id="deleteUserOutData">
        delete
          from pt_team_exit
         where id = #{id}
    </delete>


    <select id="getDeptSelected" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select DISTINCT dp.NAME,
                        dp.NAME      title,
                        dp.ID        tableId,
                        dp.id        id,
                        dp.PARENT_ID parentId,
                        'pm_dept'    tableSource
          from gs_exam.pm_dept dp
                   LEFT JOIN pm_org_info poi
                             on dp.id = poi.lk_id AND poi.status = 1
         where dp.IS_ACTIVE = 1
           AND (poi.id is null or dp.id = '1')
    </select>
    <!--外包商下拉选-->
    <select id="getOutSourceSelected" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select DISTINCT pa.id                lkId,
                        pa.enterpriseName    NAME,
                        pd.id                id,
                        pd.PARENT_ID         pid,
                        'project_assignment' tableSource
          from project_assignment pa
                   LEFT JOIN pm_org_info poi
                             on pa.id = poi.lk_id and poi.table_source = 'project_assignment' AND poi.level = 2 AND
                                poi.status = 1 AND POI.parent_id = #{id}
                   LEFT JOIN gs_exam.pm_dept pd
                             on pd.id = poi.lk_id
         where pa.is_active = 1
           and poi.parent_id is null
    </select>
    <!--新增人员下拉选-->
    <select id="getAddUserSelected" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select pd.id         tableId,
               pd.`NAME`,
               pd.PARENT_ID  tablePid,
               pu.`NAME`     userName,
               pu.LOGIN_NAME idCard,
               pu.sex,
               pu.TELPHONE   userPhone,
               'pm_dept'     tableSource
          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 pm_user_outsource puo
                             on pu.LOGIN_NAME = puo.LOGIN_NAME
         where pu.IS_ACTIVE = 1
           and puo.id is not null

    </select>
    <select id="getTreeList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select id,
               name,
               level,
               parent_id    parentId,
               table_source tableSource,
               lk_id        tableId,
               user_name    userName,
               id_card      idCard,
               sex,
               user_phone   userPhone,
               status,
               org_type     orgType
          FROM pm_org_info
         WHERE status = 1
    </select>
    <select id="getDataDetails" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select id,
               name,
               level,
               parent_id    parentId,
               table_source tableSource,
               lk_id        tableId,
               user_name    userName,
               id_card      idCard,
               sex,
               user_phone   userPhone,
               status
          FROM pm_org_info
         WHERE id = #{id}
    </select>
    <select id="getTeamUser" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select pu.`NAME`     userName,
               pu.LOGIN_NAME idCard,
               pu.dept_id    deptId,
               pu.sex,
               pu.TELPHONE   userPhone,
               'pm_dept'     tableSource
          from gs_exam.pm_user pu
                   left join pm_user_outsource puo
                             on pu.LOGIN_NAME = puo.LOGIN_NAME
         where pu.IS_ACTIVE = 1
           and pu.dept_id = #{deptId}
    </select>
    <select id="getUserNum" resultType="java.lang.Integer">
        select count(1)
          from pm_org_info
         where status = 1
           and (id_card = #{idCard} or user_phone = #{userPhone})
    </select>
    <!--查询分包商名称-->
    <select id="getTeamName" resultType="java.lang.Integer">
        select count(1)
          from pm_org_info
         where status = 1
           and parent_id = #{parentId}
           AND NAME = #{name}
    </select>

    <insert id="insertUserOutData">
        insert into pm_user_outsource(NAME, LOGIN_NAME, SEX, TELPHONE, dept_id, is_active, CREATE_TIME, UPDATE_TIME)
        values (#{userName}, #{idCard}, #{sex}, #{userPhone}, #{tableId}, 1, now(), now())
    </insert>
    <insert id="insertTeamData" useGeneratedKeys="true" keyProperty="id">
        insert into pt_team_exit(team_id, parent_id, team_name, phone, sub_id, sub_name, pro_id, pro_name, id_card,
                                 user_name, org_rel_id)
        values (#{teamId}, #{parentId}, #{teamName}, #{phone}, #{subId}, #{subName}, #{proId}, #{proName}, #{idCard},
                #{userName}, #{orgRelId})

    </insert>
    <select id="getUserList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select NAME userName, LOGIN_NAME idCard, SEX, TELPHONE userPhone, dept_id tableId
          FROM gs_exam.pm_user
         where IS_ACTIVE = 1 and  if_active = '1'
           and dept_id = #{deptId}
    </select>
    <select id="getEvaluatePersonList"
            resultType="com.bonus.gs.sub.evaluate.evaluate.beans.EvaluatePersonBean">
        select
        pp.user_id as id,
        pp.user_name as name,
        pp.user_type as type,
        pp.id_number as idCard,
        if(pp.sex = '0','男','女') as sex,
        pp.phone as phone,
        pp.post_id as deptId,
        poi.name as deptName,
        pp.role as roleId,
        sr.name as roleName
        from pj_person pp
        left join sys_role sr on pp.role = sr.id
        left join pm_org_info poi on pp.post_id = poi.id
        where pp.is_active = 1
        <if test="name != null and name != ''">
            and pp.user_name like concat('%', #{name}, '%')
        </if>
    </select>
    <select id="getLeaderSelect" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OrganizationalBean">
        select su.id       as id,
               su.username as name
          from sys_user su
          left join sys_role_user sru on su.id = sru.userId
         where su.status = 1
           and su.id > 1
            and sru.roleId = '7'
    </select>
    <select id="getConsIsExistByPro" resultType="java.lang.Integer">
        select count(1)
          from pm_org_info
         where parent_id = #{parentId} and name = #{name}
           and status = 1
    </select>
</mapper>
