<?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.ygtx.worklog.mapper.DailyReportMapper">

    <resultMap type="DailyReport" id="DailyReportResult">
        <result property="id"    column="id"    />
        <result property="weeklyReportId"    column="weekly_report_id"    />
        <result property="reportDate"    column="report_date"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="todayPlan"    column="today_plan"    />
        <result property="completionStatus"    column="completion_status"    />
        <result property="issuesSuggestions"    column="issues_suggestions"    />
        <result property="tomorrowPlan"    column="tomorrow_plan"    />
        <result property="status"    column="status"    />
        <result property="publishTime"    column="publish_time"    />
        <result property="isOverdue"    column="is_overdue"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectDailyReportVo">
        select id, weekly_report_id, report_date, user_id, user_name, dept_id, dept_name,
               project_id, project_name, today_plan, completion_status, issues_suggestions,
               tomorrow_plan, status, publish_time, is_overdue,
               create_by, create_time, update_by, update_time, remark
        from wl_daily_report
    </sql>

    <select id="selectDailyReportList" parameterType="DailyReport" resultMap="DailyReportResult">
        <include refid="selectDailyReportVo"/>
        <where>
            <if test="weeklyReportId != null "> and weekly_report_id = #{weeklyReportId}</if>
            <if test="reportDate != null "> and report_date = #{reportDate}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="projectName != null  and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="isOverdue != null  and isOverdue != ''"> and is_overdue = #{isOverdue}</if>
        </where>
        order by report_date desc, create_time desc
    </select>

    <select id="selectDailyReportById" parameterType="Long" resultMap="DailyReportResult">
        <include refid="selectDailyReportVo"/>
        where id = #{id}
    </select>

    <select id="selectDailyReportByUserAndDate" resultMap="DailyReportResult">
        <include refid="selectDailyReportVo"/>
        where user_id = #{userId} and report_date = #{reportDate}
        limit 1
    </select>

    <select id="selectPreviousDailyReport" resultMap="DailyReportResult">
        <include refid="selectDailyReportVo"/>
        where user_id = #{userId} and report_date &lt; #{reportDate}
        order by report_date desc
        limit 1
    </select>

    <select id="selectDailyReportsByWeeklyId" parameterType="Long" resultMap="DailyReportResult">
        <include refid="selectDailyReportVo"/>
        where weekly_report_id = #{weeklyReportId}
        order by report_date asc
    </select>

    <select id="selectAuthDailyReportList" resultMap="DailyReportResult">
        <include refid="selectDailyReportVo"/>
        where user_id in (
            select distinct u.user_id
            from sys_user u
            left join sys_dept d on u.dept_id = d.dept_id
            left join sys_user_role ur on u.user_id = ur.user_id
            left join sys_user_post up on u.user_id = up.user_id
            where exists (
                select 1 from wl_auth_config ac
                where ac.auth_user_id = #{authUserId}
                and ac.status = '0'
                and (
                    (ac.auth_type = '1' and ac.target_id = u.dept_id) or
                    (ac.auth_type = '2' and ac.target_id = u.user_id) or
                    (ac.auth_type = '3' and ac.target_id = ur.role_id) or
                    (ac.auth_type = '4' and ac.target_id = up.post_id)
                )
            )
        )
        and status in ('1', '2')
        <if test="query.reportDate != null"> and report_date = #{query.reportDate}</if>
        <if test="query.userName != null and query.userName != ''"> and user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.deptName != null and query.deptName != ''"> and dept_name like concat('%', #{query.deptName}, '%')</if>
        <if test="query.projectName != null and query.projectName != ''"> and project_name like concat('%', #{query.projectName}, '%')</if>
        <if test="query.status != null and query.status != ''"> and status = #{query.status}</if>
        order by report_date desc, create_time desc
    </select>

    <select id="selectUnsubmittedUserIds" resultType="Long">
        select distinct u.user_id
        from sys_user u
        where u.del_flag = '0'
        and u.status = '0'
        and not exists (
            select 1 from wl_daily_report dr
            where dr.user_id = u.user_id
            and dr.report_date = #{reportDate}
            and dr.status in ('1', '2')
        )
    </select>

    <insert id="insertDailyReport" parameterType="DailyReport" useGeneratedKeys="true" keyProperty="id">
        insert into wl_daily_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="weeklyReportId != null">weekly_report_id,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null and deptName != ''">dept_name,</if>
            <if test="projectId != null">project_id,</if>
            <if test="projectName != null and projectName != ''">project_name,</if>
            <if test="todayPlan != null">today_plan,</if>
            <if test="completionStatus != null">completion_status,</if>
            <if test="issuesSuggestions != null">issues_suggestions,</if>
            <if test="tomorrowPlan != null">tomorrow_plan,</if>
            <if test="status != null">status,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="isOverdue != null">is_overdue,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="weeklyReportId != null">#{weeklyReportId},</if>
            <if test="reportDate != null">#{reportDate},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null and deptName != ''">#{deptName},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null and projectName != ''">#{projectName},</if>
            <if test="todayPlan != null">#{todayPlan},</if>
            <if test="completionStatus != null">#{completionStatus},</if>
            <if test="issuesSuggestions != null">#{issuesSuggestions},</if>
            <if test="tomorrowPlan != null">#{tomorrowPlan},</if>
            <if test="status != null">#{status},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="isOverdue != null">#{isOverdue},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateDailyReport" parameterType="DailyReport">
        update wl_daily_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="weeklyReportId != null">weekly_report_id = #{weeklyReportId},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            <if test="todayPlan != null">today_plan = #{todayPlan},</if>
            <if test="completionStatus != null">completion_status = #{completionStatus},</if>
            <if test="issuesSuggestions != null">issues_suggestions = #{issuesSuggestions},</if>
            <if test="tomorrowPlan != null">tomorrow_plan = #{tomorrowPlan},</if>
            <if test="status != null">status = #{status},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="isOverdue != null">is_overdue = #{isOverdue},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDailyReportById" parameterType="Long">
        delete from wl_daily_report where id = #{id}
    </delete>

    <delete id="deleteDailyReportByIds" parameterType="String">
        delete from wl_daily_report where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
