<?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.gic.haoban.manage.service.dao.mapper.fee.TabHaobanQywxFeeMapper">

    <resultMap type="com.gic.haoban.manage.service.entity.fee.TabHaobanQywxFee" id="TabHaobanQywxFeeMap">
        <result property="qwFeeId" column="qw_fee_id" jdbcType="INTEGER"/>
        <result property="corpName" column="corp_name" jdbcType="VARCHAR"/>
        <result property="corpId" column="corp_id" jdbcType="VARCHAR"/>
        <result property="wxEnterpriseId" column="wx_enterprise_id" jdbcType="VARCHAR"/>
        <result property="interceptTime" column="intercept_time" jdbcType="TIMESTAMP"/>
        <result property="interceptUserNum" column="intercept_user_num" jdbcType="INTEGER"/>
        <result property="permissionUserNum" column="permission_user_num" jdbcType="INTEGER"/>
        <result property="buyPermissionUserNum" column="buy_permission_user_num" jdbcType="INTEGER"/>
        <result property="interceptFlag" column="intercept_flag" jdbcType="INTEGER"/>
        <result property="transferUserNum" column="transfer_user_num" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        qw_fee_id,
        corp_name,
        corp_id,
        wx_enterprise_id,
        intercept_time,
        intercept_user_num,
        permission_user_num,
        buy_permission_user_num,
        intercept_flag,
        transfer_user_num
    </sql>

    <!--查询指定行数据-->
    <select id="listPage" resultMap="TabHaobanQywxFeeMap">
    	<if test="interceptFlag != null and interceptFlag == 0">
    		select <include refid="Base_Column_List"/> from (
    	</if>
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_qywx_fee
        <where>
            <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
                and wx_enterprise_id = #{wxEnterpriseId}
            </if>
            <if test="interceptFlag != null">
                and intercept_flag = #{interceptFlag}
            </if>
            <if test="searchName != null and searchName != ''">
                and ( corp_name like CONCAT('%',#{searchName},'%') or corp_id like CONCAT('%',#{searchName},'%') )
            </if>
            <if test="startDate != null and startDate != ''">
                and intercept_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and intercept_time <![CDATA[ <= ]]> #{endDate}
            </if>
            <choose>
                <when test="interceptFlag != null and interceptFlag == 0">
                    and intercept_time between DATE(CURDATE()) and DATE_add(CURDATE(), INTERVAL 30 DAY)
                    order by intercept_time asc ) t 
                    GROUP BY wx_enterprise_id
                </when>
                <otherwise>
                    order by intercept_time desc
                </otherwise>
            </choose>
        </where>
    </select>


    <!--新增所有列-->
    <insert id="insert" keyProperty="qwFeeId" useGeneratedKeys="true">
        insert into tab_haoban_qywx_fee(qw_fee_id, corp_name, corp_id, wx_enterprise_id, intercept_time,
                                        intercept_user_num, permission_user_num, buy_permission_user_num,
                                        intercept_flag,transfer_user_num)
        values (#{qwFeeId}, #{corpName}, #{corpId}, #{wxEnterpriseId}, #{interceptTime}, #{interceptUserNum},
                #{permissionUserNum}, #{buyPermissionUserNum}, #{interceptFlag},#{transferUserNum})
    </insert>

    <select id="queryByWxEnterpriseId" resultMap="TabHaobanQywxFeeMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_qywx_fee
        where wx_enterprise_id = #{wxEnterpriseId} order by intercept_time desc limit 1
    </select>

</mapper>

