<?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="cn.ezhizao.project.business.equipment.mapper.BizEquipmentDetailMapper">

    <resultMap type="cn.ezhizao.project.business.equipment.domain.BizEquipmentDetail" id="BizEquipmentDetailResult">
        <id column="id" property="id"/>
        <association property="belongingDeptAlias" column="belonging_dept_id" select="getBelongingDeptAlias"/>
        <association property="usageDeptAlias" column="usage_dept_id" select="getUsageDeptAlias"/>
        <association property="categoryName" column="equipment_category_id" select="getCategoryName"/>
    </resultMap>
    <select id="getList" parameterType="BizEquipmentDetail" resultMap="BizEquipmentDetailResult">
        SELECT detail.*,equipment.equipment_name as equipmentName,equipment.standard as standard ,category.name as categoryName FROM biz_equipment_detail as detail
            left join biz_equipment equipment on detail.equipment_id = equipment.id
            left join biz_equipment_category AS category on equipment.equipment_category_id = category.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            detail.deleted = 0
            <if test="equipmentCategoryId != '' and equipmentCategoryId != null">AND  category.id = #{equipmentCategoryId}</if>
            <if test="equipmentCode != null  and equipmentCode != ''"> AND detail.equipment_code like concat('%', #{equipmentCode}, '%')</if>
            <if test="usageDeptCode != null  and usageDeptCode != ''"> AND detail.usage_dept_code LIKE concat('%', #{usageDeptCode}, '%')</if>
            <if test="usageDeptAlias != null  and usageDeptAlias != ''"> AND detail.usage_dept_alias = #{usageDeptAlias}</if>
            <if test="abandonmentDate != null "> AND detail.abandonment_date = #{abandonmentDate}</if>
            <if test="usageDeptId!= null "> AND detail.usage_dept_id = #{usageDeptId}</if>
            <if test="equipmentStatusCode != null  and equipmentStatusCode != ''"> AND detail.equipment_status_code = #{equipmentStatusCode}</if>
            <if test="equipmentName != null  and equipmentName != ''"> AND equipment.equipment_name like concat('%',#{equipmentName},'%')</if>
            <if test="equipmentId != null  and equipmentId != ''"> AND equipment.equipment_name like concat('%',#{equipmentName},'%')</if>
            <if test="tenantId != null  and tenantId != 0"> AND detail.tenant_id = #{tenantId}</if>
            <if test="categoryName!= null  and categoryName!= ''"> AND category.name like concat('%',#{categoryName},'%')</if>
        </trim>
    </select>
<select id="getBelongingDeptAlias" resultType="String">
    select dept_name as belongingDeptAlias from sys_dept where dept_id = #{belonging_dept_id}
</select>
    <select id="getUsageDeptAlias" resultType="String">
        select dept_name as usageDeptAlias from sys_dept where dept_id = #{usage_dept_id}
    </select>
    <select id="getDetailsList" parameterType="BizEquipmentDetail" resultMap="BizEquipmentDetailResult">
        select * from biz_equipment_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="equipmentCategoryId != '' and equipmentCategoryId != null">AND  equipment_category_id = #{equipmentCategoryId}</if>
            <if test="equipmentCode != null  and equipmentCode != ''"> AND equipment_code like concat('%', #{equipmentCode}, '%')</if>
            <if test="usageDeptCode != null  and usageDeptCode != ''"> AND usage_dept_code LIKE concat('%', #{usageDeptCode}, '%')</if>
            <if test="usageDeptAlias != null  and usageDeptAlias != ''"> AND usage_dept_alias = #{usageDeptAlias}</if>
            <if test="abandonmentDate != null "> AND abandonment_date = #{abandonmentDate}</if>
<!--            资源分配设备的时候设备状态展示NA的状态，设备档案管理展示所有-->
            <if test="usageDeptId!= null and usageDeptCode != ''"> AND usage_dept_id = #{usageDeptId} and equipment_status_code = 'NA' and (id not in (select  t1.common_id from biz_production_resource_group_detail t1 left join biz_equipment_detail t2 on t2.id = t1.common_id where t1.deleted = 0 and t2.deleted = 0 and common_type = 'equipmentDetail' and t2.is_virtual_equipment =0))</if>
            <if test="equipmentStatusCode != null  and equipmentStatusCode != ''"> AND equipment_status_code = #{equipmentStatusCode}</if>
            <if test="equipmentName != null  and equipmentName != ''"> AND equipment_name like concat('%',#{equipmentName},'%')</if>
            <if test="categoryName != null  and categoryName != ''"> AND equipment_category_id in (select id from biz_equipment_category where deleted = 0 and name  like concat('%',#{categoryName},'%')) </if>
            <if test="tenantId != null  and tenantId != 0"> AND tenant_id = #{tenantId}</if>
        </trim>
    </select>

    <select id="getResourceAllocationList" parameterType="BizEquipmentDetail" resultMap="BizEquipmentDetailResult">
        SELECT equipment.equipment_name as equipmentName, equipment.standard as standard, category.name as categoryName
        FROM biz_equipment as equipment
        LEFT JOIN biz_equipment_category AS category ON equipment.equipment_category_id = category.id
        LEFT JOIN biz_resource_allocation AS resource ON resource.equipment_id = equipment.id
        WHERE resource.dept_user_id = #{deptUserId}
    </select>
    <select id="getCategoryName" resultType="String">
        select name from biz_equipment_category where deleted = 0 and id = #{equipment_category_id}
    </select>

    <select id="getCategoryList" resultType="cn.ezhizao.project.business.equipment.domain.BizEquipmentCategory">
        SELECT id AS Value,name AS Label
        FROM biz_equipment_category
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="tenantId != null  and tenantId != 0"> AND tenant_id = #{tenantId}</if>
        </trim>
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_equipment_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
