<?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.cf.imes.module.system.dal.mysql.organ.OrganMapper">

    <select id="selectOrgSilentCount" resultType="java.lang.Integer">
        WITH	NoLoginOrgs AS (
            SELECT DISTINCT o.id
            FROM system_organization o
                     LEFT JOIN system_users ul ON o.id = ul.organ_id
                                AND Date (#{dateTime}) > ul.login_date
                                AND ul.deleted = 0
            WHERE o.deleted =0

        )

-- 查询这些沉寂组织的数量
        SELECT COUNT(*) AS count
        FROM NoLoginOrgs;
    </select>




    <select id="selectOrganDataSource" resultType="java.util.Map">

        select ${filed}  from system_organization where id = #{organId}  and deleted = false

    </select>


    <sql id="dateFormat">
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', QUARTER(o.update_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', MONTH(o.update_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', MONTH(o.update_time), '-', FLOOR((DayOfMonth(o.update_time)-1)/7)+1) AS date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', MONTH(o.update_time), '-', DAY(o.update_time)) as date
        </if>
    </sql>

    <select id="selectOrgCountLapseByOrderDate"
            resultType="com.cf.imes.module.system.api.organ.dto.OrgStatisticsIsLapseRespDTO">
        select count(o.id) as orgCount
        <include refid="dateFormat"/>
        from system_organization o
        where o.update_time between #{req.createTime[0]} and #{req.createTime[1]}
        and o.deleted = 1
        group by date;
    </select>

    <sql id="dateCreate">
    <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@QUARTER.getValue()">
        ,CONCAT(YEAR(o.create_time), '-', QUARTER(o.create_time)) as date
    </if>
    <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@MONTH.getValue()">
        ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time)) as date
    </if>
    <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@WEEK.getValue()">
        ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time), '-', FLOOR((DayOfMonth(o.create_time)-1)/7)+1) AS date
    </if>
    <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.OrderStatisticsUnit@DAY.getValue()">
        ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time), '-', DAY(o.create_time)) as date
    </if>
</sql>

    <select id="selectOrgCountAddByOrderDate"
            resultType="com.cf.imes.module.system.api.organ.dto.OrgStatisticsIsLapseRespDTO">
        select count(o.id) as orgCount
        <include refid="dateCreate"/>
        from system_organization o
        where o.create_time between #{req.createTime[0]} and #{req.createTime[1]}
        and o.deleted = 0
        group by date;
    </select>

    <select id="selectByContactMobile"
            resultType="com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO">

        select *

        from system_organization

        where contact_mobile = #{contactMobile}

        <if test="organId !=null ">
            and id != #{organId}
        </if>


    </select>

</mapper>