<?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.ym.mec.biz.dal.dao.StudentWithdrawDao">

    <resultMap type="com.ym.mec.biz.dal.entity.StudentWithdraw" id="StudentWithdraw">
        <result column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="bank_card_no_" property="bankCardNo"/>
        <result column="trans_no_" property="transNo"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="amount_" property="amount"/>
        <result column="fee_user_" property="feeUser"/>
        <result column="fee_platform_" property="feePlatform"/>
        <result column="description_" property="description"/>
        <result column="comment_" property="comment"/>
        <result column="withdraw_no_" property="withdrawNo"/>
        <result column="create_time_" property="createTime"/>
        <result column="modify_time_" property="modifyTime"/>
        <result column="trans_type_" property="detail.transType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="platform_account_no_" property="detail.platformAccountNo"/>
        <result column="balance_" property="detail.balance"/>
        <result column="real_name_" property="user.realName"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>
    
    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="StudentWithdraw">
		SELECT * FROM student_withdraw WHERE id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="StudentWithdraw">
		SELECT * FROM student_withdraw where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentWithdraw" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO student_withdraw
        (id_,user_id_,bank_card_no_,trans_no_,status_,amount_,fee_user_,fee_platform_,description_,comment_,create_time_,modify_time_,withdraw_no_,tenant_id_)
        VALUES(#{id},#{userId},#{bankCardNo},#{transNo},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{amount},#{feeUser},#{feePlatform},#{description},#{comment},now(),now(),#{withdrawNo},#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentWithdraw">
        UPDATE student_withdraw
        <set>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="modifyTime != null">
                modify_time_ = #{modifyTime},
            </if>
            <if test="feePlatform != null">
                fee_platform_ = #{feePlatform},
            </if>
            <if test="comment != null">
                comment_ = #{comment},
            </if>
            <if test="amount != null">
                amount_ = #{amount},
            </if>
            <if test="feeUser != null">
                fee_user_ = #{feeUser},
            </if>
            <if test="bankCardNo != null">
                bank_card_no_ = #{bankCardNo},
            </if>
            <if test="description != null">
                description_ = #{description},
            </if>
            <if test="transNo != null">
                trans_no_ = #{transNo},
            </if>
            <if test="withdrawNo != null">
                withdraw_no_ = #{withdrawNo},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM student_withdraw WHERE id_ = #{id}
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="StudentWithdraw" parameterType="map">
        SELECT sw.*,cad.platform_account_no_,cad.trans_type_,su.real_name_,cad.balance_
        FROM student_withdraw sw,sys_user_cash_account_detail cad ,sys_user su
        where cad.type_ = 'WITHDRAW' and sw.id_ = cad.attribute1_ and su.id_ = sw.user_id_ and sw.status_ = cad.status_
        	and sw.tenant_id_ = #{tenantId}
        <if test="userId != null">
            and sw.user_id_ = #{userId}
        </if>
        <if test="bankCardNo != null">
            and sw.bank_card_no_ = #{bankCardNo}
        </if>
        <if test="organId!=null and organId!=''">
            and su.organ_id_=#{organId}
        </if>
        ORDER BY sw.create_time_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(sw.id_) FROM student_withdraw sw,sys_user su
        where  su.id_ = sw.user_id_ and sw.tenant_id_ = #{tenantId}
            <if test="userId != null">
                and sw.user_id_ = #{userId}
            </if>
            <if test="bankCardNo != null">
                and sw.bank_card_no_ = #{bankCardNo}
            </if>
	        <if test="organId!=null and organId!=''">
	            and su.organ_id_=#{organId}
	        </if>
	</select>

    <select id="getByUserId" resultMap="StudentWithdraw" parameterType="Long">
        SELECT * FROM student_withdraw
        <where>
                user_id_ = #{userId}
        </where>
        LIMIT 1
    </select>
    <select id="queryByUserId" resultType="com.ym.mec.biz.dal.entity.StudentWithdraw">
        SELECT * FROM student_withdraw
        <where>
              FIND_IN_SET(user_id_,#{userIds})
        </where>
        GROUP BY user_id_
    </select>
</mapper>
