<?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.WorkDailyRoutineDao">
    
	<sql id="workDailyRoutineColumns">
		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.name AS "name",
		a.source AS "source",
		a.status AS "status",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		a.source_id AS "sourceId",
		u.name AS "createBy.name",
		o.name AS "office.name",
		o.top_company AS "office.topCompany"
	</sql>
	
	<sql id="workDailyRoutineJoins">
        LEFT JOIN sys_office o on a.office_id = o.id
        LEFT JOIN sys_office s ON s.id = a.company_id
        LEFT JOIN sys_user u ON u.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkDailyRoutine" >
		SELECT 
			<include refid="workDailyRoutineColumns"/>
		FROM work_daily_routine a
		<include refid="workDailyRoutineJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkDailyRoutine" >
		SELECT
			*
        FROM (
          <if test="sOr==null or sOr=='' or sOr=='发出'">
            SELECT
              <include refid="workDailyRoutineColumns"/>
            ,'发出' AS "sOr",''AS subStatus
            FROM work_daily_routine a
              <include refid="workDailyRoutineJoins"/>
            <where>
                a.del_flag = #{DEL_FLAG_NORMAL}
                <if test="createBy != null and createBy.name != null and createBy.name != ''">
                    AND u.name like concat('%',#{createBy.name},'%')
                </if>
                <if test="beginCreateDate != null">
                    AND a.create_date >= #{beginCreateDate}
                </if>
                <if test="endCreateDate != null">
                    AND a.create_date &lt;= #{endCreateDate}
                </if>
                <if test="name != null and name != ''">
                    AND a.name LIKE
                    <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
                    <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
                    <if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
                </if>
                <if test="source != null and source != ''">
                    AND a.source like concat('%',#{source},'%')
                </if>
                <if test="status != null and status != ''">
                    AND a.status = #{status}
                </if>
                ${sqlMap.dsf}
            </where>
          </if>
          <if test="sOr==null or sOr==''">
            UNION
          </if>
          <if test="sOr==null or sOr=='' or sOr=='收到'">
            SELECT
              <include refid="workDailyRoutineColumns"/>
              ,'收到' AS "sOr",t.status AS "subStatus"
            FROM work_daily_routine_detail t
            LEFT JOIN work_daily_routine a ON a.id = t.daily_id
            LEFT JOIN sys_office s ON s.id = a.company_id
            LEFT JOIN sys_user u ON u.id = a.create_by
              LEFT JOIN sys_office o on a.office_id = o.id
              <where>
                  a.del_flag = #{DEL_FLAG_NORMAL}
                  <if test="createBy != null and createBy.name != null and createBy.name != ''">
                      AND u.name like concat('%',#{createBy.name},'%')
                  </if>
                  <if test="beginCreateDate != null">
                      AND a.create_date >= #{beginCreateDate}
                  </if>
                  <if test="endCreateDate != null">
                      AND a.create_date &lt;= #{endCreateDate}
                  </if>
                  <if test="name != null and name != ''">
                      AND a.name LIKE
                      <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
                      <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
                      <if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
                  </if>
                  <if test="source != null and source != ''">
                      AND a.source like concat('%',#{source},'%')
                  </if>
                  <if test="status != null and status != ''">
                      AND a.status = #{status}
                  </if>
                 AND t.operator = #{currentUser.id} AND (a.company_id = #{currentUser.company.id} or find_in_set( #{currentUser.company.id}, s.parent_ids))
              </where>
          </if>
        ) t
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY t.updateDate DESC
            </otherwise>
        </choose>
	</select>
	
	<select id="findAllList" resultType="WorkDailyRoutine" >
		SELECT 
			<include refid="workDailyRoutineColumns"/>
		FROM work_daily_routine a
		<include refid="workDailyRoutineJoins"/>
		<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_daily_routine(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			name,
			source,
			status,
			office_id,
			company_id,
			source_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{name},
			#{source},
			#{status},
			#{office.id},
			#{company.id},
			#{sourceId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_daily_routine SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			source = #{source},
			status = #{status},
			office_id = #{office.id},
			company_id = #{company.id},
			source_id = #{sourceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_daily_routine
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_daily_routine SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkDailyRoutine" statementType="STATEMENT">
		select * FROM work_daily_routine  where ${propertyName} = '${value}'
	</select>

    <update id="updateStatusById">
        UPDATE work_daily_routine SET
			status = #{status}
		WHERE id = #{id}
    </update>

    <select id="findBySourceId" resultType="WorkDailyRoutine">
        SELECT a.* FROM  work_daily_routine a WHERE a.source_id = #{sourceId}
    </select>
</mapper>