<?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.reptileclient.jlifeapp.dao.jz.CustomerClientMapper">
    <resultMap id="BaseResultMap" type="com.reptileclient.jlifeapp.entity.jz.CustomerClient">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="customer_no" property="customerNo" jdbcType="VARCHAR"/>
        <result column="face" property="face" jdbcType="BIGINT"/>
        <result column="nickname" property="nickname" jdbcType="VARCHAR"/>
        <result column="tel" property="tel" jdbcType="VARCHAR"/>
        <result column="customer_account" property="customerAccount" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="weixin_uinonid" property="weixinUinonid" jdbcType="VARCHAR"/>
        <result column="weixin_openid" property="weixinOpenid" jdbcType="VARCHAR"/>
        <result column="state" property="state" jdbcType="INTEGER"/>
        <result column="customer_name" property="customerName" jdbcType="VARCHAR"/>
        <result column="id_type" property="idType" jdbcType="VARCHAR"/>
        <result column="customer_idcard" property="customerIdcard" jdbcType="VARCHAR"/>
        <result column="idcard_expire" property="idcardExpire" jdbcType="VARCHAR"/>
        <result column="gender" property="gender" jdbcType="INTEGER"/>
        <result column="city" property="city" jdbcType="BIGINT"/>
        <result column="area" property="area" jdbcType="VARCHAR"/>
        <result column="customer_addr" property="customerAddr" jdbcType="VARCHAR"/>
        <result column="idcard_front_url" property="idcardFrontUrl" jdbcType="BIGINT"/>
        <result column="idcard_back_url" property="idcardBackUrl" jdbcType="BIGINT"/>
        <result column="customer_level" property="customerLevel" jdbcType="VARCHAR"/>
        <result column="source" property="source" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="create_id" property="createId" jdbcType="BIGINT"/>
        <result column="create_user" property="createUser" jdbcType="VARCHAR"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="oper_id" property="operId" jdbcType="BIGINT"/>
        <result column="last_login_time" property="lastLoginTime" jdbcType="TIMESTAMP"/>
        <result column="oper_name" property="operName" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="channel_id" property="channelId" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
        id
        , customer_no, face, nickname, tel, customer_account, password, weixin_uinonid,
    weixin_openid, state, customer_name, id_type, customer_idcard, idcard_expire, gender, 
    city, area, customer_addr, idcard_front_url, idcard_back_url, customer_level, source, 
    create_time, create_id, create_user, update_time, oper_id, last_login_time, oper_name, 
    remark
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from customer_client
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from customer_client
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" useGeneratedKeys="true" keyProperty="id"
            parameterType="com.reptileclient.jlifeapp.entity.jz.CustomerClient">
        insert into customer_client (id, customer_no, face,
                                     nickname, tel, customer_account,
                                     password, weixin_uinonid, weixin_openid,
                                     state, customer_name, id_type,
                                     customer_idcard, idcard_expire, gender,
                                     city, area, customer_addr,
                                     idcard_front_url, idcard_back_url, customer_level,
                                     source, create_time, create_id,
                                     create_user, update_time, oper_id,
                                     last_login_time, oper_name, remark)
        values (#{id,jdbcType=BIGINT}, #{customerNo,jdbcType=VARCHAR}, #{face,jdbcType=BIGINT},
                #{nickname,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{customerAccount,jdbcType=VARCHAR},
                #{password,jdbcType=VARCHAR}, #{weixinUinonid,jdbcType=VARCHAR}, #{weixinOpenid,jdbcType=VARCHAR},
                #{state,jdbcType=INTEGER}, #{customerName,jdbcType=VARCHAR}, #{idType,jdbcType=VARCHAR},
                #{customerIdcard,jdbcType=VARCHAR}, #{idcardExpire,jdbcType=VARCHAR}, #{gender,jdbcType=INTEGER},
                #{city,jdbcType=BIGINT}, #{area,jdbcType=VARCHAR}, #{customerAddr,jdbcType=VARCHAR},
                #{idcardFrontUrl,jdbcType=BIGINT}, #{idcardBackUrl,jdbcType=BIGINT}, #{customerLevel,jdbcType=VARCHAR},
                #{source,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{createId,jdbcType=BIGINT},
                #{createUser,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, #{operId,jdbcType=BIGINT},
                #{lastLoginTime,jdbcType=TIMESTAMP}, #{operName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id"
            parameterType="com.reptileclient.jlifeapp.entity.jz.CustomerClient">
        insert into customer_client
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="customerNo != null">
                customer_no,
            </if>
            <if test="face != null">
                face,
            </if>
            <if test="nickname != null">
                nickname,
            </if>
            <if test="tel != null">
                tel,
            </if>
            <if test="customerAccount != null">
                customer_account,
            </if>
            <if test="password != null">
                password,
            </if>
            <if test="weixinUinonid != null">
                weixin_uinonid,
            </if>
            <if test="weixinOpenid != null">
                weixin_openid,
            </if>
            <if test="state != null">
                state,
            </if>
            <if test="customerName != null">
                customer_name,
            </if>
            <if test="idType != null">
                id_type,
            </if>
            <if test="customerIdcard != null">
                customer_idcard,
            </if>
            <if test="idcardExpire != null">
                idcard_expire,
            </if>
            <if test="gender != null">
                gender,
            </if>
            <if test="city != null">
                city,
            </if>
            <if test="area != null">
                area,
            </if>
            <if test="customerAddr != null">
                customer_addr,
            </if>
            <if test="idcardFrontUrl != null">
                idcard_front_url,
            </if>
            <if test="idcardBackUrl != null">
                idcard_back_url,
            </if>
            <if test="customerLevel != null">
                customer_level,
            </if>
            <if test="source != null">
                source,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="createId != null">
                create_id,
            </if>
            <if test="createUser != null">
                create_user,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="operId != null">
                oper_id,
            </if>
            <if test="lastLoginTime != null">
                last_login_time,
            </if>
            <if test="operName != null">
                oper_name,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="channelId != null">
                channel_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="customerNo != null">
                #{customerNo,jdbcType=VARCHAR},
            </if>
            <if test="face != null">
                #{face,jdbcType=BIGINT},
            </if>
            <if test="nickname != null">
                #{nickname,jdbcType=VARCHAR},
            </if>
            <if test="tel != null">
                #{tel,jdbcType=VARCHAR},
            </if>
            <if test="customerAccount != null">
                #{customerAccount,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                #{password,jdbcType=VARCHAR},
            </if>
            <if test="weixinUinonid != null">
                #{weixinUinonid,jdbcType=VARCHAR},
            </if>
            <if test="weixinOpenid != null">
                #{weixinOpenid,jdbcType=VARCHAR},
            </if>
            <if test="state != null">
                #{state,jdbcType=INTEGER},
            </if>
            <if test="customerName != null">
                #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="idType != null">
                #{idType,jdbcType=VARCHAR},
            </if>
            <if test="customerIdcard != null">
                #{customerIdcard,jdbcType=VARCHAR},
            </if>
            <if test="idcardExpire != null">
                #{idcardExpire,jdbcType=VARCHAR},
            </if>
            <if test="gender != null">
                #{gender,jdbcType=INTEGER},
            </if>
            <if test="city != null">
                #{city,jdbcType=BIGINT},
            </if>
            <if test="area != null">
                #{area,jdbcType=VARCHAR},
            </if>
            <if test="customerAddr != null">
                #{customerAddr,jdbcType=VARCHAR},
            </if>
            <if test="idcardFrontUrl != null">
                #{idcardFrontUrl,jdbcType=BIGINT},
            </if>
            <if test="idcardBackUrl != null">
                #{idcardBackUrl,jdbcType=BIGINT},
            </if>
            <if test="customerLevel != null">
                #{customerLevel,jdbcType=VARCHAR},
            </if>
            <if test="source != null">
                #{source,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createId != null">
                #{createId,jdbcType=BIGINT},
            </if>
            <if test="createUser != null">
                #{createUser,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="operId != null">
                #{operId,jdbcType=BIGINT},
            </if>
            <if test="lastLoginTime != null">
                #{lastLoginTime,jdbcType=TIMESTAMP},
            </if>
            <if test="operName != null">
                #{operName,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="channelId != null">
                #{channelId,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <insert id="insertClientByBatch">
        INSERT INTO customer_client (tel, source, customer_no)
        <foreach collection="list" item="item" index="index" open="(" close=")" separator="union all">
            SELECT
            #{item.phone} as a,
            'hema' as b,
            customer_seq () as c
            FROM
            DUAL
            WHERE NOT EXISTS
            (SELECT
            tel
            FROM
            customer_client
            WHERE tel = #{item.phone})
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.reptileclient.jlifeapp.entity.jz.CustomerClient">
        update customer_client
        <set>
            <if test="customerNo != null">
                customer_no = #{customerNo,jdbcType=VARCHAR},
            </if>
            <if test="face != null">
                face = #{face,jdbcType=BIGINT},
            </if>
            <if test="nickname != null">
                nickname = #{nickname,jdbcType=VARCHAR},
            </if>
            <if test="tel != null">
                tel = #{tel,jdbcType=VARCHAR},
            </if>
            <if test="customerAccount != null">
                customer_account = #{customerAccount,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="weixinUinonid != null">
                weixin_uinonid = #{weixinUinonid,jdbcType=VARCHAR},
            </if>
            <if test="weixinOpenid != null">
                weixin_openid = #{weixinOpenid,jdbcType=VARCHAR},
            </if>
            <if test="state != null">
                state = #{state,jdbcType=INTEGER},
            </if>
            <if test="customerName != null">
                customer_name = #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="idType != null">
                id_type = #{idType,jdbcType=VARCHAR},
            </if>
            <if test="customerIdcard != null">
                customer_idcard = #{customerIdcard,jdbcType=VARCHAR},
            </if>
            <if test="idcardExpire != null">
                idcard_expire = #{idcardExpire,jdbcType=VARCHAR},
            </if>
            <if test="gender != null">
                gender = #{gender,jdbcType=INTEGER},
            </if>
            <if test="city != null">
                city = #{city,jdbcType=BIGINT},
            </if>
            <if test="area != null">
                area = #{area,jdbcType=VARCHAR},
            </if>
            <if test="customerAddr != null">
                customer_addr = #{customerAddr,jdbcType=VARCHAR},
            </if>
            <if test="idcardFrontUrl != null">
                idcard_front_url = #{idcardFrontUrl,jdbcType=BIGINT},
            </if>
            <if test="idcardBackUrl != null">
                idcard_back_url = #{idcardBackUrl,jdbcType=BIGINT},
            </if>
            <if test="customerLevel != null">
                customer_level = #{customerLevel,jdbcType=VARCHAR},
            </if>
            <if test="source != null">
                source = #{source,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createId != null">
                create_id = #{createId,jdbcType=BIGINT},
            </if>
            <if test="createUser != null">
                create_user = #{createUser,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="operId != null">
                oper_id = #{operId,jdbcType=BIGINT},
            </if>
            <if test="lastLoginTime != null">
                last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP},
            </if>
            <if test="operName != null">
                oper_name = #{operName,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.reptileclient.jlifeapp.entity.jz.CustomerClient">
        update customer_client
        set customer_no      = #{customerNo,jdbcType=VARCHAR},
            face             = #{face,jdbcType=BIGINT},
            nickname         = #{nickname,jdbcType=VARCHAR},
            tel              = #{tel,jdbcType=VARCHAR},
            customer_account = #{customerAccount,jdbcType=VARCHAR},
            password         = #{password,jdbcType=VARCHAR},
            weixin_uinonid   = #{weixinUinonid,jdbcType=VARCHAR},
            weixin_openid    = #{weixinOpenid,jdbcType=VARCHAR},
            state            = #{state,jdbcType=INTEGER},
            customer_name    = #{customerName,jdbcType=VARCHAR},
            id_type          = #{idType,jdbcType=VARCHAR},
            customer_idcard  = #{customerIdcard,jdbcType=VARCHAR},
            idcard_expire    = #{idcardExpire,jdbcType=VARCHAR},
            gender           = #{gender,jdbcType=INTEGER},
            city             = #{city,jdbcType=BIGINT},
            area             = #{area,jdbcType=VARCHAR},
            customer_addr    = #{customerAddr,jdbcType=VARCHAR},
            idcard_front_url = #{idcardFrontUrl,jdbcType=BIGINT},
            idcard_back_url  = #{idcardBackUrl,jdbcType=BIGINT},
            customer_level   = #{customerLevel,jdbcType=VARCHAR},
            source           = #{source,jdbcType=VARCHAR},
            create_time      = #{createTime,jdbcType=TIMESTAMP},
            create_id        = #{createId,jdbcType=BIGINT},
            create_user      = #{createUser,jdbcType=VARCHAR},
            update_time      = #{updateTime,jdbcType=TIMESTAMP},
            oper_id          = #{operId,jdbcType=BIGINT},
            last_login_time  = #{lastLoginTime,jdbcType=TIMESTAMP},
            oper_name        = #{operName,jdbcType=VARCHAR},
            remark           = #{remark,jdbcType=VARCHAR}
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="selectCustomerNo" resultType="java.lang.String">
        select customer_seq()
    </select>

    <select id="selectByTel" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from customer_client
        where tel = #{tel}
        order by id desc limit 1
    </select>
    <select id="selectCountByTel" resultType="java.lang.Long">
        select count(*)
        from customer_client
        where tel = #{tel}
    </select>
    <select id="selectByWeixinOpenid" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from customer_client
        where weixin_openid = #{openId}
    </select>
    <select id="selectCustomer" resultType="java.util.Map">
        SELECT
        c.id,
        c.customer_name AS customerName,
        (select city_name from sys_city where id = c.city) as cityName,
        c.`tel`,
        c.customer_no as customerNo,
        c.customer_idcard AS customerIdcard,
        c.idcard_expire as idcardExpire,
        (select file_url from file_storage where id = c.idcard_front_url) as idcardFrontUrl,
        (select file_url from file_storage where id = c.idcard_back_url) as idcardBackUrl,
        c.gender,
        c.city,
        c.area,
        c.customer_addr AS customerAddr,
        c.customer_level AS customerLevel,
        c.create_time AS createTime,
        c.create_id AS createId,
        c.create_user AS createUser,
        c.`customer_account` AS customerAccount,
        c.remark
        FROM
        `customer_client` c
        where 1 = 1
        <if test="customerName != null and customerName != ''">
            and c.customer_name like concat('%',#{customerName},'%')
        </if>
        <if test="tel != null and tel != ''">
            and c.`tel` = #{tel}
        </if>
    </select>

</mapper>