<?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.jeeplus.modules.calendar.mapper.MyCalendarMapper">

    <sql id="myCalendarColumns">
        a.id AS "id",
        a.title AS "title",
        a.starttime AS "start",
        a.endtime AS "end",
        a.allday AS "adllDay",
        a.color AS "color",
        a.userid AS "user.id",
        tuser.name AS "user.name"
    </sql>

    <sql id="myCalendarJoins">
        LEFT JOIN sys_user tuser ON tuser.id = a.userid
    </sql>

    <select id="get" resultType="MyCalendar">
        SELECT
        <include refid="myCalendarColumns"/>
        FROM plugin_calendar a
        <include refid="myCalendarJoins"/>
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="MyCalendar">
        SELECT
        <include refid="myCalendarColumns"/>
        FROM plugin_calendar a
        <include refid="myCalendarJoins"/>
        <where>

            <if test="user != null and user.id != null and user.id != ''">
                AND a.userid = #{user.id}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
            </otherwise>
        </choose>
    </select>

    <select id="findAllList" resultType="MyCalendar">
        SELECT
        <include refid="myCalendarColumns"/>
        FROM plugin_calendar a
        <include refid="myCalendarJoins"/>
        <where>

        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
            </otherwise>
        </choose>
    </select>

    <insert id="insert">
        INSERT INTO plugin_calendar(
        id,
        title,
        starttime,
        endtime,
        allday,
        color,
        userid
        ) VALUES (
        #{id},
        #{title},
        #{start},
        #{end},
        #{adllDay},
        #{color},
        #{user.id}
        )
    </insert>

    <update id="update">
        UPDATE plugin_calendar SET
        title = #{title},
        starttime = #{start},
        endtime = #{end},
        allday = #{adllDay},
        color = #{color},
        userid = #{user.id}
        WHERE id = #{id}
    </update>


    <!--物理删除-->
    <update id="delete">
        DELETE FROM plugin_calendar
        WHERE id = #{id}
    </update>

    <!--逻辑删除-->
    <update id="deleteByLogic">
        UPDATE plugin_calendar SET
        del_flag = #{DEL_FLAG_DELETE}
        WHERE id = #{id}
    </update>


    <!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
    <select id="findUniqueByProperty" resultType="MyCalendar" statementType="STATEMENT">
        select * FROM plugin_calendar where ${propertyName} = '${value}'
    </select>
</mapper>