<?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.bonus.material.common.mapper.SelectMapper">
    <!--往来单位-->
    <select id="getUnitList" resultType="com.bonus.common.biz.domain.ProjectTreeNode">
        /*根据标段工程id关联协议查询往来单位*/
        SELECT
        *
        FROM
        (
        SELECT
        CONCAT( 'dw', sd.dept_id ) AS id,
        sd.dept_name AS NAME,
        0 AS parentId,
        1 AS LEVEL,
        null AS typeKey
        FROM
        sys_dept sd
        LEFT JOIN bm_unit bu ON sd.dept_id = bu.dept_id
        LEFT JOIN bm_agreement_info bai ON bu.unit_id = bai.unit_id
        AND bai.`status` = '1'
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        AND bp.del_flag = '0'
        WHERE
        bu.del_flag = '0'
        AND sd.del_flag = '0'
        AND sd.`status` = '0'
        <if test="projectId != null">
            AND bp.pro_id = #{projectId}
        </if>
        <if test="deptId != null">
            AND bu.dept_id = #{deptId}
        </if>

        UNION

        SELECT DISTINCT
        CONCAT( 'lx', bu.dept_id, '-', sda.dict_code ) AS id,
        sda.dict_label AS NAME,
        CONCAT( 'dw', bu.dept_id ) AS parentId,
        2 AS LEVEL,
        sda.dict_value AS typeKey
        FROM
        bm_unit bu
        LEFT JOIN sys_dict_data sda ON bu.type_id = sda.dict_code
        AND sda.dict_type = 'bm_unit_type'
        LEFT JOIN bm_agreement_info bai ON bu.unit_id = bai.unit_id
        AND bai.`status` = '1'
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        AND bp.del_flag = '0'
        WHERE
        bu.del_flag = '0'
        AND sda.STATUS = '0'
        <if test="projectId != null">
            AND bp.pro_id = #{projectId}
        </if>
        <if test="deptId != null">
            AND bu.dept_id = #{deptId}
        </if>

        UNION
        SELECT
        bu.unit_id AS id,
        bu.unit_name AS NAME,
        CONCAT( 'lx', bu.dept_id, '-', sda.dict_code ) AS parentId,
        3 AS LEVEL,
        sda.dict_value AS typeKey
        FROM
        bm_unit bu
        LEFT JOIN sys_dict_data sda ON bu.type_id = sda.dict_code
        AND sda.dict_type = 'bm_unit_type'
        LEFT JOIN bm_agreement_info bai ON bu.unit_id = bai.unit_id
        AND bai.`status` = '1'
        LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
        AND bp.del_flag = '0'
        WHERE
        bu.del_flag = '0'
        AND sda.STATUS = '0'
        <if test="projectId != null">
            AND bp.pro_id = #{projectId}
        </if>
        <if test="deptId != null">
            AND bu.dept_id = #{deptId}
        </if>
        ) ff
        ORDER BY
        LEVEL
    </select>


    <!--机具类型-->
    <select id="getMaTypeData" resultType="com.bonus.material.common.domain.vo.SelectVo">
        SELECT type_id AS id,
               type_name AS `name`
        FROM ma_type
        WHERE `level` = #{type} AND del_flag = '0'
    </select>

    <!--设备类型树-->
    <select id="getDeviceTypeTree" resultType="com.bonus.common.biz.domain.TreeNode">
        SELECT mt.type_id AS id,
            mt.type_name AS label,
            mt.parent_id AS parentId,
            mt.unit_name AS unitName,
            CASE mt.manage_type
            WHEN 0 THEN
            IFNULL(subquery0.num, 0)
            ELSE
            IFNULL(mt.storage_num, 0)
            END as num,
            mt.model_code AS modelCode,
            mt.manage_type AS manageType
        FROM ma_type mt
        left join (SELECT mt.type_id,
                    mt2.type_name AS             typeName,
                    mt.type_name  AS             typeModelName,
                    count(mm.ma_id) num
                    FROM ma_machine mm
                    LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                    LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                    WHERE mm.ma_code is not null and mm.ma_status in (1)
                    GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = mt.type_id
        WHERE mt.del_flag = '0'
        <if test="level!=null and level!=''">
            <if test="level == 2">
                AND mt.level IN ('1','2')
            </if>
            <if test="level == 3">
                AND mt.level IN ('1','2','3')
            </if>
            <if test="level == 4">
                AND mt.level IN ('1','2','3','4')
            </if>
        </if>
    </select>

    <!--资产属性-->
    <select id="getAssetAttributesCbx" resultType="com.bonus.material.common.domain.vo.SelectVo">
        SELECT prop_id AS id,
               prop_name AS `name`
        FROM ma_prop_info
        WHERE del_flag = '0'
        ORDER BY create_time
    </select>

    <!--机具厂家-->
    <select id="getDeviceFactoryCbx" resultType="com.bonus.material.common.domain.vo.SelectVo">
        SELECT supplier_id AS id,
               supplier AS `name`
               FROM ma_supplier_info
        WHERE del_flag = '0'
        ORDER BY create_time
    </select>

    <!--配件所属上级树-->
    <select id="getAccessoryTree" resultType="com.bonus.common.biz.domain.TreeNode">
        SELECT pa_id AS id,
               pa_name AS label,
               parent_id AS parentId,
               unit_id as unitName,
               level
        FROM ma_part_type
        WHERE del_flag = '0'
        ORDER BY create_time
    </select>

    <select id="getPartTree" resultType="com.bonus.common.biz.domain.TreeNode">
        SELECT pa_id AS id,
               pa_name AS label,
               parent_id AS parentId,
               level
        FROM ma_part_type
        WHERE del_flag = '0' and level != '3'
        ORDER BY create_time
    </select>

    <!--货架-->
    <select id="getGoodsShelvesCbx" resultType="com.bonus.common.biz.domain.TreeNode">
        SELECT house_id AS id,
               house_name AS label,
               parent_id AS parentId
        FROM ma_house_info
        WHERE del_flag = '0' AND status = '0'
    </select>

    <!--往来单位id和标段工程id获取协议信息-->
    <select id="getAgreementInfoById" resultType="com.bonus.material.common.domain.vo.AgreementVo">
        SELECT agreement_id AS agreementId,
               agreement_code AS agreementCode,
               is_slt AS isSlt
        FROM bm_agreement_info
        WHERE unit_id = #{unitId} AND project_id = #{projectId} AND status = '1'
    </select>

    <select id="getUseTypeTreeL4" resultType="com.bonus.common.biz.domain.TypeTreeNode">
        SELECT
            mt.type_id as typeId,
            mt1.type_name as materialName,
            mt.type_name as typeName,
            mt.parent_id as parentId,
            mt.unit_name as unitName,
            mt.unit_value as unitValue,
            mt.manage_type as manageType,
            SUM( CASE WHEN sai.agreement_id = #{agreementId} AND sai.STATUS = '0' THEN sai.num ELSE 0 END ) AS num,
            mt.LEVEL as level
        FROM
            ma_type mt
                LEFT JOIN slt_agreement_info sai ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        WHERE
            EXISTS ( SELECT 1 FROM slt_agreement_info sai2 WHERE sai2.type_id = mt.type_id AND sai2.agreement_id = #{agreementId}
                                                             AND sai2.STATUS = '0' and IFNULL(sai.lease_type,0) = 0 and sai2.num > 0)
        GROUP BY
            mt.type_id
    </select>

    <select id="getUseTypeTreeL3" resultType="com.bonus.common.biz.domain.TypeTreeNode">
        SELECT
        mt3.type_id as typeId,
        NULL as materialName,
        mt3.type_name as typeName,
        mt3.parent_id as parentId,
        mt3.unit_name as unitName,
        NULL as manageType,
        0 AS num,
        mt3.LEVEL as level
        FROM
        ma_type mt3 where type_id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getUseTypeTreeL21" resultType="com.bonus.common.biz.domain.TypeTreeNode">
        SELECT
        mt2.type_id as typeId,
        NULL as materialName,
        mt2.type_name as typeName,
        mt2.parent_id as parentId,
        mt2.unit_name as unitName,
        NULL as manageType,
        0 AS num,
        mt2.LEVEL as level
        FROM
        ma_type mt2 where type_id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>

        union

        SELECT
        mt1.type_id as typeId,
        NULL as materialName,
        mt1.type_name as typeName,
        mt1.parent_id as parentId,
        mt1.unit_name as unitName,
        NULL as manageType,
        0 AS num,
        mt1.LEVEL as level
        FROM
        ma_type mt1 left join (
        SELECT
        mt2.type_id as typeId,
        NULL as materialName,
        mt2.type_name as typeName,
        mt2.parent_id as parentId,
        mt2.unit_name as unitName,
        NULL as manageType,
        0 AS num,
        mt2.LEVEL as level
        FROM
        ma_type mt2) mt2 on mt2.parentId = mt1.type_id  where mt2.typeId in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getProjectList" resultType="com.bonus.common.biz.domain.ProjectTreeNode">
        /*根据往来单位id关联协议查询工程*/
            SELECT
            *
            FROM
            (
            SELECT
            bmp.pro_id AS id,
            bmp.pro_name AS name,
            concat( 'gs', bmp.imp_unit ) AS parentId,
            2 AS level
            FROM
            bm_project bmp
            LEFT JOIN sys_dept sd ON sd.dept_id = bmp.imp_unit
            LEFT JOIN bm_agreement_info bai ON bai.project_id = bmp.pro_id
            AND bai.`status` = '1'
            LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
            AND bu.del_flag = '0'
            WHERE
            sd.del_flag = '0'
            AND sd.`status` = '0'
            AND bmp.del_flag = '0'
            <if test="unitId != null">
                AND bu.unit_id = #{unitId}
            </if>
            <if test="deptId != null">
                AND bu.dept_id = #{deptId}
            </if>
            UNION
            SELECT DISTINCT
            concat( 'gs', sd.dept_id ) AS id,
            sd.dept_name AS proName,
            '0' AS parentId,
            1 AS level
            FROM
            sys_dept sd
            LEFT JOIN bm_project bmp ON sd.dept_id = bmp.imp_unit
            LEFT JOIN bm_agreement_info bai ON bai.project_id = bmp.pro_id
            AND bai.`status` = '1'
            LEFT JOIN bm_unit bu ON bu.unit_id = bai.unit_id
            WHERE
            bmp.pro_id IS NOT NULL
            AND sd.del_flag = '0'
            AND sd.`status` = '0'
            AND bmp.del_flag = '0'
            <if test="unitId != null">
                AND bu.unit_id = #{unitId}
            </if>
            <if test="deptId != null">
                AND bu.dept_id = #{deptId}
            </if>
            ) a
            ORDER BY
            level
    </select>

    <select id="getUnitListApp" resultType="com.bonus.material.basic.domain.BmUnit">
        /*根据标段工程id关联协议查询往来单位*/
        <if test="projectId != null">
            SELECT DISTINCT bu.unit_id AS unitId,
                    bu.unit_name AS unitName
            FROM bm_project bpl
            LEFT JOIN bm_agreement_info bai ON bpl.pro_id = bai.project_id AND bai.`status` = '1'
            LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id AND bu.del_flag = '0'
            WHERE bpl.pro_id = #{projectId} AND bpl.del_flag = '0'
            <if test="deptId != null and deptId != 0">
                AND bpl.imp_unit = #{deptId}
            </if>
        </if>
        <if test="projectId == null">
            SELECT unit_id AS unitId,
                   unit_name AS unitName
            FROM bm_unit
            WHERE del_flag = '0'
            <if test="deptId != null and deptId != 0">
                AND bpl.imp_unit = #{deptId}
            </if>
        </if>
    </select>

    <select id="getProjectListApp" resultType="com.bonus.material.basic.domain.BmProject">
        /*根据往来单位id关联协议查询工程*/
        <if test="unitId != null">
            SELECT DISTINCT bp.pro_id AS proId,
                   bp.pro_name AS proName
            FROM bm_unit bu
            LEFT JOIN bm_agreement_info bai ON bu.unit_id = bai.unit_id AND bai.`status` = '1'
            LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id AND bp.del_flag = '0'
            WHERE bu.unit_id = #{unitId} AND bu.del_flag = '0'
        </if>
        <if test="unitId == null">
            SELECT pro_id AS proId,
                   pro_name AS proName
            FROM bm_project
            WHERE del_flag = '0'
        </if>
    </select>

    <select id="getUseTypeTreeApp" resultType="com.bonus.common.biz.domain.TypeTreeNode">
        SELECT
            mt.type_id as typeId,
            mt1.type_name as materialName,
            mt.type_name as typeName,
            mt.parent_id as parentId,
            mt.unit_name as unitName,
            mt.manage_type as manageType,
            SUM( CASE WHEN sai.agreement_id = #{agreementId} AND sai.STATUS = '0' THEN sai.num ELSE 0 END ) AS num,
            mt.LEVEL as level
        FROM
            ma_type mt
                LEFT JOIN slt_agreement_info sai ON mt.type_id = sai.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        WHERE
            EXISTS ( SELECT 1 FROM slt_agreement_info sai2 WHERE sai2.type_id = mt.type_id AND sai2.agreement_id = #{agreementId}
                                                             AND sai2.STATUS = '0' and IFNULL(sai.lease_type,0) = 0 and sai2.num > 0)
            and mt.manage_type = '1'
        GROUP BY
            mt.type_id
    </select>

    <select id="getMaType" resultType="com.bonus.material.common.domain.vo.SelectVo">
        SELECT type_id AS typeId,
               type_name AS name,
               parent_id AS parentId,
               level AS level,
               unit_name AS unitName
        FROM ma_type
        WHERE del_flag = '0'
        <if test="level != null and level != ''">
            and level = #{level}
        </if>
        <if test="typeId != null">
            and parent_id = #{typeId}
        </if>
    </select>
    <select id="getConfigList" resultType="com.bonus.common.biz.domain.ProjectTreeNode">
        SELECT id,
               `name`,
               parent_id AS parentId,
               `level`
        FROM bm_standard_config
        WHERE del_flag = 0
    </select>
    <select id="getBranchProject" resultType="com.bonus.material.common.domain.vo.SelectVo">
        SELECT dict_code  as id,
               dict_label as `name`
        FROM sys_dict_data
        WHERE dict_type = 'branch_project'
          and `status` = 0
    </select>
</mapper>
