<?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.WxApplicationMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanWxApplication">
        <id column="wx_application_id" property="wxApplicationId" jdbcType="VARCHAR"/>
        <result column="corpid" property="corpid" jdbcType="VARCHAR"/>
        <result column="site_id" property="siteId" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="permanent_code" property="permanentCode" jdbcType="VARCHAR"/>
        <result column="agent_name" property="agentName" jdbcType="VARCHAR"/>
        <result column="agent_id" property="agentId" jdbcType="VARCHAR"/>
        <result column="application_type" property="applicationType" jdbcType="INTEGER"/>
        <result column="is_customized_app" property="isCustomizedApp" jdbcType="INTEGER"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    <sql id="Base_Column_List">
        wx_application_id,corpid, site_id, name, permanent_code, agent_name, agent_id, application_type,
    wx_enterprise_id, status_flag, create_time, update_time,is_customized_app
    </sql>

   <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanWxApplication">
        insert into tab_haoban_wx_application
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wxApplicationId != null">
                wx_application_id,
            </if>
            <if test="corpid != null">
                corpid,
            </if>
            <if test="siteId != null">
                site_id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="permanentCode != null">
                permanent_code,
            </if>
            <if test="agentName != null">
                agent_name,
            </if>
            <if test="agentId != null">
                agent_id,
            </if>
            <if test="applicationType != null">
                application_type,
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id,
            </if>
            <if test="statusFlag != null">
                status_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="isCustomizedApp != null">
                is_customized_app,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wxApplicationId != null">
                #{wxApplicationId,jdbcType=VARCHAR},
            </if>
            <if test="corpid != null">
                #{corpid,jdbcType=VARCHAR},
            </if>
            <if test="siteId != null">
                #{siteId,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="permanentCode != null">
                #{permanentCode,jdbcType=VARCHAR},
            </if>
            <if test="agentName != null">
                #{agentName,jdbcType=VARCHAR},
            </if>
            <if test="agentId != null">
                #{agentId,jdbcType=VARCHAR},
            </if>
            <if test="applicationType != null">
                #{applicationType,jdbcType=INTEGER},
            </if>
            <if test="wxEnterpriseId != null">
                #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="isCustomizedApp != null">
                #{isCustomizedApp,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanWxApplication">
        update tab_haoban_wx_application
        <set>
            <if test="corpid != null">
                corpid = #{corpid,jdbcType=VARCHAR},
            </if>
            <if test="siteId != null">
                site_id = #{siteId,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="permanentCode != null">
                permanent_code = #{permanentCode,jdbcType=VARCHAR},
            </if>
            <if test="agentName != null">
                agent_name = #{agentName,jdbcType=VARCHAR},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId,jdbcType=VARCHAR},
            </if>
            <if test="applicationType != null">
                application_type = #{applicationType,jdbcType=INTEGER},
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,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="isCustomizedApp != null">
                is_customized_app = #{isCustomizedApp,jdbcType=INTEGER},
            </if>
        </set>
        where wx_application_id = #{wxApplicationId,jdbcType=VARCHAR}
    </update>

    <select id="selectBySiteIdAndWxEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_application
        where site_id = #{siteId,jdbcType=VARCHAR}
        and status_flag = 1
        and wx_enterprise_id = #{wxEnterpriseId}
    </select>

    <select id="selectByWxEnterpriseIdAndApplicationType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_application
        where application_type = #{applicationType}
        and status_flag = 1
        and wx_enterprise_id = #{wxEnterpriseId}
        limit 1
    </select>

    <update id="cancelWxApplication">
        update tab_haoban_wx_application
        set status_flag = 0,
            update_time = now()
        where corpid = #{corpId}
          and site_id = #{suiteId}
          and status_flag = 1
    </update>


    <select id="selectByCorpId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_application
        where corpid = #{corpid,jdbcType=VARCHAR} and status_flag = 1
        <if test="applicationType != null ">
            and application_type = #{applicationType}
        </if>
        limit 1
    </select>

    <update id="updateCorpidById">
        update tab_haoban_wx_application
        set corpid = #{corpid},
            update_time = now()
        where wx_application_id = #{wxApplicationId}
          and status_flag = 1
    </update>
    
    <select id="listApplication" resultMap="BaseResultMap">
		select  <include refid="Base_Column_List"/> from 
		   tab_haoban_wx_application where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1 
    </select>

</mapper>