<?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.bawei.mall.product.mapper.MallProductInfoMapper">

    <resultMap type="MallProductInfo" id="MallProductInfoResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="productDesc"    column="product_desc"    />
        <result property="type"    column="type"    />
        <result property="typeIds"    column="type_ids"    />
        <result property="img"    column="img"    />
        <result property="carouselImages"    column="carousel_images"    />
        <result property="commentCount"    column="comment_count"    />
        <result property="collectCount"    column="collect_count"    />
        <result property="brand"    column="brand"    />
        <result property="status"    column="status"    />
        <result property="unit"    column="unit"    />
        <result property="keywords"    column="keywords"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="revision"    column="revision"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap type="ProductModel" id="productModelResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="productDesc"    column="product_desc"    />
        <result property="type"    column="type"    />
        <result property="typeName"    column="type_name"    />
        <result property="typeIds"    column="type_ids"    />
        <result property="img"    column="img"    />
        <result property="carouselImages"    column="carousel_images"    />
        <result property="commentCount"    column="comment_count"    />
        <result property="collectCount"    column="collect_count"    />
        <result property="brand"    column="brand"    />
        <result property="brandName"    column="brand_name"    />
        <result property="status"    column="status"    />
        <result property="unit"    column="unit"    />
        <result property="keywords"    column="keywords"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="revision"    column="revision"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectMallProductInfoVo">
        select id, name, product_desc, type, type_ids, img, carousel_images, comment_count, collect_count, brand, status, unit, keywords, rule_id, revision, create_by, create_time, update_by, update_time from mall_product_info
    </sql>

    <select id="selectMallProductInfoList" parameterType="MallProductInfo" resultMap="MallProductInfoResult">
        <include refid="selectMallProductInfoVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="brand != null  and brand != ''"> and brand = #{brand}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="keywords != null  and keywords != ''"> and keywords like concat('%', #{keywords}, '%')</if>
        </where>
    </select>

    <select id="selectMallProductInfoById" parameterType="Long" resultMap="MallProductInfoResult">
        <include refid="selectMallProductInfoVo"/>
        where id = #{id}
    </select>
    <select id="selectMallProductInfoCount" resultType="java.lang.Long">
        select count(id) from mall_product_info
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="brand != null  and brand != ''"> and brand = #{brand}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="keywords != null  and keywords != ''"> and keywords like concat('%', #{keywords}, '%')</if>
        </where>
    </select>
    <select id="selectProductModelById" resultMap="productModelResult">
        SELECT
            productInfo.id,
            productInfo.`name`,
            productInfo.product_desc,
            productInfo.type,
            typeInfo.`name` type_name,
            productInfo.type_ids,
            productInfo.img,
            productInfo.carousel_images,
            productInfo.comment_count,
            productInfo.collect_count,
            productInfo.brand,
            brandInfo.`name` brand_name,
            productInfo.`status`,
            productInfo.unit,
            productInfo.keywords,
            productInfo.rule_id,
            productInfo.revision,
            productInfo.create_by,
            productInfo.create_time,
            productInfo.update_by,
            productInfo.update_time
        FROM
            mall_product_info productInfo
        LEFT JOIN mall_product_brand_info brandInfo ON productInfo.brand = brandInfo.id
        LEFT JOIN mall_product_type_info typeInfo ON productInfo.type = typeInfo.id
        WHERE productInfo.id = #{id}
    </select>

    <insert id="insertMallProductInfo" parameterType="MallProductInfo" useGeneratedKeys="true" keyProperty="id">
        insert into mall_product_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="productDesc != null and productDesc != ''">product_desc,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="typeIds != null">type_ids,</if>
            <if test="img != null and img != ''">img,</if>
            <if test="carouselImages != null and carouselImages != ''">carousel_images,</if>
            <if test="commentCount != null">comment_count,</if>
            <if test="collectCount != null">collect_count,</if>
            <if test="brand != null and brand != ''">brand,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="unit != null and unit != ''">unit,</if>
            <if test="keywords != null and keywords != ''">keywords,</if>
            <if test="ruleId != null">rule_id,</if>
            <if test="revision != null">revision,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="productDesc != null and productDesc != ''">#{productDesc},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="typeIds != null">#{typeIds},</if>
            <if test="img != null and img != ''">#{img},</if>
            <if test="carouselImages != null and carouselImages != ''">#{carouselImages},</if>
            <if test="commentCount != null">#{commentCount},</if>
            <if test="collectCount != null">#{collectCount},</if>
            <if test="brand != null and brand != ''">#{brand},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="unit != null and unit != ''">#{unit},</if>
            <if test="keywords != null and keywords != ''">#{keywords},</if>
            <if test="ruleId != null">#{ruleId},</if>
            <if test="revision != null">#{revision},</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>
         </trim>
    </insert>

    <update id="updateMallProductInfo" parameterType="MallProductInfo">
        update mall_product_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="productDesc != null and productDesc != ''">product_desc = #{productDesc},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="typeIds != null">type_ids = #{typeIds},</if>
            <if test="img != null and img != ''">img = #{img},</if>
            <if test="carouselImages != null and carouselImages != ''">carousel_images = #{carouselImages},</if>
            <if test="commentCount != null">comment_count = #{commentCount},</if>
            <if test="collectCount != null">collect_count = #{collectCount},</if>
            <if test="brand != null and brand != ''">brand = #{brand},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="unit != null and unit != ''">unit = #{unit},</if>
            <if test="keywords != null and keywords != ''">keywords = #{keywords},</if>
            <if test="ruleId != null">rule_id = #{ruleId},</if>
            <if test="revision != null">revision = #{revision},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallProductInfoById" parameterType="Long">
        delete from mall_product_info where id = #{id}
    </delete>

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