<?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.SysUserCashAccountLogDao">

    <resultMap type="com.ym.mec.biz.dal.entity.SysUserCashAccountLog" id="SysUserCashAccountLog">
        <result column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="username_" property="username"/>
        <result column="trans_no_" property="transNo"/>
        <result column="return_fee_type_" property="returnFeeType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="amount_" property="amount"/>
        <result column="comment_" property="comment"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="organ_name_" property="organName"/>
        <result column="group_id_" property="groupId"/>
        <result column="group_name_" property="groupName"/>
        <result column="pay_time_" property="payTime"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SysUserCashAccountLog">
        SELECT * FROM sys_user_cash_account_log WHERE id_ = #{id}
    </select>

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysUserCashAccountLog" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO sys_user_cash_account_log (user_id_,organ_id_,trans_no_,return_fee_type_,group_type_,
                                               group_id_,amount_,comment_,pay_time_,create_time_,update_time_,tenant_id_)
        VALUES(#{userId},#{organId},#{transNo},#{returnFeeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
               #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{groupId},#{amount},
               #{comment},#{payTime},NOW(),NOW(),#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SysUserCashAccountLog">
        UPDATE sys_user_cash_account_log
        SET comment_ = #{comment},trans_no_ = #{transNo},pay_time_ = #{payTime},update_time_ = NOW()
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

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

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysUserCashAccountLog" parameterType="map">
        SELECT cal.*,su.username_,o.name_ organ_name_ FROM sys_user_cash_account_log cal
        LEFT JOIN sys_user su ON su.id_ = cal.user_id_
        LEFT JOIN organization o ON o.id_ = cal.organ_id_
        <include refid="queryPageSql"/>
        ORDER BY id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(cal.id_) FROM sys_user_cash_account_log cal
        LEFT JOIN sys_user su ON su.id_ = cal.user_id_
        <include refid="queryPageSql"/>
    </select>
    <sql id="queryPageSql">
        <where>
        	cal.tenant_id_ = #{tenantId}
            <if test="createStartTime != null">
                AND DATE_FORMAT(cal.create_time_,'%Y-%m-%d') >= #{createStartTime}
            </if>
            <if test="createEndTime != null">
                AND DATE_FORMAT(cal.create_time_,'%Y-%m-%d') &lt;= #{createEndTime}
            </if>
            <if test="payStartTime != null">
                AND DATE_FORMAT(cal.pay_time_,'%Y-%m-%d') >= #{payStartTime}
            </if>
            <if test="payEndTime != null">
                AND DATE_FORMAT(cal.pay_time_,'%Y-%m-%d') &lt;= #{payEndTime}
            </if>
            <if test="returnFeeFlag != null and returnFeeFlag == 1">
                AND cal.pay_time_ IS NOT NULL
            </if>
            <if test="returnFeeFlag != null and returnFeeFlag == 0">
                AND cal.pay_time_ IS NULL
            </if>
            <if test="groupType != null and groupType != ''">
                AND cal.group_type_ = #{groupType}
            </if>
            <if test="returnFeeType != null and returnFeeType != ''">
                AND cal.return_fee_type_ = #{returnFeeType}
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(cal.organ_id_,#{organId})
            </if>
            <if test="search != null and search != ''">
                AND (cal.id_ = #{search} OR cal.user_id_ = #{search} OR cal.group_id_ = #{search}
                OR su.username_ LIKE CONCAT('%',#{search},'%') OR cal.trans_no_ LIKE CONCAT('%',#{search},'%'))
            </if>
        </where>
    </sql>

</mapper>
