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

    <resultMap type="com.gic.haoban.manage.service.entity.fee.TabHaobanQywxFeeAccountStaff" id="TabHaobanQywxFeeAccountStaffMap">
        <result property="feeAccountStaffId" column="fee_account_staff_id" jdbcType="INTEGER"/>
        <result property="corpId" column="corp_id" jdbcType="VARCHAR"/>
        <result property="wxEnterpriseId" column="wx_enterprise_id" jdbcType="VARCHAR"/>
        <result property="wxUserId" column="wx_user_id" jdbcType="VARCHAR"/>
        <result property="activeCode" column="active_code" jdbcType="VARCHAR"/>
        <result property="accountType" column="account_type" jdbcType="INTEGER"/>
        <result property="expireTime" column="expire_time" jdbcType="TIMESTAMP"/>
        <result property="activeTime" column="active_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        fee_account_staff_id
        , corp_id, wx_enterprise_id, wx_user_id, active_code, account_type, expire_time, active_time
    </sql>

    <!--新增所有列-->
    <insert id="insert">
        insert into tab_haoban_qywx_fee_account_staff(fee_account_staff_id, corp_id, wx_enterprise_id,
                                                      wx_user_id, active_code, account_type, expire_time, active_time)
        values (#{feeAccountStaffId},  #{corpId}, #{wxEnterpriseId}, #{wxUserId}, #{activeCode},
                #{accountType}, #{expireTime}, #{activeTime})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update tab_haoban_qywx_fee_account_staff
        <set>
            <if test="corpId != null and corpId != ''">
                corp_id = #{corpId},
            </if>
            <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
                wx_enterprise_id = #{wxEnterpriseId},
            </if>
            <if test="activeCode != null and activeCode != ''">
                active_code = #{activeCode},
            </if>
            <if test="accountType != null">
                account_type = #{accountType},
            </if>
            <if test="expireTime != null">
                expire_time = #{expireTime},
            </if>
            <if test="activeTime != null">
                active_time = #{activeTime},
            </if>
        </set>
        where fee_account_staff_id = #{feeAccountStaffId}
    </update>

    <select id="feeDetail" resultType="com.gic.haoban.manage.api.dto.fee.HaobanQywxFeeDetailDTO">
        select
        count(distinct wx_user_id) interceptUserNum,
        DATE_FORMAT( expire_time, '%Y-%m-%d' ) interceptTime
        from tab_haoban_qywx_fee_account_staff
        where wx_enterprise_id = #{wxEnterpriseId}
        and expire_time between DATE(CURDATE()) and DATE_add(CURDATE(), INTERVAL 30 DAY)
        group by DATE_FORMAT( expire_time, '%Y-%m-%d' )
    </select>


    <select id="lastFeeInterceptTime" resultType="java.util.Date">
        select
            expire_time expireTime
        from tab_haoban_qywx_fee_account_staff
        where wx_enterprise_id = #{wxEnterpriseId}
        and expire_time between DATE(CURDATE()) and DATE_add(CURDATE(), INTERVAL 30 DAY)
        order by expire_time asc limit 1
    </select>

    <select id="lastFeeInterceptCount" resultType="java.lang.Integer">
        select
        count(distinct wx_user_id)
        from tab_haoban_qywx_fee_account_staff
        where wx_enterprise_id = #{wxEnterpriseId}
        and expire_time between DATE(CURDATE()) and DATE_add(CURDATE(), INTERVAL 30 DAY)
    </select>
    
    <delete id="deleteAll">
     	delete from tab_haoban_qywx_fee_account_staff where wx_enterprise_id = #{wxEnterpriseId}
    </delete>
</mapper>
