text: <?xml version="1.0" encoding="UTF-8" ?>
  <!--
    ~ <<
    ~  Davinci
    ~  ==
    ~  Copyright (C) 2016 - 2019 EDP
    ~  ==
    ~  Licensed under the Apache License, Version 2.0 (the "License");
    ~  you may not use this file except in compliance with the License.
    ~  You may obtain a copy of the License at
    ~        http://www.apache.org/licenses/LICENSE-2.0
    ~   Unless required by applicable law or agreed to in writing, software
    ~   distributed under the License is distributed on an "AS IS" BASIS,
    ~   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    ~   See the License for the specific language governing permissions and
    ~   limitations under the License.
    ~  >>
    ~
    -->
  
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  <mapper namespace="edp.davinci.dao.OrganizationMapper">
      <insert id="insert" parameterType="edp.davinci.model.Organization">
          <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
              SELECT LAST_INSERT_ID() AS id
          </selectKey>
          insert organization
          <trim prefix="(" suffix=")" suffixOverrides=",">
              `name`,
              `user_id`,
              `project_num`,
              `member_num`,
              `role_num`,
              `allow_create_project`,
              `member_permission`,
              `create_time`,
              `create_by`,
              <if test='description != null and description != "" '>
                  `description`,
              </if>
              <if test='avatar != null and avatar != "" '>
                  `avatar`
              </if>
          </trim>
          values
          <trim prefix=" (" suffix=")" suffixOverrides=",">
              #{name, jdbcType=VARCHAR},
              #{userId, jdbcType=BIGINT},
              #{projectNum, jdbcType=INTEGER},
              #{memberNum, jdbcType=INTEGER},
              #{roleNum, jdbcType=INTEGER},
              #{allowCreateProject, jdbcType=TINYINT},
              #{memberPermission, jdbcType=SMALLINT},
              #{createTime, jdbcType=TIMESTAMP},
              #{createBy, jdbcType=BIGINT},
              <if test='description != null and description != "" '>
                  #{description, jdbcType=VARCHAR},
              </if>
              <if test='avatar != null and avatar != "" '>
                  #{avatar, jdbcType=VARCHAR}
              </if>
          </trim>
      </insert>
  
      <update id="updateProjectNum" parameterType="edp.davinci.model.Organization">
          update organization
          <choose>
              <when test="projectNum > 0">
                  set `project_num`=#{projectNum,jdbcType=INTEGER}
              </when>
              <otherwise>
                  set `project_num`=0
              </otherwise>
          </choose>
          <where>
              `id`=#{id,jdbcType=BIGINT}
          </where>
      </update>
  
  
      <update id="updateMemberNum" parameterType="edp.davinci.model.Organization">
          update organization
          <choose>
              <when test="memberNum > 0">
                  set `member_num`=#{memberNum,jdbcType=INTEGER}
              </when>
              <otherwise>
                  set `member_num`=0
              </otherwise>
          </choose>
          <where>
              `id`=#{id,jdbcType=BIGINT}
          </where>
      </update>
  
  
      <update id="updateRoleNum" parameterType="edp.davinci.model.Organization">
          update organization
          <choose>
              <when test="roleNum > 0">
                  set `role_num`=#{roleNum,jdbcType=INTEGER}
              </when>
              <otherwise>
                  set `role_num`=0
              </otherwise>
          </choose>
          <where>
              `id`=#{id,jdbcType=BIGINT}
          </where>
      </update>
  
      <update id="addOneMemberNum" parameterType="java.util.Set">
          update organization set `role_num`=(role_num + 1) where `id` in
          <foreach collection="set" index="index" item="item" open="(" close=")" separator=",">
              #{item}
          </foreach>
      </update>
  
      <select id="getJointlyOrganization" resultType="edp.davinci.dto.organizationDto.OrganizationInfo"
              parameterType="java.util.Set">
          SELECT o.* FROM
          (organization o,
          (
          SELECT `org_id`, COUNT(1) as c
          FROM rel_user_organization
          WHERE `user_id` in
          <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">#{item}
          </foreach>
          GROUP BY `org_id`
          HAVING `c` > 1
          ) o1
          )
          LEFT JOIN rel_user_organization ruo on ruo.`org_id` = o.`id` and ruo.`user_id` = #{userId}
          WHERE o.`id` = o1.`org_id`
      </select>
  </mapper>