<?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.sapMaterial.mapper.SapMaterialMapper">

    <resultMap type="SapMaterial" id="SapMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="materialMatnr"    column="material_matnr"    />
        <result property="materialMaktx"    column="material_maktx"    />
        <result property="materialVkorg"    column="material_vkorg"    />
        <result property="materialVtweg"    column="material_vtweg"    />
        <result property="materialMeins"    column="material_meins"    />
        <result property="materialSpart"    column="material_spart"    />
        <result property="materialVtext"    column="material_vtext"    />
        <result property="materialMatkl"    column="material_matkl"    />
        <result property="materialWgbez"    column="material_wgbez"    />
        <result property="materialDwjz"    column="material_dwjz"    />
        <result property="materialChangj"    column="material_changj"    />
        <result property="materialXingh"    column="material_xingh"    />
        <result property="materialGuig"    column="material_guig"    />
        <result property="materialJiem"    column="material_jiem"    />
        <result property="materialDiany"    column="material_diany"    />
        <result property="materialTemp"    column="material_temp"    />
        <result property="materialModel"    column="material_model"    />
        <result property="materialMrbs"    column="material_mrbs"    />
        <result property="materialSfyx"    column="material_sfyx"    />
    </resultMap>

    <sql id="SapMaterialJoins">
        left join Redbook.RedBook.dbo.sdmdm1  rdm on rdm.matnr='0000000000'+sm.material_matnr
    </sql>

    <sql id="selectSapMaterialVo">
        select sm.material_id, sm.material_matnr, sm.material_maktx, sm.material_vkorg, sm.material_vtweg,
        sm.material_meins, sm.material_spart, sm.material_vtext, sm.material_matkl, sm.material_wgbez, sm.material_dwjz,
        case when rdm.当前厂价=0 then null else rdm.当前厂价 end material_changj, sm.material_xingh, sm.material_guig,
        sm.material_jiem, sm.material_diany, sm.material_temp,
        sm.material_model, material_mrbs, material_sfyx
        from sap_material sm
        <include refid="SapMaterialJoins"/>
    </sql>

    <select id="selectSapMaterialList" parameterType="SapMaterial" resultMap="SapMaterialResult">
        <include refid="selectSapMaterialVo"/>
        <where>
            <if test="materialMatnr != null  and materialMatnr != ''"> and sm.material_matnr = #{materialMatnr}</if>
            <if test="materialMaktx != null  and materialMaktx != ''"> and sm.material_maktx like concat('%', #{materialMaktx}, '%')</if>
            <if test="materialMrbs != null  and materialMrbs != ''"> and sm.material_mrbs = #{materialMrbs}</if>
            <if test="materialSfyx != null  and materialSfyx != ''"> and sm.material_sfyx = #{materialSfyx}</if>
        </where>
    </select>

    <select id="selectSapMaterialByMaterialId" parameterType="Long" resultMap="SapMaterialResult">
        <include refid="selectSapMaterialVo"/>
        where sm.material_id = #{materialId}
    </select>

    <insert id="insertSapMaterial" parameterType="SapMaterial" useGeneratedKeys="true" keyProperty="materialId">
        insert into sap_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialMatnr != null and materialMatnr != ''">material_matnr,</if>
            <if test="materialMaktx != null and materialMaktx != ''">material_maktx,</if>
            <if test="materialVkorg != null and materialVkorg != ''">material_vkorg,</if>
            <if test="materialVtweg != null and materialVtweg != ''">material_vtweg,</if>
            <if test="materialMeins != null and materialMeins != ''">material_meins,</if>
            <if test="materialSpart != null and materialSpart != ''">material_spart,</if>
            <if test="materialVtext != null and materialVtext != ''">material_vtext,</if>
            <if test="materialMatkl != null and materialMatkl != ''">material_matkl,</if>
            <if test="materialWgbez != null and materialWgbez != ''">material_wgbez,</if>
            <if test="materialDwjz != null and materialDwjz != ''">material_dwjz,</if>
            <if test="materialChangj != null">material_changj,</if>
            <if test="materialXingh != null">material_xingh,</if>
            <if test="materialGuig != null">material_guig,</if>
            <if test="materialJiem != null">material_jiem,</if>
            <if test="materialDiany != null">material_diany,</if>
            <if test="materialTemp != null">material_temp,</if>
            <if test="materialModel != null">material_model,</if>
            <if test="materialMrbs != null">material_mrbs,</if>
            <if test="materialSfyx != null">material_sfyx,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialMatnr != null and materialMatnr != ''">#{materialMatnr},</if>
            <if test="materialMaktx != null and materialMaktx != ''">#{materialMaktx},</if>
            <if test="materialVkorg != null and materialVkorg != ''">#{materialVkorg},</if>
            <if test="materialVtweg != null and materialVtweg != ''">#{materialVtweg},</if>
            <if test="materialMeins != null and materialMeins != ''">#{materialMeins},</if>
            <if test="materialSpart != null and materialSpart != ''">#{materialSpart},</if>
            <if test="materialVtext != null and materialVtext != ''">#{materialVtext},</if>
            <if test="materialMatkl != null and materialMatkl != ''">#{materialMatkl},</if>
            <if test="materialWgbez != null and materialWgbez != ''">#{materialWgbez},</if>
            <if test="materialDwjz != null and materialDwjz != ''">#{materialDwjz},</if>
            <if test="materialChangj != null">#{materialChangj},</if>
            <if test="materialXingh != null">#{materialXingh},</if>
            <if test="materialGuig != null">#{materialGuig},</if>
            <if test="materialJiem != null">#{materialJiem},</if>
            <if test="materialDiany != null">#{materialDiany},</if>
            <if test="materialTemp != null">#{materialTemp},</if>
            <if test="materialModel != null">#{materialModel},</if>
            <if test="materialMrbs != null">#{materialMrbs},</if>
            <if test="materialSfyx != null">#{materialSfyx},</if>
         </trim>
    </insert>

    <update id="updateSapMaterial" parameterType="SapMaterial">
        update sap_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialMatnr != null and materialMatnr != ''">material_matnr = #{materialMatnr},</if>
            <if test="materialMaktx != null and materialMaktx != ''">material_maktx = #{materialMaktx},</if>
            <if test="materialVkorg != null and materialVkorg != ''">material_vkorg = #{materialVkorg},</if>
            <if test="materialVtweg != null and materialVtweg != ''">material_vtweg = #{materialVtweg},</if>
            <if test="materialMeins != null and materialMeins != ''">material_meins = #{materialMeins},</if>
            <if test="materialSpart != null and materialSpart != ''">material_spart = #{materialSpart},</if>
            <if test="materialVtext != null and materialVtext != ''">material_vtext = #{materialVtext},</if>
            <if test="materialMatkl != null and materialMatkl != ''">material_matkl = #{materialMatkl},</if>
            <if test="materialWgbez != null and materialWgbez != ''">material_wgbez = #{materialWgbez},</if>
            <if test="materialDwjz != null and materialDwjz != ''">material_dwjz = #{materialDwjz},</if>
            <if test="materialChangj != null">material_changj = #{materialChangj},</if>
            <if test="materialXingh != null">material_xingh = #{materialXingh},</if>
            <if test="materialGuig != null">material_guig = #{materialGuig},</if>
            <if test="materialJiem != null">material_jiem = #{materialJiem},</if>
            <if test="materialDiany != null">material_diany = #{materialDiany},</if>
            <if test="materialTemp != null">material_temp = #{materialTemp},</if>
            <if test="materialModel != null">material_model = #{materialModel},</if>
            <if test="materialMrbs != null">material_mrbs = #{materialMrbs},</if>
            <if test="materialSfyx != null">material_sfyx = #{materialSfyx},</if>
        </trim>
        where material_id = #{materialId}
    </update>

    <delete id="deleteSapMaterialByMaterialId" parameterType="Long">
        delete from sap_material where material_id = #{materialId}
    </delete>

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