<?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.materialType.mapper.CMaterialTypeMapper">

    <resultMap type="CMaterialType" id="CMaterialTypeResult">
        <result property="typeId"    column="type_id"    />
        <result property="typeNo"    column="type_no"    />
        <result property="typeName"    column="type_name"    />
        <result property="typeState"    column="type_state"    />
    </resultMap>

    <sql id="selectCMaterialTypeVo">
        select type_id, type_no, type_name, type_state from c_material_type
    </sql>

    <select id="selectCMaterialTypeList" parameterType="CMaterialType" resultMap="CMaterialTypeResult">
        <include refid="selectCMaterialTypeVo"/>
        <where>
            <if test="typeNo != null  and typeNo != ''"> and type_no = #{typeNo}</if>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="typeState != null  and typeState != ''"> and type_state = #{typeState}</if>
        </where>
        order by type_no asc
    </select>

    <select id="selectCMaterialTypeByTypeId" parameterType="Long" resultMap="CMaterialTypeResult">
        <include refid="selectCMaterialTypeVo"/>
        where type_id = #{typeId}
    </select>

    <insert id="insertCMaterialType" parameterType="CMaterialType" useGeneratedKeys="true" keyProperty="typeId">
        insert into c_material_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeNo != null and typeNo != ''">type_no,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="typeState != null and typeState != ''">type_state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeNo != null and typeNo != ''">#{typeNo},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="typeState != null and typeState != ''">#{typeState},</if>
         </trim>
    </insert>

    <update id="updateCMaterialType" parameterType="CMaterialType">
        update c_material_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeNo != null and typeNo != ''">type_no = #{typeNo},</if>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="typeState != null and typeState != ''">type_state = #{typeState},</if>
        </trim>
        where type_id = #{typeId}
    </update>

    <delete id="deleteCMaterialTypeByTypeId" parameterType="Long">
        delete from c_material_type where type_id = #{typeId}
    </delete>

    <delete id="deleteCMaterialTypeByTypeIds" parameterType="String">
        delete from c_material_type where type_id in
        <foreach item="typeId" collection="array" open="(" separator="," close=")">
            #{typeId}
        </foreach>
    </delete>

    <select id="checkTypeNoUnique" parameterType="String" resultMap="CMaterialTypeResult">
        select top(1) type_id, type_no from c_material_type where type_no = #{typeNo} and type_state = '0'
    </select>

</mapper>
