<?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.clMaterial.mapper.CYlMaterialMapper">

    <resultMap type="CYlMaterial" id="CYlMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="materialNo"    column="material_no"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialPrice"    column="material_price"    />
        <result property="materialState"    column="material_state"    />
    </resultMap>

    <sql id="selectCYlMaterialVo">
        select material_id, material_no, material_name, material_price, material_state from c_yl_material
    </sql>

    <select id="selectCYlMaterialList" parameterType="CYlMaterial" resultMap="CYlMaterialResult">
        <include refid="selectCYlMaterialVo"/>
        <where>
            <if test="materialNo != null  and materialNo != ''"> and material_no = #{materialNo}</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialState != null  and materialState != ''"> and material_state = #{materialState}</if>
        </where>
    </select>

    <select id="selectCYlMaterialByMaterialId" parameterType="Long" resultMap="CYlMaterialResult">
        <include refid="selectCYlMaterialVo"/>
        where material_id = #{materialId}
    </select>

    <insert id="insertCYlMaterial" parameterType="CYlMaterial" useGeneratedKeys="true" keyProperty="materialId">
        insert into c_yl_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialNo != null and materialNo != ''">material_no,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialPrice != null">material_price,</if>
            <if test="materialState != null">material_state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialNo != null and materialNo != ''">#{materialNo},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialPrice != null">#{materialPrice},</if>
            <if test="materialState != null">#{materialState},</if>
         </trim>
    </insert>

    <update id="updateCYlMaterial" parameterType="CYlMaterial">
        update c_yl_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialNo != null and materialNo != ''">material_no = #{materialNo},</if>
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="materialPrice != null">material_price = #{materialPrice},</if>
            <if test="materialState != null">material_state = #{materialState},</if>
        </trim>
        where material_id = #{materialId}
    </update>

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

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

    <select id="checkMaterialNoUnique" parameterType="String" resultMap="CYlMaterialResult">
        select top(1) material_id, material_no from c_yl_material where material_no = #{materialNo} and material_state = '0'
    </select>

    <select id="getBjData" resultType="CYlMaterial">
        SELECT A.m_id_0 materialNo,A.m_name_0 materialName,
        case when isnull(CONVERT(decimal(10,2), C.XLXSCLHQ_HSJG),0)=0 then A.m_price_0 else CONVERT(decimal(10,2), C.XLXSCLHQ_HSJG) end materialPrice
        from aq_base_material  A left join c_user B on A.create_user_uid_0=B.uid_0
        left join (
        select REPLACE(REPLACE(REPLACE(XLXSBJCL_BJCLID,char(10),''),CHAR(13),''),CHAR(32),'')
        as XLXSCLHQ_BJCLID,XLXSBJCL_BJCLMC,
        isnull(XLXSCLHQ_HSJG,0.0)as XLXSCLHQ_HSJG,
        isnull(XLXSCLHQ_WHR,' ')as XLXSCLHQ_WHR,XLXSCLHQ_WHSJ
        from [BJ].[GTDB8].[JSERP8].XLXSBJCL
        left join [BJ].[GTDB8].[JSERP8].XLXSCLHQ on XLXSCLHQ_JGBID=(select xlxsjgb_JGBID
        from [BJ].[GTDB8].[JSERP8].xlxsjgb
        where xlxsjgb_tjys='3 '
        and XLXSJGB_YXQS <![CDATA[<=]]> rtrim(convert(char,getdate(),20))
        and (XLXSJGB_YXQZ is null or XLXSJGB_YXQZ>=rtrim(convert(char,getdate(),20))))
        and XLXSCLHQ_BJCLID=XLXSBJCL_BJCLID
        where 1=1
        ) C on C.XLXSCLHQ_BJCLID = A.bjm_id_0 order by A.m_id_0
    </select>

    <delete id="deleteYlMaterialBjBatch">
        delete from c_yl_material_bjdata
    </delete>

    <insert id="insertYlMaterialBjBatch">
        INSERT INTO c_yl_material_bjdata (material_bj_no, material_bj_name, material_bj_price)
        VALUES
        <foreach collection="list" index="" item="t" separator=",">
            (#{t.materialNo,jdbcType=VARCHAR},
            #{t.materialName,jdbcType=VARCHAR},
            cast(#{t.materialPrice,jdbcType=DECIMAL} as decimal(18,2)))
        </foreach>
    </insert>

    <update id="updateYlMaterialBjBatch">
        update c_yl_material set material_name = a.material_bj_name,material_price=a.material_bj_price
        from c_yl_material_bjdata a where a.material_bj_no = material_no
    </update>
</mapper>
