<?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.kmall.admin.dao.GoodsDao">

    <resultMap type="com.kmall.admin.entity.GoodsEntity" id="goodsMap">
        <result property="id" column="id"/>
        <result property="categoryId" column="category_id"/>
        <result property="goodsSn" column="goods_sn"/>
        <result property="name" column="name"/>
        <result property="brandId" column="brand_id"/>
        <result property="freightId" column="freight_id"/>
        <result property="brand" column="brand"/>
        <result property="goodsNumber" column="goods_number"/>
        <result property="keywords" column="keywords"/>
        <result property="goodsBrief" column="goods_brief"/>
        <result property="goodsDesc" column="goods_desc"/>
        <result property="isOnSale" column="is_on_sale"/>
        <result property="addTime" column="add_time"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="isDelete" column="is_delete"/>
        <result property="attributeCategory" column="attribute_category"/>
        <result property="counterPrice" column="counter_price"/>
        <result property="extraPrice" column="extra_price"/>
        <result property="isNew" column="is_new"/>
        <result property="goodsUnit" column="goods_unit"/>
        <result property="primaryPicUrl" column="primary_pic_url"/>
        <result property="listPicUrl" column="list_pic_url"/>
        <result property="retailPrice" column="retail_price"/>
        <result property="sellVolume" column="sell_volume"/>
        <result property="primaryProductId" column="primary_product_id"/>
        <result property="unitPrice" column="unit_price"/>
        <result property="promotionDesc" column="promotion_desc"/>
        <result property="promotionTag" column="promotion_tag"/>
        <result property="appExclusivePrice" column="app_exclusive_price"/>
        <result property="isAppExclusive" column="is_app_exclusive"/>
        <result property="isLimited" column="is_limited"/>
        <result property="isHot" column="is_hot"/>
        <result property="marketPrice" column="market_price"/>
        <result property="goodsType" column="goodsType"/>
        <result property="sku" column="sku"/>
        <result property="goodsBizType" column="goods_biz_type"/>
        <result column="creater_sn" property="createrSn" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />
        <result column="mod_time" property="modTime" jdbcType="TIMESTAMP" />
        <result column="tstm" property="tstm" jdbcType="TIMESTAMP" />
        <result property="prodBarcode" column="prod_barcode"/>
        <result property="unitCode" column="unit_code"/>
        <result property="goodsDesc" column="goods_desc"/>
        <result property="cusGoodsCode" column="cus_goods_code"/>
        <result property="ciqProdModel" column="ciq_prod_model"/>
        <result property="oriCntCode" column="ori_cnt_code"/>
        <result property="cusDeclEle" column="cus_decl_ele"/>
        <result property="cusRecCode" column="cus_rec_code"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.GoodsEntity">
        SELECT
            a.*, CASE
        when g.id > 0 then 2 else 0 end as goodsType
        FROM
            mall_goods a
        LEFT JOIN mall_goods_group g ON g.goods_id = a.id
        AND g.open_status != 3
        WHERE a.id = #{value}
    </select>

    <select id="queryObjectBySn" resultType="com.kmall.admin.entity.GoodsEntity">
        SELECT
            a.*, CASE
        when g.id > 0 then 2 else 0 end as goodsType
        FROM
            mall_goods a
        LEFT JOIN mall_goods_group g ON g.goods_id = a.id
        AND g.open_status != 3
        where a.goods_sn = #{goodsSn}
    </select>

    <select id="queryList" resultType="com.kmall.admin.entity.GoodsEntity">
        select
        mall_goods.*,
        mall_category.name category_name,
        mall_brand.name brand_name,
        mall_freight.name freight_name,
        case when mall_goods_group.id > 0 then 2 else 0 end as goodsType
        from mall_goods
        LEFT JOIN mall_category ON mall_goods.category_id = mall_category.id
        LEFT JOIN mall_brand ON mall_brand.id = mall_goods.brand_id
        left join mall_goods_group on mall_goods_group.goods_id = mall_goods.id and mall_goods_group.open_status != 3
        left join mall_freight on mall_goods.freight_id = mall_freight.id
        WHERE 1=1
        <!--  数据过滤  -->
        ${filterSql}
        <if test="goodsSn != null and goodsSn != ''">
            AND mall_goods.goods_sn like concat('%',#{goodsSn},'%')
        </if>
        <if test="name != null and name != ''">
            AND mall_goods.name LIKE concat('%',#{name},'%')
        </if>
        <if test="freightId != null and freightId != ''">
            AND mall_goods.freight_id = #{freightId}
        </if>
        <if test="categoryId != null and categoryId != ''">
            AND mall_goods.category_id = #{categoryId}
        </if>
        <if test="sku != null and sku != ''">
            AND mall_goods.sku LIKE concat('%',#{sku},'%')
        </if>
        <if test="category != null and category != ''">
            AND mall_goods.category_id IN (select id from mall_category where parent_id = #{category})
        </if>
        <if test="categoryTwo != null and categoryTwo != ''">
            AND mall_goods.category_id = #{categoryTwo}
        </if>
        <if test="isDelete != null and isDelete != ''">
            AND mall_goods.is_Delete = #{isDelete}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by mall_goods.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="querySame" resultType="com.kmall.admin.entity.GoodsEntity">
        select
        mall_goods.*,
        mall_category.name category_name,
        mall_brand.name brand_name,
        mall_freight.name freight_name,
        case when mall_goods_group.id > 0 then 2 else 0 end as goodsType
        from mall_goods
        LEFT JOIN mall_category ON mall_goods.category_id = mall_category.id
        LEFT JOIN mall_brand ON mall_brand.id = mall_goods.brand_id
        left join mall_goods_group on mall_goods_group.goods_id = mall_goods.id and mall_goods_group.open_status != 3
        left join mall_freight on mall_goods.freight_id = mall_freight.id
        WHERE 1=1
        <!--  数据过滤  -->
        ${filterSql}
        <if test="isSame != null and isSame != ''">
            AND ((mall_goods.sku = #{sku} AND mall_goods.goods_biz_type = #{goodsBizType}) OR mall_goods.goods_sn = #{goodsSn})
            <if test="id != null and id != ''">
                AND mall_goods.id != #{id}
            </if>
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by mall_goods.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_goods WHERE 1=1
        <!--  数据过滤  -->
        ${filterSql}
        <if test="goodsSn != null and goodsSn != ''">
            AND mall_goods.goods_sn like concat('%',#{goodsSn},'%')
        </if>
        <if test="name != null and name != ''">
            AND mall_goods.name LIKE concat('%',#{name},'%')
        </if>
        <if test="freightId != null and freightId != ''">
            AND mall_goods.freight_id = #{freightId}
        </if>
        <if test="categoryId != null and categoryId != ''">
            AND mall_goods.category_id = #{categoryId}
        </if>
        <if test="sku != null and sku != ''">
            AND mall_goods.sku LIKE concat('%',#{sku},'%')
        </if>
        <if test="category != null and category != ''">
            AND mall_goods.category_id IN (select id from mall_category where parent_id = #{category})
        </if>
        <if test="categoryTwo != null and categoryTwo != ''">
            AND mall_goods.category_id = #{categoryTwo}
        </if>
        <if test="isDelete != null and isDelete != ''">
            AND mall_goods.is_Delete = #{isDelete}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.GoodsEntity" useGeneratedKeys="true" keyProperty="id">
        insert into mall_goods(
			`category_id`,
			`goods_sn`,
			`prod_barcode`,
			`sku`,
			`goods_biz_type`,
			`name`,
			`unit_code`,
			`brand_id`,
			`brand`,
			`freight_id`,
			`goods_number`,
			`keywords`,
			`goods_brief`,
			`goods_desc`,
			`cus_goods_code`,
			`ciq_prod_model`,
			`ori_cnt_code`,
			`cus_decl_ele`,
			`cus_rec_code`,
			`is_on_sale`,
			`add_time`,
			`sort_order`,
			`is_delete`,
			`attribute_category`,
			`counter_price`,
			`extra_price`,
			`is_new`,
			`goods_unit`,
			`primary_pic_url`,
			`list_pic_url`,
			`goods_rate`,
			`retail_price`,
			`sell_volume`,
			`primary_product_id`,
			`unit_price`,
			`promotion_desc`,
			`promotion_tag`,
			`app_exclusive_price`,
			`is_app_exclusive`,
			`is_limited`,
			`is_hot`,
			`market_price`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{categoryId},
			#{goodsSn},
			#{prodBarcode},
			#{sku},
			#{goodsBizType},
			#{name},
			#{unitCode},
			#{brandId},
			#{brand},
			#{freightId},
			#{goodsNumber},
			#{keywords},
			#{goodsBrief},
			#{goodsDesc},
			#{cusGoodsCode},
			#{ciqProdModel},
			#{oriCntCode},
			#{cusDeclEle},
			#{cusRecCode},
			#{isOnSale},
			#{addTime},
			#{sortOrder},
			#{isDelete},
			#{attributeCategory},
			#{counterPrice},
			#{extraPrice},
			#{isNew},
			#{goodsUnit},
			#{primaryPicUrl},
			#{listPicUrl},
			#{goodsRate},
			#{retailPrice},
			#{sellVolume},
			#{primaryProductId},
			#{unitPrice},
			#{promotionDesc},
			#{promotionTag},
			#{appExclusivePrice},
			#{isAppExclusive},
			#{isLimited},
			#{isHot},
			#{marketPrice},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
    </insert>

    <update id="update" parameterType="com.kmall.admin.entity.GoodsEntity">
        update mall_goods
        <set>
            <if test="categoryId != null">`category_id` = #{categoryId}, </if>
            <if test="goodsSn != null">`goods_sn` = #{goodsSn}, </if>
            <if test="prodBarcode != null">`prod_barcode` = #{prodBarcode}, </if>
            <if test="sku != null">`sku` = #{sku}, </if>
            <if test="goodsBizType != null">`goods_biz_type` = #{goodsBizType}, </if>
            <if test="name != null">`name` = #{name}, </if>
            <if test="unitCode != null">`unit_code` = #{unitCode}, </if>
            <if test="brandId != null">`brand_id` = #{brandId}, </if>
            <if test="brand != null">`brand` = #{brand}, </if>
            <if test="freightId != null">`freight_id` = #{freightId}, </if>
            <if test="goodsNumber != null">`goods_number` = #{goodsNumber}, </if>
            <if test="keywords != null">`keywords` = #{keywords}, </if>
            <if test="goodsBrief != null">`goods_brief` = #{goodsBrief}, </if>
            <if test="goodsDesc != null">`goods_desc` = #{goodsDesc}, </if>
            <if test="cusGoodsCode != null">`cus_goods_code` = #{cusGoodsCode}, </if>
            <if test="ciqProdModel != null">`ciq_prod_model` = #{ciqProdModel}, </if>
            <if test="oriCntCode != null">`ori_cnt_code` = #{oriCntCode}, </if>
            <if test="cusDeclEle != null">`cus_decl_ele` = #{cusDeclEle}, </if>
            <if test="cusRecCode != null">`cus_rec_code` = #{cusRecCode}, </if>
            <if test="isOnSale != null">`is_on_sale` = #{isOnSale}, </if>
            <if test="addTime != null">`add_time` = #{addTime}, </if>
            <if test="sortOrder != null">`sort_order` = #{sortOrder}, </if>
            <if test="isDelete != null">`is_delete` = #{isDelete}, </if>
            <if test="attributeCategory != null">`attribute_category` = #{attributeCategory}, </if>
            <if test="counterPrice != null">`counter_price` = #{counterPrice}, </if>
            <if test="extraPrice != null">`extra_price` = #{extraPrice}, </if>
            <if test="isNew != null">`is_new` = #{isNew}, </if>
            <if test="goodsUnit != null">`goods_unit` = #{goodsUnit}, </if>
            <if test="primaryPicUrl != null">`primary_pic_url` = #{primaryPicUrl}, </if>
            <if test="listPicUrl != null">`list_pic_url` = #{listPicUrl}, </if>
            <if test="goodsRate != null">`goods_rate` = #{goodsRate}, </if>
            <if test="retailPrice != null">`retail_price` = #{retailPrice}, </if>
            <if test="sellVolume != null">`sell_volume` = #{sellVolume}, </if>
            <if test="primaryProductId != null">`primary_product_id` = #{primaryProductId}, </if>
            <if test="unitPrice != null">`unit_price` = #{unitPrice}, </if>
            <if test="promotionDesc != null">`promotion_desc` = #{promotionDesc}, </if>
            <if test="promotionTag != null">`promotion_tag` = #{promotionTag}, </if>
            <if test="appExclusivePrice != null">`app_exclusive_price` = #{appExclusivePrice}, </if>
            <if test="isAppExclusive != null">`is_app_exclusive` = #{isAppExclusive}, </if>
            <if test="isLimited != null">`is_limited` = #{isLimited}, </if>
            <if test="isHot != null">`is_hot` = #{isHot}, </if>
            <if test="marketPrice != null">`market_price` = #{marketPrice}, </if>
            <if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
            <if test="createTime != null">`create_time` = #{createTime}, </if>
            <if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
            <if test="modTime != null">`mod_time` = #{modTime}, </if>
            <if test="tstm != null">`tstm` = #{tstm}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
        delete from mall_goods where id = #{value}
    </delete>

    <delete id="deleteBatch">
        delete from mall_goods where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="queryMaxId" resultType="java.lang.Long" parameterType="map">
        SELECT MAX(id) FROM mall_goods
    </select>
</mapper>