<?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.mapper.BizProductionResourceGroupDetailMapper">

    <resultMap type="cn.ezhizao.project.business.domain.BizProductionResourceGroupDetail" id="BizProductionResourceGroupDetailResult">
        <id column="id" property="id"/>
        <result column="production_resource_group_id" property="productionResourceGroupId"/>
        <association property="resourceCode" column="production_resource_group_id" javaType="java.lang.String" select="getResourceCode"/>
    </resultMap>

    <select id="getResourceCode" resultType="java.lang.String">
        select code from biz_production_resource_group where deleted = 0 and id = #{productionResourceGroupId}
    </select>
    <select id="checkUseEquipment" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        select t2.* from biz_production_resource_group t1 left join biz_production_resource_group_sub_plan t2 on t1.id = t2.resource_group_id where resource_group_id = #{productionResourceGroupId} and t1.deleted = 0 and t2.deleted = 0
        and t2.production_plan_detail_id in (select t3.production_plan_detail_id from biz_daywork_item t3 where status = 1 and equipment_detail_id = #{commonId} )
    </select>

    <select id="getList" parameterType="BizProductionResourceGroupDetail" resultMap="BizProductionResourceGroupDetailResult">
        SELECT * FROM biz_production_resource_group_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="productionResourceGroupId != null  and productionResourceGroupId != ''"> AND production_resource_group_id = #{productionResourceGroupId}</if>
            <if test="commonCode != null  and commonCode != ''"> AND common_code like concat('%', #{commonCode}, '%')</if>
            <if test="commonType != null  and commonType != ''"> AND common_type = #{commonType}</if>
        </trim>
    </select>
   <select id="getEquipmentInfo" resultMap="BizProductionResourceGroupDetailResult">
       SELECT distinct t3.common_code as label, t3.common_id as value
       FROM biz_production_resource_group_detail AS t3
       INNER JOIN
       (SELECT t1.resource_group_id AS production_resource_group_id
       FROM biz_production_resource_group_sub_plan AS t1
       WHERE t1.production_plan_detail_id = #{productionPlanDetailId}
       AND t1.deleted = 0
       INTERSECT
       SELECT t2.production_resource_group_id
       FROM biz_production_resource_group_detail AS t2
       WHERE t2.common_code = #{commonCode}
       AND t2.deleted = 0) AS intersection_ids
       ON t3.production_resource_group_id = intersection_ids.production_resource_group_id
       AND t3.common_type = 'equipmentDetail'
       AND t3.deleted = 0 and t3.dept_id = #{deptId}
   </select>

    <select id="specialEquipmentList" parameterType="BizProductionResourceGroupDetail" resultMap="BizProductionResourceGroupDetailResult">
        SELECT *  FROM biz_production_resource_group_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="productionResourceGroupId != null  and productionResourceGroupId != ''"> AND production_resource_group_id = #{productionResourceGroupId}</if>
            <if test="productionPlanDetailId != null  and productionPlanDetailId != ''">AND production_resource_group_id IN (SELECT resource_group_id FROM  biz_production_resource_group_sub_plan
                WHERE deleted=0 AND production_plan_detail_id =#{productionPlanDetailId})</if>
            <if test="commonCode != null  and commonCode != ''"> AND common_code like concat('%', #{commonCode}, '%')</if>
            <if test="commonType != null  and commonType != ''"> AND common_type = #{commonType}</if>
        </trim>
    </select>
    <select id="getListByResourceGroupIds" resultType="cn.ezhizao.project.business.domain.BizProductionResourceGroupDetail">
        SELECT distinct common_code,common_name,common_id FROM biz_production_resource_group_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and common_type = 'person'
            <if test="commonCode != null  and commonCode != ''"> AND common_code like concat('%', #{commonCode}, '%')</if>
            <if test="commonName != null  and commonName != ''"> AND common_name like concat('%', #{commonName}, '%')</if>
            and production_resource_group_id in (select resource_group_id from biz_production_resource_group_sub_plan where dept_id = #{deptId} and production_plan_detail_id = #{productionPlanDetailId} and deleted = 0)
        </trim>
    </select>

    <select id="specialUserList" parameterType="BizProductionResourceGroupDetail" resultMap="BizProductionResourceGroupDetailResult">
        SELECT common_code,common_id,common_name,production_resource_group_id  FROM biz_production_resource_group_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 AND
            protem_daywork_id=0 AND
            production_resource_group_id IN (SELECT resource_group_id FROM  biz_production_resource_group_sub_plan
            WHERE deleted=0 AND production_plan_detail_id =#{productionPlanDetailId})
            <if test="deptId != null  and deptId != ''"> AND dept_id = #{deptId}</if>
            <if test="keyword != null  and keyword != ''"> AND (common_code like concat('%', #{keyword}, '%') OR common_name like concat('%', #{keyword}, '%'))</if>
            <if test="commonType != null  and commonType != ''"> AND common_type = #{commonType}</if>
        </trim>
    </select>




    <delete id="physicalDelete">
        DELETE FROM biz_production_resource_group_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
