<?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.SysMaterialInventoryDao">
    <insert id="insert" parameterType="com.api.model.operationManagement.SysMaterialInventory">
        insert into sys_material_inventory(
            period_time,
            inventory_date_start,
            inventory_date_end,
            create_id,
            create_date
        )values (
            #{periodTime},
            #{inventoryDateStart},
            #{inventoryDateEnd},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>
    <insert id="insertDetail" parameterType="com.api.model.operationManagement.SysMaterialInventoryDetail">
        insert into sys_material_inventory_detail(
            material_inventory_id,
            name,
            unit,
            should_inventory,
            actual_inventory,
            inventory_surplus_losses
        )values (
            #{materialInventoryId},
            #{name},
            #{unit},
            #{shouldInventory},
            #{actualInventory},
            #{inventorySurplusLosses}
        )
    </insert>

    <select id="list" parameterType="com.api.model.operationManagement.SearchMaterialInventory" resultType="com.api.vo.operationManagement.VoMaterialInventory">
        select * from sys_material_inventory
        <where>
            <if test="periodTime != null and periodTime != ''">
                period_time like concat('%',#{periodTime},'%')
            </if>
        </where>
    </select>
    <select id="countSMIDBySMIId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_material_inventory_detail where material_inventory_id = #{id}
    </select>
    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoMaterialInventory">
        select * from sys_material_inventory where id = #{id}
    </select>
    <select id="findDetailBySMIDId" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoMaterialInventoryDetail">
        select * from sys_material_inventory_detail where material_inventory_id = #{sysMaterialId}
    </select>

    <update id="update" parameterType="com.api.model.operationManagement.SysMaterialInventory">
        update sys_material_inventory
        set period_time = #{periodTime},
        inventory_date_start = #{inventoryDateStart},
        inventory_date_end = #{inventoryDateEnd}
        where id = #{id}
    </update>

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


</mapper>