<?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.shzg.project.worn.mapper.WornMaterialMapper">

    <resultMap id="WornMaterialResult" type="com.shzg.project.worn.domain.WornMaterial">

        <id property="id" column="id"/>

        <result property="materialName" column="material_name"/>
        <result property="materialShortName" column="material_short_name"/>
        <result property="materialCode" column="material_code"/>
        <result property="typeId" column="type_id"/>
        <result property="unitId" column="unit_id"/>
        <result property="materialCategory" column="material_category"/>
        <result property="kgFactor" column="kg_factor"/>
        <result property="specification" column="specification"/>
        <result property="model" column="model"/>
        <result property="barcode" column="barcode"/>
        <result property="weight" column="weight"/>
        <result property="description" column="description"/>
        <result property="orderNum" column="order_num"/>
        <result property="status" column="status"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>

        <result property="unitName" column="unit_name"/>
        <result property="typeName" column="type_name"/>
        <result property="typeAncestors" column="ancestors"/>
        <result property="typeParentNames" column="type_parent_names"/>

    </resultMap>


    <sql id="selectWornMaterialVo">

        SELECT
            wm.id,
            wm.material_name,
            wm.material_short_name,
            wm.material_code,
            wm.type_id,
            wm.unit_id,
            wm.material_category,
            wm.kg_factor,
            wm.specification,
            wm.model,
            wm.barcode,
            wm.weight,
            wm.description,
            wm.order_num,
            wm.status,
            wm.is_delete,
            wm.create_by,
            wm.create_time,
            wm.update_by,
            wm.update_time,

            u.unit_name,

            t.type_name,
            t.ancestors,

            (
                SELECT GROUP_CONCAT(tp.type_name ORDER BY tp.id)
                FROM worn_material_type tp
                WHERE FIND_IN_SET(tp.id, t.ancestors)
            ) AS type_parent_names

        FROM worn_material wm

                 LEFT JOIN worn_material_unit u
                           ON wm.unit_id = u.id

                 LEFT JOIN worn_material_type t
                           ON wm.type_id = t.id

    </sql>


    <select id="selectWornMaterialList"
            parameterType="com.shzg.project.worn.domain.WornMaterial"
            resultMap="WornMaterialResult">

        <include refid="selectWornMaterialVo"/>

        <where>

            <if test="keyword != null and keyword != ''">
                AND (
                wm.material_name LIKE CONCAT('%', #{keyword}, '%')
                OR wm.material_short_name LIKE CONCAT('%', #{keyword}, '%')
                OR wm.material_code LIKE CONCAT('%', #{keyword}, '%')
                OR wm.specification LIKE CONCAT('%', #{keyword}, '%')
                OR wm.model LIKE CONCAT('%', #{keyword}, '%')
                OR wm.barcode LIKE CONCAT('%', #{keyword}, '%')
                OR wm.description LIKE CONCAT('%', #{keyword}, '%')
                OR t.type_name LIKE CONCAT('%', #{keyword}, '%')
                OR u.unit_name LIKE CONCAT('%', #{keyword}, '%')
                )
            </if>

            <if test="materialName != null and materialName != ''">
                AND wm.material_name LIKE CONCAT('%',#{materialName},'%')
            </if>

            <if test="materialShortName != null and materialShortName != ''">
                AND wm.material_short_name LIKE CONCAT('%',#{materialShortName},'%')
            </if>

            <if test="materialCode != null and materialCode != ''">
                AND wm.material_code = #{materialCode}
            </if>

            <if test="typeId != null">
                AND wm.type_id = #{typeId}
            </if>

            <if test="unitId != null">
                AND wm.unit_id = #{unitId}
            </if>

            <if test="materialCategory != null and materialCategory != ''">
                AND wm.material_category = #{materialCategory}
            </if>

            <if test="kgFactor != null">
                AND wm.kg_factor = #{kgFactor}
            </if>

            <if test="specification != null and specification != ''">
                AND wm.specification = #{specification}
            </if>

            <if test="model != null and model != ''">
                AND wm.model = #{model}
            </if>

            <if test="barcode != null and barcode != ''">
                AND wm.barcode = #{barcode}
            </if>

            <if test="weight != null">
                AND wm.weight = #{weight}
            </if>

            <if test="description != null and description != ''">
                AND wm.description = #{description}
            </if>

            <if test="orderNum != null">
                AND wm.order_num = #{orderNum}
            </if>

            <if test="status != null and status != ''">
                AND wm.status = #{status}
            </if>

            <if test="isDelete != null and isDelete != ''">
                AND wm.is_delete = #{isDelete}
            </if>

        </where>

    </select>


    <select id="selectWornMaterialById"
            parameterType="Long"
            resultMap="WornMaterialResult">

        <include refid="selectWornMaterialVo"/>

        WHERE wm.id = #{id}

    </select>


    <insert id="insertWornMaterial"
            parameterType="com.shzg.project.worn.domain.WornMaterial"
            useGeneratedKeys="true"
            keyProperty="id">

        INSERT INTO worn_material

        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialShortName != null and materialShortName != ''">material_short_name,</if>
            <if test="materialCode != null and materialCode != ''">material_code,</if>
            <if test="typeId != null">type_id,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="materialCategory != null and materialCategory != ''">material_category,</if>
            <if test="kgFactor != null">kg_factor,</if>
            <if test="specification != null and specification != ''">specification,</if>
            <if test="model != null and model != ''">model,</if>
            <if test="barcode != null and barcode != ''">barcode,</if>
            <if test="weight != null">weight,</if>
            <if test="description != null and description != ''">description,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="isDelete != null and isDelete != ''">is_delete,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>

        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialShortName != null and materialShortName != ''">#{materialShortName},</if>
            <if test="materialCode != null and materialCode != ''">#{materialCode},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="materialCategory != null and materialCategory != ''">#{materialCategory},</if>
            <if test="kgFactor != null">#{kgFactor},</if>
            <if test="specification != null and specification != ''">#{specification},</if>
            <if test="model != null and model != ''">#{model},</if>
            <if test="barcode != null and barcode != ''">#{barcode},</if>
            <if test="weight != null">#{weight},</if>
            <if test="description != null and description != ''">#{description},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="isDelete != null and isDelete != ''">#{isDelete},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>

    </insert>

    <update id="updateWornMaterial"
            parameterType="com.shzg.project.worn.domain.WornMaterial">

        UPDATE worn_material

        <trim prefix="SET" suffixOverrides=",">

            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="materialShortName != null">material_short_name = #{materialShortName},</if>
            <if test="materialCode != null and materialCode != ''">material_code = #{materialCode},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="materialCategory != null">material_category = #{materialCategory},</if>
            <if test="kgFactor != null">kg_factor = #{kgFactor},</if>
            <if test="specification != null">specification = #{specification},</if>
            <if test="model != null">model = #{model},</if>
            <if test="barcode != null">barcode = #{barcode},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="description != null">description = #{description},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>

        </trim>

        WHERE id = #{id}

    </update>


    <delete id="deleteWornMaterialById" parameterType="Long">
        DELETE FROM worn_material
        WHERE id = #{id}
    </delete>


    <delete id="deleteWornMaterialByIds" parameterType="Long">
        DELETE FROM worn_material
        WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="selectMaxMaterialCode" resultType="java.lang.String">
        SELECT MAX(material_code)
        FROM worn_material
    </select>

</mapper>