<?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.material.mapper.CMaterialMapper">

    <resultMap type="CMaterial" id="CMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="materialTypeId"    column="material_type_id"    />
        <result property="materialXingh"    column="material_xingh"    />
        <result property="materialGuig"    column="material_guig"    />
        <result property="materialDiany"    column="material_diany"    />
        <result property="materialDw"    column="material_dw"    />
        <result property="typeName"    column="type_name"    />
        <result property="materialState"    column="material_state"    />
    </resultMap>

    <resultMap id="CMaterialCMaterialCostResult" type="CMaterial" extends="CMaterialResult">
        <collection property="cMaterialCostList" notNullColumn="sub_cost_id" javaType="java.util.List" resultMap="CMaterialCostResult" />
    </resultMap>

    <resultMap type="CMaterialCost" id="CMaterialCostResult">
        <result property="costId"    column="sub_cost_id"    />
        <result property="costMaterialId"    column="sub_cost_material_id"    />
        <result property="costClId"    column="sub_cost_cl_id"    />
        <result property="costClQty"    column="sub_cost_cl_qty"    />
        <result property="costClQty2"    column="sub_cost_cl_qty_2"    />
        <result property="materialPrice"    column="sub_material_price"    />
        <result property="materialName"    column="sub_material_name"    />

    </resultMap>

    <sql id="selectCMaterialVo">
        select a.material_id,a.material_type_id, a.material_xingh, a.material_guig, a.material_diany, a.material_dw,
               case when b.type_state != '0' then  b.type_name+'(停用)' else b.type_name  end type_name,
               a.material_state
        from c_material a
        left join c_material_type b on a.material_type_id = b.type_no
    </sql>

    <select id="selectCMaterialList" parameterType="CMaterial" resultMap="CMaterialResult">
        <include refid="selectCMaterialVo"/>
        <where>
            <if test="materialXingh != null  and materialXingh != ''"> and material_xingh like concat('%', #{materialXingh}, '%')</if>
            <if test="materialGuig != null  and materialGuig != ''"> and material_guig like concat('%', #{materialGuig}, '%')</if>
            <if test="materialDiany != null  and materialDiany != ''"> and material_diany like concat('%', #{materialDiany}, '%')</if>
            <if test="materialState != null  and materialState != ''"> and material_state = #{materialState}</if>
        </where>
    </select>

    <select id="selectCMaterialByMaterialId" parameterType="Long" resultMap="CMaterialCMaterialCostResult">
        select a.material_id, case when d.type_state != '0' then '' else a.material_type_id end material_type_id,
               a.material_xingh, a.material_guig, a.material_diany, a.material_dw,a.material_state,
        b.cost_id as sub_cost_id, b.cost_material_id as sub_cost_material_id,
        case when c.material_state!='0' then '' else b.cost_cl_id end sub_cost_cl_id,
        case when c.material_state!='0' then 0 else b.cost_cl_qty end sub_cost_cl_qty,
        case when c.material_state!='0' then 0 else b.cost_cl_qty_2 end sub_cost_cl_qty_2,
        case when c.material_state!='0' then 0 else c.material_price end sub_material_price,
        c.material_name as sub_material_name
        from c_material a
        left join c_material_cost b on b.cost_material_id = a.material_id
        left join c_material_type d on d.type_no = a.material_type_id
        left join c_yl_material c on c.material_no = b.cost_cl_id
        where a.material_id = #{materialId}
        ORDER BY c.material_no ASC
    </select>

    <insert id="insertCMaterial" parameterType="CMaterial">
        insert into c_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialTypeId != null and materialTypeId != ''">material_type_id,</if>
            <if test="materialXingh != null and materialXingh != ''">material_xingh,</if>
            <if test="materialGuig != null and materialGuig != ''">material_guig,</if>
            <if test="materialDiany != null">material_diany,</if>
            <if test="materialDw != null">material_dw,</if>
            <if test="materialState != null">material_state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialTypeId != null and materialTypeId != ''">#{materialTypeId},</if>
            <if test="materialXingh != null and materialXingh != ''">#{materialXingh},</if>
            <if test="materialGuig != null and materialGuig != ''">#{materialGuig},</if>
            <if test="materialDiany != null">#{materialDiany},</if>
            <if test="materialDw != null">#{materialDw},</if>
            <if test="materialState != null">#{materialState},</if>
         </trim>
    </insert>

    <update id="updateCMaterial" parameterType="CMaterial">
        update c_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialTypeId != null and materialTypeId != ''">material_type_id = #{materialTypeId},</if>
            <if test="materialXingh != null and materialXingh != ''">material_xingh = #{materialXingh},</if>
            <if test="materialGuig != null and materialGuig != ''">material_guig = #{materialGuig},</if>
            <if test="materialDiany != null">material_diany = #{materialDiany},</if>
            <if test="materialDw != null">material_dw = #{materialDw},</if>
            <if test="materialState != null">material_state = #{materialState},</if>
        </trim>
        where material_id = #{materialId}
    </update>

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

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

    <delete id="deleteCMaterialCostByCostMaterialIds" parameterType="String">
        delete from c_material_cost where cost_material_id in
        <foreach item="costMaterialId" collection="array" open="(" separator="," close=")">
            #{costMaterialId}
        </foreach>
    </delete>

    <delete id="deleteCMaterialCostByCostMaterialId" parameterType="Long">
        delete from c_material_cost where cost_material_id = #{costMaterialId}
    </delete>

    <insert id="batchCMaterialCost">
        insert into c_material_cost( cost_material_id, cost_cl_id, cost_cl_qty, cost_cl_qty_2) values
		<foreach item="item" index="index" collection="list" separator=",">
            (  #{item.costMaterialId}, #{item.costClId}, cast(#{item.costClQty} as decimal(18,4)), cast(#{item.costClQty2} as decimal(18,4)))
        </foreach>
    </insert>

    <select id="selectTemp" resultType="com.ruoyi.material.domain.temp">
        select b.type_no,[uid_0]
                ,[model_0]
                ,[model_1]
                ,[spec_0]
                ,[spec_1]
                ,[voltage_0]
                ,[voltage_1]
                ,[unit_0]
                ,RTRIM([m_id_M001]) m_id_M001
                ,RTRIM([name_qty_M001]) name_qty_M001
                ,RTRIM([m_id_M002]) m_id_M002
                ,RTRIM([name_qty_M002]) name_qty_M002
                ,RTRIM([m_id_M003]) m_id_M003
                ,RTRIM([name_qty_M003]) name_qty_M003
                ,RTRIM([name_qty_M003_1]) name_qty_M003_1
                ,RTRIM([m_id_M004]) m_id_M004
                ,RTRIM([name_qty_M004]) name_qty_M004
                ,RTRIM([name_qty_M004_1]) name_qty_M004_1
                ,RTRIM([m_id_M005]) m_id_M005
                ,RTRIM([name_qty_M005]) name_qty_M005
                ,RTRIM([name_qty_M005_1]) name_qty_M005_1
                ,RTRIM([m_id_M006]) m_id_M006
                ,RTRIM([name_qty_M006]) name_qty_M006
                ,RTRIM([m_id_M007]) m_id_M007
                ,RTRIM([name_qty_M007]) name_qty_M007
                ,RTRIM([m_id_M008]) m_id_M008
                ,RTRIM([name_qty_M008]) name_qty_M008
                ,RTRIM([m_id_M009]) m_id_M009
                ,RTRIM([name_qty_M009]) name_qty_M009
                ,RTRIM([m_id_M010]) m_id_M010
                ,RTRIM([name_qty_M010]) name_qty_M010
                ,RTRIM([name_qty_M010_1]) name_qty_M010_1
                ,RTRIM([m_id_M011]) m_id_M011
                ,RTRIM([name_qty_M011]) name_qty_M011
                ,RTRIM([m_id_M012]) m_id_M012
                ,RTRIM([name_qty_M012]) name_qty_M012
                ,RTRIM([m_id_M013]) m_id_M013
                ,RTRIM([name_qty_M013]) name_qty_M013
                ,RTRIM([m_id_M014]) m_id_M014
                ,RTRIM([name_qty_M014]) name_qty_M014
                ,RTRIM([m_id_M015]) m_id_M015
                ,RTRIM([name_qty_M015]) name_qty_M015
                ,RTRIM([m_id_M016]) m_id_M016
                ,RTRIM([name_qty_M016]) name_qty_M016
                ,RTRIM([m_id_M017]) m_id_M017
                ,RTRIM([name_qty_M017]) name_qty_M017
                ,RTRIM([m_id_M018]) m_id_M018
                ,RTRIM([name_qty_M018]) name_qty_M018
                ,RTRIM([m_id_M019]) m_id_M019
                ,RTRIM([name_qty_M019]) name_qty_M019
                ,RTRIM([name_qty_M019_1]) name_qty_M019_1
                ,RTRIM([m_id_M020]) m_id_M020
                ,RTRIM([name_qty_M020]) name_qty_M020
                ,RTRIM([m_id_M021]) m_id_M021
                ,RTRIM([name_qty_M021]) name_qty_M021
                ,RTRIM([name_qty_M021_1]) name_qty_M021_1
                ,RTRIM([m_id_M022]) m_id_M022
                ,RTRIM([name_qty_M022]) name_qty_M022
                ,RTRIM([m_id_M023]) m_id_M023
                ,RTRIM([name_qty_M023]) name_qty_M023
                ,RTRIM([m_id_M024]) m_id_M024
                ,RTRIM([name_qty_M024]) name_qty_M024
                ,RTRIM([m_id_M025]) m_id_M025
                ,RTRIM([name_qty_M025]) name_qty_M025
                ,RTRIM([m_id_M026]) m_id_M026
                ,RTRIM([name_qty_M026]) name_qty_M026
                ,RTRIM([m_id_M027]) m_id_M027
                ,RTRIM([name_qty_M027]) name_qty_M027
                ,RTRIM([m_id_M028]) m_id_M028
                ,RTRIM([name_qty_M028]) name_qty_M028
                ,RTRIM([m_id_M029]) m_id_M029
                ,RTRIM([name_qty_M029]) name_qty_M029
                ,RTRIM([m_id_M030]) m_id_M030
                ,RTRIM([name_qty_M030]) name_qty_M030
                ,RTRIM([m_id_M031]) m_id_M031
                ,RTRIM([name_qty_M031]) name_qty_M031
                ,RTRIM([m_id_M032]) m_id_M032
                ,RTRIM([name_qty_M032]) name_qty_M032
                ,RTRIM([m_id_M033]) m_id_M033
                ,RTRIM([name_qty_M033]) name_qty_M033
                ,RTRIM([m_id_M034]) m_id_M034
                ,RTRIM([name_qty_M034]) name_qty_M034
                ,RTRIM([m_id_M035]) m_id_M035
                ,RTRIM([name_qty_M035]) name_qty_M035
                ,RTRIM([m_id_M036]) m_id_M036
                ,RTRIM([name_qty_M036]) name_qty_M036
                ,RTRIM([m_id_M037]) m_id_M037
                ,RTRIM([name_qty_M037]) name_qty_M037
                ,RTRIM([name_qty_M037_1]) name_qty_M037_1
                ,RTRIM([m_id_M038]) m_id_M038
                ,RTRIM([name_qty_M038]) name_qty_M038
                ,RTRIM([m_id_M039]) m_id_M039
                ,RTRIM([name_qty_M039]) name_qty_M039
                ,RTRIM([name_qty_M039_1]) name_qty_M039_1
                ,RTRIM([m_id_M040]) m_id_M040
                ,RTRIM([name_qty_M040]) name_qty_M040
                ,RTRIM([m_id_M041]) m_id_M041
                ,RTRIM([name_qty_M041]) name_qty_M041
                ,RTRIM([m_id_M042]) m_id_M042
                ,RTRIM([name_qty_M042]) name_qty_M042
                ,RTRIM([name_qty_M042_1]) name_qty_M042_1
                ,RTRIM([m_id_M043]) m_id_M043
                ,RTRIM([name_qty_M043]) name_qty_M043
                ,RTRIM([m_id_M044]) m_id_M044
                ,RTRIM([name_qty_M044]) name_qty_M044
                ,RTRIM([m_id_M045]) m_id_M045
                ,RTRIM([name_qty_M045]) name_qty_M045
                ,RTRIM([m_id_M046]) m_id_M046
                ,RTRIM([name_qty_M046]) name_qty_M046
                ,RTRIM([m_id_M047]) m_id_M047
                ,RTRIM([name_qty_M047]) name_qty_M047
                ,RTRIM([m_id_M048]) m_id_M048
                ,RTRIM([name_qty_M048]) name_qty_M048
                ,RTRIM([name_qty_M048_1]) name_qty_M048_1
                ,RTRIM([m_id_M049]) m_id_M049
                ,RTRIM([name_qty_M049]) name_qty_M049
                ,RTRIM([m_id_M050]) m_id_M050
                ,RTRIM([name_qty_M050]) name_qty_M050
                ,RTRIM([m_id_M051]) m_id_M051
                ,RTRIM([name_qty_M051]) name_qty_M051
                ,RTRIM([m_id_M052]) m_id_M052
                ,RTRIM([name_qty_M052]) name_qty_M052
                ,RTRIM([m_id_M053]) m_id_M053
                ,RTRIM([name_qty_M053]) name_qty_M053
                ,RTRIM([m_id_M054]) m_id_M054
                ,RTRIM([name_qty_M054]) name_qty_M054
                ,RTRIM([m_id_M055]) m_id_M055
                ,RTRIM([name_qty_M055]) name_qty_M055
                ,RTRIM([m_id_M056]) m_id_M056
                ,RTRIM([name_qty_M056]) name_qty_M056
                ,RTRIM([m_id_M057]) m_id_M057
                ,RTRIM([name_qty_M057]) name_qty_M057
                ,RTRIM([m_id_M058]) m_id_M058
                ,RTRIM([name_qty_M058]) name_qty_M058
                ,RTRIM([m_id_M059]) m_id_M059
                ,RTRIM([name_qty_M059]) name_qty_M059
                ,RTRIM([m_id_M060]) m_id_M060
                ,RTRIM([name_qty_M060]) name_qty_M060
                ,RTRIM([m_id_M061]) m_id_M061
                ,RTRIM([name_qty_M061]) name_qty_M061
                ,RTRIM([m_id_M062]) m_id_M062
                ,RTRIM([name_qty_M062]) name_qty_M062
                ,RTRIM([m_id_M063]) m_id_M063
                ,RTRIM([name_qty_M063]) name_qty_M063
                ,RTRIM([m_id_M064]) m_id_M064
                ,RTRIM([name_qty_M064]) name_qty_M064
                ,RTRIM([m_id_M065]) m_id_M065
                ,RTRIM([name_qty_M065]) name_qty_M065
                ,RTRIM([m_id_M066]) m_id_M066
                ,RTRIM([name_qty_M066]) name_qty_M066
                ,RTRIM([m_id_M067]) m_id_M067
                ,RTRIM([name_qty_M067]) name_qty_M067
                ,RTRIM([m_id_M068]) m_id_M068
                ,RTRIM([name_qty_M068]) name_qty_M068

        from temp_3 a left join c_material_type b on a.desp_0 = b.type_name
    </select>
</mapper>
