<?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.cultivate.dao.impl.CultivateRaysDaoImpl">

    <resultMap type="com.pcloud.book.cultivate.entity.CultivateRays" id="CultivateRaysMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="officialAccountsId" column="official_accounts_id" jdbcType="INTEGER"/>
        <result property="gains1" column="gains1" jdbcType="INTEGER"/>
        <result property="gains2" column="gains2" jdbcType="INTEGER"/>
        <result property="gains3" column="gains3" jdbcType="INTEGER"/>
        <result property="state" column="state" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List" >
        id, wechat_user_id, official_accounts_id, gains1, gains2, gains3, state, end_time
    </sql>

    <!--查询单个-->
    <select id="getById" resultMap="CultivateRaysMap">
        select
          <include refid="Base_Column_List"/>
        from book.cultivate_rays
        where id = #{id}
    </select>


    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.cultivate_rays(wechat_user_id, official_accounts_id, gains1, gains2, gains3, state, create_time, update_time, end_time)
        values (#{wechatUserId}, #{officialAccountsId}, 0, 0, 0, #{state}, now(), now(), now())
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.cultivate_rays
        <set>
            <if test="gains1 != null">
                gains1 = #{gains1},
            </if>
            <if test="gains2 != null">
                gains2 = #{gains2},
            </if>
            <if test="gains3 != null">
                gains3 = #{gains3},
            </if>
            <if test="state != null">
                state = #{state},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.cultivate_rays where id = #{id}
    </delete>

    <select id="getByWechatUserId" resultMap="CultivateRaysMap" parameterType="long">
        select
        <include refid="Base_Column_List"/>
        from book.cultivate_rays
        where wechat_user_id = #{wechatUserId}
        limit 1
    </select>
</mapper>