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

    <resultMap type="BaseStoreInfo" id="BaseStoreInfoResult">
        <result property="objId" column="obj_id"/>
        <result property="storeCode" column="store_code"/>
        <result property="storeName" column="store_name"/>
        <result property="storeType" column="store_type"/>
        <result property="storeArea" column="store_area"/>
        <result property="storeStatus" column="store_status"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
        <result property="plantCode" column="factory_code"/>
        <result property="plantName" column="factory_name"/>
    </resultMap>

    <sql id="selectBaseStoreInfoVo">
        select si.obj_id,
               si.store_code,
               si.store_name,
               si.store_type,
               si.store_area,
               si.store_status,
               si.is_flag,
               si.created_by,
               si.created_time,
               si.updated_by,
               si.updated_time,
               si.factory_code,
               bf.factory_name
        from base_storeinfo si
                 left join base_factory bf on bf.factory_code = si.factory_code
    </sql>

    <select id="selectBaseStoreInfoList" parameterType="BaseStoreInfo" resultMap="BaseStoreInfoResult">
        <include refid="selectBaseStoreInfoVo"/>
        <where>
            <if test="storeCode != null  and storeCode != ''">and si.store_code = #{storeCode}</if>
            <if test="storeName != null  and storeName != ''">and si.store_name like concat(concat('%', #{storeName}),
                '%')
            </if>
            <if test="storeType != null ">and si.store_type = #{storeType}</if>
            <if test="storeArea != null  and storeArea != ''">and si.store_area = #{storeArea}</if>
            <if test="storeStatus != null ">and si.store_status = #{storeStatus}</if>
            <if test="isFlag != null ">and si.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and si.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and si.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and si.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and si.updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="selectBaseStoreInfoByObjId" parameterType="Long" resultMap="BaseStoreInfoResult">
        <include refid="selectBaseStoreInfoVo"/>
        where si.obj_id = #{objId}
    </select>

    <insert id="insertBaseStoreInfo" parameterType="BaseStoreInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_storeinfo.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_storeinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="storeCode != null">store_code,</if>
            <if test="storeName != null">store_name,</if>
            <if test="storeType != null">store_type,</if>
            <if test="storeArea != null">store_area,</if>
            <if test="storeStatus != null">store_status,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
            <if test="plantCode != null">factory_code,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="storeCode != null">#{storeCode},</if>
            <if test="storeName != null">#{storeName},</if>
            <if test="storeType != null">#{storeType},</if>
            <if test="storeArea != null">#{storeArea},</if>
            <if test="storeStatus != null">#{storeStatus},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
            <if test="plantCode != null">#{plantCode},</if>
        </trim>
    </insert>

    <update id="updateBaseStoreInfo" parameterType="BaseStoreInfo">
        update base_storeinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="storeCode != null">store_code = #{storeCode},</if>
            <if test="storeName != null">store_name = #{storeName},</if>
            <if test="storeType != null">store_type = #{storeType},</if>
            <if test="storeArea != null">store_area = #{storeArea},</if>
            <if test="storeStatus != null">store_status = #{storeStatus},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
            <if test="plantCode != null">factory_code = #{plantCode},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteBaseStoreInfoByObjId" parameterType="Long">
        delete
        from base_storeinfo
        where obj_id = #{objId}
    </delete>

    <delete id="deleteBaseStoreInfoByObjIds" parameterType="String">
        delete from base_storeinfo where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>
</mapper>