<?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.workdailyroutine.dao.WorkDailyRoutineDetailDao">
    
	<sql id="workDailyRoutineDetailColumns">
		a.id AS "id",
		a.daily_id AS "workDailyRoutine.id",
		a.operator AS "operator.id",
		a.office_id AS "office.id",
		a.status AS "status",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		u.name AS "operator.name",
		s.name AS "office.name",
		s.top_company AS "office.topCompany"
	</sql>
	
	<sql id="workDailyRoutineDetailJoins">
        LEFT JOIN sys_user u ON u.id = a.operator
        LEFT JOIN sys_office s ON s.id = u.office_id
	</sql>
	
    
	<select id="get" resultType="WorkDailyRoutineDetail" >
		SELECT 
			<include refid="workDailyRoutineDetailColumns"/>
		FROM work_daily_routine_detail a
		<include refid="workDailyRoutineDetailJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkDailyRoutineDetail" >
		SELECT 
			<include refid="workDailyRoutineDetailColumns"/>
		FROM work_daily_routine_detail a
		<include refid="workDailyRoutineDetailJoins"/>
		<where>
			
			<if test="workDailyRoutine !=null and workDailyRoutine.id != null and workDailyRoutine.id != ''">
				AND a.daily_id = #{workDailyRoutine.id}
			</if>
			<if test="operator != null and operator.id != null and operator.id != ''">
				AND a.operator = #{operator.id}
			</if>
		</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="WorkDailyRoutineDetail" >
		SELECT 
			<include refid="workDailyRoutineDetailColumns"/>
		FROM work_daily_routine_detail a
		<include refid="workDailyRoutineDetailJoins"/>
		<where>
			
		</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_daily_routine_detail(
			id,
			daily_id,
			operator,
			office_id,
			status,
			update_date,
			remarks
		) VALUES (
			#{id},
			#{workDailyRoutine.id},
			#{operator.id},
			#{office.id},
			#{status},
			#{updateDate},
			#{remarks}
		)
	</insert>
	
	<update id="update">
		UPDATE work_daily_routine_detail SET 	
			daily_id = #{workDailyRoutine.id},
			operator = #{operator.id},
			office_id = #{office.id},
			status = #{status},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_daily_routine_detail
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_daily_routine_detail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkDailyRoutineDetail" statementType="STATEMENT">
		select * FROM work_daily_routine_detail  where ${propertyName} = '${value}'
	</select>

    <select id="countUndone" resultType="int">
        SELECT count(*) FROM work_daily_routine_detail
        <where>
            <if test="workDailyRoutine !=null and workDailyRoutine.id != null and workDailyRoutine.id != ''">
                AND daily_id = #{workDailyRoutine.id}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
        </where>
    </select>
</mapper>