<?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.yonge.cooleshow.biz.dal.dao.PlatformCashAccountRecordDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="account_id_" jdbcType="INTEGER" property="accountId"/>
        <result column="trans_amount_" jdbcType="VARCHAR" property="transAmount"/>
        <result column="in_or_out_" jdbcType="VARCHAR" property="inOrOut"/>
        <result column="post_status_" jdbcType="VARCHAR" property="postStatus"/>
        <result column="biz_type_" jdbcType="VARCHAR" property="bizType"/>
        <result column="biz_id_" jdbcType="INTEGER" property="bizId"/>
        <result column="memo_" jdbcType="VARCHAR" property="memo"/>
        <result column="order_no_" jdbcType="VARCHAR" property="orderNo"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
		<result column="account_period_time_" jdbcType="TIMESTAMP" property="accountPeriodTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_,
		account_id_, trans_amount_, in_or_out_, post_status_, biz_type_, biz_id_, memo_, order_no_, create_time_, update_time_,account_period_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord">
        insert into platform_cash_account_record(account_id_, trans_amount_, in_or_out_, post_status_, biz_type_,
        biz_id_, memo_, order_no_, create_time_, update_time_,account_period_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.accountId}, #{entity.transAmount}, #{entity.inOrOut}, #{entity.postStatus}, #{entity.bizType},
            #{entity.bizId}, #{entity.memo}, #{entity.orderNo}, #{entity.createTime}, #{entity.updateTime}, #{entity.accountPeriodTime})
        </foreach>
    </insert>

    <update id="batchUpdate">
        <foreach collection="list" item="param" separator=";">
            UPDATE platform_cash_account_record
            <set>
                <if test="param.accountId">
    				account_id_ = #{param.accountId},
	    		</if>
                <if test="param.transAmount">
    				trans_amount_ = #{param.transAmount},
	    		</if>
                <if test="param.memo">
    				memo_ = #{param.memo},
	    		</if>
                <if test="param.orderNo">
    				order_no_ = #{param.orderNo},
	    		</if>
	    		<if test="param.bizId">
	    			biz_id_ = #{param.bizId},
	    		</if>
	    		<if test="param.inOrOut">
	    			in_or_out_ = #{param.inOrOut},
	    		</if>
	    		<if test="param.bizType">
	    			biz_type_ = #{param.bizType},
	    		</if>
	    		<if test="param.postStatus">
	    			post_status_ = #{param.postStatus},
	    		</if>
				<if test="param.accountPeriodTime">
					account_period_time_ = #{param.accountPeriodTime},
				</if>
                update_time_ = NOW()
            </set>
            WHERE id_ = #{param.id}
        </foreach>
    </update>

	<select id="queryPage" resultType="com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord">
    	select * from platform_cash_account_record
		<where>
			<if test="param.orderNo">
				and order_no_ = #{param.orderNo}
			</if>
			<if test="param.bizId">
				and biz_id_ = #{param.bizId}
			</if>
			<if test="param.inOrOut">
				and in_or_out_ = #{param.inOrOut}
			</if>
			<if test="param.bizType">
				and biz_type_ = #{param.bizType}
			</if>
			<if test="param.status">
				and post_status_ = #{param.status}
			</if>
			<if test="param.startDate">
				and update_time_ &gt;= #{param.startDate}
			</if>
			<if test="param.endDate">
				and update_time_ &lt;= #{param.endDate}
			</if>
		</where>
    </select>
    
    <select id="querySummary" resultType="com.yonge.cooleshow.biz.dal.vo.PlatformCashAccountRecordSummaryVo">
		select
			sum(case when in_or_out_ = 'IN' and post_status_ = 'WAIT' then trans_amount_ else 0 end) advancesReceived,
			sum(case when in_or_out_ = 'IN' and post_status_ = 'RECORDED' then trans_amount_ else 0 end) settledReceived,
			sum(case when in_or_out_ = 'OUT' and post_status_ = 'WAIT' then trans_amount_ else 0 end) advancePayment,
			sum(case when in_or_out_ = 'OUT' and post_status_ = 'RECORDED' then trans_amount_ else 0 end) settledPayment
		from platform_cash_account_record
		<where>
			<if test="param.orderNo">
				and order_no_ = #{param.orderNo}
			</if>
			<if test="param.bizId">
				and biz_id_ = #{param.bizId}
			</if>
			<if test="param.inOrOut">
				and in_or_out_ = #{param.inOrOut}
			</if>
			<if test="param.bizType">
				and biz_type_ = #{param.bizType}
			</if>
			<if test="param.status">
				and post_status_ = #{param.status}
			</if>
			<if test="param.startDate">
				and update_time_ &gt;= #{param.startDate}
			</if>
			<if test="param.endDate">
				and update_time_ &lt;= #{param.endDate}
			</if>
		</where>
    </select>
    
    <select id="queryByBizTypeAndPaymentDays" resultType="com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord">
    	select * from platform_cash_account_record
    	where biz_type_ = #{bizType} and TIMESTAMPDIFF(HOUR, create_time_, now()) &gt;= ${24 * paymentDays}
    	  and post_status_ = 'WAIT'
    </select>

	<select id="queryCanAccountByBizType"
			resultType="com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord">
		select * from platform_cash_account_record
		where post_status_ = 'WAIT' and account_period_time_ &lt; now()
		and biz_type_ IN
		<foreach collection="bizTypes" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<update id="cancelRecord">
		update platform_cash_account_record set post_status_ = 'CANCEL'
		where post_status_ = 'WAIT' and order_no_ = #{orderNo} and biz_id_ = #{bizId}
		and biz_type_ = #{bizType}
	</update>

    <update id="mallRecordedRecord">
		update platform_cash_account_record set post_status_ = 'RECORDED'
		where post_status_ = 'WAIT' and order_no_ = #{orderNo} and biz_id_ = #{bizId}
	</update>
</mapper>
