<?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.TemporaryReceivablesMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.TemporaryReceivables">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="corporate_name" jdbcType="VARCHAR" property="corporateName" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="receivables_time" jdbcType="TIMESTAMP" property="receivablesTime" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="financial_pay_no" jdbcType="VARCHAR" property="financialPayNo" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    id, order_no, corporate_name, amount, receivables_time, aid, remarks, status, create_time,
    financial_pay_no
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.TemporaryReceivablesExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from temporary_receivables
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    select
    <include refid="Base_Column_List" />
    from temporary_receivables
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    delete from temporary_receivables
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.TemporaryReceivablesExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    delete from temporary_receivables
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.TemporaryReceivables">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    insert into temporary_receivables (id, order_no, corporate_name,
      amount, receivables_time, aid,
      remarks, status, create_time,
      financial_pay_no)
    values (#{id,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR}, #{corporateName,jdbcType=VARCHAR},
      #{amount,jdbcType=DECIMAL}, #{receivablesTime,jdbcType=TIMESTAMP}, #{aid,jdbcType=VARCHAR},
      #{remarks,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
      #{financialPayNo,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.TemporaryReceivables">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    insert into temporary_receivables
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="corporateName != null">
        corporate_name,
      </if>
      <if test="amount != null">
        amount,
      </if>
      <if test="receivablesTime != null">
        receivables_time,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="financialPayNo != null">
        financial_pay_no,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="corporateName != null">
        #{corporateName,jdbcType=VARCHAR},
      </if>
      <if test="amount != null">
        #{amount,jdbcType=DECIMAL},
      </if>
      <if test="receivablesTime != null">
        #{receivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="financialPayNo != null">
        #{financialPayNo,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.TemporaryReceivablesExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    select count(*) from temporary_receivables
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    update temporary_receivables
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.orderNo != null">
        order_no = #{record.orderNo,jdbcType=VARCHAR},
      </if>
      <if test="record.corporateName != null">
        corporate_name = #{record.corporateName,jdbcType=VARCHAR},
      </if>
      <if test="record.amount != null">
        amount = #{record.amount,jdbcType=DECIMAL},
      </if>
      <if test="record.receivablesTime != null">
        receivables_time = #{record.receivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.aid != null">
        aid = #{record.aid,jdbcType=VARCHAR},
      </if>
      <if test="record.remarks != null">
        remarks = #{record.remarks,jdbcType=VARCHAR},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=INTEGER},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.financialPayNo != null">
        financial_pay_no = #{record.financialPayNo,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    update temporary_receivables
    set id = #{record.id,jdbcType=INTEGER},
      order_no = #{record.orderNo,jdbcType=VARCHAR},
      corporate_name = #{record.corporateName,jdbcType=VARCHAR},
      amount = #{record.amount,jdbcType=DECIMAL},
      receivables_time = #{record.receivablesTime,jdbcType=TIMESTAMP},
      aid = #{record.aid,jdbcType=VARCHAR},
      remarks = #{record.remarks,jdbcType=VARCHAR},
      status = #{record.status,jdbcType=INTEGER},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      financial_pay_no = #{record.financialPayNo,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.TemporaryReceivables">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    update temporary_receivables
    <set>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="corporateName != null">
        corporate_name = #{corporateName,jdbcType=VARCHAR},
      </if>
      <if test="amount != null">
        amount = #{amount,jdbcType=DECIMAL},
      </if>
      <if test="receivablesTime != null">
        receivables_time = #{receivablesTime,jdbcType=TIMESTAMP},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="financialPayNo != null">
        financial_pay_no = #{financialPayNo,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.TemporaryReceivables">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Oct 14 11:33:48 CST 2019.
    -->
    update temporary_receivables
    set order_no = #{orderNo,jdbcType=VARCHAR},
      corporate_name = #{corporateName,jdbcType=VARCHAR},
      amount = #{amount,jdbcType=DECIMAL},
      receivables_time = #{receivablesTime,jdbcType=TIMESTAMP},
      aid = #{aid,jdbcType=VARCHAR},
      remarks = #{remarks,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      financial_pay_no = #{financialPayNo,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

   <delete id="deleteByids" parameterType="java.util.List">
    delete from temporary_receivables
    where id in
	 <foreach close=")" collection="list" index="index" item="item" open="(" separator=",">
            #{item}
         </foreach>
  </delete>
   <delete id="deleteByaid" parameterType="java.lang.String">
    delete from temporary_receivables
    where aid = #{aid}
  </delete>
  <delete id="deleteNormalByaid">
  delete from temporary_receivables
    where aid = #{aid} and status &lt; 2
  </delete>
  <select id="selectByaid" resultMap="BaseResultMap">

    select
    <include refid="Base_Column_List" />
    from temporary_receivables
    where aid = #{aid,jdbcType=INTEGER}
  </select>
  <select id="selectReceivablesList" parameterType="java.lang.String" resultType="com.goafanti.order.bo.ReceivablesListBo">
  	 select a.id,a.order_no orderNo,b.contract_no contractNo,a.corporate_name corporateName,c.nickname,
    b.total_amount totalAmount,d.name depName,a.status ,b.settlement_amount settlementAmount,
	a.financial_pay_no financialPayNo,a.amount,date_format(a.receivables_time, '%Y-%m-%d')receivablesTimes,
	date_format(a.create_time, '%Y-%m-%d %H:%i:%S')createTimes,a.remarks,c.username
	from temporary_receivables a left join t_order_new b on a.order_no=b.order_no left join user c on b.buyer_id=c.id
	left join department d on b.order_dep=d.id left join admin e on b.salesman_id=e.id
	where 1=1
	<if test="aid != null">
	and  a.aid= #{aid}
	</if>
	<if test="contractNo != null">
	b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="orderNo != null">
	and a.order_no = #{orderNo}
	</if>
	<if test="adminName != null">
	and e.name like concat('%',#{adminName},'%')
	</if>
	<if test="nickname != null">
	and c.nickname like concat('%',#{nickname},'%')
	</if>
	<if test="corporateName != null">
	and a.corporate_name like concat('%',#{corporateName},'%')
	</if>
	<if test="orderDeps != null">
	and b.order_dep in
        <foreach close=")" collection="orderDeps" item="depId" open="(" separator=",">
            #{depId}
        </foreach>
	</if>
	<if test="adminDeps != null">
	and  e.department_id in
        <foreach close=")" collection="adminDeps" item="depId" open="(" separator=",">
            #{depId}
        </foreach>
	</if>
	<if test="status != null">
	and a.status= #{status}
	</if>
	order by a.status desc,b.contract_no,a.create_time desc
	<if test="page_sql!=null">
	${page_sql}
	</if>
  </select>
  <select id="selectReceivablesCount"  resultType="java.lang.Integer">
  	select count(*) from temporary_receivables a left join t_order_new b on a.order_no=b.order_no left join user c on b.buyer_id=c.id left join department d on b.order_dep=d.id
	left join admin e on b.salesman_id=e.id
	where 1=1
	<if test="aid != null">
	and  a.aid= #{aid}
	</if>
	<if test="contractNo != null">
	b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="orderNo != null">
	and a.order_no = #{orderNo}
	</if>
	<if test="adminName != null">
	and e.name like concat('%',#{adminName},'%')
	</if>
	<if test="nickname != null">
	and c.nickname like concat('%',#{nickname},'%')
	</if>
	<if test="corporateName != null">
	and a.corporate_name like concat('%',#{corporateName},'%')
	</if>
      <if test="orderDeps != null">
          and b.order_dep in
          <foreach close=")" collection="orderDeps" item="depId" open="(" separator=",">
              #{depId}
          </foreach>
      </if>
      <if test="adminDeps != null">
          and  e.department_id in
          <foreach close=")" collection="adminDeps" item="depId" open="(" separator=",">
              #{depId}
          </foreach>
      </if>
	<if test="status != null">
	and a.status= #{status}
	</if>
  </select>
  <select id="getCountlistReceivablesAmount" parameterType="Map" resultType="java.lang.String">
  select  ifnull(sum(a.amount),0) amount from temporary_receivables a left join t_order_new b on a.order_no=b.order_no
	left join user c on b.buyer_id=c.id left join department d on b.order_dep=d.id left join admin e on b.salesman_id=e.id
	where 1=1
	<if test="aid != null">
	and  a.aid= #{aid}
	</if>
	<if test="contractNo != null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="orderNo != null">
	and a.order_no = #{orderNo}
	</if>
	<if test="adminName != null">
	and  b.salesman_id like concat('%',#{adminName},'%')
	</if>
	<if test="nickname != null">
	and c.nickname like concat('%',#{nickname},'%')
	</if>
	<if test="corporateName != null">
	and a.corporate_name like concat('%',#{corporateName},'%')
	</if>
      <if test="orderDeps != null">
          and b.order_dep in
          <foreach close=")" collection="orderDeps" item="depId" open="(" separator=",">
              #{depId}
          </foreach>
      </if>
      <if test="adminDeps != null">
          and  e.department_id in
          <foreach close=")" collection="adminDeps" item="depId" open="(" separator=",">
              #{depId}
          </foreach>
      </if>
	<if test="status != null">
	and a.status= #{status}
	</if>
	</select>

	<select id="getCountlistReceivablesTotalAmount" parameterType="Map" resultType="java.lang.String">
  select ifnull(sum(x.total_amount),0) from (select  b.order_no,b.total_amount  from temporary_receivables a
  left join t_order_new b on a.order_no=b.order_no left join user c on b.buyer_id=c.id left join department d on b.order_dep=d.id
	left join admin e on b.salesman_id=e.id
	where 1=1
	<if test="aid != null">
	and  a.aid= #{aid}
	</if>
	<if test="contractNo != null">
	and b.contract_no like concat('%',#{contractNo},'%')
	</if>
	<if test="orderNo != null">
	and a.order_no = #{orderNo}
	</if>
	<if test="adminName != null">
	and  b.salesman_id like concat('%',#{adminName},'%')
	</if>
	<if test="nickname != null">
	and c.nickname like concat('%',#{nickname},'%')
	</if>
	<if test="corporateName != null">
	and a.corporate_name like concat('%',#{corporateName},'%')
	</if>
        <if test="orderDeps != null">
            and b.order_dep in
            <foreach close=")" collection="orderDeps" item="depId" open="(" separator=",">
                #{depId}
            </foreach>
        </if>
        <if test="adminDeps != null">
            and  e.department_id in
            <foreach close=")" collection="adminDeps" item="depId" open="(" separator=",">
                #{depId}
            </foreach>
        </if>
	<if test="status != null">
	and a.status= #{status}
	</if>
	group by b.order_no)x
	</select>

    <insert id="insertBatch" parameterType="com.goafanti.common.model.TemporaryReceivables">
    insert into temporary_receivables (id, order_no, corporate_name, amount, receivables_time,
     aid, remarks, status, create_time, financial_pay_no) values
      <foreach item="item" index="index" collection="list" separator=",">
      (#{item.id,jdbcType=INTEGER}, #{item.orderNo,jdbcType=VARCHAR}, #{item.corporateName,jdbcType=VARCHAR},
      #{item.amount,jdbcType=DECIMAL}, #{item.receivablesTime,jdbcType=TIMESTAMP}, #{item.aid,jdbcType=VARCHAR},
      #{item.remarks,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER}, #{item.createTime,jdbcType=TIMESTAMP},
      #{item.financialPayNo,jdbcType=VARCHAR})
    </foreach>
  </insert>

  <update id="updateRepeatOrderNo" >
	update temporary_receivables x ,(select a.order_no,a.amount,a.receivables_time,a.nickname
	from (select a.order_no,a.corporate_name,a.amount,a.receivables_time,b.nickname,count(1) counts
	from temporary_receivables a left join (select x1.order_no,y1.nickname from t_order_new x1
	left join user y1 on x1.buyer_id=y1.id )b on a.order_no=b.order_no where a.corporate_name=b.nickname
	group by a.order_no,a.corporate_name,a.amount,a.receivables_time)a where a.counts>1 )y set x.status=1
	where x.order_no=y.order_no and x.amount=y.amount and x.receivables_time=y.receivables_time
	and x.corporate_name=y.nickname and x.aid= #{aid}
  </update>
  <select id="selectReceivablesNumberRepeats"  parameterType="com.goafanti.common.model.TemporaryReceivables" resultType="java.lang.Integer">
   select  count(*) from t_order_bill_new a left join user b on a.payer_id=b.id
	where a.order_no= #{orderNo}
	and b.nickname= #{corporateName}
	and a.transaction_amount= #{amount}
	and a.financial_pay_time= #{receivablesTime}
  </select>
</mapper>
