<?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.goafanti.common.dao.AmbSystemMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.AmbSystem">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="parent_id" jdbcType="BIGINT" property="parentId" />
    <result column="ancestors" jdbcType="VARCHAR" property="ancestors" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="order_num" jdbcType="INTEGER" property="orderNum" />
    <result column="leader" jdbcType="VARCHAR" property="leader" />
    <result column="status" jdbcType="CHAR" property="status" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="lvl" jdbcType="INTEGER" property="lvl" />
    <result column="finance_id" jdbcType="VARCHAR" property="financeId" />
    <result column="ancestors_names" jdbcType="VARCHAR" property="ancestorsNames" />
    <result column="accountant" jdbcType="VARCHAR" property="accountant" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
    <result column="members_count" jdbcType="INTEGER" property="membersCount" />
    <result column="received" jdbcType="DECIMAL" property="received" />
    <result column="foreign" jdbcType="DECIMAL" property="foreign" />
    <result column="surplus" jdbcType="DECIMAL" property="surplus" />
  </resultMap>
  <sql id="Base_Column_List">
    id, parent_id, ancestors, `name`, order_num, leader, `status`, create_by, create_time,
    update_by, update_time, lvl, finance_id, ancestors_names, accountant, remarks, total_amount,
    members_count, received, `foreign`, surplus
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from amb_system
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from amb_system
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.AmbSystem" useGeneratedKeys="true">
    insert into amb_system (parent_id, ancestors, `name`,
      order_num, leader, `status`,
      create_by, create_time, update_by,
      update_time, lvl, finance_id,
      ancestors_names, accountant, remarks,
      total_amount, members_count, received,
      `foreign`, surplus)
    values (#{parentId,jdbcType=BIGINT}, #{ancestors,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
      #{orderNum,jdbcType=INTEGER}, #{leader,jdbcType=VARCHAR}, #{status,jdbcType=CHAR},
      #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR},
      #{updateTime,jdbcType=TIMESTAMP}, #{lvl,jdbcType=INTEGER}, #{financeId,jdbcType=VARCHAR},
      #{ancestorsNames,jdbcType=VARCHAR}, #{accountant,jdbcType=VARCHAR}, #{remarks,jdbcType=VARCHAR},
      #{totalAmount,jdbcType=DECIMAL}, #{membersCount,jdbcType=INTEGER}, #{received,jdbcType=DECIMAL},
      #{foreign,jdbcType=DECIMAL}, #{surplus,jdbcType=DECIMAL})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.AmbSystem" useGeneratedKeys="true">
    insert into amb_system
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="parentId != null">
        parent_id,
      </if>
      <if test="ancestors != null">
        ancestors,
      </if>
      <if test="name != null">
        `name`,
      </if>
      <if test="orderNum != null">
        order_num,
      </if>
      <if test="leader != null">
        leader,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="lvl != null">
        lvl,
      </if>
      <if test="financeId != null">
        finance_id,
      </if>
      <if test="ancestorsNames != null">
        ancestors_names,
      </if>
      <if test="accountant != null">
        accountant,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="totalAmount != null">
        total_amount,
      </if>
      <if test="membersCount != null">
        members_count,
      </if>
      <if test="received != null">
        received,
      </if>
      <if test="foreign != null">
        `foreign`,
      </if>
      <if test="surplus != null">
        surplus,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="parentId != null">
        #{parentId,jdbcType=BIGINT},
      </if>
      <if test="ancestors != null">
        #{ancestors,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="orderNum != null">
        #{orderNum,jdbcType=INTEGER},
      </if>
      <if test="leader != null">
        #{leader,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=CHAR},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lvl != null">
        #{lvl,jdbcType=INTEGER},
      </if>
      <if test="financeId != null">
        #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="ancestorsNames != null">
        #{ancestorsNames,jdbcType=VARCHAR},
      </if>
      <if test="accountant != null">
        #{accountant,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="totalAmount != null">
        #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="membersCount != null">
        #{membersCount,jdbcType=INTEGER},
      </if>
      <if test="received != null">
        #{received,jdbcType=DECIMAL},
      </if>
      <if test="foreign != null">
        #{foreign,jdbcType=DECIMAL},
      </if>
      <if test="surplus != null">
        #{surplus,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.AmbSystem">
    update amb_system
    <set>
      <if test="parentId != null">
        parent_id = #{parentId,jdbcType=BIGINT},
      </if>
      <if test="ancestors != null">
        ancestors = #{ancestors,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="orderNum != null">
        order_num = #{orderNum,jdbcType=INTEGER},
      </if>
      <if test="leader != null">
        leader = #{leader,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=CHAR},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lvl != null">
        lvl = #{lvl,jdbcType=INTEGER},
      </if>
      <if test="financeId != null">
        finance_id = #{financeId,jdbcType=VARCHAR},
      </if>
      <if test="ancestorsNames != null">
        ancestors_names = #{ancestorsNames,jdbcType=VARCHAR},
      </if>
      <if test="accountant != null">
        accountant = #{accountant,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="totalAmount != null">
        total_amount = #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="membersCount != null">
        members_count = #{membersCount,jdbcType=INTEGER},
      </if>
      <if test="received != null">
        received = #{received,jdbcType=DECIMAL},
      </if>
      <if test="foreign != null">
        `foreign` = #{foreign,jdbcType=DECIMAL},
      </if>
      <if test="surplus != null">
        surplus = #{surplus,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.AmbSystem">
    update amb_system
    set parent_id = #{parentId,jdbcType=BIGINT},
      ancestors = #{ancestors,jdbcType=VARCHAR},
      `name` = #{name,jdbcType=VARCHAR},
      order_num = #{orderNum,jdbcType=INTEGER},
      leader = #{leader,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=CHAR},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      lvl = #{lvl,jdbcType=INTEGER},
      finance_id = #{financeId,jdbcType=VARCHAR},
      ancestors_names = #{ancestorsNames,jdbcType=VARCHAR},
      accountant = #{accountant,jdbcType=VARCHAR},
      remarks = #{remarks,jdbcType=VARCHAR},
      total_amount = #{totalAmount,jdbcType=DECIMAL},
      members_count = #{membersCount,jdbcType=INTEGER},
      received = #{received,jdbcType=DECIMAL},
      `foreign` = #{foreign,jdbcType=DECIMAL},
      surplus = #{surplus,jdbcType=DECIMAL}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateAncestorsNames">
    update amb_system
    set  ancestors_names=replace(ancestors_names,#{useName},#{name})
    where FIND_IN_SET( #{id},ancestors)
  </update>

  <select id="getAncestorsList" resultType="com.goafanti.ambSystem.bo.AmbAll">
    select id,name,lvl,parent_id parentId
    from amb_system where status=0
    <if test="list !=null and list.size>0 ">
    and  id in
    <foreach close=")" collection="list" item="id" open="(" separator=",">
      #{id}
    </foreach>
  </if>
  </select>

  <select id="selectAmbSystemList" resultType="com.goafanti.ambSystem.bo.OutAmb">
    select a.id,a.name ,a.ancestors_names ancestorsNames ,a.status,
    b.name leaderName,c.name financeName,d.name accountantName,
    date_format(a.create_time,'%Y-%m-%d %H:%i:%s')createTimes
    from amb_system a
    left join admin b on a.leader =b.id
    left join admin c on a.finance_id =c.id
    left join admin d on a.accountant =d.id
    where 1=1
    <if test="lvl &lt; 5">
      and a.lvl= #{lvl}
    </if>
    <if test="lvl &gt; 4">
      and a.lvl in(5,6)
    </if>
    <if test="leader !=null">
      and a.leader= #{leader}
    </if>
    <if test="name !=null">
      and a.name  like concat('%',#{name},'%')
    </if>
    <if test="ancestors !=null">
      and (a.id=#{ancestors} or find_in_set( #{ancestors} ,a.ancestors))
    </if>
    order by id
    <if test="page_sql !=null">
      ${page_sql}
    </if>
  </select>

  <select id="selectAmbSystemCount" resultType="java.lang.Integer">
    select count(*)
    from amb_system a
    left join admin b on a.leader =b.id
    left join admin c on a.finance_id =c.id
    where 1=1
    <if test="lvl &lt; 5">
      and a.lvl= #{lvl}
    </if>
    <if test="lvl &gt; 4">
      and a.lvl in(5,6)
    </if>
    <if test="leader !=null">
      and a.leader= #{leader}
    </if>
    <if test="name !=null">
      and a.name  like concat('%',#{name},'%')
    </if>
    <if test="ancestors !=null">
      and (a.id=#{ancestors} or find_in_set( #{ancestors} ,a.ancestors))
    </if>
  </select>
    <select id="selectByParameter"  parameterType="com.goafanti.common.model.AmbSystem" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from amb_system
        where 1=1
          <if test="parentId !=null">
            and  parent_id = #{parentId}
          </if>
      <if test="leader !=null">
        and  leader = #{leader}
      </if>
    </select>
    <select id="selectByName" resultType="java.lang.Integer">
      select
       count(*)
      from amb_system
      where name= #{name}
    </select>
  <select id="selectDtailsAmb" resultType="com.goafanti.ambSystem.bo.OutAmbDtails">
    select a.id,a.name,a.parent_id parentId,a.accountant,a.lvl,a.status,a.ancestors, a.ancestors_names ancestorsNames,
        a.order_num orderNum,a.remarks, date_format(a.create_time,'%Y-%m-%d %H:%i:%s')createTimes, a.leader,
        a.finance_id financeId, b.name leaderName,c.name financeName,d.name accountantName
    from amb_system a left join admin b on a.leader =b.id left join admin c on a.finance_id =c.id
    left join admin d on a.accountant =d.id
    where a.id= #{id}
  </select>
    <select id="selectBySuperId" parameterType="com.goafanti.common.model.AmbSystem" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List" />
  from amb_system
  where  lvl= #{lvl}+1
and find_in_set(#{id},ancestors)
</select>
</mapper>
