<?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.yeejoin.amos.boot.module.common.api.mapper.DutyPersonShiftMapper">
    <select id="calMaxPersonAndShiftNum" resultType="java.util.Map">
        select
        (select
             IFNULL(max(num),0)
        from
        (SELECT
        ifnull(count(1),0) num
        FROM
        cb_duty_person_shift s,
        cb_duty_shift ds,
        cb_dynamic_form_instance i
        WHERE
        ds.sequence_nbr = s.shift_id
        and s.instance_id = i.instance_id
        AND i.field_code = 'userId'
        and s.duty_date >= #{beginDate}
        and s.duty_date <![CDATA[<=]]> #{endDate}
        AND s.shift_id is not null <!--// BUG 2807 更新人员车辆排版值班的保存逻辑 如果没有填写数据则保存空数据 。 同步修改 查询 导出相关逻辑 by kongfm 2021-09-14-->
        and s.app_Key = #{appKey}
        GROUP BY s.duty_date,s.shift_id <!--增添分组条件 根据班次分组技术 -->
        ) a) as maxDutyPersonNumDay,
        (select
             IFNULL(max(num),0)
        from
        (SELECT
        ifnull(count(DISTINCT(s.shift_id)),0) num
        FROM
        cb_duty_person_shift s,
        cb_duty_shift ds,
        cb_dynamic_form_instance i
        WHERE
          ds.sequence_nbr = s.shift_id
          and s.instance_id = i.instance_id
          AND i.field_code = 'userId'
          and s.duty_date >= #{beginDate}
          and s.duty_date <![CDATA[<=]]> #{endDate}
          and s.app_Key = #{appKey}
        GROUP BY s.duty_date) b) as maxShiftNumDay
    </select>
    <select id="bankViewData" resultType="java.util.Map">
        SELECT
            S1.*,
            S2.name as shiftName
        from
        (SELECT
            s.shift_id as shiftId,
            GROUP_CONCAT(i.field_value) AS userName
        FROM
            cb_duty_person_shift s,
            cb_duty_shift ds,
            cb_dynamic_form_instance i
        WHERE
            s.instance_id = i.instance_id
          AND ds.sequence_nbr = s.shift_id
          AND i.field_code = 'userName'
          AND s.duty_date = #{dutyDate}
          AND s.shift_id is not null
          AND s.app_key = #{appKey}
          and i.group_code =#{groupCode}
        GROUP BY
             s.shift_id
        ) S1,
        cb_duty_shift S2
        where
        s1.shiftId = s2.sequence_nbr
    </select>
    <select id="stationViewData" resultType="java.util.Map">
        select
            i.field_value as postTypeName,
            count(1) as total
        from
            cb_duty_person_shift s,
            cb_dynamic_form_instance i
        where
            s.instance_id = i.instance_id
          and i.field_code = 'postTypeName'
          AND s.duty_date = #{dutyDate}
          AND s.shift_id is not null
          AND s.app_key = #{appKey}
          and i.group_code =#{groupCode}
        GROUP BY i.field_value
    </select>
    <select id="genRangeDate" resultType="map">
        SELECT
        DATE_FORMAT(DATE( DATE_ADD( #{beginDate}, INTERVAL @s DAY )),'%Y-%m-%d') AS date,
        @s := @s + 1 AS `index`
        FROM
        mysql.help_topic,
        ( SELECT @s := 0 ) temp
        WHERE
        @s <![CDATA[<=]]>  DATEDIFF(#{endDate},#{beginDate})
    </select>
</mapper>
