<?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.sckw.goods.mapper.GoodsMapper">

    <resultMap type="com.sckw.goods.entity.KwBusinessGoods" id="BaseResultMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="descn" column="descn"/>
        <result property="price" column="price"/>
        <result property="stockQuantity" column="stock_quantity"/>
        <result property="imageUrl" column="image_url"/>
        <result property="categoryId" column="category_id"/>
        <result property="status" column="status"/>
        <result property="sort" column="sort"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        `name`,
        goods_num,
        descn,
            price,
            stock_quantity,
            image_url,
            category_id,
            `status`,
            sort,
            create_by,
            update_by,
            create_time,
            update_time,
            del_flag
    </sql>

    <select id="findList" parameterType="com.sckw.goods.entity.req.GoodsFindListParam">
        select
        id,name
        from kw_business_goods
        <where>
            del_flag = 0
            <if test="name != null and name != ''">
                and `name` like concat ('%',#{name},'%')
            </if>
        </where>
    </select>


    <resultMap id="mainEntityMap" type="com.sckw.goods.entity.res.GoodsFindPageRes">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="goodsNum" property="goodsNum"/>
        <result column="descn" property="descn"/>
        <result column="price" property="price"/>
        <result column="stockQuantity" property="stockQuantity"/>
        <result column="unitType" property="unitType"/>
        <result column="specSize" property="specSize"/>
        <result column="invoiceRate" property="invoiceRate"/>
        <result column="imageUrl" property="imageUrl"/>
        <result column="categoryId" property="categoryId"/>
        <result column="isInventory" property="isInventory"/>
        <result column="price" property="price"/>
        <result column="createBy" property="createBy"/>
        <result column="createTime" property="createTime"/>
        <result column="categoryName" property="categoryName"/>
        <association property="goodsMoreRes" column="id" select="selectMore" />
        <collection property="inventoryList" column="id" select="selectListDetail" />
        <collection property="replenishList" column="id" select="replenishListDetail" />

    </resultMap>
    <select id="selectMore" resultType="com.sckw.goods.entity.res.GoodsMoreRes">
        select id,goods_id,other_unit_type,other_id,address_id,address_name from kw_business_goods_more where del_flag = 0 and goods_id = #{id}
    </select>
    <select id="selectListDetail" resultType="com.sckw.goods.entity.res.InventoryDetailRes">
        select id,goods_id,goods_num from kw_business_inventory_detail where del_flag = 0 and goods_id = #{id}
    </select>
    <select id="replenishListDetail" resultType="com.sckw.goods.entity.res.GoodsReplenishRes">
        select id,goods_id, name, info from kw_business_goods_replenish where del_flag = 0 and goods_id = #{id}
    </select>

    <select id="findPage" parameterType="com.sckw.goods.entity.req.GoodsPageParam" resultMap="mainEntityMap">
        select
            g.id,
            g.`name`,
            g.goods_num goodsNum,
            g.descn,
            g.price,
            g.stock_quantity stockQuantity,
            g.unit unit,
            g.spec_size specSize,
            g.invoice_rate_type invoiceRateType,
            g.image_url imageUrl,
            g.category_id categoryId,
            g.is_inventory isInventory,
            g.company_id companyId,
            g.company_name companyName,
            g.`status`,
            g.sort,
            g.create_by createBy,
            g.update_by updateBy,
            g.create_time createTime,
            g.update_time updateTime,
            c.category_name categoryName
        from kw_business_goods as g
        left join kw_business_category as c on g.category_id = c.id
        <where>
            g.del_flag = 0
            <if test="name != null and name !=''">
                AND g.`name` like concat('%',#{name},'%')
            </if>
            <if test="goodsNum != null and goodsNum !=''">
                AND g.goods_num like concat('%',#{goodsNum},'%')
            </if>
            <if test="descn != null and descn !=''">
                AND g.descn = #{descn}
            </if>
            <if test="categoryId != null and categoryId !=''">
                AND g.category_id = #{categoryId}
            </if>
            <if test="status != null and status !=''">
                AND g.`status` = #{status}
            </if>
            <if test="companyName != null and companyName !=''">
                AND g.company_name = #{companyName}
            </if>
            <if test="companyId != null">
                AND g.company_id = #{companyId}
            </if>
            <if test="unit != null">
                AND g.unit = #{unitType}
            </if>
            <if test="invoiceRateType != null">
                AND g.invoice_rate_type = #{invoiceRateTypee}
            </if>
            <if test="isInventory != null">
                AND g.is_inventory = #{isInventory}
            </if>
        </where>
        order by g.sort desc,g.create_time desc
    </select>

    <select id="findById" resultType="com.sckw.goods.entity.res.GoodsDetailByIdRes">
        select
        g.id,
        g.`name`,
        g.goods_num,
        g.descn,
        g.price,
        g.stock_quantity,
        g.unit,
        g.spec_size,
        g.invoice_rate,
        g.image_url,
        g.category_id,
        g.is_inventory,
        g.company_id,
        g.company_name,
        g.`status`,
        g.sort,
        g.create_by,
        g.update_by,
        g.create_time,
        g.update_time,
        c.category_name categoryName
        from kw_business_goods as g
        left join kw_business_category as c on g.category_id = c.id
        <where>
            g.id = #{id}
        </where>
    </select>

    <insert id="insert" parameterType="com.sckw.goods.entity.KwBusinessGoods">
        insert into kw_business_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                `name`,
            </if>
            <if test="goodsNum != null">
                goods_num,
            </if>
            <if test="descn != null">
                descn,
            </if>
            <if test="price != null">
                price,
            </if>
            <if test="stockQuantity != null">
                stock_quantity,
            </if>
            <if test="unitType != null and unitType != ''">
                unit_type,
            </if>
            <if test="specSize != null and specSize != ''">
                spec_size,
            </if>
            <if test="invoiceRate != null and invoiceRate != ''">
                invoice_rate,
            </if>
            <if test="imageUrl != null">
                image_url,
            </if>
            <if test="categoryId != null">
                category_id,
            </if>
            <if test="isInventory != null and isInventory != ''">
                is_inventory,
            </if>
            <if test="companyId != null">
                company_id,
            </if>
            <if test="companyName != null and companyName != ''">
                company_name,
            </if>
            <if test="status != null">
                `status`,
            </if>
            <if test="sort != null">
                sort,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="name != null and name != ''">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="goodsNum != null">
                #{goodsNum,jdbcType=VARCHAR},
            </if>
            <if test="descn != null and descn != ''">
                #{descn,jdbcType=VARCHAR},
            </if>
            <if test="price != null">
                #{price,jdbcType=FLOAT},
            </if>
            <if test="stockQuantity != null">
                #{stockQuantity},
            </if>
            <if test="unitType != null and unitType != ''">
                #{unitType,jdbcType=VARCHAR},
            </if>
            <if test="specSize != null and specSize != ''">
                #{specSize},
            </if>
            <if test="invoiceRate != null and invoiceRate != ''">
                #{invoiceRate},
            </if>
            <if test="imageUrl != null and imageUrl != ''">
                #{imageUrl,jdbcType=VARCHAR},
            </if>
            <if test="categoryId != null">
                #{categoryId,jdbcType=BIGINT},
            </if>
            <if test="isInventory != null">
                #{isInventory},
            </if>
            <if test="companyId != null">
                #{companyId},
            </if>
            <if test="companyName != null and companyName != ''">
                #{companyName},
            </if>
            <if test="status != null">
                #{status,jdbcType=INTEGER},
            </if>
            <if test="sort != null">
                #{sort,jdbcType=INTEGER},
            </if>
            <if test="createBy != null and createBy != ''">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=DATE},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=DATE},
            </if>
            <if test="delFlag != null">
                #{delFlag,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>

    <delete id="delete">
        update kw_business_goods set del_flag = 1 where id = #{id}
    </delete>
</mapper>