<?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.sunxung.factoring.mapper.weekWorkPlan.WeekWorkPlanMapper">
	<resultMap type="WeekWorkPlan" id="weekWorkPlanMap">
		<id column="id" property="id"/>
		<result column="lastWorkId" property="lastWorkId"/>
		<result column="userId" property="user.id"/>
		<result column="title" property="title"/>
		<result column="workContent" property="workContent"/>
		<result column="workStateId" property="workState.id"/>
		<result column="level" property="level"/>
		<result column="startTime" property="startTime"/>
		<result column="endTime" property="endTime"/>
		<result column="weekth" property="weekth"/>
		<result column="workTime" property="workTime"/>
		<result column="user_create" property="userCreate"/>
		<result column="user_modified" property="userModified"/>
		<result column="gmt_create" property="gmtCreate"/>
		<result column="gmt_modified" property="gmtModified"/>
	</resultMap>
	
	<sql id="field">
		w.id,
		w.lastWorkId,
		w.userId,
		w.title,
		w.workContent,
		w.workStateId,
		w.level,
		w.startTime,
		w.endTime,
		w.weekth,
		w.workTime,
		w.user_create,
		w.user_modified,
		w.gmt_create,
		w.gmt_modified
	</sql>
	
	<sql id="_where">
		<if test="null != userId">
			and w.userId=#{userId}
		</if>
		<if test="null != workStateId">
			and w.workStateId=#{workStateId}
		</if>
		<if test="null != startTime">
			and w.startTime=#{startTime}
		</if>
		<if test="null != endTime">
			and w.endTime=#{endTime}
		</if>
		<if test="null != yearMonth">
			and DATE_FORMAT(w.workTime,'%y-%M')=DATE_FORMAT(#{yearMonth},'%y-%M')
		</if>
	</sql>
	
	<select id="findBySearch" parameterType="SearchWeekWorkPlan" resultMap="weekWorkPlanMap">
		select <include refid="field" /> from week_work_plan w
		left join t_dictionary d on w.workStateId=d.id
		<where>
			<include refid="_where" />
		</where>
		<choose>
			<!--月历，周历中需要先按时间排序  -->
			<when test="null != yearMonth or null != week">
				order by w.workTime asc,d.gradation asc,w.level desc,CONVERT(w.title using gbk) asc
			</when>
			<!-- 周工作计划列表需要先按任务状态排序 -->
			<otherwise>
				order by d.gradation asc,w.level desc,w.workTime asc,CONVERT(w.title using gbk) asc 
			</otherwise>
		</choose>
	</select>
	
	<select id="countBySearch" parameterType="SearchWeekWorkPlan" resultType="Integer">
		select count(*) from week_work_plan w
		<where>
			<include refid="_where" />
		</where>
	</select>
	
	<insert id="add" parameterType="WeekWorkPlan" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		insert into week_work_plan
		(lastWorkId,userId,title,workContent,workStateId,level,startTime,endTime,weekth,workTime,user_create,user_modified) 
		values
		(#{lastWorkId},#{user.id},#{title},#{workContent},#{workState.id},#{level},#{startTime},#{endTime},#{weekth},#{workTime},#{userCreate},#{userModified})
	</insert>
	
	<update id="update" parameterType="WeekWorkPlan">
		update week_work_plan w
		<set>
			w.user_modified=#{userModified}
			<if test="title != null and title != ''">
				,w.title=#{title}
			</if>
			<if test="workContent != null and workContent != ''">
				,w.workContent=#{workContent}
			</if>
			<if test="workState != null and workState.id != null">
				,w.workStateId=#{workState.id}
			</if>
			<if test="level != null and level != ''">
				,w.level=#{level}
			</if>
			<if test="startTime != null">
				,w.startTime=#{startTime}
			</if>
			<if test="endTime != null">
				,w.endTime=#{endTime}
			</if>
			<if test="weekth != null and weekth != ''">
				,w.weekth=#{weekth}
			</if>
			<if test="workTime != null">
				,w.workTime=#{workTime}
			</if>
		</set>
		<where>
			w.id=#{id}
		</where>
	</update>
	
	<delete id="delete" parameterType="Long">
		delete from week_work_plan where id=#{id}
	</delete>
	
	<select id="get" parameterType="Long" resultMap="weekWorkPlanMap">
		select <include refid="field" /> from week_work_plan w where w.id=#{id}
	</select>
	
	<select id="getByLastWeekWorkId" parameterType="Long" resultMap="weekWorkPlanMap">
		select <include refid="field" /> from week_work_plan w where w.lastWorkId=#{lastWeekWorkId}
	</select>
	
	<select id="getByTitleWorkTime" resultMap="weekWorkPlanMap">
		select <include refid="field" /> from week_work_plan w where w.title=#{title} and w.workTime=#{workTime} and w.userId=#{userId} limit 1
	</select>
	
	<sql id="find_where">
		<if test="null != userId">
			and w.userId=#{userId}
		</if>
		<if test="null != workStateId">
			and w.workStateId=#{workStateId}
		</if>
		<if test="null != startTime">
			and w.startTime=#{startTime}
		</if>
		<if test="null != endTime">
			and w.endTime=#{endTime}
		</if>
		<if test="null != userList and userList.size()>0">
			and w.userId in
			<foreach collection="userList" item="item" index="index" open="(" separator="," close=")">
				#{item.id}
			</foreach>
		</if>
		
	</sql>
	<select id="findLastWeekWorkPlans" parameterType="SearchWeekWorkPlan" resultMap="weekWorkPlanMap">
		select <include refid="field" /> from week_work_plan w
		left join sys_user s on s.id=w.userId
		<!-- left join sys_user_org_rel r on w.userId=r.user_id -->
		left join t_dictionary d on w.workStateId=d.id
		<where>
			s.is_enabled=1
			<include refid="find_where" />
		</where>
		order by w.userId asc,d.gradation asc,w.level desc,w.workTime asc,CONVERT(w.title using gbk) asc
	</select>
	
	<select id="countLastWeekWorkPlans" parameterType="SearchWeekWorkPlan" resultType="Integer">
		select count(*) from week_work_plan w
		left join sys_user s on s.id=w.userId
		<!-- left join sys_user_org_rel r on w.userId=r.user_id -->
		left join t_dictionary d on w.workStateId=d.id
		<where>
			s.is_enabled=1
			<include refid="find_where" />
		</where>
	</select>
	
	<select id="findWorkingWorkPlanAll" resultMap="weekWorkPlanMap">
		select <include refid="field" /> from week_work_plan w 
		where <![CDATA[DATE_FORMAT(w.workTime,'%y-%m-%d') < DATE_FORMAT(#{workTime},'%y-%m-%d')]]>
		and w.workStateId=#{workStateId}
	</select>


</mapper>