<?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.ruoyi.ibs.task.mapper.WorkRecordMapper">

    <select id="selectWorkRecordList" resultType="WorkRecordVO">
        select
            r.id,r.user_name,r.nick_name,r.post_id,p.post_name AS postName,r.template_id,r.frequency_type,r.start_time,r.end_time,r.status,r.remark,r.is_overdue,r.read_time,t.work_type,t.work_detail
        from work_record r
        left join work_template t on r.template_id = t.id
        left join sys_post p on r.post_id = p.post_id
        <where>
            r.is_alter = 0
            <if test="username != null and username != ''"> and r.user_name = #{username}</if>
            <if test="dto.frequencyType != null and dto.frequencyType != ''"> and r.frequency_type = #{dto.frequencyType}</if>
            <if test="dto.workType != null and dto.workType != ''"> and t.work_type = #{dto.workType}</if>
            <if test="dto.postId != null and dto.postId != ''">and r.post_id = #{dto.postId}</if>
            <if test="dto.validTime != null ">
                and #{dto.validTime} between r.start_time and r.end_time
            </if>
            <if test="dto.status != null  and dto.status != ''"> and r.status = #{dto.status}</if>
        </where>
        order by r.start_time desc, r.frequency_type, r.status, r.post_id
    </select>

    <select id="selectAllWorkRecordList" resultType="WorkRecordVO">
        select
            r.id,r.user_name,r.nick_name,r.post_id,p.post_name AS postName,r.template_id,r.frequency_type,r.start_time,r.end_time,r.status,r.remark,r.is_overdue,r.read_time,t.work_type,t.work_detail
        from work_record r
        left join work_template t on r.template_id = t.id
        left join sys_post p on r.post_id = p.post_id
        <where>
            r.is_alter = 0
            <if test="postIds != null and postIds.size() > 0">
                AND r.post_id IN
                <foreach collection="postIds" item="postId" open="(" separator="," close=")">
                    #{postId}
                </foreach>
            </if>
            <if test="username != null and username != ''"> and r.user_name = #{username}</if>
            <if test="dto.frequencyType != null and dto.frequencyType != ''"> and r.frequency_type = #{dto.frequencyType}</if>
            <if test="dto.workType != null and dto.workType != ''"> and t.work_type = #{dto.workType}</if>
            <if test="dto.postId != null and dto.postId != ''">and r.post_id = #{dto.postId}</if>
            <if test="dto.validTime != null ">
                and #{dto.validTime} between r.start_time and r.end_time
            </if>
            <if test="dto.status != null  and dto.status != ''"> and r.status = #{dto.status}</if>
        </where>
        order by r.frequency_type, r.status, r.post_id
    </select>

    <update id="batchSubmitWorkRecord" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            update work_record
            <set>
                <if test="item.status != null and item.status != ''">status = #{item.status},</if>
                <if test="item.remark != null and item.remark != ''">remark = #{item.remark},</if>
                update_time = sysdate(),
                update_by = #{item.update_by}
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <update id="updateOverdueRecord">
        update work_record
        set is_overdue = 1,
            update_time = sysdate(),
            update_by = 'admin'
        where
            end_time &lt; #{now}
            and is_overdue = 0
    </update>

    <update id="updateReadTime">
        update work_record
        set read_time = sysdate(),
            update_time = sysdate(),
            update_by = #{username}
        where
            id = #{id}
    </update>

    <select id="selectCompleteStatus" resultType="WorkCompleteVO">
        SELECT
        r.user_name AS userName,
        r.nick_name AS nickName,
        r.post_id AS postId,
        p.post_name AS postName,
        r.frequency_type AS frequencyType,
        COUNT(*) AS totalCount,
        SUM(CASE WHEN r.status = '2' THEN 1 ELSE 0 END) AS completeCount,
        CASE
            WHEN COUNT(*) > 0 THEN
                ROUND(SUM(CASE WHEN r.status = '2' THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2)
            ELSE 0
        END AS completeRate
        FROM work_record r
        LEFT JOIN sys_post p ON r.post_id = p.post_id
        <where>
            r.is_alter = 0
            <if test="postIds != null and postIds.size() > 0">
                AND r.post_id IN
                <foreach collection="postIds" item="postId" open="(" separator="," close=")">
                    #{postId}
                </foreach>
            </if>
            <if test="dto.validTime != null">
                AND #{dto.validTime} BETWEEN r.start_time AND r.end_time
            </if>
            <if test="dto.postId != null">
                AND r.post_id = #{dto.postId}
            </if>
            <if test="dto.frequencyType != null and dto.frequencyType != ''">
                AND r.frequency_type = #{dto.frequencyType}
            </if>
        </where>
        GROUP BY r.user_name, r.nick_name, r.post_id, p.post_name, r.frequency_type
        ORDER BY completeRate DESC
    </select>

    <select id="selectRecordsToGenerate" resultType="WorkRecord">
        select
            t.post_id as postId,
            u.user_name as userName,
            u.nick_name as nickName,
            t.id as templateId,
            t.work_type as workType,
            t.work_detail as workDetail,
            t.frequency_type as frequencyType,
            #{startTime} as startTime,
            #{endTime} as endTime,
            0 as isAlter,
            '0' as status
        from work_template t
        inner join sys_user_post up on t.post_id = up.post_id
        inner join sys_user u on up.user_id = u.user_id
        where t.frequency_type = #{frequencyType}
            and t.is_enabled = 0
            and t.del_flag = 0
            and not exists (
                select 1 from work_record r
                where r.user_name = u.user_name
                  and r.template_id = t.id
                  and r.frequency_type = #{frequencyType}
                  and r.start_time = #{startTime}
                  and r.end_time = #{endTime}
            )
        order by t.post_id, u.user_id
    </select>

    <insert id="batchInsertWorkRecord" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        <foreach collection="workRecordList" item="item" separator=";">
            insert into work_record
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.postId != null">post_id,</if>
                <if test="item.userName != null">user_name,</if>
                <if test="item.nickName != null">nick_name,</if>
                <if test="item.templateId != null">template_id,</if>
                <if test="item.startTime != null">start_time,</if>
                <if test="item.endTime != null">end_time,</if>
                <if test="item.frequencyType != null">frequency_type,</if>
                <if test="item.status != null">status,</if>
                <if test="item.isAlter != null">is_alter,</if>
                create_time,
                is_overdue
            </trim>
            values
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.postId != null">#{item.postId},</if>
                <if test="item.userName != null">#{item.userName},</if>
                <if test="item.nickName != null">#{item.nickName},</if>
                <if test="item.templateId != null">#{item.templateId},</if>
                <if test="item.startTime != null">#{item.startTime},</if>
                <if test="item.endTime != null">#{item.endTime},</if>
                <if test="item.frequencyType != null">#{item.frequencyType},</if>
                <if test="item.status != null">#{item.status},</if>
                <if test="item.isAlter != null">#{item.isAlter},</if>
                sysdate(),
                0
            </trim>
        </foreach>
    </insert>

    <select id="selectAlterList" resultType="AlterVO">
        select id, user_name, nick_name, alter_type, alter_detail, cust_id, cust_name, read_time, status, remark, is_feedback, cust_isn
        from work_record
        <where>
            is_alter = 1
            and user_name= #{username}
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
            <if test="alterType != null and alterType != ''">
                and alter_type LIKE CONCAT('%', #{alterType}, '%')
            </if>
        </where>
        order by create_time desc, status asc
    </select>

    <select id="selectAlterCount" resultType="AlterCountVO">
        select
        count(1) as alterCount,
        sum(case
        when wr.status = '2'
        then 1 else 0
        end) as completeCount
        from work_record wr
        left join sys_user su on su.user_name = wr.user_name
        left join sys_dept d on su.dept_id = d.dept_id
        where wr.is_alter = 1
        and wr.create_time &lt;= #{reportTime}
        and (
        <choose>
            <when test="role != null and role == 'outlet'"> su.dept_id = #{deptId} </when>
            <when test="role != null and role == 'branch'"> (su.dept_id = #{deptId} or find_in_set(#{deptId},d.ancestors)) </when>
            <when test="role != null and role == 'private'"> (left(su.dept_id,3) = left(#{deptId},3) and wr.cust_type in ('0', '1')) </when>
            <when test="role != null and role == 'public'"> (left(su.dept_id,3) = left(#{deptId},3) and wr.cust_type = '2') </when>
            <when test="role != null and role == 'ops'"> left(su.dept_id,3) = left(#{deptId},3) </when>
            <when test="role != null and role == 'head'"> left(su.dept_id,3) = left(#{deptId},3) </when>
        </choose>)
    </select>

    <select id="selectAllAlterList" resultType="AlterVO">
        select wr.id, wr.user_name, wr.nick_name, wr.alter_type, wr.alter_detail, wr.cust_id, wr.cust_name, wr.read_time, wr.status, wr.remark, wr.cust_type, wr.is_feedback, wr.cust_isn
        from work_record wr
        left join sys_user su on su.user_name = wr.user_name
        left join sys_dept d on su.dept_id = d.dept_id
        where wr.is_alter = 1
        <if test="status != null and status != ''">
            and wr.status = #{status}
        </if>
        <if test="alterType != null and alterType != ''">
            and wr.alter_type LIKE CONCAT('%', #{alterType}, '%')
        </if>
        <!-- "走访异常提醒"类型直接通过用户名匹配,其他类型按角色权限处理 -->
        and ((wr.alter_type = '走访异常提醒' and wr.user_name = #{username}) or
            (wr.alter_type != '走访异常提醒' and (
            <choose>
                <when test="role != null and role == 'outlet'"> su.dept_id = #{deptId} </when>
                <when test="role != null and role == 'branch'"> (su.dept_id = #{deptId} or find_in_set(#{deptId},d.ancestors)) </when>
                <when test="role != null and role == 'private'"> (left(su.dept_id,3) = left(#{deptId},3) and wr.cust_type in ('0', '1')) </when>
                <when test="role != null and role == 'public'"> (left(su.dept_id,3) = left(#{deptId},3) and wr.cust_type = '2') </when>
                <when test="role != null and role == 'ops'"> left(su.dept_id,3) = left(#{deptId},3) </when>
                <when test="role != null and role == 'head'"> left(su.dept_id,3) = left(#{deptId},3) </when>
            </choose> )))
        order by wr.create_time desc, wr.status asc, wr.user_name desc
    </select>

    <select id="selectAlterConfigList" resultType="AlterConfigVO">
        select id, alter_type, prod_type, warn_role, warn_threshold, update_time, update_by, type
        from alter_config
        <where>
            <if test="alterType != null and alterType != ''">
                and alter_type like concat('%', #{alterType},'%')
            </if>
        </where>
    </select>

    <update id="updateAlterConfig">
        update alter_config
        <set>
            <if test="warnThreshold != null">warn_threshold = #{warnThreshold},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </set>
        where id = #{id}
    </update>

</mapper>