<?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.TBaseRegionMapper">

    <resultMap type="TBaseRegion" id="TBaseRegionResult">
        <id property="regionId"    column="region_id"    />
        <result property="plantId"    column="plant_id"    />
        <result property="plantType"    column="plant_type"    />
        <result property="plantName"    column="plant_name"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="regionCode"    column="region_code"    />
        <result property="regionName"    column="region_name"    />
        <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="selectTBaseRegionVo">
        select region_id, plant_id, region_code, region_name, remarks, approve_status, approve_time, dept_id, del_flag, creater_code, createdate, updater_code, updatedate from t_base_region
    </sql>



    <select id="selectTBaseRegionList" parameterType="TBaseRegion" resultMap="TBaseRegionResult">
        select d.region_id, d.plant_id,bp.plant_name,bp.plant_type,bp.plant_code,su.user_name updater, d.region_code, d.region_name, d.remarks, d.approve_status, d.approve_time, d.dept_id, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate from t_base_region d
        left join sys_user su on su.user_id = d.updater_code
        left join t_base_plant bp on bp.plant_id = d.plant_id
        <where>
            <if test="plantId != null "> and plant_id = #{plantId}</if>
            <if test="regionCode != null  and regionCode != ''"> and region_code = #{regionCode}</if>
            <if test="regionName != null  and regionName != ''"> and region_name like concat('%', #{regionName}, '%')</if>
            <if test="remarks != null  and remarks != ''"> and d.remarks = #{remarks}</if>
            <if test="approveStatus != null "> and d.approve_status = #{approveStatus}</if>
            <if test="approveTime != null "> and d.approve_time = #{approveTime}</if>
            <if test="deptId != null "> and d.dept_id = #{deptId}</if>
            <if test="createrCode != null "> and d.creater_code = #{createrCode}</if>
            <if test="createdate != null "> and d.createdate = #{createdate}</if>
            <if test="updaterCode != null "> and d.updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and d.updatedate = #{updatedate}</if>
            and d.del_flag=0
        </where>
    </select>
    <select id="selectAllRegionByPlantId" parameterType="long"  resultMap="TBaseRegionResult">
        select d.region_id, d.region_code, d.region_name from t_base_region d where d.plant_id=#{plantId} and d.del_flag=0 and d.approve_status=2
    </select>

    <select id="selectTBaseRegionById" parameterType="Long" resultMap="TBaseRegionResult">
        <include refid="selectTBaseRegionVo"/>
        where region_id = #{regionId}
    </select>

    <select id="selectTBaseRegionByName" resultMap="TBaseRegionResult">
        <include refid="selectTBaseRegionVo"/>
        where region_name = #{name} and plant_id=#{plantId} and del_flag=0
    </select>

    <insert id="insertTBaseRegion" parameterType="TBaseRegion" useGeneratedKeys="true" keyProperty="regionId">
        insert into t_base_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="plantId != null">plant_id,</if>
            <if test="regionCode != null">region_code,</if>
            <if test="regionName != null">region_name,</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>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="plantId != null">#{plantId},</if>
            <if test="regionCode != null">#{regionCode},</if>
            <if test="regionName != null">#{regionName},</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>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
         </trim>
    </insert>

    <update id="updateTBaseRegion" parameterType="TBaseRegion">
        update t_base_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantId != null">plant_id = #{plantId},</if>
            <if test="regionCode != null">region_code = #{regionCode},</if>
            <if test="regionName != null">region_name = #{regionName},</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 region_id = #{regionId}
    </update>

    <delete id="deleteTBaseRegionById" parameterType="Long">
        update t_base_region set del_flag=1 where region_id = #{regionId}
    </delete>

    <delete id="deleteTBaseRegionByIds" parameterType="String">
        update t_base_region set del_flag=1 where region_id in
        <foreach item="regionId" collection="array" open="(" separator="," close=")">
            #{regionId}
        </foreach>
    </delete>

    <update id="updateTBaseRegionByRegionIds" parameterType="TBaseRegion">
        update t_base_region set approve_status=#{approveStatus},approve_time=#{approveTime}
        <where>
            region_id in
            <foreach item="regionId" collection="regionIds" open="(" separator="," close=")">
                #{regionId}
            </foreach>
            <if test="approveStatus ==1">
                and approve_status=0
            </if>
            <if test="approveStatus ==2">
                and approve_status=1
            </if>
        </where>
    </update>
</mapper>
