<?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.yeejoin.equipmanage.mapper.EquipmentCategoryMapper">

    <resultMap id="CategoryAmount" type="com.yeejoin.equipmanage.common.entity.vo.EquipTypeAmountVO">
        <result column="id" property="id"></result>
        <result column="amount" property="amount"></result>
        <result column="categoryName" property="categoryName"></result>
        <result column="equipmentName" property="equipmentName"></result>
        <result column="unitName" property="unitName"></result>
    </resultMap>

    <resultMap id="Category" type="com.yeejoin.equipmanage.common.entity.EquipmentCategory">
        <result column="id" property="id"></result>
        <result column="parent_id" property="parentId"></result>
        <result column="CODE" property="code"></result>
        <result column="NAME" property="name"></result>
        <result column="is_consumptive" property="isConsumptive"></result>
        <result column="description" property="description"></result>
        <result column="remark" property="remark"></result>
        <result column="create_date" property="createDate"></result>
        <result column="type" property="type"></result>
        <result column="industry_code" property="industryCode"></result>
    </resultMap>

    <select id="getEquipCategoryListByParentId" resultType="Map">
		SELECT
		id,parent_id parentId,name 'name',code 'code',is_consumptive
		isConsumptive,curLevel
		FROM
		(
		SELECT
		t1.id,t1.name,t1.parent_id,t1.code,t1.is_consumptive,
		CASE
		WHEN RIGHT(
		code, 2 ) = "00" THEN
		1
		ELSE
		2
		END AS curLevel,
		IF( find_in_set( parent_id,
		@pids ) > 0, @pids := concat( @pids, ',', id
		), 0 ) OR IF(t1.parent_id
		= #{parentId},1,0) AS ischild
		FROM
		( SELECT
		id,name,parent_id,code,is_consumptive,1 AS curLevel FROM
		wl_equipment_category t ORDER BY id ) t1,
		( SELECT @pids :=
		#{parentId}) t2
		) t3
		WHERE
		ischild != 0
	</select>

    <select id="getEquipCategoryListcount"
            resultType="java.util.Map" parameterType="java.util.Map">
        select
        (select wl_equipment_category.`name` from wl_equipment_category
        where
        wl_equipment_category.parent_id=#{ids.id} and
        left(wl_equipment_category.`code`, 2) = d.ercode) name,
        d.type,
        d.ercode ,SUM(d.amount) amount from statisticsequipment_view d
        <where>
            <if test="ids.ids != null and ids.ids.size() > 0">
                and d.ercode in
                <foreach item="item" collection="ids.ids" open="("
                         separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <!--区域 -->
            <if
                    test="ids.warehouseStructureId != null and ids.warehouseStructureId != '' ">
                and d.warehouseStructureId =#{ids.warehouseStructureId}
            </if>
            <!-- 规格型号 -->
            <if test="ids.standard != null and ids.standard != '' ">
                and d.standard =#{ids.standard}
            </if>
            <!--状态 -->
            <if test="ids.StateName != null and ids.StateName != '' ">
                and d.StateName =#{ids.StateName}
            </if>
            <!--品牌 -->
            <if test="ids.brand != null and ids.brand != '' ">
                and d.brand =#{ids.brand}
            </if>
        </where>

        GROUP BY d.ercode ,d.type
    </select>


    <select id="getEquipmentCount" resultType="java.util.Map"
            parameterType="java.util.Map">
        select
        d.name_mx name,
        d.type,
        d.ercode ,
        SUM(d.amount) amount,
        un.name as unit,
        wle.img
        from
        statisticsequipment_view d
        left join wl_equipment as wle on d.code = wle.code
        left join wl_unit as un on wle.unit_id = un.id
        where
        d.ercode =#{date.ercode}
        <!--区域 -->
        <if
                test="date.warehouseStructureId != null and date.warehouseStructureId != '' ">
            and d.warehouseStructureId =#{date.warehouseStructureId}
        </if>
        <!-- 规格型号 -->
        <if test="date.standard != null and date.standard != '' ">
            and d.standard =#{date.standard}
        </if>
        <!--状态 -->
        <if test="date.StateName != null and date.StateName != '' ">
            and d.StateName =#{date.StateName}
        </if>
        <!--品牌 -->
        <if test="date.brand != null and date.brand != '' ">
            and d.brand =#{date.brand}
        </if>

        GROUP BY d.name_mx,d.type

    </select>

    <select id="getDetailed" resultType="java.util.Map"
            parameterType="java.util.Map">
        select
        d.name_mx name,
        IFNULL(d.StateName,'') StateName,
        IFNULL(
        d.brand,'') brand,
        IFNULL( d.standard,'')standard,
        IFNULL(
        d.qr_code,'')qr_code,
        d.ercode ,
        d.id_mx id,
        d.amount,
        wle.img,
        wu.name as utname
        from
        statisticsequipment_view d
        left join wl_equipment as wle on d.code = wle.code
        left join wl_unit as wu on wu.id = wle.unit_id
        where d.ercode =#{date.ercode}
        <!--区域 -->
        <if
                test="date.warehouseStructureId != null and date.warehouseStructureId != '' ">
            and d.warehouseStructureId =#{date.warehouseStructureId}
        </if>
        <!-- 规格型号 -->
        <if test="date.standard != null and date.standard != '' ">
            and d.standard =#{date.standard}
        </if>
        <!--状态 -->
        <if test="date.StateName != null and date.StateName != '' ">
            and d.StateName =#{date.StateName}
        </if>
        <!--品牌 -->
        <if test="date.brand != null and date.brand != '' ">
            and d.brand =#{date.brand}
        </if>
        <!--名称-->
        <if test="date.name != null and date.name != '' ">
            and d.name_mx =#{date.name}
        </if>
    </select>

    <select id="getEquipCategoryLevelByParentId" resultType="int">
		SELECT
		(count(1)) as 'Level' FROM wl_equipment_category WHERE
		FIND_IN_SET(id,getParent(#{parentId}));
	</select>

    <select id="getEquipCategoryCodeListByParentId" resultType="Map">
		SELECT
		temp.*,
		CONCAT((CASE
		WHEN temp.curLevel = 1 THEN
		IFNULL(max(c.
		CODE), temp. CODE) + 1000000
		WHEN temp.curLevel = 2 THEN
		IFNULL(max(c.
		CODE), temp. CODE) + 10000
		WHEN temp.curLevel = 3 THEN
		IFNULL(max(c.
		CODE), temp. CODE) + 100
		WHEN temp.curLevel = 4 THEN
		IFNULL(max(c.
		CODE), temp. CODE) + 1
		END),'') AS newCode
		FROM
		(
		SELECT
		ec.*, IFNULL(
		(
		SELECT
		count(1) + 1 AS LEVEL
		FROM
		wl_equipment_category
		WHERE
		FIND_IN_SET(id, getParent(ec.parent_id))
		),
		1
		) curLevel
		FROM
		wl_equipment_category ec
		WHERE
		ec.id = #{parentId}
		) temp
		LEFT JOIN
		wl_equipment_category c ON temp.id = c.parent_id
		WHERE
		temp.curLevel <![CDATA[ < ]]>
		5
		AND c. NAME NOT LIKE '%其他%'
		OR c. NAME IS NULL
	</select>

    <select id="selectCategoryAmount" resultType="java.lang.Integer">
		SELECT
			ifnull(sum( wlst.amount),0)
		FROM
			wl_equipment wle
			LEFT JOIN wl_equipment_detail wled ON wle.id = wled.equipment_id
			LEFT JOIN wl_stock wlst ON wlst.equipment_detail_id = wled.id
		    LEFT JOIN wl_equipment_category wec on wec.id = wle.category_id
		WHERE
			LEFT ( wle.CODE, 1 ) = #{codeHead}
			AND wlst.org_code = #{orgCode}
            AND wec.industry_code = #{industryCode}
	</select>

    <select id="CategoryAmountList" resultMap="CategoryAmount">
        SELECT
        wled.id,
        wled.name equipmentName,
        wlec.name categoryName,
        wlun.name unitName,
        sum(ifnull(wlst.amount,0)) amount
        FROM
        wl_equipment_detail wled
        LEFT JOIN wl_equipment wle ON wle.id = wled.equipment_id
        left join wl_equipment_category wlec on wlec.id = wle.category_id
        LEFT JOIN wl_stock wlst ON wlst.equipment_detail_id = wled.id
        left join wl_unit wlun on wle.unit_id = wlun.id
        WHERE
        LEFT (wle.CODE, #{hierarchy}) = #{codeHead}
        AND wlst.org_code = #{orgCode}
        <if test="equipmentName!=null and equipmentName!=''">
            AND wle.name like concat('%',#{equipmentName},'%')
        </if>
        group by equipmentName
    </select>
    <select id="checkFather" resultType="int">
		select sum(cut) from
			( select
			count(1) as cut from
			 wl_equipment_category
			 where parent_id = #{id}
			 union
			 select
			count(1) as cut from
			 wl_equipment
			 where category_id=  #{id}
			 ) as cuts
	</select>
    <select id="islvlOne" resultType="int">
		 select
		 count(1)
		 from wl_equipment_category
		 where id =#{id}
		 and parent_id is null
	</select>
	<select id="checkUsed" resultType="int">
		 select sum(tt) from (
        select count(1)as tt from wl_equipment where category_id  = #{id}
        union all
        select count(1)as tt from f_fire_fighting_system where system_type  = #{id} ) as tdata
	</select>

    <select id="getEquipmentCategoryList" resultMap="Category">
        SELECT ec.id,
               ec.parent_id,
               ec.CODE,
               ec.NAME,
               ec.is_consumptive,
               ec.description,
               ec.industry_code,
               ec.remark,
               ec.create_date,
               i.`name` as industryName,
               -- industry_code = 2代表消防设备
               IF(LEFT(ec.CODE, 1) = #{head} and ec.industry_code = 2, 'car', 'equipment')
                        AS type
        FROM wl_equipment_category ec
                 left join wl_industry i on ec.industry_code = i.code
    </select>

    <select id="getEquipmentCategoryListNotFacilities" resultMap="Category">
        SELECT ec.id,
               ec.parent_id,
               ec.CODE,
               ec.NAME,
               CONCAT(
                       ec.NAME,
                       CONCAT(
                               '(',
                               (
                                   SELECT
                                       count( 1 )
                                   FROM
                                       p_input_item pit
                                   WHERE
                                       pit.equipment_type = ec.CODE
                                     AND pit.is_delete = 0
                                     AND pit.biz_org_code LIKE concat( '%', #{bizOrgCode}, '%' )
                               ),
                               ')'
                           )
                   ) AS countNAME,
               ec.is_consumptive,
               ec.description,
               ec.industry_code,
               ec.remark,
               ec.create_date,
               i.`name` as industryName,
               -- industry_code = 2代表消防设备
               IF(LEFT(ec.CODE, 1) = #{head} and ec.industry_code = 2, 'car', 'equipment')
                        AS type
        FROM wl_equipment_category ec
                 left join wl_industry i on ec.industry_code = i.code
        WHERE
            ec.code NOT LIKE CONCAT ('930','%') and ec.code NOT LIKE CONCAT ('92','%') and ec.industry_code = '2'
    </select>

    <select id="getEquipmentCategoryListByFacilities" resultMap="Category">
        SELECT ec.id,
        ec.parent_id,
        ec.CODE,
        ec.NAME,
        CONCAT(
        ec.NAME,
        CONCAT(
        '(',
        (
        SELECT
        count( 1 )
        FROM
        p_input_item pit
        WHERE
        pit.equipment_type = ec.CODE
        AND pit.is_delete = 0
        AND pit.biz_org_code LIKE concat( '%', #{bizOrgCode}, '%' )
        ),
        ')'
        )
        ) AS countNAME,
        ec.is_consumptive,
        ec.description,
        ec.industry_code,
        ec.remark,
        ec.create_date,
        i.`name` as industryName,
        IF(LEFT(ec.CODE, 1) = 2 and ec.industry_code = 2, 'car', 'equipment')
        AS type
        FROM wl_equipment_category ec
        left join wl_industry i on ec.industry_code = i.code
        WHERE
        ec.code LIKE CONCAT ('930','%')
        UNION ALL
        SELECT ec.id,
        ec.parent_id,
        ec.code as CODE,
        ec.NAME,
        CONCAT(
        ec.NAME,
        CONCAT(
        '(',
        (
        SELECT
        count( 1 )
        FROM
        p_input_item pit
        WHERE
        pit.equipment_type = ec.CODE
        AND pit.is_delete = 0
        AND pit.biz_org_code LIKE concat( '%', 'LSHLZ1yOrS3WfXqzjn', '%' )
        ),
        ')'
        )
        ) AS countNAME,
        ec.is_consumptive,
        ec.description,
        ec.industry_code,
        ec.remark,
        ec.create_date,
        i.`name` as industryName,
        IF(LEFT(ec.CODE, 1) = 2 and ec.industry_code = 2, 'car', 'equipment')
        AS type
        FROM wl_equipment_category ec
        left join wl_industry i on ec.industry_code = i.code
        WHERE
        <!--            (ec.code LIKE CONCAT ('92','%') and ec.parent_id = '1302863809086623746') or (ec.code LIKE CONCAT ('92','%') and ec.parent_id = '846')-->
        ec.code like CONCAT ('92', '%')
    </select>



    <select id="getEquipmentCategoryCarList" resultMap="Category">
        SELECT
            id,
            parent_id,
            CODE,
            NAME,
            is_consumptive,
            description,
            remark,
            create_date
        FROM
            wl_equipment_category
            where LEFT ( CODE, 1 ) = '2' and industry_code = '2' -- industry_code = 2(消防行业code)
    </select>

    <select id="getEquipmentCategoryEquipmentList" resultMap="Category">
        SELECT
            id,
            parent_id,
            CODE,
            NAME,
            is_consumptive,
            description,
            remark,
            create_date
        FROM
            wl_equipment_category
            where LEFT ( CODE, 1 ) != '2' and industry_code = '2' -- industry_code = 2(消防行业code)
    </select>

    <select id="getByEqpDetailId" resultType="com.yeejoin.equipmanage.common.entity.EquipmentCategory">
        SELECT
            ec.*
        FROM
            wl_equipment_detail ed
                LEFT JOIN wl_equipment e ON e.id = ed.equipment_id
                LEFT JOIN wl_equipment_category ec ON ec.id = e.category_id
        WHERE 1=1
            <if test="eqpDetailId != null">
                and ed.id = #{eqpDetailId}
            </if>
    </select>

    <select id="getAllChildCategoryIdList" resultType="Long">
        SELECT
            id
        FROM
            (
                SELECT
                    t1.id,
                    IF
                        ( FIND_IN_SET( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), 0 ) AS isChild
                FROM
                        ( SELECT id, parent_id FROM wl_equipment_category ORDER BY id ) t1,
                        ( SELECT @pids := #{categoryId} ) t2
            ) t3
        WHERE
            t3.isChild != 0
    </select>
    <select id="getColaEquipmentCategoryList" resultMap="Category">
        SELECT
            id,
            parent_id,
            CODE,
            NAME,
            is_consumptive,
            description,
            remark,
            create_date,
        CASE
            LEFT ( CODE, 1 )
            WHEN #{head} THEN 'car'
            ELSE 'equipment'
            end
            AS type,
            industry_code
        FROM
            wl_equipment_category
        WHERE 1=1
            <if test="industryCode!=null and industryCode!=''">
                AND wl_equipment_category.industry_code = #{industryCode}
            </if>
    </select>



    <select id="getColaEquipmentCategoryListEqu" resultMap="Category">
        SELECT
        id,
        parent_id,
        CODE,
        NAME,
        is_consumptive,
        description,
        remark,
        create_date,
        CASE
        LEFT ( CODE, 1 )
        WHEN #{head} THEN 'car'
        ELSE 'equipment'
        end
        AS type,
        industry_code
        FROM
        wl_equipment_category
        WHERE   LEFT ( CODE, 1 ) != '2'
        <if test="industryCode!=null and industryCode!=''">
            AND wl_equipment_category.industry_code = #{industryCode}
        </if>
    </select>


    <select id="getColaEquipmentCategoryListCar" resultMap="Category">
        SELECT
        id,
        parent_id,
        CODE,
        NAME,
        is_consumptive,
        description,
        remark,
        create_date,
        CASE
        LEFT ( CODE, 1 )
        WHEN #{head} THEN 'car'
        ELSE 'equipment'
        end
        AS type,
        industry_code
        FROM
        wl_equipment_category
        WHERE  LEFT ( CODE, 1 ) = '2'
        <if test="industryCode!=null and industryCode!=''">
            AND wl_equipment_category.industry_code = #{industryCode}
        </if>
    </select>

    <select id="countByFireTypeAndCode" resultType="int">
        select count(*) from p_input_item
        where facilities_type like concat(#{code}, '%')
          AND biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
            AND input_type!='1'
    </select>

    <select id="countByEquipTypeAndCode" resultType="int">
        select count(*) from p_input_item
        where equipment_type like concat(#{code}, '%')
          AND biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
          AND input_type!='1'
    </select>

    <select id="countByKeyPartsType" resultType="int">
        select count(*) from p_input_item
        where key_parts_type is not null
          AND biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
          AND input_type!='1'
         AND key_parts_type='0'
    </select>

    <select id="countByCustomType" resultType="int">
        select count(*) from p_input_item
        where custom_type is not null
          AND biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
          AND input_type!='1'
          AND custom_type='0'
    </select>

    <select id="countAll" resultType="int">
        select count(*) from p_input_item
        where
           biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
          AND input_type!='1'
    </select>

    <select id="countByEquipType" resultType="int">
        select count(*) from p_input_item
        where equipment_type is not null
          AND biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
          AND input_type!='1'
    </select>

    <select id="countByFacilitiesType" resultType="int">
        select count(*) from p_input_item
        where facilities_type is not null
          AND biz_org_code like concat(#{bizOrgCode}, '%')
          AND is_delete = '0'
          AND input_type!='1'
    </select>
    <select id="getEquipItemCount" resultType="java.lang.String">
        SELECT
            CONCAT('(' , COUNT(1) ,')') as countName
        FROM
            p_input_item pit
        WHERE
            pit.equipment_type is not null
          AND is_delete = 0
          AND pit.biz_org_code LIKE concat( '%', #{bizOrgCode}, '%' )
    </select>
    <select id="getFireItemCount" resultType="java.lang.String">
        SELECT
          CONCAT('(' , COUNT(1) ,')') as countName
        FROM
            p_input_item pit
        WHERE
            pit.facilities_type is not null
          AND is_delete = 0
          AND pit.biz_org_code LIKE concat( '%', #{bizOrgCode}, '%' )
    </select>
</mapper>
