<?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.api.manage.dao.operationManagement.SysMaterialRecordDao">

    <select id="list" parameterType="com.api.model.operationManagement.SearchMaterialRecord" resultType="com.api.vo.operationManagement.VoMaterialRecord">
        select smr.*,sm.name,sm.unit,su.actual_name as create_name,sm.stock
        from sys_material_record as smr,sys_material as sm,sys_user as su
        <where>
            smr.material_id = sm.id
            and smr.create_id = su.id
            <if test="name != null and name != ''">
                and sm.name like concat('%',#{name},'%')
            </if>
            <if test="type != null">
                and smr.type = #{type}
            </if>
        </where>
        order by smr.create_date desc
    </select>
    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoFBIMaterialRecord">
        select smr.*,sm.unit,sm.name,su.actual_name as create_name
        from sys_material_record as smr,sys_material as sm,sys_user as su
        where smr.material_id = sm.id and smr.create_id = su.id and smr.id = #{id}
    </select>

    <update id="delivery" parameterType="com.api.model.operationManagement.SysMaterialRecord">
        update sys_material
        set stock = stock - #{num}
        where stock - #{num} <![CDATA[ >= ]]> 0 and id = #{materialId}
    </update>

    <update id="warehousing" parameterType="com.api.model.operationManagement.SysMaterialRecord">
        update sys_material
        set stock = stock + #{num}
        where id = #{materialId}
    </update>
    <insert id="insert" parameterType="com.api.model.operationManagement.SysMaterialRecord">
        insert into sys_material_record(
            code,
            material_id,
            type,
            num,
            unit_price,
            discount_rate,
            discount_frontal,
            total_price,
            is_invoice,
            <if test="invoiceTitleType != null">
                invoice_title_type,
            </if>
            <if test="invoiceTitleName != null and invoiceTitleName != ''">
                invoice_title_name,
            </if>
            <if test="acquiringEin != null and acquiringEin != ''">
                acquiring_ein,
            </if>
            <if test="tel != null and tel != ''">
                tel,
            </if>
            <if test="invoiceDate != null">
                invoice_date,
            </if>
            <if test="remakes != null and remakes != ''">
                remakes,
            </if>
            create_id,
            create_date
        )values (
            #{code},
            #{materialId},
            #{type},
            #{num},
            #{unitPrice},
            #{discountRate},
            #{discountFrontal},
            #{totalPrice},
            #{isInvoice},
            <if test="invoiceTitleType != null">
                #{invoiceTitleType},
            </if>
            <if test="invoiceTitleName != null and invoiceTitleName != ''">
                #{invoiceTitleName},
            </if>
            <if test="acquiringEin != null and acquiringEin != ''">
                #{acquiringEin},
            </if>
            <if test="tel != null and tel != ''">
                #{tel},
            </if>
            <if test="invoiceDate != null">
                #{invoiceDate},
            </if>
            <if test="remakes != null and remakes != ''">
                #{remakes},
            </if>
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from sys_material_record where id = #{id}
    </delete>

</mapper>