<?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.iim.dao.MyCalendarDao">

	<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",
		a.location AS "location",
		a.noticetime AS "notice",
		tuser.name AS "user.name",
		a.company_id AS "companyId",
		a.office_id AS "officeId"
		<!--新加location字段和notice字段 -->
	</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 calendar a
		<include refid="myCalendarJoins" />
		WHERE a.id = #{id}
	</select>

    <select id="getByUserIdCount" resultType="MyCalendar">
        SELECT
        a.userid AS "user.id",
        count(*) AS "count"
        FROM calendar a
        <include refid="myCalendarJoins" />
        WHERE 1=1
        <if test="start != null  and start != ''">
        AND	str_to_date(a.starttime,'%Y-%m-%d') = str_to_date(#{start},'%Y-%m-%d')
        or ( str_to_date(a.starttime,'%Y-%m-%d') &lt; str_to_date( #{start},'%Y-%m-%d')
        and str_to_date(a.endtime,'%Y-%m-%d') &gt;= str_to_date( #{start},'%Y-%m-%d'))
        </if>
        GROUP BY a.userid
    </select>

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

			<if test="user != null and user.id != null and user.id != ''">
				AND a.userid = #{user.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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="findListByDate" resultType="MyCalendar">
		SELECT
		<include refid="myCalendarColumns" />
		FROM calendar a
		<include refid="myCalendarJoins" />
		<where>

			<if test="user != null and user.id != null and user.id != ''">
				AND a.userid = #{user.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="start != null  and start != '' and end != null  and end != ''">
				AND a.starttime &lt;= #{start}
				AND a.endtime &gt;= #{end}
			</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="findListByStart" resultType="MyCalendar">
		SELECT
		<include refid="myCalendarColumns" />
		FROM calendar a
		<include refid="myCalendarJoins" />
		<where>

			<if test="user != null and user.id != null and user.id != ''">
				AND a.userid = #{user.id}
			</if>

			<if test="start != null  and start != ''">
				AND a.starttime like CONCAT(#{start},'%')
			</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 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 calendar(
		id,
		title,
		starttime,
		endtime,
		allday,
		color,
		userid,
		location,
		company_id,
		office_id,
		noticetime
		) VALUES (
		#{id},
		#{title},
		#{start},
		#{end},
		#{adllDay},
		#{color},
		#{user.id},
		#{location},
		#{companyId},
		#{officeId},
		#{notice}
		)
	</insert>

	<update id="update">
		UPDATE calendar SET
		title = #{title},
		starttime = #{start},
		endtime = #{end},
		allday = #{adllDay},
		color = #{color},
		userid = #{user.id},
		location = #{location},
		company_id = #{companyId},
		office_id = #{officeId},
		noticetime = #{notice}
		WHERE id = #{id}
	</update>


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

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


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

</mapper>