<?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" />
    <result column="merchant_type" property="merchantType" />
    <result column="reserve_income" property="reserveIncome" />
    <result column="group_income" property="groupIncome" />
    <result column="group_times" property="groupTimes" />
  </resultMap>

  <select id="listPersonalMerchantSettle" resultMap="PersonalMerchantSettleVO">
    SELECT
    pms.personal_merchant_settle_id,
    pms.`year_month`,
    pms.merchant_id,
    pms.merchant_index,
    su.nick_name as merchant_name,
    pms.actual_income,
    pms.settle_status,
    pms.del_flag,
    pms.create_by,
    pms.create_time,
    pms.update_by,
    pms.update_time,
    pms.remark
    FROM
    personal_merchant_settle pms
    LEFT JOIN merchant m on pms.merchant_id = m.merchant_id
    LEFT JOIN sys_user su on m.admin_id = su.user_id
    where m.merchant_type = 1
    and pms.del_flag = '0'
    <if test="merchantIndex != null  and merchantIndex != ''">and pms.merchant_index like concat('%', #{merchantIndex},
      '%')
    </if>
    <if test="merchantName != null  and merchantName != ''">and su.nick_name like concat('%', #{merchantName}, '%')</if>
    <if test="settleStatus != null  and settleStatus != ''">and pms.settle_status = #{settleStatus}</if>
    <if test="yearMonthStart != null and yearMonthStart != ''">
      and pms.`year_month` >= #{yearMonthStart}
    </if>
    <if test="yearMonthEnd != null and yearMonthEnd != ''">
      and pms.`year_month` &lt;= #{yearMonthEnd}
    </if>
    ORDER BY pms.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.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>-->
<!--        <if test="item.merchantType != null and item.merchantType != ''">merchant_type = #{item.merchantType},</if>-->
<!--        <if test="item.reserveIncome != null">reserve_income = #{item.reserveIncome},</if>-->
<!--        <if test="item.groupIncome != null ">group_income = #{item.groupIncome},</if>-->
<!--        <if test="item.groupTimes != null and item.groupTimes != ''">group_times = #{item.groupTimes},</if>-->
<!--      </trim>-->
<!--      where  personal_merchant_settle_id = #{item.personalMerchantSettleId};-->
<!--    </foreach>-->
<!--  </update>-->

  <update id="updateBatchById" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
      update personal_merchant_settle
      <set>
        <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.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>
        <if test="item.merchantType != null and item.merchantType != ''">merchant_type = #{item.merchantType},</if>
        <if test="item.reserveIncome != null">reserve_income = #{item.reserveIncome},</if>
        <if test="item.groupIncome != null ">group_income = #{item.groupIncome},</if>
        <if test="item.groupTimes != null and item.groupTimes != ''">group_times = #{item.groupTimes},</if>

      </set>
      where    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,create_by, create_time, update_by, update_time, remark,merchant_type,reserve_income,group_income,group_times
    ) VALUES
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.yearMonth},#{item.merchantId},#{item.merchantIndex},#{item.merchantName},#{item.actualIncome},#{item.settleStatus},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime},#{item.remark},#{item.merchantType},#{item.reserveIncome},#{item.groupIncome},#{item.groupTimes}
      )
    </foreach>
  </insert>
<!--  账单结算分页列表-->
  <select id="bilStatementList" resultMap="PersonalMerchantSettleVO">
    <include refid="Base_Column_List"/>
    where del_flag = '0'

    <if test="merchantId != null">and merchant_id =#{merchantId} </if>
    <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>
    ORDER BY settle_status
  </select>
<!--  账单结算金额信息-->
  <select id="getSettleAmount" resultType="BigDecimal">
    select  ifNull(SUM(actual_income),0) from personal_merchant_settle
    where del_flag = '0'
    <if test="merchantId != null">and merchant_id =#{merchantId} </if>
    <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>
  </select>
</mapper>
