<?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.zcxk.dao.OrgMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.Org" id="OrgMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="tenantId" column="TENANT_ID" jdbcType="VARCHAR"/>
        <result property="orgType" column="ORG_TYPE" jdbcType="VARCHAR"/>
        <result property="parentOrgId" column="PARENT_ORG_ID" jdbcType="INTEGER"/>
        <result property="orgAreaId" column="ORG_AREA_ID" jdbcType="INTEGER"/>
        <result property="orgName" column="ORG_NAME" jdbcType="VARCHAR"/>
        <result property="orgState" column="ORG_STATE" jdbcType="INTEGER"/>
        <result property="remark" column="REMARK" jdbcType="VARCHAR"/>
        <result property="orgLeaderName" column="ORG_LEADER_NAME" jdbcType="VARCHAR"/>
        <result property="orgLeaderPhone" column="ORG_LEADER_PHONE" jdbcType="VARCHAR"/>
        <result property="orgLeaderEmail" column="ORG_LEADER_EMAIL" jdbcType="VARCHAR"/>
        <result property="orgLeaderSex" column="ORG_LEADER_SEX" jdbcType="VARCHAR"/>
        <result property="status" column="STATUS" jdbcType="INTEGER"/>
        <result property="dateCreate" column="DATE_CREATE" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/>
        <result property="dateUpdate" column="DATE_UPDATE" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
        <result property="parentId" column="parentId" jdbcType="INTEGER"/>
        <result property="parentName" column="parentName" jdbcType="VARCHAR"/>
        <result property="areaName" column="areaName" jdbcType="VARCHAR"/>

    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        ID, TENANT_ID, ORG_TYPE, PARENT_ORG_ID, ORG_AREA_ID, ORG_NAME, ORG_STATE, REMARK, ORG_LEADER_NAME, ORG_LEADER_PHONE, ORG_LEADER_EMAIL, ORG_LEADER_SEX, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="OrgMap">
        select   a.id, a.tenant_id, a.org_type, a.parent_org_id, a.org_area_id, a.org_name, a.org_state, a.remark, a.org_leader_name, a.org_leader_phone, a.org_leader_email, a.org_leader_sex, a.status
        ,b.id parentId,b.ORG_NAME parentName,c.NAME areaName
        from uims_org a left join uims_org b on a.PARENT_ORG_ID=b.ID
        left join uims_area c on a.ORG_AREA_ID=c.id
        where a.ID = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="OrgMap">
        select
        <include refid="Base_Column_List"/>
        from uims_org
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="OrgMap">
        select
        <include refid="Base_Column_List"/>

        from uims_org
        <where>
        <if test="programItems !=null">
            id in
            <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
        </if>

            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and TENANT_ID = #{tenantId}
            </if>
            <if test="orgType != null and orgType != ''">
                and ORG_TYPE = #{orgType}
            </if>
            <if test="parentOrgId != null">
                and PARENT_ORG_ID = #{parentOrgId}
            </if>
            <if test="orgAreaId != null">
                and ORG_AREA_ID = #{orgAreaId}
            </if>
            <if test="orgName != null and orgName != ''">
                and ORG_NAME = #{orgName}
            </if>
            <if test="orgState != null">
                and ORG_STATE = #{orgState}
            </if>
            <if test="remark != null and remark != ''">
                and REMARK = #{remark}
            </if>
            <if test="orgLeaderName != null and orgLeaderName != ''">
                and ORG_LEADER_NAME = #{orgLeaderName}
            </if>
            <if test="orgLeaderPhone != null and orgLeaderPhone != ''">
                and ORG_LEADER_PHONE = #{orgLeaderPhone}
            </if>
            <if test="orgLeaderEmail != null and orgLeaderEmail != ''">
                and ORG_LEADER_EMAIL = #{orgLeaderEmail}
            </if>
            <if test="orgLeaderSex != null and orgLeaderSex != ''">
                and ORG_LEADER_SEX = #{orgLeaderSex}
            </if>
            <if test="status != null">
                and STATUS = #{status}
            </if>
            <if test="dateCreate != null">
                and DATE_CREATE = #{dateCreate}
            </if>
            <if test="createBy != null and createBy != ''">
                and CREATE_BY = #{createBy}
            </if>
            <if test="dateUpdate != null">
                and DATE_UPDATE = #{dateUpdate}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and UPDATE_BY = #{updateBy}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_org(ID, TENANT_ID, ORG_TYPE, PARENT_ORG_ID, ORG_AREA_ID, ORG_NAME, ORG_STATE, REMARK, ORG_LEADER_NAME, ORG_LEADER_PHONE, ORG_LEADER_EMAIL, ORG_LEADER_SEX, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY)
        values ( #{id}, #{tenantId}, #{orgType}, #{parentOrgId}, #{orgAreaId}, #{orgName}, #{orgState}, #{remark}, #{orgLeaderName}, #{orgLeaderPhone}, #{orgLeaderEmail}, #{orgLeaderSex}, #{status}, #{dateCreate}, #{createBy}, #{dateUpdate}, #{updateBy})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_org(ID, TENANT_ID, ORG_TYPE, PARENT_ORG_ID, ORG_AREA_ID, ORG_NAME, ORG_STATE, REMARK,
        ORG_LEADER_NAME, ORG_LEADER_PHONE, ORG_LEADER_EMAIL, ORG_LEADER_SEX, STATUS, DATE_CREATE, CREATE_BY,
        DATE_UPDATE, UPDATE_BY)
        values
        <foreach collection="orgs" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.tenantId}, #{item.orgType}, #{item.parentOrgId}, #{item.orgAreaId}, #{item.orgName},
            #{item.orgState}, #{item.remark}, #{item.orgLeaderName}, #{item.orgLeaderPhone}, #{item.orgLeaderEmail},
            #{item.orgLeaderSex}, #{item.status}, #{item.dateCreate}, #{item.createBy}, #{item.dateUpdate},
            #{item.updateBy} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_org
        <set>
            <if test="tenantId != null and tenantId != ''">
                TENANT_ID = #{tenantId},
            </if>
            <if test="orgType != null and orgType != ''">
                ORG_TYPE = #{orgType},
            </if>
            <if test="parentOrgId != null">
                PARENT_ORG_ID = #{parentOrgId},
            </if>
            <if test="orgAreaId != null">
                ORG_AREA_ID = #{orgAreaId},
            </if>
            <if test="orgName != null and orgName != ''">
                ORG_NAME = #{orgName},
            </if>
            <if test="orgState != null">
                ORG_STATE = #{orgState},
            </if>
            <if test="remark != null and remark != ''">
                REMARK = #{remark},
            </if>
            <if test="orgLeaderName != null and orgLeaderName != ''">
                ORG_LEADER_NAME = #{orgLeaderName},
            </if>
            <if test="orgLeaderPhone != null and orgLeaderPhone != ''">
                ORG_LEADER_PHONE = #{orgLeaderPhone},
            </if>
            <if test="orgLeaderEmail != null and orgLeaderEmail != ''">
                ORG_LEADER_EMAIL = #{orgLeaderEmail},
            </if>
            <if test="orgLeaderSex != null and orgLeaderSex != ''">
                ORG_LEADER_SEX = #{orgLeaderSex},
            </if>
            <if test="status != null">
                STATUS = #{status},
            </if>
            <if test="dateCreate != null">
                DATE_CREATE = #{dateCreate},
            </if>
            <if test="createBy != null and createBy != ''">
                CREATE_BY = #{createBy},
            </if>
            <if test="dateUpdate != null">
                DATE_UPDATE = #{dateUpdate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                UPDATE_BY = #{updateBy},
            </if>
        </set>
        where ID = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from uims_org where ID = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_org
    </select>
    <select id="selectPage" resultMap="OrgMap">
        select
        <include refid="Base_Column_List"/>
        from uims_org
        <where>
            status!=0
            <if test="org.id != null">
                and ID = #{org.id}
            </if>
            <if test="org.tenantId != null and org.tenantId != ''">
                and TENANT_ID = #{org.tenantId}
            </if>
            <if test="org.orgType != null and org.orgType != ''">
                and ORG_TYPE = #{org.orgType}
            </if>
            <if test="org.parentOrgId != null">
                and PARENT_ORG_ID = #{org.parentOrgId}
            </if>
            <if test="org.orgAreaId != null">
                and ORG_AREA_ID = #{org.orgAreaId}
            </if>
            <if test="org.orgName != null and org.orgName != ''">
                and ORG_NAME = #{org.orgName}
            </if>
            <if test="org.orgState != null">
                and ORG_STATE = #{org.orgState}
            </if>
            <if test="org.remark != null and org.remark != ''">
                and REMARK = #{org.remark}
            </if>
            <if test="org.orgLeaderName != null and org.orgLeaderName != ''">
                and ORG_LEADER_NAME = #{org.orgLeaderName}
            </if>
            <if test="org.orgLeaderPhone != null and org.orgLeaderPhone != ''">
                and ORG_LEADER_PHONE = #{org.orgLeaderPhone}
            </if>
            <if test="org.orgLeaderEmail != null and org.orgLeaderEmail != ''">
                and ORG_LEADER_EMAIL = #{org.orgLeaderEmail}
            </if>
            <if test="org.orgLeaderSex != null and org.orgLeaderSex != ''">
                and ORG_LEADER_SEX = #{org.orgLeaderSex}
            </if>
            <if test="org.status != null">
                and STATUS = #{org.status}
            </if>
            <if test="org.dateCreate != null">
                and DATE_CREATE = #{org.dateCreate}
            </if>
            <if test="org.createBy != null and org.createBy != ''">
                and CREATE_BY = #{org.createBy}
            </if>
            <if test="org.dateUpdate != null">
                and DATE_UPDATE = #{org.dateUpdate}
            </if>
            <if test="org.updateBy != null and org.updateBy != ''">
                and UPDATE_BY = #{org.updateBy}
            </if>
        </where>
    </select>
    <select id="findOrgUser" resultType="com.zcxk.entity.Org">
        select a.ID,a.ORG_NAME orgName from uims_org a join uims_user b on
         (a.ID=b.DEPT_ORG_ID or a.ID=b.COMPANY_ORG_ID)
         where a.id=#{id} and b.status!=0
         limit 1
    </select>
    <select id="selectTrees" resultType="com.zcxk.dto.OrgTree">
          select ID, TENANT_ID, ORG_TYPE, PARENT_ORG_ID, ORG_AREA_ID, ORG_NAME, ORG_STATE, REMARK, ORG_LEADER_NAME, ORG_LEADER_PHONE, ORG_LEADER_EMAIL, ORG_LEADER_SEX, STATUS, DATE_CREATE, CREATE_BY, DATE_UPDATE, UPDATE_BY
           from uims_org
           <where>
              and status!=0
               <if test="tenantId!=null ">
                  and TENANT_ID=#{tenantId}
               </if>
               <if test="orgType=='company'||orgType=='department'">
                  and ORG_TYPE=#{orgType}
               </if>
               <if test="id!=null">
                   and id=#{id}
               </if>
           </where>

    </select>
    <select id="findOrgType" resultType="com.zcxk.entity.Org">
        select a.ORG_TYPE ,  b.ORG_TYPE parentType,c.ORG_TYPE childType
         from uims_org a left join uims_org b  ON (a.PARENT_ORG_ID=b.ID and b.status=1)
                         LEFT JOIN uims_org c ON (a.id=c.PARENT_ORG_ID and c.status=1)
        where a.id=#{id}  limit 1
    </select>

    <select id="findOrgByTenant" resultType="com.zcxk.entity.Org">

        select
            id as "id",
            PARENT_ORG_ID as "parentOrgId",
            ORG_TYPE as "orgType"
        from uims_org
        where `STATUS` = 1 and TENANT_ID= #{tenantId}
    </select>
    <select id="findParentOrgByChildId" resultType="String">
       select getParentList(#{childId})
    </select>
    <select id="countCompanyByUser" resultType="java.lang.Integer">
        select count(1)
        from uims_org p
        left join uims_org o on p.id=o.PARENT_ORG_ID
        where p.`STATUS` = 1 and o.`STATUS` =1
        and p.PARENT_ORG_ID = 0 and o.ORG_TYPE = 'company'
        and p.TENANT_ID = #{tenantId}
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            and o.id in
            <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
        </if>
    </select>

    <select id="getCompanyByUser" resultType="com.zcxk.dto.OrgBaseTreeInfoDto">
        select
            p.id as "id",
            p.org_name as "orgName",
            p.PARENT_ORG_ID as "parentOrgId",
            p.ORG_TYPE as "orgType"
        from uims_org p
        where p.`STATUS` = 1
        and p.ORG_TYPE = 'company'
        and p.TENANT_ID = #{tenantId}
        and (1>2
        <if test="condition != null and condition != ''">
            or p.org_name like concat('%', #{condition},'%')
        </if>
        <if test="companyIds != null and companyIds.size() > 0">
            or p.id in
            <foreach collection="companyIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        )
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            and p.id in
            <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
        </if>
        order by p.org_name asc
    </select>


</mapper>