<?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.xhpc.user.mapper.XhpcAppUserMapper">

    <resultMap type="com.xhpc.user.domain.XhpcAppUser" id="XhpcAppUserResult">
        <result column="app_user_id" property="appUserId"/>
        <result column="phone" property="phone"/>
        <result column="weixin_open_id" property="weixinOpenId"/>
        <result column="alipay_open_id" property="alipayOpenId"/>
        <result column="weixin_login" property="weixinLogin"/>
        <result column="alipay_login" property="alipayLogin"/>
        <result column="avatar" property="avatar"/>
        <result column="balance" property="balance"/>
        <result column="password" property="password"/>
        <result column="status" property="status"/>
        <result column="is_refund_application" property="isRefundApplication"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="remark" property="remark"/>
        <result column="soc" property="soc"/>
    </resultMap>

    <insert id="insert" parameterType="com.xhpc.user.domain.XhpcAppUser" useGeneratedKeys="true"
            keyProperty="appUserId">
        INSERT INTO xhpc_app_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != appUserId and '' != appUserId">
                app_user_id,
            </if>
            <if test="null != phone and '' != phone">
                phone,
            </if>
            <if test="null != weixinOpenId and '' != weixinOpenId">
                weixin_open_id,
            </if>
            <if test="null != alipayOpenId and '' != alipayOpenId">
                alipay_open_id,
            </if>
            <if test="null != weixinLogin and '' != weixinLogin">
                weixin_login,
            </if>
            <if test="null != alipayLogin and '' != alipayLogin">
                alipay_login,
            </if>
            <if test="null != avatar and '' != avatar">
                avatar,
            </if>
            <if test="null != balance and '' != balance">
                balance,
            </if>
            <if test="null != password and '' != password">
                password,
            </if>
            <if test="null != status and '' != status">
                status,
            </if>
            <if test="null != isRefundApplication and '' != isRefundApplication">
                is_refund_application,
            </if>
            <if test="null != delFlag and '' != delFlag">
                del_flag,
            </if>
            <if test="null != createBy and '' != createBy">
                create_by,
            </if>
            <if test="null != createTime ">
                create_time,
            </if>
            <if test="null != updateBy and '' != updateBy">
                update_by,
            </if>
            <if test="null != updateTime ">
                update_time,
            </if>
            <if test="null != remark and '' != remark">
                remark,
            </if>
            <if test="null != soc and '' != soc">
                soc
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != appUserId and '' != appUserId">
                #{appUserId},
            </if>
            <if test="null != phone and '' != phone">
                #{phone},
            </if>
            <if test="null != weixinOpenId and '' != weixinOpenId">
                #{weixinOpenId},
            </if>
            <if test="null != alipayOpenId and '' != alipayOpenId">
                #{alipayOpenId},
            </if>
            <if test="null != weixinLogin and '' != weixinLogin">
                #{weixinLogin},
            </if>
            <if test="null != alipayLogin and '' != alipayLogin">
                #{alipayLogin},
            </if>
            <if test="null != avatar and '' != avatar">
                #{avatar},
            </if>
            <if test="null != balance and '' != balance">
                #{balance},
            </if>
            <if test="null != password and '' != password">
                #{password},
            </if>
            <if test="null != status and '' != status">
                #{status},
            </if>
            <if test="null != isRefundApplication and '' != isRefundApplication">
                #{isRefundApplication},
            </if>
            <if test="null != delFlag and '' != delFlag">
                #{delFlag},
            </if>
            <if test="null != createBy and '' != createBy">
                #{createBy},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </if>
            <if test="null != updateBy and '' != updateBy">
                #{updateBy},
            </if>
            <if test="null != updateTime ">
                #{updateTime},
            </if>
            <if test="null != remark and '' != remark">
                #{remark},
            </if>
            <if test="null != soc and '' != soc">
                #{soc}
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.xhpc.user.domain.XhpcInternetUser">
        UPDATE xhpc_app_user
        <set>
            <if test="null != phone and '' != phone">phone = #{phone},</if>
            <if test="null != weixinOpenId and '' != weixinOpenId">weixin_open_id = #{weixinOpenId},</if>
            <if test="null != alipayOpenId and '' != alipayOpenId">alipay_open_id = #{alipayOpenId},</if>
            <if test="null != weixinLogin ">weixin_login = #{weixinLogin},</if>
            <if test="null != alipayLogin ">alipay_login = #{alipayLogin},</if>
            <if test="null != avatar and '' != avatar">avatar = #{avatar},</if>
            <if test="null != balance ">balance = #{balance},</if>
            <if test="null != password and '' != password">password = #{password},</if>
            <if test="null != status">status = #{status},</if>
            <if test="null != isRefundApplication">is_refund_application = #{isRefundApplication},</if>
            <if test="null != delFlag and '' != delFlag">del_flag = #{delFlag},</if>
            <if test="null != createBy and '' != createBy">create_by = #{createBy},</if>
            <if test="null != createTime ">create_time = #{createTime},</if>
            <if test="null != updateBy and '' != updateBy">update_by = #{updateBy},</if>
            <if test="null != updateTime ">update_time = #{updateTime},</if>
            <if test="null != remark and '' != remark">remark = #{remark},</if>
            <if test="null != soc and '' != soc">soc = #{soc}</if>
        </set>
        WHERE app_user_id = #{appUserId}
    </update>

    <delete id="deleteById" parameterType="java.lang.Long">
        update  xhpc_app_user set del_flag = 2 where app_user_id = #{appUserId}
    </delete>

    <select id="info" parameterType="java.lang.Long" resultType="java.util.Map">
        select  app_user_id appUserId, phone, weixin_open_id weixinOpenId,
        weixin_login weixinlogin,alipay_login alipayLogin,is_refund_application isRefundApplication,
        alipay_open_id alipayOpenId, avatar, balance, password,
        `status`,create_by createBy ,create_time createTime,
        update_time updateTime, update_by updateBy,
         del_flag delflag, remark,soc,
        CASE WHEN `status` = 0 THEN '正常' else  '禁用'  end statusName
        from  xhpc_app_user
        WHERE del_flag = 0 and app_user_id = #{appUserId}
    </select>

    <select id="selectAppUserList" parameterType="java.lang.Long" resultType="java.util.Map">
        select xau.app_user_id appUserId, xau.phone, xau.balance,soc,
        xau.`status`,xau.create_time createTime,xau.is_refund_application isRefundApplication,
        CASE WHEN xau.`status` = 0 THEN '正常' else '禁用' end statusName,
        CASE WHEN a.count > 0 THEN '老用户' else '新用户' end newUser
        from xhpc_app_user xau
        LEFT JOIN (select count(1) count ,user_id from xhpc_charge_order where del_flag = 0 ) a on a.user_id =
        xau.app_user_id
        WHERE xau.del_flag = 0
        <if test="phone != null and phone != ''">
            and xau.phone like concat(concat('%', #{phone}), '%')
        </if>
        ORDER BY xau.create_time DESC
    </select>

    <select id="userSum" resultType="map">
        select
          count(xau.app_user_id) userSum,
        (select count(DISTINCT user_id) from xhpc_charge_order where datediff(now(),create_time)>30) userVitality
        from xhpc_app_user xau
        LEFT JOIN (select count(1) count ,user_id from xhpc_charge_order where del_flag = 0 ) a on a.user_id =
        xau.app_user_id
        WHERE xau.del_flag = 0
        <if test="phone != null and phone != ''">
            and xau.phone like concat(concat('%', #{phone}), '%')
        </if>
        ORDER BY xau.create_time DESC
    </select>



    <select id="getAppUserByPhone" parameterType="java.lang.String" resultMap="XhpcAppUserResult">
        select *
        from xhpc_app_user
        WHERE del_flag = 0  and phone = #{phone} LIMIT 1
    </select>

    <select id="getAppUserByOpenid" parameterType="java.lang.String" resultMap="XhpcAppUserResult">
        select *
        from xhpc_app_user
        WHERE del_flag = 0  and (weixin_open_id = #{openid}or alipay_open_id = #{openid}) LIMIT 1
    </select>
</mapper> 