<?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.ruoyi.oa.mapper.SysOaWarehouseMasterMapper">


    <!-- ===================================================== -->
    <!--  主表 + 子表 统一 ResultMap                            -->
    <!-- ===================================================== -->
    <resultMap id="SysOaWarehouseMasterResult"
               type="com.ruoyi.oa.domain.vo.SysOaWarehouseMasterVo">

        <id     property="masterId"    column="master_id"/>
        <result property="masterNum"   column="master_num"/>
        <result property="type"        column="type"/>
        <result property="projectId"   column="project_id"/>
        <result property="signTime"    column="sign_time"/>
        <result property="signUser"    column="sign_user"/>
        <result property="remark"      column="remark"/>
        <result property="projectName" column="project_name"/>
        <result property="status"      column="status"/>
        <result property="nearestEndTime"      column="nearest_end_time"/>
        <result property="logCount"    column="logCount"/>
        <result property="returnType" column="return_type"/>
        <result property="withdrawLock" column="withdraw_lock"/>
        <result property="requirementId" column="requirement_id"/>
        <result property="requirementName" column="requirementName"/>

        <collection property="warehouseList"
                    column="master_id"
                    javaType="list"
                    ofType="com.ruoyi.oa.domain.vo.SysOaWarehouseDetailVo"
                    select="selectDetailByMasterId"/>

        <collection property="warehouseTaskList"
                    column="master_id"
                    javaType="list"
                    ofType="com.ruoyi.oa.domain.SysOaWarehouseTask"
                    select="selectTaskByMasterId"/>
    </resultMap>

    <!-- ===================================================== -->
    <!--  分页查询（只查主表字段，真正参与 LIMIT / OFFSET）       -->
    <!-- ===================================================== -->
    <select id="selectVoPagePlus"
            resultMap="SysOaWarehouseMasterResult">
        SELECT
            sowm.master_id,
            sowm.master_num,
            sowm.type,
            sowm.project_id,
            sowm.sign_time,
            sowm.sign_user,
            sowm.remark,
            sowm.status,
            sowm.is_like,
            ${ew.sqlSelect},
            sop.project_name,
            req.title AS requirementName
        FROM sys_oa_warehouse_master sowm
                 LEFT JOIN sys_oa_project sop ON sop.project_id = sowm.project_id
                 LEFT JOIN oa_requirements req ON req.requirement_id = sowm.requirement_id
            ${ew.getCustomSqlSegment}
    </select>

    <!-- ===================================================== -->
    <!--  分页查询（包含日志操作次数）                          -->
    <!-- ===================================================== -->
    <select id="selectVoPagePlusWithLogCount"
            resultMap="SysOaWarehouseMasterResult">
        SELECT
            sowm.master_id,
            sowm.master_num,
            sowm.type,
            sowm.project_id,
            sowm.sign_time,
            sowm.sign_user,
            sowm.remark,
            sowm.status,
            sowm.return_type,
            sowm.withdraw_lock,
            ${ew.sqlSelect},
            sop.project_name,
            IFNULL(log_count.log_count, 0) AS logCount
        FROM sys_oa_warehouse_master sowm
                 LEFT JOIN sys_oa_project sop
                           ON sop.project_id = sowm.project_id
                 LEFT JOIN (
                     SELECT 
                         master_id,
                         COUNT(1) AS log_count
                     FROM sys_oa_warehouse_log
                     WHERE del_flag = 0
                     GROUP BY master_id
                 ) log_count ON log_count.master_id = sowm.master_id
            ${ew.getCustomSqlSegment}
    </select>

    <select id="selectDetailByMasterId"
            parameterType="long"
            resultType="com.ruoyi.oa.domain.vo.SysOaWarehouseDetailVo">
        SELECT
            sowd.id                     AS id,
            sowd.master_id,
            sowd.sign_price,
            sowd.amount,
            sowd.project_id,
            sowd.remark,
            sow.brand,
            sow.name                    AS warehouseName,
            sow.unit,
            COALESCE(sow.model,  sowt.model)               AS model,
            COALESCE(sow.brand,  sowt.brand)               AS brand,
            COALESCE(sow.specifications, sowt.specifications) AS specifications,
            sowt.task_inventory,
            sop.project_name
        FROM sys_oa_warehouse_detail sowd
                 LEFT JOIN sys_oa_warehouse         sow  ON sow.id         = sowd.warehouse_id
                 LEFT JOIN sys_oa_warehouse_task    sowt ON sowt.master_id = sowd.master_id
                 LEFT JOIN sys_oa_project           sop  ON sop.project_id = sowd.project_id
        WHERE sowd.del_flag = '0'
          AND sowd.master_id = #{masterId}
        ORDER BY sowd.id
    </select>

    <!-- ===================================================== -->
    <!--  子查询：任务列表 by masterId                          -->
    <!-- ===================================================== -->
    <select id="selectTaskByMasterId"
            parameterType="long"
            resultType="com.ruoyi.oa.domain.SysOaWarehouseTask">
        SELECT
            task_id,
            master_id,
            name,
            model,
            task_inventory,
            brand,
            specifications
        FROM sys_oa_warehouse_task
        WHERE master_id = #{masterId}
        ORDER BY task_id
    </select>

</mapper>
