<?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.zbf.system.mapper.TBaseGoodsMapper">

    <resultMap type="TBaseGoods" id="TBaseGoodsResult">
            <result property="id"    column="id"    />
            <result property="goodsId"    column="goods_id"    />
            <result property="goodsName"    column="goods_name"    />
            <result property="goodsShortName"    column="goods_short_name"    />
            <result property="pyName"    column="py_name"    />
            <result property="barcode"    column="barcode"    />
            <result property="goodsTypeId"    column="goods_type_id"    />
            <result property="specification"    column="specification"    />
            <result property="model"    column="model"    />
            <result property="price"    column="price"    />
            <result property="glength"    column="glength"    />
            <result property="gwidth"    column="gwidth"    />
            <result property="gheight"    column="gheight"    />
            <result property="recoil"    column="recoil"    />
            <result property="gvolume"    column="gvolume"    />
            <result property="scaleUnit"    column="scale_unit"    />
            <result property="unit"    column="unit"    />
            <result property="warnUp"    column="warn_up"    />
            <result property="warnDown"    column="warn_down"    />
            <result property="produceArea"    column="produce_area"    />
            <result property="grossWeight"    column="gross_weight"    />
            <result property="netWeight"    column="net_weight"    />
            <result property="providerId"    column="provider_id"    />
            <result property="hsnumber"    column="hsnumber"    />
            <result property="abc"    column="abc"    />
            <result property="hazardous"    column="hazardous"    />
            <result property="storageConditions"    column="storage_conditions"    />
            <result property="period"    column="period"    />
            <result property="periodUnit"    column="period_unit"    />
            <result property="addtax"    column="addtax"    />
            <result property="season"    column="season"    />
            <result property="tariffrate"    column="tariffrate"    />
            <result property="remark"    column="remark"    />
            <result property="status"    column="status"    />
            <result property="version"    column="version"    />
            <result property="img"    column="img"    />
            <result property="createBy"    column="create_by"    />
            <result property="createDate"    column="create_date"    />
            <result property="updateDate"    column="update_date"    />
            <result property="scFlag"    column="sc_flag"    />
            <result property="costPrice"    column="cost_price"    />
            <result property="customerId"    column="customer_id"    />
            <result property="isOpen"    column="is_open"    />
            <result property="defaultStorageAreaId"    column="default_storage_area_id"    />
            <result property="updateBy"    column="update_by"    />
            <result property="materialCharacteristics"    column="material_characteristics"    />
            <result property="recheckDays"    column="recheck_days"    />
            <result property="defaultStorageId"    column="default_storage_id"    />
            <result property="storageId"    column="storage_id"    />
            <result property="reinspectionDays"    column="reInspection_days"    />
            <result property="isSampling"    column="is_sampling"    />
            <result property="isCheck"    column="is_check"    />
            <result property="goodsTid"    column="goods_tid"    />
            <result property="goodsEpc"    column="goods_epc"    />
    </resultMap>

    <sql id="selectTBaseGoodsVo">
        select id, goods_id, goods_name, goods_short_name, py_name, barcode, goods_type_id, specification, model, price, glength, gwidth, gheight, recoil, gvolume, scale_unit, unit, warn_up, warn_down, produce_area, gross_weight, net_weight, provider_id, hsnumber, abc, hazardous, storage_conditions, period, period_unit, addtax, season, tariffrate, remark, status, version, img, create_by, create_date, update_date, sc_flag, cost_price, customer_id, is_open, default_storage_area_id, update_by, material_characteristics, recheck_days, default_storage_id, storage_id, reInspection_days, is_sampling, is_check,goods_tid,goods_epc from t_base_goods
    </sql>

    <select id="selectTBaseGoodsList" parameterType="TBaseGoods" resultMap="TBaseGoodsResult">
        <include refid="selectTBaseGoodsVo"/>
        <where>
            <if test="goodsId != null  and goodsId != ''"> and goods_id like concat('%', #{goodsId}, '%')</if>
            <if test="goodsName != null  and goodsName != ''"> and goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="goodsShortName != null  and goodsShortName != ''"> and goods_short_name like concat('%', #{goodsShortName}, '%')</if>
            <if test="pyName != null  and pyName != ''"> and py_name like concat('%', #{pyName}, '%')</if>
            <if test="barcode != null  and barcode != ''"> and barcode like concat('%', #{barcode}, '%')</if>
            <if test="goodsTypeId != null  and goodsTypeId != ''"> and goods_type_id like concat('%', #{goodsTypeId}, '%')</if>
            <if test="specification != null  and specification != ''"> and specification like concat('%', #{specification}, '%')</if>
            <if test="model != null  and model != ''"> and model like concat('%', #{model}, '%')</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="recoil != null  and recoil != ''"> and recoil like concat('%', #{recoil}, '%')</if>
            <if test="produceArea != null  and produceArea != ''"> and produce_area like concat('%', #{produceArea}, '%')</if>
            <if test="providerId != null  and providerId != ''"> and provider_id like concat('%', #{providerId}, '%')</if>
            <if test="hsnumber != null  and hsnumber != ''"> and hsnumber like concat('%', #{hsnumber}, '%')</if>
            <if test="abc != null  and abc != ''"> and abc like concat('%', #{abc}, '%')</if>
            <if test="hazardous != null  and hazardous != ''"> and hazardous like concat('%', #{hazardous}, '%')</if>
            <if test="storageConditions != null  and storageConditions != ''"> and storage_conditions like concat('%', #{storageConditions}, '%')</if>
            <if test="period != null "> and period = #{period}</if>
            <if test="periodUnit != null  and periodUnit != ''"> and period_unit like concat('%', #{periodUnit}, '%')</if>
            <if test="addtax != null "> and addtax = #{addtax}</if>
            <if test="season != null  and season != ''"> and season like concat('%', #{season}, '%')</if>
            <if test="tariffrate != null "> and tariffrate = #{tariffrate}</if>
            <if test="status != null  and status != ''"> and status like concat('%', #{status}, '%')</if>
            <if test="version != null  and version != ''"> and version like concat('%', #{version}, '%')</if>
            <if test="img != null  and img != ''"> and img like concat('%', #{img}, '%')</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="scFlag != null  and scFlag != ''"> and sc_flag like concat('%', #{scFlag}, '%')</if>
            <if test="costPrice != null "> and cost_price = #{costPrice}</if>
            <if test="customerId != null  and customerId != ''"> and customer_id like concat('%', #{customerId}, '%')</if>
            <if test="isOpen != null  and isOpen != ''"> and is_open like concat('%', #{isOpen}, '%')</if>
            <if test="defaultStorageAreaId != null  and defaultStorageAreaId != ''"> and default_storage_area_id like concat('%', #{defaultStorageAreaId}, '%')</if>
            <if test="materialCharacteristics != null  and materialCharacteristics != ''"> and material_characteristics like concat('%', #{materialCharacteristics}, '%')</if>
            <if test="recheckDays != null "> and recheck_days = #{recheckDays}</if>
            <if test="defaultStorageId != null  and defaultStorageId != ''"> and default_storage_id like concat('%', #{defaultStorageId}, '%')</if>
            <if test="storageId != null and storageId != '' "> and storage_id = #{storageId}</if>
            <if test="reinspectionDays != null "> and reInspection_days = #{reinspectionDays}</if>
            <if test="isSampling != null  and isSampling != ''"> and is_sampling like concat('%', #{isSampling}, '%')</if>
            <if test="isCheck != null  and isCheck != ''"> and is_check like concat('%', #{isCheck}, '%')</if>
            <if test="goodsSearch != null  and goodsSearch != ''"> and (REPLACE(goods_name, ' ', '') like concat('%', #{goodsSearch}, '%') OR REPLACE(goods_id, ' ', '') like concat('%', #{goodsSearch}, '%'))</if>

            <if test="selectIds !=null and selectIds.length>0">
                and id in
                <foreach collection="selectIds" item="itemId" open="(" close=")" separator=",">
                    #{itemId}
                </foreach>
            </if>
        </where>
        order by id asc
    </select>

    <select id="selectTBaseGoodsById" parameterType="Long" resultMap="TBaseGoodsResult">
            <include refid="selectTBaseGoodsVo"/>
            where id = #{id}
    </select>

    <insert id="insertTBaseGoods" parameterType="TBaseGoods" useGeneratedKeys="true" keyProperty="id">
        insert into t_base_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="goodsId != null and goodsId != ''">goods_id,</if>
                    <if test="goodsName != null">goods_name,</if>
                    <if test="goodsShortName != null">goods_short_name,</if>
                    <if test="pyName != null">py_name,</if>
                    <if test="barcode != null">barcode,</if>
                    <if test="goodsTypeId != null">goods_type_id,</if>
                    <if test="specification != null">specification,</if>
                    <if test="model != null">model,</if>
                    <if test="price != null">price,</if>
                    <if test="glength != null">glength,</if>
                    <if test="gwidth != null">gwidth,</if>
                    <if test="gheight != null">gheight,</if>
                    <if test="recoil != null">recoil,</if>
                    <if test="gvolume != null">gvolume,</if>
                    <if test="scaleUnit != null">scale_unit,</if>
                    <if test="unit != null">unit,</if>
                    <if test="warnUp != null">warn_up,</if>
                    <if test="warnDown != null">warn_down,</if>
                    <if test="produceArea != null">produce_area,</if>
                    <if test="grossWeight != null">gross_weight,</if>
                    <if test="netWeight != null">net_weight,</if>
                    <if test="providerId != null">provider_id,</if>
                    <if test="hsnumber != null">hsnumber,</if>
                    <if test="abc != null">abc,</if>
                    <if test="hazardous != null">hazardous,</if>
                    <if test="storageConditions != null">storage_conditions,</if>
                    <if test="period != null">period,</if>
                    <if test="periodUnit != null">period_unit,</if>
                    <if test="addtax != null">addtax,</if>
                    <if test="season != null">season,</if>
                    <if test="tariffrate != null">tariffrate,</if>
                    <if test="remark != null">remark,</if>
                    <if test="status != null">status,</if>
                    <if test="version != null">version,</if>
                    <if test="img != null">img,</if>
                    <if test="createBy != null">create_by,</if>
                    <if test="createDate != null">create_date,</if>
                    <if test="updateDate != null">update_date,</if>
                    <if test="scFlag != null">sc_flag,</if>
                    <if test="costPrice != null">cost_price,</if>
                    <if test="customerId != null">customer_id,</if>
                    <if test="isOpen != null">is_open,</if>
                    <if test="defaultStorageAreaId != null">default_storage_area_id,</if>
                    <if test="updateBy != null">update_by,</if>
                    <if test="materialCharacteristics != null">material_characteristics,</if>
                    <if test="recheckDays != null">recheck_days,</if>
                    <if test="defaultStorageId != null">default_storage_id,</if>
                    <if test="storageId != null">storage_id,</if>
                    <if test="reinspectionDays != null">reInspection_days,</if>
                    <if test="isSampling != null">is_sampling,</if>
                    <if test="isCheck != null">is_check,</if>
                    <if test="goodsTid != null">goods_tid,</if>
                    <if test="goodsEpc != null">goods_epc,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                    <if test="goodsId != null and goodsId != ''">#{goodsId},</if>
                    <if test="goodsName != null">#{goodsName},</if>
                    <if test="goodsShortName != null">#{goodsShortName},</if>
                    <if test="pyName != null">#{pyName},</if>
                    <if test="barcode != null">#{barcode},</if>
                    <if test="goodsTypeId != null">#{goodsTypeId},</if>
                    <if test="specification != null">#{specification},</if>
                    <if test="model != null">#{model},</if>
                    <if test="price != null">#{price},</if>
                    <if test="glength != null">#{glength},</if>
                    <if test="gwidth != null">#{gwidth},</if>
                    <if test="gheight != null">#{gheight},</if>
                    <if test="recoil != null">#{recoil},</if>
                    <if test="gvolume != null">#{gvolume},</if>
                    <if test="scaleUnit != null">#{scaleUnit},</if>
                    <if test="unit != null">#{unit},</if>
                    <if test="warnUp != null">#{warnUp},</if>
                    <if test="warnDown != null">#{warnDown},</if>
                    <if test="produceArea != null">#{produceArea},</if>
                    <if test="grossWeight != null">#{grossWeight},</if>
                    <if test="netWeight != null">#{netWeight},</if>
                    <if test="providerId != null">#{providerId},</if>
                    <if test="hsnumber != null">#{hsnumber},</if>
                    <if test="abc != null">#{abc},</if>
                    <if test="hazardous != null">#{hazardous},</if>
                    <if test="storageConditions != null">#{storageConditions},</if>
                    <if test="period != null">#{period},</if>
                    <if test="periodUnit != null">#{periodUnit},</if>
                    <if test="addtax != null">#{addtax},</if>
                    <if test="season != null">#{season},</if>
                    <if test="tariffrate != null">#{tariffrate},</if>
                    <if test="remark != null">#{remark},</if>
                    <if test="status != null">#{status},</if>
                    <if test="version != null">#{version},</if>
                    <if test="img != null">#{img},</if>
                    <if test="createBy != null">#{createBy},</if>
                    <if test="createDate != null">#{createDate},</if>
                    <if test="updateDate != null">#{updateDate},</if>
                    <if test="scFlag != null">#{scFlag},</if>
                    <if test="costPrice != null">#{costPrice},</if>
                    <if test="customerId != null">#{customerId},</if>
                    <if test="isOpen != null">#{isOpen},</if>
                    <if test="defaultStorageAreaId != null">#{defaultStorageAreaId},</if>
                    <if test="updateBy != null">#{updateBy},</if>
                    <if test="materialCharacteristics != null">#{materialCharacteristics},</if>
                    <if test="recheckDays != null">#{recheckDays},</if>
                    <if test="defaultStorageId != null">#{defaultStorageId},</if>
                    <if test="storageId != null">#{storageId},</if>
                    <if test="reinspectionDays != null">#{reinspectionDays},</if>
                    <if test="isSampling != null">#{isSampling},</if>
                    <if test="isCheck != null">#{isCheck},</if>
                    <if test="goodsTid != null">#{goods_tid},</if>
                    <if test="goodsEpc != null">#{goods_epc},</if>
        </trim>
    </insert>

    <update id="updateTBaseGoods" parameterType="TBaseGoods">
        update t_base_goods
        <trim prefix="SET" suffixOverrides=",">
                    <if test="goodsId != null and goodsId != ''">goods_id = #{goodsId},</if>
                    <if test="goodsName != null">goods_name = #{goodsName},</if>
                    <if test="goodsShortName != null">goods_short_name = #{goodsShortName},</if>
                    <if test="pyName != null">py_name = #{pyName},</if>
                    <if test="barcode != null">barcode = #{barcode},</if>
                    <if test="goodsTypeId != null">goods_type_id = #{goodsTypeId},</if>
                    <if test="specification != null">specification = #{specification},</if>
                    <if test="model != null">model = #{model},</if>
                    <if test="price != null">price = #{price},</if>
                    <if test="glength != null">glength = #{glength},</if>
                    <if test="gwidth != null">gwidth = #{gwidth},</if>
                    <if test="gheight != null">gheight = #{gheight},</if>
                    <if test="recoil != null">recoil = #{recoil},</if>
                    <if test="gvolume != null">gvolume = #{gvolume},</if>
                    <if test="scaleUnit != null">scale_unit = #{scaleUnit},</if>
                    <if test="unit != null">unit = #{unit},</if>
                    <if test="warnUp != null">warn_up = #{warnUp},</if>
                    <if test="warnDown != null">warn_down = #{warnDown},</if>
                    <if test="produceArea != null">produce_area = #{produceArea},</if>
                    <if test="grossWeight != null">gross_weight = #{grossWeight},</if>
                    <if test="netWeight != null">net_weight = #{netWeight},</if>
                    <if test="providerId != null">provider_id = #{providerId},</if>
                    <if test="hsnumber != null">hsnumber = #{hsnumber},</if>
                    <if test="abc != null">abc = #{abc},</if>
                    <if test="hazardous != null">hazardous = #{hazardous},</if>
                    <if test="storageConditions != null">storage_conditions = #{storageConditions},</if>
                    <if test="period != null">period = #{period},</if>
                    <if test="periodUnit != null">period_unit = #{periodUnit},</if>
                    <if test="addtax != null">addtax = #{addtax},</if>
                    <if test="season != null">season = #{season},</if>
                    <if test="tariffrate != null">tariffrate = #{tariffrate},</if>
                    <if test="remark != null">remark = #{remark},</if>
                    <if test="status != null">status = #{status},</if>
                    <if test="version != null">version = #{version},</if>
                    <if test="img != null">img = #{img},</if>
                    <if test="createBy != null">create_by = #{createBy},</if>
                    <if test="createDate != null">create_date = #{createDate},</if>
                    <if test="updateDate != null">update_date = #{updateDate},</if>
                    <if test="scFlag != null">sc_flag = #{scFlag},</if>
                    <if test="costPrice != null">cost_price = #{costPrice},</if>
                    <if test="customerId != null">customer_id = #{customerId},</if>
                    <if test="isOpen != null">is_open = #{isOpen},</if>
                    <if test="defaultStorageAreaId != null">default_storage_area_id = #{defaultStorageAreaId},</if>
                    <if test="updateBy != null">update_by = #{updateBy},</if>
                    <if test="materialCharacteristics != null">material_characteristics = #{materialCharacteristics},</if>
                    <if test="recheckDays != null">recheck_days = #{recheckDays},</if>
                    <if test="defaultStorageId != null">default_storage_id = #{defaultStorageId},</if>
                    <if test="storageId != null">storage_id = #{storageId},</if>
                    <if test="reinspectionDays != null">reInspection_days = #{reinspectionDays},</if>
                    <if test="isSampling != null">is_sampling = #{isSampling},</if>
                    <if test="isCheck != null">is_check = #{isCheck},</if>
                    <if test="goodsTid != null">goods_tid = #{goodsTid},</if>
                    <if test="goodsEpc != null">goods_epc = #{goodsEpc},</if>
        </trim>
        where id = #{id}
    </update>


    <update id="updateRFID" parameterType="TBaseGoods">
        update t_base_goods
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsTid != null">goods_tid = #{goodsTid},</if>
            <if test="goodsEpc != null">goods_epc = #{goodsEpc},</if>
        </trim>
        where goods_Id = #{goodsId}
    </update>

    <update id="enableTBaseGoodsByIds" parameterType="String">
        update t_base_goods set status=0 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="disabledTBaseGoodsByIds" parameterType="String">
        update t_base_goods set status=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteTBaseGoodsById" parameterType="Long">
        delete from t_base_goods  where id = #{id}
    </delete>

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

    <select id="queryTBaseGoodsByGoodsId" parameterType="String" resultMap="TBaseGoodsResult">
        <include refid="selectTBaseGoodsVo"/>
        where
        recoil like concat("%",#{goodsId},"%")
        or goods_name like concat("%",#{goodsId},"%")
        or goods_id like concat(#{goodsId},"%")
    </select>

    <select id="selectTotalPriceBy" resultType="map">
        SELECT
            parent.material_class_name AS parent_name,
            SUM(g.price) AS total_price
        FROM
            t_base_financial_class AS child
                JOIN t_base_goods AS g ON child.id = g.goods_type_id
                JOIN t_base_financial_class AS parent ON child.parent_id = parent.id
        WHERE
            child.parent_id IS NOT NULL
        GROUP BY
            parent.id, parent.material_class_name;
    </select>


    <select id="selectChild" resultType="map">
        SELECT
            f.material_class_name AS name,
            SUM(g.price) AS value
        FROM
            t_base_financial_class AS f
            JOIN
            t_base_goods AS g ON f.id = g.goods_type_id
        WHERE
            f.parent_id IS NOT NULL
        GROUP BY
            f.parent_id, f.material_class_name;
    </select>
</mapper>
