<?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.work.dao.report.WorkReportDao">
    
	<sql id="workReportColumns">
		a.id AS "id",
		a.type AS "type",
		a.this_content AS "thisContent",
		a.this_files AS "thisFiles",
		a.next_content AS "nextContent",
		a.next_files AS "nextFiles",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.status AS "status",
		a.start_date AS "startDate",
		a.finish_date AS "finishDate",
		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",
		createBy.name AS "createBy.name"
	</sql>
	
	<sql id="workReportJoins">
		LEFT JOIN sys_user createBy ON createBy.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkReport" >
		SELECT 
			<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		WHERE a.id = #{id}
	</select>

  <select id="findList" resultType="WorkReport" >
		SELECT
		<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="createBy !=null and createBy.id !=null and createBy.id !=''">
				AND a.create_by = #{createBy.id}
			</if>
			<!-- 按发布人姓名搜索 -->
			<if test="create !=null and create !=''">
				AND createBy.name like   concat('%',#{create},'%')
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</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>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findListByUser" resultType="WorkReport" >
		SELECT
		<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="currentUser != null and currentUser.id !=null and currentUser.id != ''">
				AND a.create_by = #{currentUser.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="getCreateDateList" resultType="string">
		SELECT create_date FROM work_report
		WHERE  #{date} LIKE  LEFT (create_date,7)
		<if test="companyId != null and companyId !=''">
			AND    company_id = #{companyId}
		</if>
		ORDER BY  create_date asc
	</select>


	<select id="findByCompany2" resultType="WorkReport">
		SELECT
		<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		<where>
			a.del_flag = #{wr.DEL_FLAG_NORMAL}
			<if test="wr.officeId != null and wr.officeId != ''">
				AND a.office_id = #{wr.officeId}
			</if>
			<if test="wr.companyId != null and wr.companyId != ''">
				AND a.company_id = #{wr.companyId}
			</if>
			<if test="date!=null and date !=''">
				AND #{date} LIKE  LEFT(a.create_date,7)
			</if>
			<if test="wr.type != null and wr.type != ''">
				AND a.type = #{type}
			</if>
			<if test="wr.status != null and wr.status != ''">
				AND a.status = #{wr.status}
			</if>
		</where>
		<choose>
			<when test="wr.page !=null and wr.page.orderBy != null and wr.page.orderBy != ''">
				ORDER BY ${wr.page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

    <select id="findByCompany" resultType="WorkReport" >
        SELECT
        <include refid="workReportColumns"/>
        FROM work_report a
        <include refid="workReportJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="officeId != null and officeId != ''">
                AND a.office_id = #{officeId}
            </if>
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </if>
            <if test="createDate != null and createDate != ''">
                AND Date(a.create_date) = #{createDate}
            </if>
            <if test="type != null and type != ''">
                AND a.type = #{type}
            </if>
            <if test="status != null and status != ''">
                AND a.status = #{status}
            </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="findReportUserByCompany" resultType="WorkReport" >
        SELECT
        <include refid="workReportColumns"/>
        FROM work_report a
        <include refid="workReportJoins"/>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="officeId != null and officeId != ''">
                AND a.office_id = #{officeId}
            </if>
            <if test="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </if>
            <if test="createDate != null and createDate != ''">
                AND Date(a.create_date) = #{createDate}
            </if>
            <if test="type != null and type != ''">
                AND a.type = #{type}
            </if>
            <if test="status != null and status != ''">
                AND a.status = #{status}
            </if>
        </where>
        GROUP BY create_by
        <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="findGetList" resultType="WorkReport">
		SELECT 
			<include refid="workReportColumns"/>
			<if test="isSelf">,
                r.read_flag
			</if>
		FROM work_report  a
		<include refid="workReportJoins"/>
		<!-- 我的报告-->
		<if test="isSelf">
			JOIN work_report_record r ON r.report_id = a.id
			<if test="currentUser != null and currentUser.id !=null and currentUser.id !=''">
				AND r.user_id = #{currentUser.id}
			</if>
			<if test="readFlag != null and readFlag != ''">
					AND r.read_flag = #{readFlag}
				</if>
		</if>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		<!-- 按发布人姓名搜索 -->
		<if test="create !=null and create !=''">
			AND createBy.name like   concat('%',#{create},'%')
		</if>
		<if test="type != null and type != ''">
			AND a.TYPE = #{type}
		</if>
		<if test="companyId != null and companyId != ''">
			AND a.company_id = #{companyId}
		</if>
		<if test="status != null and status != ''">
			AND a.STATUS = #{status}
		</if>
		<if test="isSelf">
			AND a.STATUS = '1'
		</if>
		ORDER BY r.read_flag asc ,a.update_date desc
	</select>
	
		<!-- 发出的 -->
	<select id="findOutList" resultType="WorkReport">
		SELECT 
			<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.create_by = #{currentUser.id}
		<if test="type != null and type != ''">
			AND a.TYPE = #{type}
		</if>
		<if test="status != null and status != ''">
			AND a.STATUS = #{status}
		</if>
        <if test="companyId != null and companyId != ''">
            AND a.company_id = #{companyId}
        </if>
		ORDER BY a.update_date DESC
	</select>
	
	
	<select id="findAllList" resultType="WorkReport" >
		SELECT 
			<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		<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_report(
			id,
			type,
			this_content,
			this_files,
			next_content,
			next_files,
			status,
			start_date,
			finish_date,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			company_id,
			office_id,
			del_flag
		) VALUES (
			#{id},
			#{type},
			#{thisContent},
			#{thisFiles},
			#{nextContent},
			#{nextFiles},
			#{status},
			#{startDate},
			#{finishDate},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{companyId},
			#{officeId},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE work_report SET 	
			type = #{type},
			this_content = #{thisContent},
			this_files = #{thisFiles},
			next_content = #{nextContent},
			next_files = #{nextFiles},
			status = #{status},
			start_date = #{startDate},
			finish_date = #{finishDate},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			company_id = #{companyId},
			office_id = #{officeId},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>

	<!-- 更新环信群组id -->
	<update id="updateEasemobGroupId" >
		UPDATE work_report SET
			easemobInfo_group_id = #{easemobGroupId}
		WHERE id = #{id}
	</update>

	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_report
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_report SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReport" statementType="STATEMENT">
		select * FROM work_report  where ${propertyName} = '${value}'
	</select>
	
	<!--获取最新插入的数据ID  -->
	<select id="findNewId" resultType="WorkReport">
		SELECT * FROM work_report a  ORDER BY  a.create_date DESC  LIMIT 1 
	</select>


	<select id="reportlist" resultType="WorkReport">
		SELECT a.id,
		a.update_date as updateDate,
		a.create_date as createDate,
		u.name as userName,
		u.photo,
		u.id userId
		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 a.create_by=#{userId}
		     and DATE_FORMAT(a.create_date, '%Y-%m-%d')=#{time}
		ORDER BY  a.create_date DESC
	</select>


	<select id="reportlistAll" resultType="WorkReport">
	SELECT a.id,
		a.update_date as updateDate,
		a.create_date as createDate,
		u.name as userName,
		u.photo,
		u.id userId
		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-%d')=#{time}
		     ${sqlStr}
		ORDER BY  a.create_date DESC
	</select>
	<select id="getCreateDateList1" resultType="string">
		SELECT a.create_date FROM work_report a
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	       and a.create_by=#{userId}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompany1" resultType="WorkReport">
		SELECT
		<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="getCreateDateListAll" resultType="string">
		SELECT a.create_date
		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}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompanyAll" resultType="WorkReport">
		SELECT
		a.*
		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
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByCompanySelf" resultType="WorkReport">
		SELECT
		<include refid="workReportColumns"/>
		FROM work_report a
		<include refid="workReportJoins"/>
		where
		a.del_flag = '0'
		<if test="time != null and time != ''">
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		</if>
		<if test="startDate != null and endDate != null and startDate != '' and endDate != ''">
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') BETWEEN #{startDate} AND #{endDate}
		</if>
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByManage" resultType="WorkReport">
		SELECT
		a.id AS "id",
		a.type AS "type",
		a.this_content AS "thisContent",
		a.this_files AS "thisFiles",
		a.next_content AS "nextContent",
		a.next_files AS "nextFiles",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.status AS "status",
		a.start_date AS "startDate",
		a.finish_date AS "finishDate",
		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",
		u.name AS "createBy.name"
		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
		a.del_flag = '0'
		<if test="time != null and time != ''">
			and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		</if>
		<if test="startDate != null and endDate != null and startDate != '' and endDate != ''">
			and DATE_FORMAT(a.create_date, '%Y-%m-%d') BETWEEN #{startDate} AND #{endDate}
		</if>
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>
</mapper>