<?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.pcloud.book.weixinclock.dao.impl.WeixinClockUserCouponDaoImpl" >
    <resultMap id="BaseResultMap" type="com.pcloud.book.weixinclock.entity.WeixinClockUserCoupon">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="weixin_clock_id" property="weixinClockId" jdbcType="BIGINT" />
        <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
        <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR" />
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
        <result column="clock_coupon_id" property="clockCouponId" jdbcType="BIGINT" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, weixin_clock_id, wx_user_id, wx_group_id, wechat_user_id, clock_coupon_id, create_time, update_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.weixinclock.entity.WeixinClockUserCoupon" useGeneratedKeys="true" keyProperty="id">
        insert into weixin_clock_user_coupon (weixin_clock_id, wx_user_id, wx_group_id, wechat_user_id, clock_coupon_id, create_time, update_time)
        values (#{weixinClockId}, #{wxUserId}, #{wxGroupId}, #{wechatUserId}, #{clockCouponId}, now(), now())
    </insert>

    <update id="update" parameterType="com.pcloud.book.weixinclock.entity.WeixinClockUserCoupon">
        update weixin_clock_user_coupon
        <set>
            <if test="weixinClockId != null">
                weixin_clock_id = #{weixinClockId},
            </if>
            <if test="wxUserId != null">
                wx_user_id = #{wxUserId},
            </if>
            <if test="wxGroupId">
                wx_group_id = #{wxGroupId},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="clockCouponId != null">
                clock_coupon_id = #{clockCouponId},
            </if>
            update_time = now(),
        </set>
        where id = #{id}
    </update>

    <select id="getByGroupAndUser" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from weixin_clock_user_coupon
        where wx_group_id = #{wxGroupId}
        and wechat_user_id = #{wechatUserId}
    </select>

    <update id="updateWechatUserId" parameterType="map">
        update weixin_clock_user_coupon set wechat_user_id = #{wechatUserId}
        where wx_user_id = #{wxUserId} and wechat_user_id is null
    </update>

    <select id="getNoWechatUserId" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from weixin_clock_user_coupon where wechat_user_id is null
    </select>
</mapper>