<?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.workcalendar.dao.WorkCalendarDao">
    
	<sql id="workCalendarColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.subject AS "subject",
		a.address AS "address",
		a.content AS "content",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.repeat_type AS "repeatType",
		a.repeat_date AS "repeatDate",
		a.remind AS "remind",
		a.remind_time AS "remindTime",
		a.send_daily AS "sendDaily",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		u.name AS "createBy.name"
	</sql>
	
	<sql id="workCalendarJoins">
        LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="WorkCalendar" >
		SELECT 
			<include refid="workCalendarColumns"/>
		FROM work_calendar a
		<include refid="workCalendarJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkCalendar" >
		SELECT DISTINCT
			<include refid="workCalendarColumns"/>
		FROM work_calendar a
		<include refid="workCalendarJoins"/>
        LEFT JOIN work_calendar_share cs on cs.cal_id = a.id
		<where>
			((a.create_by = #{currentUser.id} or cs.user_id = #{currentUser.id}) AND a.company_id = #{currentUser.company.id})
            <if test="startDate !=null and endDate !=null">
                AND a.start_date &lt;= #{endDate} AND (a.end_date >= #{startDate} OR a.end_date IS NULL)
            </if>
            AND a.del_flag = #{DEL_FLAG_NORMAL}
        </where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkCalendar" >
		SELECT 
			<include refid="workCalendarColumns"/>
		FROM work_calendar a
		<include refid="workCalendarJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_calendar(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			subject,
			address,
			content,
			start_date,
			end_date,
			repeat_type,
			repeat_date,
			remind,
			remind_time,
			send_daily,
			office_id,
			company_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{subject},
			#{address},
			#{content},
			#{startDate},
			#{endDate},
			#{repeatType},
			#{repeatDate},
			#{remind},
			#{remindTime},
			#{sendDaily},
			#{office.id},
			#{company.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_calendar SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			subject = #{subject},
			address = #{address},
			content = #{content},
			start_date = #{startDate},
			end_date = #{endDate},
			repeat_type = #{repeatType},
			repeat_date = #{repeatDate},
			remind = #{remind},
			remind_time = #{remindTime},
			send_daily = #{sendDaily},
			office_id = #{office.id},
			company_id = #{company.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_calendar
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_calendar SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkCalendar" statementType="STATEMENT">
		select * FROM work_calendar  where ${propertyName} = '${value}'
	</select>

    <update id="deleteUsers">
        DELETE FROM work_calendar_share WHERE cal_id = #{id}
    </update>

    <insert id="batchInsert" parameterType="java.util.Map">
        INSERT INTO work_calendar_share
        (cal_id ,user_id)
        VALUES
        <foreach collection="userList" item="user" separator=",">
            ( #{calId}, #{user.id})
        </foreach>
    </insert>

    <select id="queryUserList" resultType="User">
        SELECT t1.id,t1.name,GROUP_CONCAT(t3.name ORDER BY t3.parent_ids SEPARATOR '-') AS 'officeName'
        FROM work_calendar_share t
        LEFT JOIN sys_user t1 ON t.user_id = t1.id
        LEFT JOIN sys_office t2 ON t1.office_id = t2.id
	    LEFT JOIN sys_office t3 ON (FIND_IN_SET(t3.id,t2.parent_ids) OR t3.id = t2.id)
	    WHERE t.cal_id = #{id}
	    GROUP BY t1.id;
    </select>

    <select id="findTaskList" resultType="WorkCalendar" >
        SELECT
        <include refid="workCalendarColumns"/>
        FROM work_calendar a
        LEFT JOIN sys_user u on u.id = a.create_by
        <where>
          a.start_date &lt;= #{startDate} AND a.end_date >= #{startDate}
          AND a.remind = #{remind}
            <choose>
                <when test="repeatType !=null and repeatType !=''">
                    AND ((a.repeat_type='1' AND find_in_set(#{weekDay},a.repeat_date)) OR (a.repeat_type='2' AND a.repeat_date = #{monthDay}))
                </when>
                <otherwise>
                    AND ((a.repeat_type='1' AND find_in_set(#{weekDay},a.repeat_date)) OR (a.repeat_type='2' AND a.repeat_date = #{monthDay}) OR (a.repeat_type = '0'))
                </otherwise>
            </choose>
          <if test="remindTime!=null and nextRemindTime!=null">
            AND a.remind_time >= #{remindTime} AND a.remind_time &lt; #{nextRemindTime}
          </if>
          AND a.del_flag = #{DEL_FLAG_NORMAL}
        </where>
    </select>

	<!--查询我的工作我的管理-->
	<select id="findReportCount" resultType="CalendarCountEntity">
        SELECT
          '我的工作' title,
           DATE_FORMAT(t.create_date, '%Y-%m-%d') start,
           DATE_FORMAT(t.create_date, '%Y-%m-%d') end,
           ifnull(count(*),0) count
        FROM work_report t
        WHERE
	       DATE_FORMAT(t.create_date, '%Y-%m') = #{time}
	       and t.create_by=#{userId}
        GROUP BY
	       DATE_FORMAT(t.create_date, '%Y-%m-%d')
        UNION all
        SELECT
          '我的管理' title,
           DATE_FORMAT(a.create_date, '%Y-%m-%d') start,
           DATE_FORMAT(a.create_date, '%Y-%m-%d') end,
           ifnull(count(*),0) count
           FROM
	         work_report a
	         LEFT JOIN sys_user u on u.id = a.create_by
		     LEFT JOIN sys_office o ON o.id = a.office_id
		     LEFT JOIN sys_office s ON s.id = a.company_id
           WHERE
	       DATE_FORMAT(a.create_date, '%Y-%m')  = #{time}
	       ${sqlStr}
            GROUP BY
	       DATE_FORMAT(a.create_date, '%Y-%m-%d')
    </select>

	<select id="findHomeList" resultType="WorkCalendar" >
		SELECT
		<include refid="workCalendarColumns"/>
		FROM work_calendar a
		<include refid="workCalendarJoins"/>
		<where>
			a.id in (select cal_id from work_calendar_share w where w.user_id=#{currentUser.id}) or a.create_by=#{currentUser.id}
			and YEARWEEK(date_format(a.start_date,'%Y-%m-%d')) = YEARWEEK(now()) AND a.del_flag = #{DEL_FLAG_NORMAL}
		</where>

	</select>
</mapper>