<?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.zhyc.module.stock.mapper.WzStockInMapper">
    
    <resultMap type="WzStockIn" id="WzStockInResult">
        <result property="stockInCode"    column="stock_in_code"    />
        <result property="docDate"    column="doc_date"    />
        <result property="createDate"    column="create_date"    />
        <result property="docId"    column="doc_id"    />
        <result property="businessType"    column="business_type"    />
        <result property="repositoryId"    column="repository_id"    />
        <result property="repositoryName"    column="repository_name"    />
        <result property="stockInType"    column="stock_in_type"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="departmentId"    column="department_id"    />
        <result property="departmentName"    column="department_name"    />
        <result property="operatorId"    column="operator_id"    />
        <result property="operatorName"    column="operator_name"    />
        <result property="remark"    column="remark"    />
        <result property="single"    column="single"    />
        <result property="reviewer"    column="reviewer"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialSpecification"    column="material_specification"    />
        <result property="materialUnit"    column="material_unit"    />
        <result property="count"    column="count"    />
        <result property="stockInAdjustRemark"    column="stock_in_adjust_remark"    />
    </resultMap>

    <sql id="selectWzStockInVo">
        select stock_in_code, doc_date, create_date, doc_id, business_type, repository_id, repository_name, stock_in_type, supplier_id, supplier_name, department_id, department_name, operator_id, operator_name, remark, single, reviewer, material_id, material_name, material_specification, material_unit, count, stock_in_adjust_remark from wz_stock_in
    </sql>

    <select id="selectWzStockInList" parameterType="WzStockIn" resultMap="WzStockInResult">
        <include refid="selectWzStockInVo"/>
        <where>  
            <if test="params.beginDocDate != null and params.beginDocDate != '' and params.endDocDate != null and params.endDocDate != ''"> and doc_date between #{params.beginDocDate} and #{params.endDocDate}</if>
            <if test="businessType != null  and businessType != ''"> and business_type = #{businessType}</if>
            <if test="repositoryName != null  and repositoryName != ''"> and repository_name like concat('%', #{repositoryName}, '%')</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="docDate != null"> and doc_date = #{docDate}</if>
        </where>
    </select>
    
    <select id="selectWzStockInByStockInCode" parameterType="Long" resultMap="WzStockInResult">
        <include refid="selectWzStockInVo"/>
        where stock_in_code = #{stockInCode}
    </select>

    <insert id="insertWzStockIn" parameterType="WzStockIn" useGeneratedKeys="true" keyProperty="stockInCode">
        insert into wz_stock_in
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="docDate != null">doc_date,</if>
            <if test="createDate != null">create_date,</if>
            <if test="docId != null">doc_id,</if>
            <if test="businessType != null">business_type,</if>
            <if test="repositoryId != null">repository_id,</if>
            <if test="repositoryName != null">repository_name,</if>
            <if test="stockInType != null">stock_in_type,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="supplierName != null">supplier_name,</if>
            <if test="departmentId != null">department_id,</if>
            <if test="departmentName != null">department_name,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="operatorName != null">operator_name,</if>
            <if test="remark != null">remark,</if>
            <if test="single != null">single,</if>
            <if test="reviewer != null">reviewer,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialSpecification != null">material_specification,</if>
            <if test="materialUnit != null">material_unit,</if>
            <if test="count != null">count,</if>
            <if test="stockInAdjustRemark != null">stock_in_adjust_remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="docDate != null">#{docDate},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="docId != null">#{docId},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="repositoryId != null">#{repositoryId},</if>
            <if test="repositoryName != null">#{repositoryName},</if>
            <if test="stockInType != null">#{stockInType},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="supplierName != null">#{supplierName},</if>
            <if test="departmentId != null">#{departmentId},</if>
            <if test="departmentName != null">#{departmentName},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="operatorName != null">#{operatorName},</if>
            <if test="remark != null">#{remark},</if>
            <if test="single != null">#{single},</if>
            <if test="reviewer != null">#{reviewer},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="materialSpecification != null">#{materialSpecification},</if>
            <if test="materialUnit != null">#{materialUnit},</if>
            <if test="count != null">#{count},</if>
            <if test="stockInAdjustRemark != null">#{stockInAdjustRemark},</if>
         </trim>
    </insert>

    <update id="updateWzStockIn" parameterType="WzStockIn">
        update wz_stock_in
        <trim prefix="SET" suffixOverrides=",">
            <if test="docDate != null">doc_date = #{docDate},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
            <if test="docId != null">doc_id = #{docId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="repositoryId != null">repository_id = #{repositoryId},</if>
            <if test="repositoryName != null">repository_name = #{repositoryName},</if>
            <if test="stockInType != null">stock_in_type = #{stockInType},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="supplierName != null">supplier_name = #{supplierName},</if>
            <if test="departmentId != null">department_id = #{departmentId},</if>
            <if test="departmentName != null">department_name = #{departmentName},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="operatorName != null">operator_name = #{operatorName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="single != null">single = #{single},</if>
            <if test="reviewer != null">reviewer = #{reviewer},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialSpecification != null">material_specification = #{materialSpecification},</if>
            <if test="materialUnit != null">material_unit = #{materialUnit},</if>
            <if test="count != null">count = #{count},</if>
            <if test="stockInAdjustRemark != null">stock_in_adjust_remark = #{stockInAdjustRemark},</if>
        </trim>
        where stock_in_code = #{stockInCode}
    </update>

    <delete id="deleteWzStockInByStockInCode" parameterType="Long">
        delete from wz_stock_in where stock_in_code = #{stockInCode}
    </delete>

    <delete id="deleteWzStockInByStockInCodes" parameterType="String">
        delete from wz_stock_in where stock_in_code in 
        <foreach item="stockInCode" collection="array" open="(" separator="," close=")">
            #{stockInCode}
        </foreach>
    </delete>

    <select id="getEarliestStockIn" resultMap="WzStockInResult">
        SELECT * FROM wz_stock_in ORDER BY doc_date DESC LIMIT 1
    </select>
</mapper>