<?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.CompanyPurchaseMapper">
    
    <resultMap type="CompanyPurchase" id="CompanyPurchaseResult">
        <result property="id"    column="id"    />
        <result property="materialId"    column="material_id"    />
        <result property="batchNo"    column="batch_no"    />
        <result property="companyId"    column="company_id"    />
        <result property="num"    column="num"    />
        <result property="opCompanyId"    column="op_company_id"    />
        <result property="cityIds"    column="city_ids"    />
        <result property="cityName"    column="city_name"    />
        <result property="linkDesc"    column="link_desc"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="updateUserId"    column="update_user_id"    />
        <result property="purchaseTime"    column="purchase_time"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectCompanyPurchaseVo">
        select id, material_id, batch_no, company_id, num, op_company_id, city_ids, city_name, link_desc, remark, create_time, update_time, create_user_id, update_user_id, purchase_time, is_del from company_purchase
    </sql>

    <select id="selectCompanyPurchaseList" parameterType="CompanyPurchase" resultMap="CompanyPurchaseResult">
        <include refid="selectCompanyPurchaseVo"/>
        <where>  
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="batchNo != null  and batchNo != ''"> and batch_no = #{batchNo}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="opCompanyId != null "> and op_company_id = #{opCompanyId}</if>
            <if test="cityIds != null  and cityIds != ''"> and city_ids = #{cityIds}</if>
            <if test="cityName != null  and cityName != ''"> and city_name like concat('%', #{cityName}, '%')</if>
            <if test="linkDesc != null  and linkDesc != ''"> and link_desc = #{linkDesc}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="updateUserId != null "> and update_user_id = #{updateUserId}</if>
            <if test="purchaseTime != null "> and purchase_time = #{purchaseTime}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectCompanyPurchaseById" parameterType="Long" resultMap="CompanyPurchaseResult">
        <include refid="selectCompanyPurchaseVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertCompanyPurchase" parameterType="CompanyPurchase" useGeneratedKeys="true" keyProperty="id">
        insert into company_purchase
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null">material_id,</if>
            <if test="batchNo != null">batch_no,</if>
            <if test="companyId != null">company_id,</if>
            <if test="num != null">num,</if>
            <if test="opCompanyId != null">op_company_id,</if>
            <if test="cityIds != null">city_ids,</if>
            <if test="cityName != null">city_name,</if>
            <if test="linkDesc != null">link_desc,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="updateUserId != null">update_user_id,</if>
            <if test="purchaseTime != null">purchase_time,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null">#{materialId},</if>
            <if test="batchNo != null">#{batchNo},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="num != null">#{num},</if>
            <if test="opCompanyId != null">#{opCompanyId},</if>
            <if test="cityIds != null">#{cityIds},</if>
            <if test="cityName != null">#{cityName},</if>
            <if test="linkDesc != null">#{linkDesc},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="updateUserId != null">#{updateUserId},</if>
            <if test="purchaseTime != null">#{purchaseTime},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateCompanyPurchase" parameterType="CompanyPurchase">
        update company_purchase
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="batchNo != null">batch_no = #{batchNo},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="num != null">num = #{num},</if>
            <if test="opCompanyId != null">op_company_id = #{opCompanyId},</if>
            <if test="cityIds != null">city_ids = #{cityIds},</if>
            <if test="cityName != null">city_name = #{cityName},</if>
            <if test="linkDesc != null">link_desc = #{linkDesc},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="updateUserId != null">update_user_id = #{updateUserId},</if>
            <if test="purchaseTime != null">purchase_time = #{purchaseTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyPurchaseById" parameterType="Long">
        delete from company_purchase where id = #{id}
    </delete>

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