<?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.WxEnterpriseMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise">
        <id column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="permanent_code" property="permanentCode" jdbcType="VARCHAR"/>
        <result column="corp_name" property="corpName" jdbcType="VARCHAR"/>
        <result column="corp_type" property="corpType" jdbcType="VARCHAR"/>
        <result column="corp_square_logo_url" property="corpSquareLogoUrl" jdbcType="VARCHAR"/>
        <result column="corp_user_max" property="corpUserMax" jdbcType="VARCHAR"/>
        <result column="corp_full_name" property="corpFullName" jdbcType="VARCHAR"/>
        <result column="subject_type" property="subjectType" jdbcType="VARCHAR"/>
        <result column="verified_end_time" property="verifiedEndTime" jdbcType="TIMESTAMP"/>
        <result column="corp_wxqrcode" property="corpWxqrcode" jdbcType="VARCHAR"/>
        <result column="corp_scale" property="corpScale" jdbcType="VARCHAR"/>
        <result column="corp_industry" property="corpIndustry" jdbcType="VARCHAR"/>
        <result column="corp_sub_industry" property="corpSubIndustry" jdbcType="VARCHAR"/>
        <result column="location" property="location" jdbcType="VARCHAR"/>
        <result column="auth_info" property="authInfo" jdbcType="VARCHAR"/>
        <result column="agent" property="agent" jdbcType="VARCHAR"/>
        <result column="agentid" property="agentid" jdbcType="VARCHAR"/>
        <result column="appid" property="appid" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="square_logo_url" property="squareLogoUrl" jdbcType="VARCHAR"/>
        <result column="round_logo_url" property="roundLogoUrl" jdbcType="VARCHAR"/>
        <result column="wx_secret_key" property="wxSecretKey" jdbcType="VARCHAR"/>
        <result column="member_secret" property="memberSecret" jdbcType="VARCHAR"/>
        <result column="contact_secret" property="contactSecret" jdbcType="VARCHAR"/>
        <result column="level" property="level" jdbcType="INTEGER"/>
        <result column="contact_flag" property="contactFlag" jdbcType="INTEGER"/>
        <result column="bind_flag" property="bindFlag" jdbcType="INTEGER"/>
        <result column="wx_secret_key_last_time" property="wxSecretKeyLastTime" jdbcType="TIMESTAMP"/>
        <result column="member_secret_last_time" property="memberSecretLastTime" jdbcType="TIMESTAMP"/>
        <result column="contact_secret_last_time" property="contactSecretLastTime" jdbcType="TIMESTAMP"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="agree_external_userid_flag" property="agreeExternalUseridFlag" jdbcType="INTEGER"/>
        <result column="agree_external_userid_time" property="agreeExternalUseridTime" jdbcType="TIMESTAMP"/>
        <result column="external_flag" property="externalFlag" jdbcType="INTEGER"/>
        <result column="userid_flag" property="useridFlag" jdbcType="INTEGER"/>
        <result column="wx_security_type" property="wxSecurityType" jdbcType="INTEGER"/>
        <result column="auto_active_flag" property="autoActiveFlag" jdbcType="INTEGER"/>
        <result column="wx_corpid" property="wxCorpid"/>
        <result column="open_corpid" property="openCorpid"/>
        <result column="wxa_appid" property="wxaAppid" />
        <result column="url_host" property="urlHost"/>
        <result column="callback_flag" property="callbackFlag"/>
    </resultMap>

    <sql id="Base_Column_List">
        wx_enterprise_id, permanent_code, corp_name, corp_type, corp_square_logo_url,
    corp_user_max, corp_full_name, subject_type, verified_end_time, corp_wxqrcode, corp_scale, 
    corp_industry, corp_sub_industry, location, auth_info, agent, agentid, appid, name, 
    square_logo_url, round_logo_url, level, bind_flag, status_flag, create_time, update_time,contact_flag,wx_secret_key,wx_secret_key_last_time,member_secret_last_time,contact_secret_last_time
    ,member_secret,contact_secret,agree_external_userid_flag,agree_external_userid_time,external_flag,userid_flag ,wx_security_type , wx_corpid , open_corpid , auto_active_flag , wxa_appid , url_host,callback_flag
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_enterprise
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
    </select>
    
    <select id="selectByWxaAppid" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_enterprise
        where wxa_appid = #{appid} and wx_security_type = 5 and status_flag = 1
    </select>
 
    <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise">
        insert into tab_haoban_wx_enterprise
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wxEnterpriseId != null">
                wx_enterprise_id,
            </if>
            <if test="permanentCode != null">
                permanent_code,
            </if>
            <if test="corpName != null">
                corp_name,
            </if>
            <if test="corpType != null">
                corp_type,
            </if>
            <if test="corpSquareLogoUrl != null">
                corp_square_logo_url,
            </if>
            <if test="corpUserMax != null">
                corp_user_max,
            </if>
            <if test="corpFullName != null">
                corp_full_name,
            </if>
            <if test="subjectType != null">
                subject_type,
            </if>
            <if test="verifiedEndTime != null">
                verified_end_time,
            </if>
            <if test="corpWxqrcode != null">
                corp_wxqrcode,
            </if>
            <if test="corpScale != null">
                corp_scale,
            </if>
            <if test="corpIndustry != null">
                corp_industry,
            </if>
            <if test="corpSubIndustry != null">
                corp_sub_industry,
            </if>
            <if test="location != null">
                location,
            </if>
            <if test="authInfo != null">
                auth_info,
            </if>
            <if test="agent != null">
                agent,
            </if>
            <if test="agentid != null">
                agentid,
            </if>
            <if test="appid != null">
                appid,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="squareLogoUrl != null">
                square_logo_url,
            </if>
            <if test="roundLogoUrl != null">
                round_logo_url,
            </if>
            <if test="level != null">
                level,
            </if>
            <if test="bindFlag != null">
                bind_flag,
            </if>
            <if test="statusFlag != null">
                status_flag,
            </if>
            <if test="contactFlag != null">
                contact_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="wxSecretKey != null">
                wx_secret_key,
            </if>
            <if test="contactSecret != null">
                contact_secret,
            </if>
            <if test="agreeExternalUseridFlag != null">
                agree_external_userid_flag,
            </if>
            <if test="agreeExternalUseridTime != null">
                agree_external_userid_time,
            </if>
            <if test="externalFlag != null">
                external_flag,
            </if>
            <if test="useridFlag != null">
                userid_flag,
            </if>
            <if test="null != wxCorpid">
            	wx_corpid ,
            </if>
             <if test="null != openCorpid">
            	open_corpid ,
            </if>
            <if test="null != wxaAppid">
                wxa_appid ,
            </if>
            <if test="null != urlHost">
                url_host ,
            </if>
            <if test="null != wxSecurityType">
                wx_security_type ,
            </if>
            <if test="null != callbackFlag">
                callback_flag ,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wxEnterpriseId != null">
                #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="permanentCode != null">
                #{permanentCode,jdbcType=VARCHAR},
            </if>
            <if test="corpName != null">
                #{corpName,jdbcType=VARCHAR},
            </if>
            <if test="corpType != null">
                #{corpType,jdbcType=VARCHAR},
            </if>
            <if test="corpSquareLogoUrl != null">
                #{corpSquareLogoUrl,jdbcType=VARCHAR},
            </if>
            <if test="corpUserMax != null">
                #{corpUserMax,jdbcType=VARCHAR},
            </if>
            <if test="corpFullName != null">
                #{corpFullName,jdbcType=VARCHAR},
            </if>
            <if test="subjectType != null">
                #{subjectType,jdbcType=VARCHAR},
            </if>
            <if test="verifiedEndTime != null">
                #{verifiedEndTime,jdbcType=TIMESTAMP},
            </if>
            <if test="corpWxqrcode != null">
                #{corpWxqrcode,jdbcType=VARCHAR},
            </if>
            <if test="corpScale != null">
                #{corpScale,jdbcType=VARCHAR},
            </if>
            <if test="corpIndustry != null">
                #{corpIndustry,jdbcType=VARCHAR},
            </if>
            <if test="corpSubIndustry != null">
                #{corpSubIndustry,jdbcType=VARCHAR},
            </if>
            <if test="location != null">
                #{location,jdbcType=VARCHAR},
            </if>
            <if test="authInfo != null">
                #{authInfo,jdbcType=VARCHAR},
            </if>
            <if test="agent != null">
                #{agent,jdbcType=VARCHAR},
            </if>
            <if test="agentid != null">
                #{agentid,jdbcType=VARCHAR},
            </if>
            <if test="appid != null">
                #{appid,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="squareLogoUrl != null">
                #{squareLogoUrl,jdbcType=VARCHAR},
            </if>
            <if test="roundLogoUrl != null">
                #{roundLogoUrl,jdbcType=VARCHAR},
            </if>
            <if test="level != null">
                #{level,jdbcType=INTEGER},
            </if>
            <if test="bindFlag != null">
                #{bindFlag,jdbcType=INTEGER},
            </if>
            <if test="statusFlag != null">
                #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="contactFlag != null">
                #{contactFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="wxSecretKey != null">
                #{wxSecretKey,jdbcType=TIMESTAMP},
            </if>
            <if test="contactSecret != null">
                #{contactSecret,jdbcType=TIMESTAMP},
            </if>
            <if test="agreeExternalUseridFlag != null">
                #{agreeExternalUseridFlag,jdbcType=INTEGER},
            </if>
            <if test="agreeExternalUseridTime != null">
                #{agreeExternalUseridTime,jdbcType=TIMESTAMP},
            </if>
            <if test="externalFlag != null">
                #{externalFlag,jdbcType=INTEGER},
            </if>
            <if test="useridFlag != null">
                #{useridFlag} ,
            </if>
            <if test="null != wxCorpid">
            	#{wxCorpid} ,
            </if>
            <if test="null != openCorpid">
            	#{openCorpid} ,
            </if>
            <if test="null != wxaAppid">
                #{wxaAppid} ,
            </if>
            <if test="null != urlHost">
                #{urlHost} ,
            </if>
            <if test="null != wxSecurityType">
                #{wxSecurityType} ,
            </if>
            <if test="null != callbackFlag">
                #{callbackFlag} ,
            </if>
            
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise">
        update tab_haoban_wx_enterprise
        <set>
            <if test="permanentCode != null">
                permanent_code = #{permanentCode,jdbcType=VARCHAR},
            </if>
            <if test="corpName != null">
                corp_name = #{corpName,jdbcType=VARCHAR},
            </if>
            <if test="corpType != null">
                corp_type = #{corpType,jdbcType=VARCHAR},
            </if>
            <if test="corpSquareLogoUrl != null">
                corp_square_logo_url = #{corpSquareLogoUrl,jdbcType=VARCHAR},
            </if>
            <if test="corpUserMax != null">
                corp_user_max = #{corpUserMax,jdbcType=VARCHAR},
            </if>
            <if test="corpFullName != null">
                corp_full_name = #{corpFullName,jdbcType=VARCHAR},
            </if>
            <if test="subjectType != null">
                subject_type = #{subjectType,jdbcType=VARCHAR},
            </if>
            <if test="verifiedEndTime != null">
                verified_end_time = #{verifiedEndTime,jdbcType=TIMESTAMP},
            </if>
            <if test="corpWxqrcode != null">
                corp_wxqrcode = #{corpWxqrcode,jdbcType=VARCHAR},
            </if>
            <if test="corpScale != null">
                corp_scale = #{corpScale,jdbcType=VARCHAR},
            </if>
            <if test="corpIndustry != null">
                corp_industry = #{corpIndustry,jdbcType=VARCHAR},
            </if>
            <if test="corpSubIndustry != null">
                corp_sub_industry = #{corpSubIndustry,jdbcType=VARCHAR},
            </if>
            <if test="location != null">
                location = #{location,jdbcType=VARCHAR},
            </if>
            <if test="authInfo != null">
                auth_info = #{authInfo,jdbcType=VARCHAR},
            </if>
            <if test="agent != null">
                agent = #{agent,jdbcType=VARCHAR},
            </if>
            <if test="agentid != null">
                agentid = #{agentid,jdbcType=VARCHAR},
            </if>
            <if test="appid != null">
                appid = #{appid,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="squareLogoUrl != null">
                square_logo_url = #{squareLogoUrl,jdbcType=VARCHAR},
            </if>
            <if test="roundLogoUrl != null">
                round_logo_url = #{roundLogoUrl,jdbcType=VARCHAR},
            </if>
            <if test="level != null">
                level = #{level,jdbcType=INTEGER},
            </if>
            <if test="bindFlag != null">
                bind_flag = #{bindFlag,jdbcType=INTEGER},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="contactFlag != null">
                contact_flag = #{contactFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="wxSecretKey != null">
                wx_secret_key = #{wxSecretKey,jdbcType=VARCHAR},
            </if>
            <if test="contactSecret != null">
                contact_secret = #{contactSecret,jdbcType=VARCHAR},
            </if>
            <if test="wxSecretKeyLastTime != null">
                wx_secret_key_last_time = #{wxSecretKeyLastTime,jdbcType=TIMESTAMP},
            </if>
            <if test="contactSecretLastTime != null">
                contact_secret_last_time = #{contactSecretLastTime,jdbcType=TIMESTAMP},
            </if>
            <if test="memberSecret != null">
                member_secret = #{memberSecret,jdbcType=TIMESTAMP},
            </if>
            <if test="memberSecretLastTime != null">
                member_secret_last_time = #{memberSecretLastTime,jdbcType=TIMESTAMP},
            </if>
            <if test="agreeExternalUseridFlag != null">
                agree_external_userid_flag = #{agreeExternalUseridFlag,jdbcType=INTEGER},
            </if>
            <if test="agreeExternalUseridTime != null">
                agree_external_userid_time = #{agreeExternalUseridTime,jdbcType=TIMESTAMP},
            </if>
            <if test="externalFlag != null">
                external_flag = #{externalFlag,jdbcType=INTEGER},
            </if>
            <if test="useridFlag != null">
                userid_flag = #{useridFlag},
            </if>
            <if test="null != wxCorpid">
            	wx_corpid = #{wxCorpid},
            </if>
            <if test="null != openCorpid">
            	open_corpid = #{openCorpid},
            </if>
            <if test="null != wxaAppid">
                wxa_appid = #{wxaAppid},
            </if>
            <if test="null != urlHost">
                url_host = #{urlHost},
            </if>
            <if test="null != callbackFlag">
                callback_flag = #{callbackFlag}
            </if>
        </set>
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
    </update>

    <select id="list" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_enterprise
        where status_flag = 1
        <if test="gicContactFlag != null">
            and contact_flag = #{gicContactFlag,jdbcType=INTEGER}
        </if>
        <if test=" appType != null and appType == 1">
            and wx_security_type = 5
        </if>
        <if test=" appType != null and appType == 0">
            and wx_security_type != 5
        </if>
        <if test="version != null and version == 2">
            <if test="keyword != null and keyword != '' and enterpriseIds != null and enterpriseIds.size() > 0">
                and (corp_name like CONCAT('%',#{keyword},'%')
                or wx_enterprise_id IN
                <foreach collection="enterpriseIds" item="id" index="index" open="(" close=")" separator=",">
                    #{id,jdbcType=VARCHAR}
                </foreach>
                )
            </if>
            <if test="keyword != null and keyword != '' and (enterpriseIds == null or enterpriseIds.size() == 0)">
                and corp_name like CONCAT('%',#{keyword},'%')
            </if>
        </if>

        <if test="version == null or version == ''">
            <if test="keyword != null and keyword != ''">
                and corp_name like CONCAT('%',#{keyword},'%')
            </if>
            <if test="enterpriseIdsNotIn != null and enterpriseIdsNotIn.size() > 0">
                and wx_enterprise_id NOT IN
                <foreach collection="enterpriseIdsNotIn" item="id" index="index" open="(" close=")" separator=",">
                    #{id,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="enterpriseIds != null and enterpriseIds.size() > 0">
                and wx_enterprise_id IN
                <foreach collection="enterpriseIds" item="id" index="index" open="(" close=")" separator=",">
                    #{id,jdbcType=VARCHAR}
                </foreach>
            </if>
        </if>
        order by create_time desc
    </select>

    <select id="listAll" resultMap="BaseResultMap">
        select
        a.*
        from tab_haoban_wx_enterprise a
        left join tab_haoban_wx_enterprise_related b on a.wx_enterprise_id = b.wx_enterprise_id
        where a.status_flag = 1 and b.status_flag = 1
        group by a.wx_enterprise_id
    </select>

    <select id="listByIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_enterprise
        where status_flag = 1
        <if test="enterpriseIds != null and enterpriseIds.size() > 0">
            and wx_enterprise_id IN
            <foreach collection="enterpriseIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="getEnterpriseBycorpId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_enterprise
        where  ( wx_corpid = #{corpId} or open_corpid = #{corpId} )
        <if test="statusFlag != null">
            and status_flag = #{statusFlag}
        </if>
        order by create_time desc limit 1
    </select>

    <update id="updateExternalFlagById">
        update tab_haoban_wx_enterprise
        set external_flag = #{externalFlag},
            update_time   = now()
        where wx_enterprise_id = #{wxEnterpriseId}
    </update>


    <update id="updateAutoActiveFlagById">
        update tab_haoban_wx_enterprise
        set auto_active_flag = #{autoActiveFlag},
            update_time   = now()
        where wx_enterprise_id = #{wxEnterpriseId}
    </update>
    
    <update id="updateWxSecurityType">
    	 update tab_haoban_wx_enterprise
        set wx_security_type = #{wxSecurityType},
            update_time   = now()
        where wx_enterprise_id = #{wxEnterpriseId}
    </update>

    <select id="listAllEnterprise" resultType="com.gic.haoban.manage.api.dto.WxEnterpriseDTO" >
        select
        b.wx_enterprise_id wxEnterpriseId,
        b.corp_name corpName,
        a.enterprise_id enterpriseId
        from tab_haoban_wx_enterprise_related a
        left join tab_haoban_wx_enterprise b on a.wx_enterprise_id = b.wx_enterprise_id
        where a.status_flag = 1 and b.status_flag = 1
    </select>

    <select id="queryWxEnterpriseByEnterpriseId" resultType="java.lang.String">
        select
            a.wx_enterprise_id wxEnterpriseId
        from tab_haoban_wx_enterprise_related a
                 inner join tab_haoban_wx_enterprise b on a.wx_enterprise_id = b.wx_enterprise_id
        where a.status_flag = 1 and b.status_flag = 1
        and a.enterprise_id = #{enterpriseId}
    </select>

    <select id="getWxAppidListByList" resultType="java.lang.String">
        select
            wxa_appid
        from  tab_haoban_wx_enterprise
        where
            status_flag = 1
            and wx_security_type = 5
        <if test="wxAppidList != null and wxAppidList.size() > 0">
            and wxa_appid IN
            <foreach collection="wxAppidList" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>
    <select id="listAllByWxSecurityType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_enterprise
        where
            status_flag = 1
        and wx_security_type = #{wxSecurityType}

    </select>

    <update id="updateWxaAppid">
    	 update tab_haoban_wx_enterprise
        set wxa_appid = #{wxaAppid},
            update_time   = now()
        where wx_enterprise_id = #{wxEnterpriseId}
    </update>

    <select id="listWxEnterpriseByCallbackFlag" resultType="java.lang.String" >
        select
            wx_enterprise_id
        from tab_haoban_wx_enterprise
        where
        status_flag = 1
        and callback_flag = #{callbackFlag}
    </select>

</mapper>