<?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.ruoyi.jarvis.mapper.ErpProductMapper">
    
    <resultMap type="ErpProduct" id="ErpProductResult">
        <result property="id"              column="id"              />
        <result property="productId"       column="product_id"      />
        <result property="title"           column="title"           />
        <result property="mainImage"       column="main_image"      />
        <result property="price"           column="price"           />
        <result property="stock"           column="stock"           />
        <result property="productStatus"   column="product_status"  />
        <result property="saleStatus"      column="sale_status"     />
        <result property="userName"        column="user_name"       />
        <result property="onlineTime"      column="online_time"     />
        <result property="offlineTime"     column="offline_time"    />
        <result property="soldTime"        column="sold_time"       />
        <result property="createTimeXy"    column="create_time_xy"  />
        <result property="updateTimeXy"    column="update_time_xy"  />
        <result property="appid"           column="appid"           />
        <result property="productUrl"      column="product_url"     />
        <result property="remark"          column="remark"          />
        <result property="createTime"      column="create_time"     />
        <result property="updateTime"      column="update_time"     />
    </resultMap>

    <sql id="selectErpProductVo">
        select id, product_id, title, main_image, price, stock, product_status, sale_status, 
               user_name, online_time, offline_time, sold_time, create_time_xy, update_time_xy, 
               appid, product_url, remark, create_time, update_time 
        from erp_product
    </sql>

    <select id="selectErpProductList" parameterType="ErpProduct" resultMap="ErpProductResult">
        <include refid="selectErpProductVo"/>
        <where>  
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="productStatus != null "> and product_status = #{productStatus}</if>
            <if test="saleStatus != null "> and sale_status = #{saleStatus}</if>
            <if test="userName != null  and userName != ''"> and user_name = #{userName}</if>
            <if test="appid != null  and appid != ''"> and appid = #{appid}</if>
        </where>
        order by update_time_xy desc, id desc
    </select>
    
    <select id="selectErpProductById" parameterType="Long" resultMap="ErpProductResult">
        <include refid="selectErpProductVo"/>
        where id = #{id}
    </select>

    <select id="selectErpProductByProductIdAndAppid" resultMap="ErpProductResult">
        <include refid="selectErpProductVo"/>
        where product_id = #{productId} and appid = #{appid}
    </select>
        
    <insert id="insertErpProduct" parameterType="ErpProduct" useGeneratedKeys="true" keyProperty="id">
        insert into erp_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productId != null">product_id,</if>
            <if test="title != null">title,</if>
            <if test="mainImage != null">main_image,</if>
            <if test="price != null">price,</if>
            <if test="stock != null">stock,</if>
            <if test="productStatus != null">product_status,</if>
            <if test="saleStatus != null">sale_status,</if>
            <if test="userName != null">user_name,</if>
            <if test="onlineTime != null">online_time,</if>
            <if test="offlineTime != null">offline_time,</if>
            <if test="soldTime != null">sold_time,</if>
            <if test="createTimeXy != null">create_time_xy,</if>
            <if test="updateTimeXy != null">update_time_xy,</if>
            <if test="appid != null">appid,</if>
            <if test="productUrl != null">product_url,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productId != null">#{productId},</if>
            <if test="title != null">#{title},</if>
            <if test="mainImage != null">#{mainImage},</if>
            <if test="price != null">#{price},</if>
            <if test="stock != null">#{stock},</if>
            <if test="productStatus != null">#{productStatus},</if>
            <if test="saleStatus != null">#{saleStatus},</if>
            <if test="userName != null">#{userName},</if>
            <if test="onlineTime != null">#{onlineTime},</if>
            <if test="offlineTime != null">#{offlineTime},</if>
            <if test="soldTime != null">#{soldTime},</if>
            <if test="createTimeXy != null">#{createTimeXy},</if>
            <if test="updateTimeXy != null">#{updateTimeXy},</if>
            <if test="appid != null">#{appid},</if>
            <if test="productUrl != null">#{productUrl},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateErpProduct" parameterType="ErpProduct">
        update erp_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="productId != null">product_id = #{productId},</if>
            <if test="title != null">title = #{title},</if>
            <if test="mainImage != null">main_image = #{mainImage},</if>
            <if test="price != null">price = #{price},</if>
            <if test="stock != null">stock = #{stock},</if>
            <if test="productStatus != null">product_status = #{productStatus},</if>
            <if test="saleStatus != null">sale_status = #{saleStatus},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="onlineTime != null">online_time = #{onlineTime},</if>
            <if test="offlineTime != null">offline_time = #{offlineTime},</if>
            <if test="soldTime != null">sold_time = #{soldTime},</if>
            <if test="createTimeXy != null">create_time_xy = #{createTimeXy},</if>
            <if test="updateTimeXy != null">update_time_xy = #{updateTimeXy},</if>
            <if test="appid != null">appid = #{appid},</if>
            <if test="productUrl != null">product_url = #{productUrl},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteErpProductById" parameterType="Long">
        delete from erp_product where id = #{id}
    </delete>

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

    <insert id="batchInsertOrUpdateErpProduct" parameterType="java.util.List">
        insert into erp_product 
            (product_id, title, main_image, price, stock, product_status, sale_status, 
             user_name, online_time, offline_time, sold_time, create_time_xy, update_time_xy, 
             appid, product_url, remark)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.productId}, #{item.title}, #{item.mainImage}, #{item.price}, #{item.stock}, 
             #{item.productStatus}, #{item.saleStatus}, #{item.userName}, #{item.onlineTime}, 
             #{item.offlineTime}, #{item.soldTime}, #{item.createTimeXy}, #{item.updateTimeXy}, 
             #{item.appid}, #{item.productUrl}, #{item.remark})
        </foreach>
        ON DUPLICATE KEY UPDATE
            title = VALUES(title),
            main_image = VALUES(main_image),
            price = VALUES(price),
            stock = VALUES(stock),
            product_status = VALUES(product_status),
            sale_status = VALUES(sale_status),
            user_name = VALUES(user_name),
            online_time = VALUES(online_time),
            offline_time = VALUES(offline_time),
            sold_time = VALUES(sold_time),
            create_time_xy = VALUES(create_time_xy),
            update_time_xy = VALUES(update_time_xy),
            product_url = VALUES(product_url),
            remark = VALUES(remark),
            update_time = NOW()
    </insert>

</mapper>

