<?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.GoodsSpecificationDao">

    <resultMap type="com.kmall.admin.entity.GoodsSpecificationEntity" id="goodsSpecificationMap">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="specificationId" column="specification_id"/>
        <result property="value" column="value"/>
        <result property="picUrl" column="pic_url"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.GoodsSpecificationEntity">
		select
			`id`,
			`goods_id`,
			`specification_id`,
			`value`,
			`pic_url`
		from mall_goods_specification
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.kmall.admin.entity.GoodsSpecificationEntity">
        select
        mall_goods_specification.id,
        mall_goods_specification.goods_id,
        mall_goods_specification.specification_id,
        mall_goods_specification.value,
        mall_goods_specification.pic_url,
        mall_goods.name goods_name,
        mall_specification.name specification_name
        from mall_goods_specification
        LEFT JOIN mall_goods ON mall_goods.id = mall_goods_specification.goods_id
        LEFT JOIN mall_specification ON mall_specification.id = mall_goods_specification.specification_id
        WHERE 1=1
        <if test="goodsName != null and goodsName.trim() != ''">
            AND mall_goods.name LIKE concat('%',#{goodsName},'%')
        </if>
        <if test="goodsId != null">
            AND mall_goods_specification.goods_id = #{goodsId}
        </if>
        <if test="specificationId != null">
            AND mall_goods_specification.specification_id = #{specificationId}
        </if>
        <if test="ids != null">
            AND mall_goods_specification.id in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by mall_goods_specification.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_specification
        LEFT JOIN mall_goods ON mall_goods.id = mall_goods_specification.goods_id
        WHERE 1=1
        <if test="goodsName != null and goodsName.trim() != ''">
            AND mall_goods.name LIKE concat('%',#{goodsName},'%')
        </if>
        <if test="goodsId != null">
            AND mall_goods_specification.goods_id = #{goodsId}
        </if>
        <if test="specificationId != null">
            AND mall_goods_specification.specification_id = #{specificationId}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.GoodsSpecificationEntity" useGeneratedKeys="true"
            keyProperty="id">
		insert into mall_goods_specification(
			`goods_id`,
			`specification_id`,
			`value`,
			`pic_url`)
		values(
			#{goodsId},
			#{specificationId},
			#{value},
			#{picUrl})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.GoodsSpecificationEntity">
        update mall_goods_specification
        <set>
            <if test="goodsId != null">`goods_id` = #{goodsId},</if>
            <if test="specificationId != null">`specification_id` = #{specificationId},</if>
            <if test="value != null">`value` = #{value},</if>
            <if test="picUrl != null">`pic_url` = #{picUrl}</if>
        </set>
        where id = #{id}
    </update>

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

    <delete id="deleteByGoodsId">
        delete from mall_goods_specification where goods_id = #{goodsId}
    </delete>

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

</mapper>