<?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.ylx.massage.mapper.TConsumptionLogMapper">

    <resultMap type="com.ylx.massage.domain.TConsumptionLog" id="TConsumptionLogMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="amount" column="amount" jdbcType="NUMERIC"/>
        <result property="billType" column="bill_type" jdbcType="INTEGER"/>
        <result property="billNo" column="bill_no" jdbcType="VARCHAR"/>
        <result property="openId" column="open_id" jdbcType="VARCHAR"/>
        <result property="deptId" column="dept_id" jdbcType="VARCHAR"/>
        <result property="deptName" column="dept_name" jdbcType="VARCHAR"/>
        <result property="isGet" column="is_get" jdbcType="INTEGER"/>
        <result property="note" column="note" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="isDelete" column="is_delete" jdbcType="INTEGER"/>
    </resultMap>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into t_consumption_log(amount,bill_type,bill_no,open_id,is_get,note,create_time,update_time,is_delete)
        values
        <foreach collection="entities" item="entity" separator=",">
            (
             #{entity.amount},#{entity.billType},#{entity.billNo},#{entity.openId},#{entity.isGet},
             #{entity.note},#{entity.createTime},#{entity.updateTime},#{entity.isDelete}
            )
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="id">
        insert into t_consumption_log(id,amount,bill_type,bill_no,open_id,is_get,note)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id},#{entity.amount},#{entity.billType},#{entity.billNo},#{entity.openId},#{entity.isGet},#{entity.note})
        </foreach>
        on duplicate key update
        id = values(id),amount = values(amount), bill_type = values(bill_type), bill_no = values(bill_no), open_id = values(open_id), is_get = values(is_get),
        note = values(note)
    </insert>


    <select id="getBlockGetAmount" resultType="com.ylx.massage.domain.vo.HomeBlock">
        SELECT
            DATE_FORMAT( create_time, '%Y-%m' ) AS MONTH,
            IFNULL(- sum( amount ), 0 ) getAmount
        FROM
            t_consumption_log
        WHERE
            is_delete = 0
          AND bill_type = 4
          AND dept_id = #{deptId}
          AND create_time BETWEEN #{start} AND #{end}
        GROUP BY
            MONTH
        ORDER BY
            MONTH
    </select>

</mapper>

