<?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.younike.system.mapper.PersonalMerchantSettleMapper">
  <resultMap id="PersonalMerchantSettleVO" type="com.younike.system.domain.vo.PersonalMerchantSettleVO">
    <id column="personal_merchant_settle_id" property="personalMerchantSettleId" />
    <result column="year_month" property="yearMonth" />
    <result column="merchant_id" property="merchantId" />
    <result column="merchant_index" property="merchantIndex" />
    <result column="merchant_name" property="merchantName" />
    <result column="actual_income" property="actualIncome" />
    <result column="settle_status" property="settleStatus" />
    <result column="settleStatusName" property="settleStatusName" />
    <result column="del_flag" property="delFlag" />
    <result column="create_by" property="createBy" />
    <result column="create_time" property="createTime" />
    <result column="update_by" property="updateBy" />
    <result column="update_time" property="updateTime" />
    <result column="remark" property="remark" />
  </resultMap>

  <select id="listPersonalMerchantSettle" resultMap="PersonalMerchantSettleVO">
    <include refid="Base_Column_List"/>
    <where>
      del_flag = '0'
      <if test="merchantIndex != null  and merchantIndex != ''"> and merchant_index like concat('%', #{merchantIndex}, '%')</if>
      <if test="merchantName != null  and merchantName != ''"> and merchant_name like concat('%', #{merchantName}, '%')</if>
      <if test="settleStatus != null  and settleStatus != ''"> and settle_status = #{settleStatus}</if>
      <if test="yearMonthStart != null and yearMonthStart != ''">
        and `year_month`  >= #{yearMonthStart}
      </if>
      <if test="yearMonthEnd != null and yearMonthEnd != ''">
        and `year_month` &lt;= #{yearMonthEnd}
      </if>
    </where>
    ORDER BY settle_status
  </select>

  <update id="updateBatchById" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index">
      update personal_merchant_settle
      <trim prefix="SET" suffixOverrides=",">
        <if test="item.yearMonth != null and item.yearMonth != ''">`year_month` = #{item.yearMonth},</if>
        <if test="item.merchantId != null">merchant_id = #{item.merchantId},</if>
        <if test="item.merchantIndex != null and item.merchantIndex != ''">merchant_index = #{item.merchantIndex},</if>
        <if test="item.merchantName != null and item.merchantName != ''">merchant_name = #{item.merchantName},</if>
        <if test="item.actualIncome != null">actual_income = #{item.actualIncome},</if>
        <if test="item.settleStatus != null and item.settleStatus != ''">settle_status = #{item.settleStatus},</if>
        <if test="item.delFlag != null">del_flag = #{item.delFlag},</if>
        <if test="item.createBy != null">create_by = #{item.createBy},</if>
        <if test="item.createTime != null">create_time = #{item.createTime},</if>
        <if test="item.updateBy != null">update_by = #{item.updateBy},</if>
        <if test="item.updateTime != null">update_time = #{item.updateTime},</if>
        <if test="item.remark != null and item.remark != ''">remark = #{item.remark},</if>
      </trim>
      where 1 = 1 and personal_merchant_settle_id = #{item.personalMerchantSettleId};
    </foreach>
  </update>

  <!-- 根据年月和商家id查询个体户账单结算记录 -->
  <select id="getByMerchantIdAndYearMonth" resultMap="PersonalMerchantSettleResult">
    <include refid="Base_Column_List"/>
    <where>
      del_flag = '0'
      and settle_status = '001'
      and merchant_id = #{merchantId},
      and `year_month` = #{yearMonth}
    </where>
  </select>

  <insert id="saveBatch" parameterType="java.util.List">
    insert into personal_merchant_settle (
     `year_month`, merchant_id, merchant_index, merchant_name, actual_income, settle_status, del_flag, create_by, create_time, update_by, update_time, remark
    ) VALUES
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.yearMonth},#{item.merchantId},#{item.merchantIndex},#{item.merchantName},#{item.actualIncome},#{item.settleStatus},#{item.createBy},#{item.updateBy},#{item.remark}
      )
    </foreach>
  </insert>

</mapper>