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

    <resultMap type="TBasePlant" id="TBasePlantResult">
        <id property="plantId"    column="plant_id"    />
        <result property="company"    column="company"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="plantName"    column="plant_name"    />
        <result property="proAbility"    column="pro_ability"    />
        <result property="plantType"    column="plant_type"    />
        <result property="remarks"    column="remarks"    />
        <result property="approveStatus"    column="approve_status"    />
        <result property="approveTime"    column="approve_time"    />
        <result property="deptId"    column="dept_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updater"    column="updater"    />
        <result property="updatedate"    column="updatedate"    />
    </resultMap>

    <sql id="selectTBasePlantVo">
        select company,plant_id, plant_code, plant_name, pro_ability, plant_type, remarks, approve_status, approve_time, dept_id, del_flag, creater_code, createdate, updater_code, updatedate from t_base_plant
    </sql>

    <select id="selectAllPlantName" resultMap="TBasePlantResult">
        select d.company,d.plant_id,d.plant_name,d.plant_type from t_base_plant d
        <where>
            and del_flag=0
            and approve_status=2
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
    </select>

    <select id="selectTBasePlantList" parameterType="TBasePlant" resultMap="TBasePlantResult">
        select d.company,d.plant_id, d.plant_code, d.plant_name, d.pro_ability, d.plant_type, d.remarks, d.approve_status, d.approve_time, d.dept_id, d.del_flag, d.creater_code, d.createdate, su.user_name updater, d.updatedate from t_base_plant d
        left join sys_user su on su.user_id = d.updater_code
        <where>
            <if test="plantCode != null  and plantCode != ''"> and plant_code = #{plantCode}</if>
            <if test="plantCode != null  and plantCode != ''"> and plant_code = #{plantCode}</if>
            <if test="plantName != null  and plantName != ''"> and plant_name like concat('%', #{plantName}, '%')</if>
            <if test="company != null  and company != ''"> and company like concat('%', #{company}, '%')</if>
            <if test="proAbility != null  and proAbility != ''"> and pro_ability = #{proAbility}</if>
            <if test="plantType != null  and plantType != ''"> and plant_type = #{plantType}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="approveStatus != null "> and approve_status = #{approveStatus}</if>
            <if test="approveTime != null "> and approve_time = #{approveTime}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="createrCode != null "> and creater_code = #{createrCode}</if>
            <if test="createdate != null "> and createdate = #{createdate}</if>
            <if test="updaterCode != null "> and updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and updatedate = #{updatedate}</if>
            and d.del_flag=0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
    </select>

    <select id="selectTBasePlantById" parameterType="Long" resultMap="TBasePlantResult">
        <include refid="selectTBasePlantVo"/>
        where plant_id = #{plantId}
    </select>

    <select id="selectTBasePlantByName" parameterType="String" resultMap="TBasePlantResult">
        <include refid="selectTBasePlantVo"/>
        where plant_name = #{name} and del_flag=0
    </select>

    <insert id="insertTBasePlant" parameterType="TBasePlant" useGeneratedKeys="true" keyProperty="plantId">
        insert into t_base_plant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="company != null">company,</if>
            <if test="plantCode != null">plant_code,</if>
            <if test="plantName != null">plant_name,</if>
            <if test="proAbility != null">pro_ability,</if>
            <if test="plantType != null">plant_type,</if>
            <if test="remarks != null">remarks,</if>
            <if test="approveStatus != null">approve_status,</if>
            <if test="approveTime != null">approve_time,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            createdate,
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="company != null">#{company},</if>
            <if test="plantCode != null">#{plantCode},</if>
            <if test="plantName != null">#{plantName},</if>
            <if test="proAbility != null">#{proAbility},</if>
            <if test="plantType != null">#{plantType},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
            <if test="approveTime != null">#{approveTime},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            sysdate(),
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
         </trim>
    </insert>

    <update id="updateTBasePlant" parameterType="TBasePlant">
        update t_base_plant
        <trim prefix="SET" suffixOverrides=",">
            <if test="company != null">company = #{company},</if>
            <if test="plantCode != null">plant_code = #{plantCode},</if>
            <if test="plantName != null">plant_name = #{plantName},</if>
            <if test="proAbility != null">pro_ability = #{proAbility},</if>
            <if test="plantType != null">plant_type = #{plantType},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
        </trim>
        where plant_id = #{plantId}
    </update>

    <delete id="deleteTBasePlantById" parameterType="Long">
        update t_base_plant set del_flag=1 where plant_id = #{plantId}
    </delete>

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

    <update id="updateTBasePlantByPlantIds" parameterType="TBasePlant">
        update t_base_plant set approve_status=#{approveStatus},approve_time=#{approveTime}
        <where>
            plant_id in
            <foreach item="plantId" collection="plantIds" open="(" separator="," close=")">
                #{plantId}
            </foreach>
            <if test="approveStatus ==1">
                and approve_status=0
            </if>
            <if test="approveStatus ==2">
                and approve_status=1
            </if>
        </where>
    </update>
</mapper>
