<?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.project.project.item.mapper.ItemMapper">

    <resultMap type="Item" id="ItemResult">
        <result property="id"       column="id"       />
        <result property="caseNo"       column="case_no"       />
        <result property="itemNo"    column="item_no"    />
        <result property="itemType"     column="item_type"     />
        <result property="brand"  column="brand"  />
        <result property="number"         column="number"          />
        <result property="price"       column="price"       />
        <result property="source"     column="source"     />
        <result property="creatTime"     column="creat_time"     />
    </resultMap>

    <sql id="selectItemVo">
        select id,case_no, item_no, item_type, brand, number, price, source, creat_time
        from item
    </sql>

    <select id="selectItemById" parameterType="Integer" resultMap="ItemResult">
        <include refid="selectItemVo"/>
        where id = #{id}
    </select>

    <select id="selectItemByCaseNo" parameterType="String" resultMap="ItemResult">
        <include refid="selectItemVo"/>
        where case_no = #{caseno}
    </select>

    <select id="selectItemList" parameterType="Item" resultMap="ItemResult">
        <include refid="selectItemVo"/>
        <where>
            <if test="itemNo != null and itemNo != ''">
                AND item_no like '%'||#{itemNo}||'%' <!-- concat('%', #{noticeTitle}, '%') -->
            </if>
        </where>
    </select>

    <insert id="insertItem" parameterType="Item">
        insert into item (
        <if test="caseNo != null and caseNo != '' ">case_no, </if>
        <if test="itemNo != null and itemNo != '' ">item_no, </if>
        <if test="itemType != null and itemType != '' ">item_type, </if>
        <if test="brand != null and brand != '' ">brand, </if>
        <if test="number != null and number != ''">number,</if>
        <if test="price != null and price != ''">price,</if>
        <if test="source != null and source != ''">source,</if>
        creat_time
        )values(
        <if test="caseNo != null and caseNo != ''">#{caseNo}, </if>
        <if test="itemNo != null and itemNo != ''">#{itemNo}, </if>
        <if test="itemType != null and itemType != ''">#{itemType}, </if>
        <if test="brand != null and brand != ''">#{brand}, </if>
        <if test="number != null and number != ''">#{number},</if>
        <if test="price != null and price != ''">#{price},</if>
        <if test="source != null and source != ''">#{source},</if>
        (datetime(CURRENT_TIMESTAMP,'localtime'))
        )
    </insert>

    <update id="updateItem" parameterType="Item">
        update item
        <set>
            <if test="itemNo != null and itemNo != ''">item_no = #{itemNo}, </if>
            <if test="itemType != null and itemType != ''">item_type = #{itemType}, </if>
            <if test="brand != null and brand != ''">brand = #{brand}, </if>
            <if test="number != null">number = #{number}, </if>
            <if test="price != null and price != ''">price = #{price}, </if>
            <if test="source != null and source != ''">source = #{source}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteItemByIds" parameterType="String">
        delete from item where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectItemByCaseId" parameterType="String" resultType="integer">
        select COUNT(*) from item where case_no = (SELECT case_no FROM cases where id  = #{id})
    </select>

    <select id="selectItemByItemNo" parameterType="Item" resultType="integer">
        select count(*) from item where item_no = #{itemNo} and case_No = #{caseNo} and id != #{id}
    </select>
</mapper>