<?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.ruoyi.project.system.mapper.SysPlantMapper">

    <resultMap type="SysPlant" id="SysPlantResult">
        <result property="plantId"    column="plant_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="name"    column="name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="cname"    column="cname"    />
        <result property="deptCode"    column="dept_code"    />
        <result property="remarks"    column="remarks"    />
        <result property="pType"    column="p_type"    />
        <result property="parentName" column="parent_name" />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectSysPlantVo">
        select d.plant_id, d.parent_id, d.name, d.order_num, d.del_flag, d.cname, d.dept_code, d.remarks, d.p_type from sys_plant d
    </sql>

    <select id="selectSysPlantList" parameterType="SysPlant" resultMap="SysPlantResult">
        <include refid="selectSysPlantVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat(concat('%', #{name}), '%')</if>
            <if test="cname != null  and cname != ''"> and cname like concat(concat('%', #{cname}), '%')</if>
            <if test="deptCode != null  and deptCode != ''"> and dept_code = #{deptCode}</if>
            <if test="pType != null "> and p_type = #{pType}</if>
            and d.del_flag = 0
        </where>
        order by order_num , name
    </select>

    <select id="selectMySysPlantList" parameterType="SysPlant" resultMap="SysPlantResult">
        SELECT
        sp.NAME
        FROM
        SYS_PLANT sp
        WHERE
        sp.DEPT_CODE IN (
        SELECT DISTINCT
        m.DEPT_NAME
        FROM
        SYS_DEPT m
          LEFT JOIN SYS_USER_DEPT ur ON m.DEPT_ID = ur.DEPT_ID
        WHERE
        ur.user_id = #{plantId}
        )
            and sp.del_flag = 0
        order by sp.order_num , sp.name
    </select>
    <select id="selectMySysPlantList3" parameterType="SysPlant" resultMap="SysPlantResult">
        SELECT
            sp.NAME
        FROM
            SYS_PLANT sp
        WHERE
                sp.DEPT_CODE IN (
                SELECT DISTINCT
                    m.DEPT_NAME
                FROM
                    SYS_DEPT m
                        LEFT JOIN SYS_USER ur ON m.DEPT_ID = ur.DEPT_ID
                WHERE
                    ur.user_id = #{plantId}
            )
          and sp.del_flag = 0
        order by sp.order_num , sp.name
    </select>

    <select id="selectSysPlantByDeptId" parameterType="Long" resultMap="SysPlantResult">
        SELECT
            sp.NAME
        FROM
            SYS_PLANT sp
                LEFT JOIN SYS_DEPT sd on sp.DEPT_CODE = sd.DEPT_NAME
        where sd.DEPT_ID = #{deptId} or FIND_IN_SET(#{deptId}, sd.ancestors) > 0
    </select>


    <select id="selectSysPlantById" parameterType="Long" resultMap="SysPlantResult">
        select t.plant_id, t.parent_id, t.name, t.order_num, t.del_flag, t.cname, t.dept_code, t.remarks, t.p_type, p.name as parent_name
        from sys_plant t
        left join sys_plant p on p.plant_id = t.parent_id
        where t.plant_id = #{plantId}
    </select>

    <insert id="insertSysPlant" parameterType="SysPlant">
        <selectKey keyProperty="plantId" resultType="long" order="BEFORE">
            SELECT seq_sys_plant.NEXTVAL as plantId FROM DUAL
        </selectKey>
        insert into sys_plant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="plantId != null">plant_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="name != null">name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="cname != null">cname,</if>
            <if test="deptCode != null">dept_code,</if>
            <if test="remarks != null">remarks,</if>
            <if test="pType != null">p_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="plantId != null">#{plantId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="name != null">#{name},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="cname != null">#{cname},</if>
            <if test="deptCode != null">#{deptCode},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="pType != null">#{pType},</if>
         </trim>
    </insert>

    <update id="updateSysPlant" parameterType="SysPlant">
        update sys_plant
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="cname != null">cname = #{cname},</if>
            <if test="deptCode != null">dept_code = #{deptCode},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="pType != null">p_type = #{pType},</if>
        </trim>
        where plant_id = #{plantId}
    </update>

    <update id="deleteSysPlantById" parameterType="Long">
        update sys_plant set del_flag = 2 where plant_id = #{plantId}
    </update>

    <update id="deleteSysPlantByIds" parameterType="String">
        update sys_plant set del_flag = 2 where plant_id in
        <foreach item="plantId" collection="array" open="(" separator="," close=")">
            #{plantId}
        </foreach>
    </update>

    <select id="selectSysPlantByPlants" parameterType="List" resultMap="SysPlantResult">
        <include refid="selectSysPlantVo"/>
        <where>
            parent_id in
            (SELECT PLANT_ID from SYS_PLANT where
                name in
            <choose>
                <when test="list != null and list.size > 0">
                    <foreach collection="list" item="id" index="index" open="(" close=")" separator="," >
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    null
                </otherwise>
            </choose>
            )
            and d.del_flag = 0
            and p_type = 2
        </where>
        order by order_num , name
        <!-- 数据范围过滤 -->
    </select>
</mapper>
