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

    <resultMap type="TWarehouse" id="TWarehouseResult">
        <result property="fId"    column="f_id"    />
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="orderNum" column="order_num"/>
        <result property="fNo"    column="f_no"    />
        <result property="fName"    column="f_name"    />
        <result property="fAddr"    column="f_addr"    />
        <result property="fTotalgross"    column="f_totalgross"    />
        <result property="fContacts"    column="f_contacts"    />
        <result property="fTel"    column="f_tel"    />
        <result property="fCharg"    column="f_charg"    />
        <result property="fStatus"    column="f_status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="fLocation"    column="f_location"    />
        <result property="fWarehouseInformation"    column="f_warehouse_information"    />
    </resultMap>

    <sql id="selectTWarehouseVo">
        select f_id, parent_id, ancestors,  order_num, f_no, f_name, f_addr, f_totalgross,f_contacts, f_tel, f_charg, f_status, del_flag, create_by, create_time, update_by, update_time, remark,f_location, f_warehouse_information from t_warehouse
    </sql>

    <select id="selectTWarehouseList" parameterType="TWarehouse" resultMap="TWarehouseResult">
        <include refid="selectTWarehouseVo"/>
        <where>
            <if test="parentId != null  and parentId != ''"> and parent_id = #{parentId} </if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors like concat('%', #{ancestors}, '%')</if>
            <if test="fNo != null  and fNo != ''"> and f_no like concat('%', #{fNo}, '%')</if>
            <if test="fName != null  and fName != ''"> and f_name like concat('%', #{fName}, '%')</if>
            <if test="fAddr != null  and fAddr != ''"> and f_addr like concat('%', #{fAddr}, '%')</if>
            <if test="fTotalgross != null "> and f_totalgross like concat('%', #{fTotalgross}, '%')</if>
            <if test="fContacts != null  and fContacts != ''"> and f_contacts = #{fContacts}</if>
            <if test="fTel != null  and fTel != ''"> and f_tel = #{fTel}</if>
            <if test="fCharg != null "> and f_charg = #{fCharg}</if>
            <if test="fStatus != null  and fStatus != ''"> and f_status = #{fStatus}</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by parent_id, order_num
    </select>

    <select id="selectTWarehouseLists" parameterType="TWarehouse" resultMap="TWarehouseResult">
        <include refid="selectTWarehouseVo"/>
        <where>
            parent_id = 100
        </where>
    </select>

    <select id="selectTWarehouseById" parameterType="Long" resultMap="TWarehouseResult">
        <include refid="selectTWarehouseVo"/>
        where f_id = #{fId}
    </select>

    <insert id="insertTWarehouse" parameterType="TWarehouse" useGeneratedKeys="true" keyProperty="fId">
        insert into t_warehouse
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null and parentId != 0">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="orderNum != null and orderNum != ''">order_num,</if>
            <if test="fNo != null and fNo != ''">f_no,</if>
            <if test="fName != null and fName != ''">f_name,</if>
            <if test="fAddr != null and fAddr != ''">f_addr,</if>
            <if test="fTotalgross != null">f_totalgross,</if>
            <if test="fContacts != null">f_contacts,</if>
            <if test="fTel != null">f_tel,</if>
            <if test="fCharg != null">f_charg,</if>
            <if test="fStatus != null">f_status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="fLocation != null">f_location,</if>
            <if test="fWarehouseInformation != null">f_warehouse_information,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null and parentId != 0">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="orderNum != null and orderNum != ''">#{orderNum},</if>
            <if test="fNo != null and fNo != ''">#{fNo},</if>
            <if test="fName != null and fName != ''">#{fName},</if>
            <if test="fAddr != null and fAddr != ''">#{fAddr},</if>
            <if test="fTotalgross != null">#{fTotalgross},</if>
            <if test="fContacts != null">#{fContacts},</if>
            <if test="fTel != null">#{fTel},</if>
            <if test="fCharg != null">#{fCharg},</if>
            <if test="fStatus != null">#{fStatus},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="fLocation != null">#{fLocation},</if>
            <if test="fWarehouseInformation != null">#{fWarehouseInformation},</if>
        </trim>
    </insert>

    <update id="updateTWarehouse" parameterType="TWarehouse">
        update t_warehouse
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
            <if test="fNo != null and fNo != ''">f_no = #{fNo},</if>
            <if test="fName != null and fName != ''">f_name = #{fName},</if>
            <if test="fAddr != null and fAddr != ''">f_addr = #{fAddr},</if>
            <if test="fTotalgross != null">f_totalgross = #{fTotalgross},</if>
            <if test="fContacts != null">f_contacts = #{fContacts},</if>
            <if test="fTel != null">f_tel = #{fTel},</if>
            <if test="fCharg != null">f_charg = #{fCharg},</if>
            <if test="fStatus != null">f_status = #{fStatus},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="fLocation != null">f_location = #{fLocation},</if>
            <if test="fWarehouseInformation != null">f_warehouse_information = #{fWarehouseInformation},</if>
        </trim>
        where f_id = #{fId}
    </update>

    <delete id="deleteTWarehouseById" parameterType="Long">
        delete from t_warehouse where f_id = #{fId}
    </delete>

    <delete id="deleteTWarehouseByIds" parameterType="String">
        delete from t_warehouse where f_id in
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>

    <select id="checkFNoUnique" parameterType="String" resultMap="TWarehouseResult">
	    select f_id,f_no from t_warehouse where f_no = #{fNo} limit 1
    </select>

    <select id="checkUFNnameUnique" parameterType="String" resultMap="TWarehouseResult">
        select f_id,f_name from t_warehouse where f_name = #{fAame} limit 1
    </select>

    <select id="checkUFAaddrUnique" parameterType="String" resultMap="TWarehouseResult">
        select f_id,f_addr from t_warehouse where f_addr = #{fAddr} limit 1
    </select>

    <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
        select count(*) from t_warehouse where f_status = 0 and del_flag = '0' and find_in_set(#{fId}, ancestors)
    </select>

    <select id="selectChildrenDeptById" parameterType="Long" resultMap="TWarehouseResult">
        select * from t_warehouse where find_in_set(#{fId}, ancestors)
    </select>
    <select id="selectTWarehousById" resultMap="TWarehouseResult">
        select f_id  from t_warehouse  where parent_id = #{fId} limit 1
    </select>

    <update id="updateDeptChildren" parameterType="java.util.List">
        update t_warehouse set ancestors =
        <foreach collection="depts" item="item" index="index"
                 separator=" " open="case f_id" close="end">
            when #{item.fId} then #{item.ancestors}
        </foreach>
        where f_id in
        <foreach collection="depts" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.fId}
        </foreach>
    </update>

    <update id="updateDeptStatus" parameterType="TWarehouse">
        update t_warehouse
        <set>
            <if test="fStatus != null and fStatus != ''">f_status = #{fStatus},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where f_id in (${ancestors})
    </update>
    <update id="updatefTotalgross">
        update t_warehouse
        <set>
            <if test="fTotalgross != null and fTotalgross != ''">f_totalgross =  #{fTotalgross},</if>
            update_time = sysdate()
        </set>
        where f_id =  #{fId}
    </update>

    <select id="selectTWarehouseFTotalgross" resultType="java.math.BigDecimal">
        SELECT
            sum( f_totalgross ) AS fTotalgross
        FROM
            t_warehouse
        WHERE
            ancestors LIKE  concat('%',#{fId}, '%')
    </select>

    <select id="checkDeptNameUnique" resultMap="TWarehouseResult">
        <include refid="selectTWarehouseVo"/>
        where f_name=#{fName} and parent_id = #{parentId} limit 1
    </select>
    <select id="hasChildByfId" parameterType="Long" resultType="int">
        select count(1) from t_warehouse
        where del_flag = '0' and parent_id = #{fId} limit 1
    </select>
    <select id="checkDeptExistarehouse" parameterType="Long" resultType="int">
        select count(1) from t_warehousebills where f_warehouseid = #{fId} and del_flag = '0'
    </select>
    <select id="checkDeptExistWarehouseItems" parameterType="Long" resultType="int">
        select count(1) from t_warehousebillsitems where f_warehouselocid = #{fId} and del_flag = '0'
    </select>
</mapper>