<?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.mmc.iuav.user.dao.signin.SignInDao">

    <resultMap id="signInList" type="com.mmc.iuav.user.entity.signin.SignInDO">
        <result property="id" column="id"/>
        <result property="status" column="status"/>
        <result property="integral" column="integral"/>
        <result property="period" column="period"/>
        <collection property="userContinuousCheckInVO" ofType="com.mmc.iuav.user.model.vo.signin.UserContinuousCheckInVO"
        select="continuousCheckIn" column="{signInId=id}">
        </collection>
    </resultMap>

    <select id="continuousCheckIn" resultType="com.mmc.iuav.user.model.vo.signin.UserContinuousCheckInVO">
        select id,sign_in_id,number_of_days,bonus_points from user_continuous_check_in where sign_in_id=#{signInId}
    </select>

    <insert id="SignIn" parameterType="com.mmc.iuav.user.entity.signin.SignInDO" useGeneratedKeys="true" keyProperty="id">
        insert into user_sign_in(status,`period`,integral,create_time,update_time)
        values(#{status},#{period},#{integral},NOW(),NOW())
    </insert>

    <insert id="CheckIn" parameterType="com.mmc.iuav.user.model.vo.signin.UserContinuousCheckInVO" useGeneratedKeys="true" keyProperty="id">
        insert into user_continuous_check_in(sign_in_id,number_of_days,bonus_points,create_time,update_time)
        values(#{signInId},#{numberOfDays},#{bonusPoints},NOW(),NOW())
    </insert>
    <update id="update" parameterType="com.mmc.iuav.user.entity.signin.SignInDO">
        update user_sign_in
        set
        <if test="status!=null">
             status=#{status},
        </if>
        <if test="period!=null">
            `period`=#{period},
        </if>
        <if test="integral!=null">
            integral=#{integral}
        </if>
        where id=#{id}
    </update>
    <delete id="remove">
        DELETE from user_continuous_check_in where sign_in_id=#{id}
    </delete>

    <select id="SignInList" resultMap="signInList">
        select id,status,`integral`,`period` from user_sign_in
        ORDER BY id desc
    </select>

    <select id="selectUserAppSignIn" parameterType="com.mmc.iuav.user.model.vo.signin.AppSignInVO"
    resultType="com.mmc.iuav.user.entity.signin.UserAppSignInDO">
        select sign_time,sign_in_days ,user_account_id from user_app_sign_in where user_account_id=#{userAccountId}
    </select>

    <insert id="userAppSignIn" parameterType="com.mmc.iuav.user.model.vo.signin.AppSignInVO">
        insert into user_app_sign_in(sign_time,sign_in_days,user_account_id,create_time,update_time)
        values (#{signTime},sign_in_days+1,#{userAccountId},NOW(),NOW())
    </insert>

    <update id="updateUserAppSignIn" parameterType="com.mmc.iuav.user.model.vo.signin.AppSignInVO">
        update user_app_sign_in set
            sign_time=#{signTime},
            sign_in_days=sign_in_days+1,
            update_time=NOW()
            where user_account_id=#{userAccountId}
    </update>

    <delete id="deleteSignInRecord" parameterType="com.mmc.iuav.user.model.vo.signin.AppSignInVO">
        delete from sign_in_record where user_account_id=#{userAccountId}
    </delete>

    <insert id="insertSignInRecord" parameterType="com.mmc.iuav.user.model.vo.signin.AppSignInVO">
        insert into sign_in_record(user_account_id,sign_in_time,create_time,update_time)
         values(#{userAccountId},#{signTime},NOW(),NOW())
    </insert>

    <update id="updateUserAppSignInD" parameterType="com.mmc.iuav.user.model.vo.signin.AppSignInVO">
        update user_app_sign_in set
                                    sign_time=#{signTime},
                                    sign_in_days=1,
                                    update_time=NOW()
        where user_account_id=#{userAccountId}
    </update>

    <select id="signInRecord" resultType="com.mmc.iuav.user.entity.signin.SignInRecordDO">
        select user_account_id,sign_in_time from sign_in_record where user_account_id=#{userAccountId}
    </select>


</mapper>
