<?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.AmbInvestMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.AmbInvest">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="initiate_amb_id" jdbcType="BIGINT" property="initiateAmbId" />
    <result column="accept_amb_id" jdbcType="BIGINT" property="acceptAmbId" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="operator" jdbcType="VARCHAR" property="operator" />
    <result column="comment" jdbcType="VARCHAR" property="comment" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="count_amount" jdbcType="DECIMAL" property="countAmount" />
  </resultMap>
  <sql id="Base_Column_List">
    id, initiate_amb_id, accept_amb_id, amount, `status`, `operator`, `comment`, create_time,
    count_amount
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from amb_invest
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from amb_invest
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.AmbInvest" useGeneratedKeys="true">
    insert into amb_invest (initiate_amb_id, accept_amb_id, amount,
      `status`, `operator`, `comment`,
      create_time, count_amount)
    values (#{initiateAmbId,jdbcType=BIGINT}, #{acceptAmbId,jdbcType=BIGINT}, #{amount,jdbcType=DECIMAL},
      #{status,jdbcType=INTEGER}, #{operator,jdbcType=VARCHAR}, #{comment,jdbcType=VARCHAR},
      #{createTime,jdbcType=TIMESTAMP}, #{countAmount,jdbcType=DECIMAL})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.AmbInvest" useGeneratedKeys="true">
    insert into amb_invest
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="initiateAmbId != null">
        initiate_amb_id,
      </if>
      <if test="acceptAmbId != null">
        accept_amb_id,
      </if>
      <if test="amount != null">
        amount,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="operator != null">
        `operator`,
      </if>
      <if test="comment != null">
        `comment`,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="countAmount != null">
        count_amount,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="initiateAmbId != null">
        #{initiateAmbId,jdbcType=BIGINT},
      </if>
      <if test="acceptAmbId != null">
        #{acceptAmbId,jdbcType=BIGINT},
      </if>
      <if test="amount != null">
        #{amount,jdbcType=DECIMAL},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="operator != null">
        #{operator,jdbcType=VARCHAR},
      </if>
      <if test="comment != null">
        #{comment,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="countAmount != null">
        #{countAmount,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.AmbInvest">
    update amb_invest
    <set>
      <if test="initiateAmbId != null">
        initiate_amb_id = #{initiateAmbId,jdbcType=BIGINT},
      </if>
      <if test="acceptAmbId != null">
        accept_amb_id = #{acceptAmbId,jdbcType=BIGINT},
      </if>
      <if test="amount != null">
        amount = #{amount,jdbcType=DECIMAL},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="operator != null">
        `operator` = #{operator,jdbcType=VARCHAR},
      </if>
      <if test="comment != null">
        `comment` = #{comment,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="countAmount != null">
        count_amount = #{countAmount,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.AmbInvest">
    update amb_invest
    set initiate_amb_id = #{initiateAmbId,jdbcType=BIGINT},
      accept_amb_id = #{acceptAmbId,jdbcType=BIGINT},
      amount = #{amount,jdbcType=DECIMAL},
      `status` = #{status,jdbcType=INTEGER},
      `operator` = #{operator,jdbcType=VARCHAR},
      `comment` = #{comment,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      count_amount = #{countAmount,jdbcType=DECIMAL}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectInvestList" resultType="com.goafanti.ambSystem.bo.OutInvestList">
    select a.id,a.initiate_amb_id initiateAmbId,b.name initiateName,a.accept_amb_id acceptAmbId,
    c.name acceptName,a.amount ,a.status ,d.name operator,a.comment ,a.count_amount countAmount,
    date_format(a.create_time,'%Y-%m-%d %H:%i:%S') createTimes,d.amb_role ambRole
    from amb_invest a  left join amb_system b on a.initiate_amb_id =b.id
    left join amb_system c on a.accept_amb_id =c.id
    left join admin d on a.operator =d.id
    where 1=1
    <if test="roleType==0">
      and a.operator= #{aid}
    </if>
    <if test="roleType==1">
      and a.status in (1,2,3)
      and b.accountant =#{aid}
    </if>
    <if test="initiateAmbId != null ">
      and a.initiate_amb_id= #{initiateAmbId}
    </if>
    <if test="acceptAmbId !=null">
      and a.accept_amb_id = #{acceptAmbId}
    </if>
    <if test="operator !=null">
      and a.operator= #{operator}
    </if>
    <if test="startTime!=null and endTime !=null">
      and a.create_time between #{startTime} and #{endTime}
    </if>
    <if test="status !=null">
      and a.status= #{status}
    </if>
    order by
    <if test="roleType==0 or roleType==1">
    a.status ,
    </if>
    a.create_time desc
    <if test="page_sql !=null">
      ${page_sql}
    </if>
  </select>

  <select id="selectInvestCount" resultType="integer">
    select count(*)
    from amb_invest a left join amb_system b on a.initiate_amb_id =b.id
    where 1=1
    <if test="roleType==0">
      and a.operator= #{aid}
    </if>
    <if test="roleType==1">
      and a.status in (1,2,3)
      and b.accountant =#{aid}
    </if>
    <if test="startTime!=null and endTime !=null">
      and a.create_time between #{startTime} and #{endTime}
    </if>
    <if test="initiateAmbId != null ">
      and a.initiate_amb_id= #{initiateAmbId}
    </if>
    <if test="acceptAmbId !=null">
      and a.accept_amb_id = #{acceptAmbId}
    </if>
    <if test="operator !=null">
      and a.operator= #{operator}
    </if>
    <if test="status !=null">
      and a.status= #{status}
    </if>
  </select>
  <select id="selectCountAmountById" resultType="java.math.BigDecimal">
    select sum(amount)amount
    from amb_invest
    where status=2 and  accept_amb_id = #{acceptAmbId} and initiate_amb_id= #{initiateAmbId}
  </select>
  <select id="selectAmbInvestDtailsList" resultType="com.goafanti.ambSystem.bo.OutInvestList">
    select a.id,a.initiate_amb_id initiateAmbId ,a.accept_amb_id acceptAmbId,a.amount ,a.status ,
           a.comment , date_format(a.create_time,'%Y-%m-%d %H:%i:%S') createTimes,
           b.name initiateName,c.name acceptName,d.name operator
    from amb_invest  a left join amb_system b on a.initiate_amb_id =b.id
    left join amb_system c on a.accept_amb_id =c.id left join admin d on a.operator =d.id
    where  1=1
    <if test="type==0">
      and a.status in (0,1,3)
    </if>
      and a.initiate_amb_id= #{id}
    order by  a.create_time desc
  </select>
</mapper>
