<?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.sgzb.material.mapper.SelectMapper">

    <!--往来单位-->
    <select id="getUnitData" resultType="com.bonus.sgzb.material.domain.SelectVo">
        /*根据标段工程id关联协议查询往来单位*/
        <if test="id != null and id != ''">
            SELECT DISTINCT bui.unit_id AS id,
                    bui.unit_name AS `name`
            FROM bm_project_lot bpl
            LEFT JOIN bm_agreement_info bai ON bpl.lot_id = bai.project_id AND bai.`status` = '1'
            LEFT JOIN bm_unit_info bui ON bai.unit_id = bui.unit_id AND bui.del_flag = '0'
            WHERE bpl.lot_id = #{id} AND bpl.del_flag = '0'
        </if>
        <if test="id == null or id == ''">
            SELECT unit_id AS id,
                   unit_name AS `name`
            FROM bm_unit_info
            WHERE del_flag = '0'
        </if>
    </select>

    <!--标段工程-->
    <select id="getProData" resultType="com.bonus.sgzb.material.domain.SelectVo">
        /*根据往来单位id关联协议查询工程*/
        <if test="id != null and id != ''">
            SELECT DISTINCT bpl.lot_id AS id,
                   bpl.lot_name AS `name`
            FROM bm_unit_info bui
            LEFT JOIN bm_agreement_info bai ON bui.unit_id = bai.unit_id AND bai.`status` = '1'
            LEFT JOIN bm_project_lot bpl ON bai.project_id = bpl.lot_id AND bpl.del_flag = '0'
            WHERE bui.unit_id = #{id} AND bui.del_flag = '0'
        </if>
        <if test="id == null or id == ''">
            SELECT lot_id AS id,
                   lot_name AS `name`
            FROM bm_project_lot
            WHERE del_flag = '0'
        </if>
    </select>

    <!--机具类型-->
    <select id="getMaTypeData" resultType="com.bonus.sgzb.material.domain.SelectVo">
        SELECT type_id AS id,
               type_name AS `name`
        FROM ma_type
        WHERE `level` = #{type} AND del_flag = '0'
    </select>
    <!--数据字典下拉选-->
    <select id="getDictByPidCbx" resultType="com.bonus.sgzb.material.domain.SelectVo">
        SELECT sd2.id,sd2.`name`
        FROM sys_dic sd
        LEFT JOIN sys_dic sd2 ON sd.id = sd2.p_id
        WHERE sd.`value` = #{parentValue} AND sd.p_id = 0
    </select>
    <!--单位树/归属部门/所属上级-->
    <select id="getDeptTree" resultType="com.bonus.sgzb.material.domain.TreeNode">
        SELECT dept_id AS id,
               parent_id AS parentId,
               dept_name AS label
        FROM sys_dept
        WHERE del_flag = '0'
        ORDER BY order_num
    </select>
    <!--岗位下拉选-->
    <select id="getPostCbx" resultType="com.bonus.sgzb.material.domain.SelectVo">
        SELECT post_id AS id,
               post_name AS `name`
        FROM sys_post
        WHERE status = '0'
        ORDER BY post_sort
    </select>
    <!--角色下拉选-->
    <select id="getRoleCbx" resultType="com.bonus.sgzb.material.domain.SelectVo">
        SELECT role_id AS id,
               role_name AS `name`
        FROM sys_role
        WHERE status = '0'
        ORDER BY role_sort
    </select>
    <!--单位类型下拉选-->
    <select id="getUnitTypeCbx" resultType="com.bonus.sgzb.material.domain.SelectVo">
        SELECT sd2.id,sd2.`name`
        FROM sys_dic sd
                 LEFT JOIN sys_dic sd2 ON sd.id = sd2.p_id
        WHERE sd.`value` = #{parentValue} AND sd.p_id = 0
    </select>
    <!--设备类型树-->
    <select id="getDeviceTypeTree" resultType="com.bonus.sgzb.material.domain.TreeNode">
        SELECT mt.type_id AS id,
            mt.type_name AS label,
            mt.parent_id AS parentId,
            mt.unit_name AS unitName,
            mt.company_id AS companyId,
            mt.code,
            CASE mt.manage_type
            WHEN 0 THEN
            IFNULL(subquery0.num, 0)
            ELSE
            IFNULL(mt.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 (15)
                    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.sgzb.material.domain.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.sgzb.material.domain.SelectVo">
        SELECT supplier_id AS id,
               supplier AS `name`
               FROM ma_supplier_info
        WHERE del_flag = '0'
        ORDER BY create_time
    </select>
    <!--工程项目-->
    <select id="getProCbx" resultType="com.bonus.sgzb.material.domain.SelectVo">
        SELECT pro_id AS id,
               pro_name AS `name`
        FROM bm_project_info
        WHERE del_flag = '0' AND status = '0'
        ORDER BY create_time
    </select>
    <!--配件所属上级树-->
    <select id="getAccessoryTree" resultType="com.bonus.sgzb.material.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.sgzb.material.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.sgzb.material.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>
    <!--用户/维修员/库管员/采购员-下拉选-->
    <select id="getUserByRoleIdCbxSelect" resultType="com.bonus.sgzb.material.domain.SelectVo">
            SELECT su.user_id AS id,
                   su.nick_name AS `name`
            FROM sys_role sr
            LEFT JOIN sys_user_role sur ON sr.role_id = sur.role_id
            LEFT JOIN sys_user su ON sur.user_id = su.user_id AND su.del_flag = '0'
            WHERE sr.role_key = #{roleKey} AND sr.del_flag = '0'
    </select>
    <!--用户/维修员/库管员/采购员-树-->
    <select id="getUserByRoleIdCbxTree" resultType="com.bonus.sgzb.material.domain.TreeNode">
        SELECT dept_id AS id,
               parent_id AS parentId,
               dept_name AS label
        FROM sys_dept
        WHERE del_flag = '0'
    UNION ALL
        SELECT su.user_id AS id,
               su.dept_id AS parentId,
               su.nick_name AS label
        FROM sys_role sr
        LEFT JOIN sys_user_role sur ON sr.role_id = sur.role_id
        LEFT JOIN sys_user su ON sur.user_id = su.user_id AND su.del_flag = '0'
        WHERE sr.role_key = #{roleKey} AND sr.del_flag = '0'
    </select>
    <!--往来单位id和标段工程id获取协议信息-->
    <select id="getAgreementInfoById" resultType="com.bonus.sgzb.material.domain.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>

</mapper>