<?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="cn.iocoder.yudao.module.system.dal.mysql.biz.ScheduleShiftMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="selectScheduleShiftList" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.ScheduleShiftPageRespVO">
        SELECT
            temp_schedule_shift.shift_ids,
            temp_schedule_shift.shift_dates ,
            su.id,
            su.nickname,
            su.work_status,
            nsc.id AS nurseScheduleId,
            nsc.schedule_name,
            nsc.schedule_date,
            CASE WHEN ( SELECT COUNT( 1 ) FROM nurse_schedule_shift WHERE nurse_id = su.id AND type = 0 AND deleted = 0 AND DATE_FORMAT( work_date, '%Y-%m' ) = #{pageReqVO.scheduleDate} ) > 0 THEN
            0 ELSE 1
            END AS "type"
        FROM system_users su
        LEFT JOIN
            (
            SELECT
            nss.schedule_id,
            nss.nurse_id,
            group_concat( nss.shift_id SEPARATOR ';' ) AS 'shift_ids',
            group_concat( DATE_FORMAT( nss.work_date, '%Y-%m-%d' ) SEPARATOR ';' ) AS 'shift_dates'
            FROM
            nurse_schedule_shift nss where nss.deleted = 0
            <if test="pageReqVO.queryFlag != null">
                AND DATE_FORMAT( nss.work_date, '%Y-%m' ) = #{pageReqVO.scheduleDate}
            </if>
        GROUP BY nss.schedule_id,nss.nurse_id
            ) temp_schedule_shift
            ON temp_schedule_shift.nurse_id = su.id AND su.deleted = 0
        LEFT JOIN system_post sp ON FIND_IN_SET(sp.id, REPLACE(su.post_ids, ' ', '')) AND sp.deleted = 0
        LEFT JOIN nurse_schedule nsc ON temp_schedule_shift.schedule_id = nsc.id AND nsc.schedule_date = #{pageReqVO.scheduleDate} AND nsc.deleted = 0
        WHERE
            su.dept_id = #{pageReqVO.deptId}
            <if test="pageReqVO.queryFlag != null">
                AND (nsc.schedule_date = #{pageReqVO.scheduleDate} OR nsc.schedule_date is NULL)
            </if>
            <if test="pageReqVO.orgType != null">
                AND su.org_type = #{pageReqVO.orgType}
            </if>
            <if test="pageReqVO.tenantIds != null">
                AND su.tenant_id in
                <foreach item="id" collection="pageReqVO.tenantIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        ORDER BY sp.sort ASC
    </select>

    <insert id="batchInsert"  parameterType="java.util.List">
        INSERT INTO nurse_schedule_shift (nurse_id, shift_id, shift_name, schedule_id, work_date, type, tenant_id)
        VALUES
        <foreach item="item" index="index" collection="list" open="(" separator="),(" close=")">
            #{item.nurseId}, #{item.shiftId}, #{item.shiftName}, #{item.scheduleId}, #{item.workDate}, #{item.type}, #{item.tenantId}
        </foreach>
    </insert>

    <update id="deleteWorkDate">
        UPDATE nurse_schedule_shift
        SET deleted = 1
        WHERE nurse_id = #{nurseId}
          AND schedule_id = #{scheduleId}
          AND  DATE_FORMAT(work_date,'%Y-%m') = #{workDate}
          AND deleted = 0
    </update>


</mapper>