<?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.rmcp.core.dao.OrgMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.Org">
    <!--@mbg.generated-->
    <!--@Table rmcp_org-->
    <id column="ID" property="id" />
    <result column="TENANT_ID" property="tenantId" />
    <result column="ORG_TYPE" property="orgType" />
    <result column="PARENT_ORG_ID" property="parentOrgId" />
    <result column="ORG_AREA_ID" property="orgAreaId" />
    <result column="ORG_NAME" property="orgName" />
    <result column="ORG_STATE" property="orgState" />
    <result column="REMARK" property="remark" />
    <result column="ORG_LEADER_NAME" property="orgLeaderName" />
    <result column="ORG_LEADER_PHONE" property="orgLeaderPhone" />
    <result column="ORG_LEADER_EMAIL" property="orgLeaderEmail" />
    <result column="ORG_LEADER_SEX" property="orgLeaderSex" />
    <result column="STATUS" property="status" />
    <result column="DATE_CREATE" property="dateCreate" />
    <result column="CREATE_BY" property="createBy" />
    <result column="DATE_UPDATE" property="dateUpdate" />
    <result column="UPDATE_BY" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    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>

  <insert id="replaceInsert" parameterType="com.zcxk.rmcp.core.entity.Org">
    REPLACE into rmcp_org
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        ID,
      </if>
      <if test="tenantId != null">
        TENANT_ID,
      </if>
      <if test="orgType != null">
        ORG_TYPE,
      </if>
      <if test="parentOrgId != null">
        PARENT_ORG_ID,
      </if>
      <if test="orgAreaId != null">
        ORG_AREA_ID,
      </if>
      <if test="orgName != null">
        ORG_NAME,
      </if>
      <if test="orgState != null">
        ORG_STATE,
      </if>
      <if test="remark != null">
        REMARK,
      </if>
      <if test="orgLeaderName != null">
        ORG_LEADER_NAME,
      </if>
      <if test="orgLeaderPhone != null">
        ORG_LEADER_PHONE,
      </if>
      <if test="orgLeaderEmail != null">
        ORG_LEADER_EMAIL,
      </if>
      <if test="orgLeaderSex != null">
        ORG_LEADER_SEX,
      </if>
      <if test="status != null">
        `STATUS`,
      </if>
      <if test="dateCreate != null">
        DATE_CREATE,
      </if>
      <if test="createBy != null">
        CREATE_BY,
      </if>
      <if test="dateUpdate != null">
        DATE_UPDATE,
      </if>
      <if test="updateBy != null">
        UPDATE_BY,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id},
      </if>
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="orgType != null">
        #{orgType},
      </if>
      <if test="parentOrgId != null">
        #{parentOrgId},
      </if>
      <if test="orgAreaId != null">
        #{orgAreaId},
      </if>
      <if test="orgName != null">
        #{orgName},
      </if>
      <if test="orgState != null">
        #{orgState},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="orgLeaderName != null">
        #{orgLeaderName},
      </if>
      <if test="orgLeaderPhone != null">
        #{orgLeaderPhone},
      </if>
      <if test="orgLeaderEmail != null">
        #{orgLeaderEmail},
      </if>
      <if test="orgLeaderSex != null">
        #{orgLeaderSex},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="dateCreate != null">
        #{dateCreate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="dateUpdate != null">
        #{dateUpdate},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <insert id="insertSelective" keyColumn="ID" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.Org" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_org
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        TENANT_ID,
      </if>
      <if test="orgType != null">
        ORG_TYPE,
      </if>
      <if test="parentOrgId != null">
        PARENT_ORG_ID,
      </if>
      <if test="orgAreaId != null">
        ORG_AREA_ID,
      </if>
      <if test="orgName != null">
        ORG_NAME,
      </if>
      <if test="orgState != null">
        ORG_STATE,
      </if>
      <if test="remark != null">
        REMARK,
      </if>
      <if test="orgLeaderName != null">
        ORG_LEADER_NAME,
      </if>
      <if test="orgLeaderPhone != null">
        ORG_LEADER_PHONE,
      </if>
      <if test="orgLeaderEmail != null">
        ORG_LEADER_EMAIL,
      </if>
      <if test="orgLeaderSex != null">
        ORG_LEADER_SEX,
      </if>
      <if test="status != null">
        `STATUS`,
      </if>
      <if test="dateCreate != null">
        DATE_CREATE,
      </if>
      <if test="createBy != null">
        CREATE_BY,
      </if>
      <if test="dateUpdate != null">
        DATE_UPDATE,
      </if>
      <if test="updateBy != null">
        UPDATE_BY,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="orgType != null">
        #{orgType},
      </if>
      <if test="parentOrgId != null">
        #{parentOrgId},
      </if>
      <if test="orgAreaId != null">
        #{orgAreaId},
      </if>
      <if test="orgName != null">
        #{orgName},
      </if>
      <if test="orgState != null">
        #{orgState},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="orgLeaderName != null">
        #{orgLeaderName},
      </if>
      <if test="orgLeaderPhone != null">
        #{orgLeaderPhone},
      </if>
      <if test="orgLeaderEmail != null">
        #{orgLeaderEmail},
      </if>
      <if test="orgLeaderSex != null">
        #{orgLeaderSex},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="dateCreate != null">
        #{dateCreate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="dateUpdate != null">
        #{dateUpdate},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.Org">
    <!--@mbg.generated-->
    update rmcp_org
    <set>
      <if test="tenantId != null">
        TENANT_ID = #{tenantId},
      </if>
      <if test="orgType != null">
        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">
        ORG_NAME = #{orgName},
      </if>
      <if test="orgState != null">
        ORG_STATE = #{orgState},
      </if>
      <if test="remark != null">
        REMARK = #{remark},
      </if>
      <if test="orgLeaderName != null">
        ORG_LEADER_NAME = #{orgLeaderName},
      </if>
      <if test="orgLeaderPhone != null">
        ORG_LEADER_PHONE = #{orgLeaderPhone},
      </if>
      <if test="orgLeaderEmail != null">
        ORG_LEADER_EMAIL = #{orgLeaderEmail},
      </if>
      <if test="orgLeaderSex != null">
        ORG_LEADER_SEX = #{orgLeaderSex},
      </if>
      <if test="status != null">
        `STATUS` = #{status},
      </if>
      <if test="dateCreate != null">
        DATE_CREATE = #{dateCreate},
      </if>
      <if test="createBy != null">
        CREATE_BY = #{createBy},
      </if>
      <if test="dateUpdate != null">
        DATE_UPDATE = #{dateUpdate},
      </if>
      <if test="updateBy != null">
        UPDATE_BY = #{updateBy},
      </if>
    </set>
    where ID = #{id}
  </update>

  <delete id="deleteById">
    DELETE FROM rmcp_org where ID = #{id}
  </delete>

  <select id="findList" resultMap="BaseResultMap">
    select * from rmcp_org where STATUS = 1
    <if test="userCondition.tenantId != null">and TENANT_ID = #{userCondition.tenantId}</if>
  </select>

  <select id="findByName" resultMap="BaseResultMap">
    select id,ORG_NAME from rmcp_org where STATUS = 1 and TENANT_ID = #{tenantId} and ORG_NAME = #{name}
  </select>

  <select id="findByParentIdAndName" resultMap="BaseResultMap">
    select id,ORG_NAME from rmcp_org where STATUS = 1 and PARENT_ORG_ID = #{parentId} and ORG_NAME = #{name}
  </select>

  <select id="selectTrees" resultType="com.zcxk.rmcp.api.vo.OrgTreeVo">
    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 rmcp_org
    <where>
      and status!=0
      <if test="org.tenantId!=null ">
        and TENANT_ID=#{org.tenantId}
      </if>
      <if test="org.orgType=='company'||org.orgType=='department'">
        and ORG_TYPE=#{org.orgType}
      </if>
      <if test="org.id!=null">
        and id=#{org.id}
      </if>
    </where>
    </select>
</mapper>