<?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">
        SELECT COUNT(*) AS count
        FROM system_organization o
        LEFT JOIN system_users ul
        ON o.id = ul.organ_id
        AND ul.deleted = 0
        AND DATE(#{dateTime}) &lt;= ul.login_date
        WHERE o.deleted = 0
        AND ul.id IS NULL;
    </select>

    <select id="selectOrgCountAdd">
        select count(*) from system_organization
        WHERE create_time BETWEEN #{startTime} AND #{endTime}
    </select>

    <select id="selectOrgCountDel">
        select count(*) from system_organization
        WHERE deleted = true
        AND update_time BETWEEN #{startTime} AND #{endTime}
    </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.StatisticsUnit@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.StatisticsUnit@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.StatisticsUnit@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.StatisticsUnit@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.createTimeForQry[0]} and #{req.createTimeForQry[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.StatisticsUnit@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.StatisticsUnit@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.StatisticsUnit@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.StatisticsUnit@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.createTimeForQry[0]} and #{req.createTimeForQry[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>
        and deleted = false


    </select>

    <select id="selectPageWithProductExpire"
            resultType="com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO">
        SELECT o.*
        FROM system_organization o
        <where>
            o.deleted = false
            <if test="reqVO.contactName != null and reqVO.contactName != ''">
                AND o.contact_name LIKE CONCAT('%', #{reqVO.contactName}, '%')
            </if>
            <if test="reqVO.contactMobile != null and reqVO.contactMobile != ''">
                AND o.contact_mobile LIKE CONCAT('%', #{reqVO.contactMobile}, '%')
            </if>
            <if test="reqVO.status != null">
                AND o.status = #{reqVO.status}
            </if>
            <if test="reqVO.exactName != null and reqVO.exactName != ''">
                AND o.name = #{reqVO.exactName}
            </if>
            <if test="reqVO.province != null and reqVO.province != ''">
                AND o.province = #{reqVO.province}
            </if>
            <if test="reqVO.city != null and reqVO.city != ''">
                AND o.city = #{reqVO.city}
            </if>
            <if test="reqVO.county != null and reqVO.county != ''">
                AND o.county = #{reqVO.county}
            </if>
            <if test="reqVO.expireTime != null and reqVO.expireTime.length == 2">
                AND o.expire_time BETWEEN #{reqVO.expireTime[0]} AND #{reqVO.expireTime[1]}
            </if>
            <if test="reqVO.remark != null and reqVO.remark != ''">
                AND o.remark LIKE CONCAT('%', #{reqVO.remark}, '%')
            </if>
            <if test="reqVO.name != null and reqVO.name != ''">
                AND (
                o.name LIKE CONCAT('%', #{reqVO.name}, '%')
                <if test="reqVO.pyAll != null and reqVO.pyAll != ''">
                    OR o.pinyin_full LIKE CONCAT('%', #{reqVO.pyAll}, '%')
                </if>
                <if test="reqVO.pyFirstChar != null and reqVO.pyFirstChar != ''">
                    OR o.pinyin_initial LIKE CONCAT('%', #{reqVO.pyFirstChar}, '%')
                </if>
                )
            </if>
            AND o.id IN (
            SELECT pr.organ_id
            FROM purchase_record pr
            LEFT JOIN (
            SELECT purchase_id, MAX(delay_time) as max_delay_time
            FROM product_delay_record
            WHERE deleted = false
            GROUP BY purchase_id
            ) pdr ON pr.id = pdr.purchase_id
            WHERE pr.initial = true
            AND pr.deleted = false
            AND pr.status = 1
            <if test="reqVO.endTime != null">
                AND COALESCE(pdr.max_delay_time, pr.end_time) >= #{reqVO.endTime[0]}
            </if>
            <if test="reqVO.endTime != null">
                AND COALESCE(pdr.max_delay_time, pr.end_time) &lt;= #{reqVO.endTime[1]}
            </if>
            <if test="reqVO.productName != null and reqVO.productName != ''">
                AND pr.product_name LIKE CONCAT('%', #{reqVO.productName}, '%')
            </if>
            <if test="reqVO.price != null">
                AND pr.total_amount = #{reqVO.price}
            </if>
            )
        </where>
        ORDER BY o.id DESC
    </select>

</mapper>