<?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.ErpProductRelationMapper">
    
    <resultMap type="ErpProductRelation" id="ErpProductRelationResult">
        <result property="id"                column="id"                />
        <result property="favoriteProductId" column="favorite_product_id"/>
        <result property="appid"             column="appid"             />
        <result property="erpProductId"      column="erp_product_id"    />
        <result property="erpProductTitle"   column="erp_product_title" />
        <result property="erpProductUrl"     column="erp_product_url"   />
        <result property="erpProductStatus"  column="erp_product_status"/>
        <result property="remark"            column="remark"            />
        <result property="createTime"        column="create_time"       />
        <result property="updateTime"        column="update_time"       />
    </resultMap>

    <sql id="selectErpProductRelationVo">
        select id, favorite_product_id, appid, erp_product_id, erp_product_title, 
               erp_product_url, erp_product_status, remark, create_time, update_time
        from erp_product_relation
    </sql>

    <select id="selectErpProductRelationList" parameterType="ErpProductRelation" resultMap="ErpProductRelationResult">
        <include refid="selectErpProductRelationVo"/>
        <where>  
            <if test="favoriteProductId != null "> and favorite_product_id = #{favoriteProductId}</if>
            <if test="appid != null  and appid != ''"> and appid = #{appid}</if>
            <if test="erpProductId != null  and erpProductId != ''"> and erp_product_id = #{erpProductId}</if>
            <if test="erpProductStatus != null  and erpProductStatus != ''"> and erp_product_status = #{erpProductStatus}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectErpProductRelationById" parameterType="Long" resultMap="ErpProductRelationResult">
        <include refid="selectErpProductRelationVo"/>
        where id = #{id}
    </select>
    
    <select id="selectErpProductRelationByFavoriteId" parameterType="Long" resultMap="ErpProductRelationResult">
        <include refid="selectErpProductRelationVo"/>
        where favorite_product_id = #{favoriteProductId}
        order by create_time desc
    </select>
    
    <select id="selectErpProductRelationByAppid" parameterType="String" resultMap="ErpProductRelationResult">
        <include refid="selectErpProductRelationVo"/>
        where appid = #{appid}
        order by create_time desc
    </select>
        
    <insert id="insertErpProductRelation" parameterType="ErpProductRelation" useGeneratedKeys="true" keyProperty="id">
        insert into erp_product_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="favoriteProductId != null">favorite_product_id,</if>
            <if test="appid != null and appid != ''">appid,</if>
            <if test="erpProductId != null and erpProductId != ''">erp_product_id,</if>
            <if test="erpProductTitle != null and erpProductTitle != ''">erp_product_title,</if>
            <if test="erpProductUrl != null and erpProductUrl != ''">erp_product_url,</if>
            <if test="erpProductStatus != null and erpProductStatus != ''">erp_product_status,</if>
            <if test="remark != null and remark != ''">remark,</if>
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="favoriteProductId != null">#{favoriteProductId},</if>
            <if test="appid != null and appid != ''">#{appid},</if>
            <if test="erpProductId != null and erpProductId != ''">#{erpProductId},</if>
            <if test="erpProductTitle != null and erpProductTitle != ''">#{erpProductTitle},</if>
            <if test="erpProductUrl != null and erpProductUrl != ''">#{erpProductUrl},</if>
            <if test="erpProductStatus != null and erpProductStatus != ''">#{erpProductStatus},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            sysdate(),
        </trim>
    </insert>

    <update id="updateErpProductRelation" parameterType="ErpProductRelation">
        update erp_product_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="favoriteProductId != null">favorite_product_id = #{favoriteProductId},</if>
            <if test="appid != null and appid != ''">appid = #{appid},</if>
            <if test="erpProductId != null and erpProductId != ''">erp_product_id = #{erpProductId},</if>
            <if test="erpProductTitle != null and erpProductTitle != ''">erp_product_title = #{erpProductTitle},</if>
            <if test="erpProductUrl != null and erpProductUrl != ''">erp_product_url = #{erpProductUrl},</if>
            <if test="erpProductStatus != null and erpProductStatus != ''">erp_product_status = #{erpProductStatus},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            update_time = sysdate(),
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteErpProductRelationById" parameterType="Long">
        delete from erp_product_relation where id = #{id}
    </delete>

    <delete id="deleteErpProductRelationByIds" parameterType="String">
        delete from erp_product_relation where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteErpProductRelationByFavoriteId" parameterType="Long">
        delete from erp_product_relation where favorite_product_id = #{favoriteProductId}
    </delete>
    
    <select id="countErpProductRelationByFavoriteId" parameterType="Long" resultType="Integer">
        select count(*) from erp_product_relation where favorite_product_id = #{favoriteProductId}
    </select>
    
</mapper>
