<?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.fs.company.mapper.CompanyProductMapper">
    
    <resultMap type="CompanyProduct" id="CompanyProductResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="company_name"/> <!-- 新增 -->
        <result property="productCode"    column="product_code"    />
        <result property="productName"    column="product_name"    />
        <result property="status"    column="status"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCompanyProductVo">
        select
        cp.id,
        cp.company_id,
        c.company_name,
        cp.product_code,
        cp.product_name,
        cp.status,
        cp.sort,
        cp.create_time,
        cp.update_time,
        cp.create_by,
        cp.update_by,
        cp.del_flag,
        cp.remark
        from company_product cp
        left join company c on cp.company_id = c.company_id
    </sql>

    <select id="selectCompanyProductList" parameterType="CompanyProduct" resultMap="CompanyProductResult">
        <include refid="selectCompanyProductVo"/>
        <where>
            <if test="companyId != null"> and cp.company_id = #{companyId}</if>
            <if test="productCode != null and productCode != ''"> and cp.product_code = #{productCode}</if>
            <if test="productName != null and productName != ''">
                and cp.product_name like concat('%', #{productName}, '%')
            </if>
            <if test="status != null"> and cp.status = #{status}</if>
            <if test="sort != null"> and cp.sort = #{sort}</if>
        </where>
        order by cp.sort, cp.create_time desc
    </select>
    
    <select id="selectCompanyProductById" parameterType="Long" resultMap="CompanyProductResult">
        <include refid="selectCompanyProductVo"/>
        where cp.id = #{id}
    </select>
        
    <insert id="insertCompanyProduct" parameterType="CompanyProduct" useGeneratedKeys="true" keyProperty="id">
        insert into company_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="productCode != null and productCode != ''">product_code,</if>
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="status != null">status,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="productCode != null and productCode != ''">#{productCode},</if>
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="status != null">#{status},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCompanyProduct" parameterType="CompanyProduct">
        update company_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="productCode != null and productCode != ''">product_code = #{productCode},</if>
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyProductById" parameterType="Long">
        delete from company_product where id = #{id}
    </delete>

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