<?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.meter.MeterOrganizeMapper">
    <select id="selectMaxOrganizeNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(organize_no, 12)), 0) FROM meter_organize
    </select>

    <select id="selectListByLabCapacity" resultType="com.casic.missiles.dto.business.labExecutive.SampleHandOutInitListResponse">
        SELECT dept_id AS measureSegmentId, organize_name AS measureSegment
        FROM meter_organize
        WHERE is_del = 0
        AND<!--同一项目若可能有多个实验室可以检测，该查询返回数量会大于检定项目数量-->
        <foreach collection="list" item="item" open="(" separator="or" close=")">
            lab_capacity LIKE concat('%',#{item},'%')
        </foreach>
    </select>

    <select id="selectDeptDirectorByDeptId" resultType="java.lang.Long">
        SELECT su.ID AS id
        FROM  sys_user su
        JOIN meter_organize mo ON mo.director = su.NAME
        WHERE su.DEPT_ID = #{deptId}
        LIMIT 1
    </select>

    <select id="selectStaffCount" resultType="com.casic.missiles.dto.cockpit.LabAssetsResponse">
        SELECT mo.dept_id, mo.organize_name AS deptName, COUNT(ms.id) AS staffCount
        FROM meter_organize mo
        LEFT JOIN meter_staff ms ON (mo.dept_id = ms.dept_id AND ms.is_del = 0)
        WHERE mo.is_del = 0 AND is_lab = 1
        GROUP BY mo.dept_id, mo.organize_name
    </select>
</mapper>
