<?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.casic.missiles.mapper.business.BusinessBoardMapper">

    <sql id="baseQueryConditionOne">
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.staffId != null and request.staffId !=''">
            AND ms.id = #{request.staffId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND blei.end_time &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND blei.end_time &lt;= #{request.endTime}
        </if>
    </sql>

    <sql id="baseQueryConditionTwo">
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND blei.end_time &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND blei.end_time &lt;= #{request.endTime}
        </if>
    </sql>

    <sql id="baseOutputParamOne">
        mo.id, mo.dept_id, mo.organize_no, mo.organize_name,
    </sql>

    <sql id="baseOutputParamTwo">
        mo.dept_id AS deptId, ANY_VALUE(mo.organize_name) AS deptName, ms.id AS staffId, ANY_VALUE(ms.name) AS staffName,
    </sql>

    <sql id="baseOutputParamThree">
        mo.id, ANY_VALUE(mo.dept_id) AS deptId, ANY_VALUE(mo.organize_name) AS deptName,
    </sql>

    <select id="selectSamplesCompletedToday" resultType="com.casic.missiles.dto.business.board.BoardWorkStatisticsResponse">
        SELECT <include refid="baseOutputParamOne"/> COUNT(blei.id) AS samplesCompletedToday
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id AND DATE(blei.end_time) = DATE(NOW())
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        GROUP BY mo.id
    </select>

    <select id="selectCertificatesToday" resultType="com.casic.missiles.dto.business.board.BoardWorkStatisticsResponse">
        SELECT <include refid="baseOutputParamOne"/> COUNT(bcr.id) AS certificatesToday
        FROM meter_organize mo
        LEFT JOIN business_certificate_report bcr ON bcr.measure_dept_id = mo.dept_id
            AND DATE(bcr.issuance_date) = DATE(NOW()) AND bcr.approval_status = 7 AND bcr.is_del = 0
        WHERE mo.is_lab = 1 AND mo.is_del = 0 GROUP BY mo.id
    </select>

    <select id="selectExpireSamplesToday" resultType="com.casic.missiles.dto.business.board.BoardWorkStatisticsResponse">
        SELECT <include refid="baseOutputParamOne"/> COUNT(bo.id) AS expireSamplesToday
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id AND blei.measure_status IN (1,2,3)
        LEFT JOIN business_order bo ON bo.id = blei.order_id AND DATE(bo.require_over_time) = DATE(NOW()) <!--实验室检测记录中的委托书id个数等价于样品id个数-->
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        GROUP BY mo.id
    </select>

    <select id="selectSamplesThisYear" resultType="com.casic.missiles.dto.business.board.BoardWorkStatisticsResponse">
        SELECT <include refid="baseOutputParamOne"/> COUNT(bosr.order_id) AS samplesYear
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id
        LEFT JOIN business_order_sample_relation bosr ON bosr.order_id = blei.order_id AND bosr.sample_id = blei.sample_id
            AND YEAR(bosr.real_deliver_time) = YEAR(NOW()) <!--送达时间是今年的就代表今年的-->
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        GROUP BY mo.id
    </select>

    <select id="selectExpireSamplesThisYear" resultType="com.casic.missiles.dto.business.board.BoardWorkStatisticsResponse">
        SELECT <include refid="baseOutputParamOne"/> COUNT(bosr.id) AS expireSamplesYear
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id
        LEFT JOIN business_order bo ON bo.id = blei.order_id AND blei.end_time > bo.require_over_time
        LEFT JOIN business_order_sample_relation bosr ON bosr.order_id = bo.id AND bosr.sample_id = blei.sample_id
            AND YEAR(bosr.real_deliver_time) = YEAR(NOW())
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        GROUP BY mo.id
    </select>

    <select id="selectInMeasureExpireSamplesThisYear" resultType="com.casic.missiles.dto.business.board.BoardWorkStatisticsResponse">
        SELECT <include refid="baseOutputParamOne"/> COUNT(bosr.id) AS inMeasureExpireSamplesYear
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id
        LEFT JOIN business_order bo ON bo.id = blei.order_id AND blei.end_time > bo.require_over_time
        LEFT JOIN business_order_sample_relation bosr ON bosr.order_id = bo.id AND bosr.sample_id = blei.sample_id
            AND bosr.sample_status = 3 AND YEAR(bosr.real_deliver_time) = YEAR(NOW())
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        GROUP BY mo.id
    </select>

    <select id="selectStaffSamples" resultType="com.casic.missiles.dto.business.board.BoardDeptOutputResponse">
        SELECT <include refid="baseOutputParamTwo"/> COUNT(blei.id) AS samples
        FROM meter_organize mo
        JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
        LEFT JOIN business_lab_executive_info blei ON blei.measure_person_id = ms.id AND blei.measure_status = 4
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <include refid="baseQueryConditionOne"/>
        <if test="request.year != null and request.year !=''">
            AND YEAR(blei.end_time) = #{request.year}
        </if>
        <if test="request.month != null and request.month !=''">
            AND MONTH(blei.end_time) = #{request.month}
        </if>
        GROUP BY mo.dept_id, ms.id
    </select>

    <select id="selectStaffCertificates" resultType="com.casic.missiles.dto.business.board.BoardDeptOutputResponse">
        SELECT <include refid="baseOutputParamTwo"/> COUNT(bcr.id) AS certificates
        FROM meter_organize mo
        JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
        LEFT JOIN business_certificate_report bcr ON bcr.measure_person_id = ms.id AND bcr.approval_status = 7 AND bcr.is_del = 0
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.staffId != null and request.staffId !=''">
            AND ms.id = #{request.staffId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND bcr.issuance_date &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND bcr.issuance_date &lt;= #{request.endTime}
        </if>
        <if test="request.year != null and request.year !=''">
            AND YEAR(bcr.issuance_date) = #{request.year}
        </if>
        <if test="request.month != null and request.month !=''">
            AND MONTH(bcr.issuance_date) = #{request.month}
        </if>
		GROUP BY mo.dept_id, ms.id
    </select>

    <select id="selectActualAmount" resultType="com.casic.missiles.dto.business.board.BoardDeptOutputResponse">
        SELECT <include refid="baseOutputParamTwo"/> IFNULL(SUM(fapi.actual_payment_amount), 0) AS actualAmount
		FROM meter_organize mo
        JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
        LEFT JOIN(
			SELECT DISTINCT ms.id AS staff_id, blei.order_id AS order_id
            FROM meter_organize mo
            JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
            LEFT JOIN business_lab_executive_info blei ON blei.measure_person_id = ms.id AND blei.measure_status = 4
			WHERE blei.order_id IS NOT NULL
            <include refid="baseQueryConditionOne"/>
		) mmb ON ms.id = mmb.staff_id
		LEFT JOIN finance_agreement_order_relation faor ON faor.order_id = mmb.order_id
		LEFT JOIN finance_agreement_payment_info fapi ON fapi.agreement_id = faor.agreement_id
		GROUP BY mo.dept_id, ms.id
    </select>

    <select id="selectSamplesTotal" resultType="com.casic.missiles.dto.business.board.BoardDeptOutputResponse">
        SELECT <include refid="baseOutputParamTwo"/> COUNT(bosr.sample_id) AS samplesTotal
		FROM meter_organize mo
        JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
		LEFT JOIN business_lab_executive_info blei ON blei.measure_person_id = ms.id AND blei.measure_status = 4
		LEFT JOIN finance_agreement_order_relation faor ON faor.order_id = blei.order_id<!--有合同的委托书-->
		LEFT JOIN business_order_sample_relation bosr ON bosr.order_id = faor.order_id
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <include refid="baseQueryConditionOne"/>
		GROUP BY mo.dept_id, ms.id
    </select>

    <select id="selectPersonalCertificatesTotal" resultType="com.casic.missiles.dto.business.board.BoardDeptOutputResponse">
        SELECT <include refid="baseOutputParamTwo"/> COUNT(bcr.id) AS personalCertificatesTotal
        FROM meter_organize mo
        JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
	    LEFT JOIN business_lab_executive_info blei ON blei.measure_person_id = ms.id AND blei.measure_status = 4
        LEFT JOIN business_certificate_report bcr ON bcr.measure_person_id = ms.id
            AND bcr.sample_id = blei.sample_id AND bcr.is_del = 0 <!--样品和人员均关联-->
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <include refid="baseQueryConditionOne"/>
		GROUP BY mo.dept_id, ms.id
    </select>

    <select id="selectCertificatesTotal" resultType="com.casic.missiles.dto.business.board.BoardDeptOutputResponse">
        SELECT <include refid="baseOutputParamTwo"/> COUNT(bcr.id) AS certificatesTotal
        FROM meter_organize mo
        JOIN meter_staff ms ON ms.dept_id = mo.dept_id AND ms.is_del = 0
        LEFT JOIN business_lab_executive_info blei ON blei.measure_person_id = ms.id AND blei.measure_status = 4
        LEFT JOIN business_certificate_report bcr ON bcr.sample_id = blei.sample_id AND bcr.is_del = 0 <!--仅样品关联-->
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <include refid="baseQueryConditionOne"/>
        GROUP BY mo.dept_id, ms.id
    </select>

    <select id="selectDeptSamples" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(bleii.id) AS samples
        FROM meter_organize mo
        LEFT JOIN
            (SELECT blei.id, blei.measure_dept_id
            FROM business_lab_executive_info blei
            WHERE blei.measure_status = 4
            <if test="request.startTime != null and request.startTime !=''">
                AND blei.end_time &gt;= #{request.startTime}
            </if>
            <if test="request.endTime != null and request.endTime !=''">
                AND blei.end_time &lt;= #{request.endTime}
            </if>
            ) bleii ON bleii.measure_dept_id = mo.dept_id
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        GROUP BY mo.id
    </select>

    <select id="selectDeptCertificates" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(bcr.id) AS certificates
        FROM meter_organize mo
        LEFT JOIN business_certificate_report bcr ON bcr.measure_dept_id = mo.dept_id AND bcr.approval_status = 7 AND bcr.is_del = 0
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND bcr.issuance_date &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND bcr.issuance_date &lt;= #{request.endTime}
        </if>
        GROUP BY mo.id
    </select>

    <select id="selectDeptSamplesTotal" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(blei.id) AS samplesTotal
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <include refid="baseQueryConditionTwo"/>
        GROUP BY mo.id
    </select>

    <select id="selectDeptExpireSamples" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(bosr.id) AS expireSamples
        FROM meter_organize mo
        LEFT JOIN business_lab_executive_info blei ON blei.measure_dept_id = mo.dept_id
        LEFT JOIN business_order bo ON bo.id = blei.order_id AND blei.end_time > bo.require_over_time
        LEFT JOIN business_order_sample_relation bosr ON bosr.order_id = bo.id AND bosr.sample_id = blei.sample_id
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND bosr.real_deliver_time &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND bosr.real_deliver_time &lt;= #{request.endTime}
        </if>
        GROUP BY mo.id
    </select>

    <select id="selectDeptDevices" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(efa.id) AS deptDevices
        FROM meter_organize mo
        LEFT JOIN equipment_fixed_assets efa ON efa.use_dept = mo.dept_id
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        GROUP BY mo.id
    </select>

    <select id="selectDeptExpireDevices" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(efa.id) AS deptExpireDevices
        FROM meter_organize mo
        LEFT JOIN equipment_fixed_assets efa ON efa.use_dept = mo.dept_id AND DATE(efa.valid_date) &lt; DATE(NOW())
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND efa.mesure_date &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND efa.mesure_date &lt;= #{request.endTime}
        </if>
        GROUP BY mo.id
    </select>

    <select id="selectDeptPassDevices" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(efa.id) AS deptPassDevices
        FROM meter_organize mo
        LEFT JOIN equipment_fixed_assets efa ON efa.use_dept = mo.dept_id AND (efa.mesure_result = 1 OR efa.mesure_result = 2)
        WHERE mo.is_lab = 1 AND mo.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND efa.mesure_date &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND efa.mesure_date &lt;= #{request.endTime}
        </if>
        GROUP BY mo.id
    </select>

    <select id="selectDeptRepairDevices" resultType="com.casic.missiles.dto.business.board.BoardDeptComprehensiveResponse">
        SELECT <include refid="baseOutputParamThree"/> COUNT(eae.id) AS deptRepairDevices
        FROM meter_organize mo
        LEFT JOIN equipment_apply_equipment eae ON eae.use_dept = mo.dept_id
        JOIN equipment_apply ea ON ea.id = eae.apply_id AND ea.acceptance_check_id IS NOT NULL<!--该字段不空为检修保养验收单-->
        WHERE ea.is_del = 0 AND eae.is_del = 0
        <if test="request.deptId != null and request.deptId !=''">
            AND mo.dept_id = #{request.deptId}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            AND ea.create_time &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            AND ea.create_time &lt;= #{request.endTime}
        </if>
        <if test="businessKeys != null and businessKeys.size() > 0">
            AND ea.id IN
            <foreach collection='businessKeys' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
        GROUP BY mo.id
    </select>

</mapper>
