<?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.frozen.mapper.DdSaleMapper">
    
    <resultMap type="DdSale" id="DdSaleResult">
        <result property="id"    column="id"    />
        <result property="saleDate"    column="sale_date"    />
        <result property="custName"    column="cust_name"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="custAddr"    column="cust_addr"    />
        <result property="salesper"    column="salesper"    />
        <result property="quaranNo"    column="quaran_no"    />
        <result property="apprNo"    column="appr_no"    />
        <result property="price"    column="price"    />
        <result property="tech"    column="tech"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <resultMap id="DdSaleDdSaleItemResult" type="DdSale" extends="DdSaleResult">
        <collection property="ddSaleItemList" ofType="DdSaleItem" column="id" select="selectDdSaleItemList" />
    </resultMap>

    <resultMap type="DdSaleItem" id="DdSaleItemResult">
        <result property="id"    column="id"    />
        <result property="saleId"    column="sale_id"    />
        <result property="itemType"    column="item_type"    />
        <result property="itemCode"    column="item_code"    />
        <result property="qty"    column="qty"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="tankId"    column="tank_id"    />
        <result property="bucketId"    column="bucket_id"    />
        <result property="rackId"    column="rack_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectDdSaleVo">
        select id, sale_date, cust_name, cust_phone, cust_addr, salesper, quaran_no, appr_no, price, tech, remark, create_by, create_time from dd_sl dd_sl_alias
    </sql>

    <select id="selectDdSaleList" parameterType="DdSale" resultMap="DdSaleResult">
        <include refid="selectDdSaleVo"/>
        <where>  
            <if test="saleDate != null "> and sale_date = #{saleDate}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
            <if test="custPhone != null  and custPhone != ''"> and cust_phone = #{custPhone}</if>
            <if test="custAddr != null  and custAddr != ''"> and cust_addr = #{custAddr}</if>
            <if test="salesper != null  and salesper != ''"> and salesper = #{salesper}</if>
            <if test="quaranNo != null  and quaranNo != ''"> and quaran_no = #{quaranNo}</if>
            <if test="apprNo != null  and apprNo != ''"> and appr_no = #{apprNo}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="tech != null  and tech != ''"> and tech = #{tech}</if>
            ${params.dataScope}
        </where>
    </select>
    
    <select id="selectDdSaleById" parameterType="Long" resultMap="DdSaleDdSaleItemResult">
        select id, sale_date, cust_name, cust_phone, cust_addr, salesper, quaran_no, appr_no, price, tech, remark, create_by, create_time
        from dd_sl
        where id = #{id}
    </select>

    <select id="selectDdSaleItemList" resultMap="DdSaleItemResult">
        select id, sale_id, item_type, item_code, qty, unit_price, tank_id, bucket_id, rack_id, create_time
        from dd_sl_item
        where sale_id = #{sale_id}
    </select>

    <insert id="insertDdSale" parameterType="DdSale" useGeneratedKeys="true" keyProperty="id">
        insert into dd_sl
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="saleDate != null">sale_date,</if>
            <if test="custName != null and custName != ''">cust_name,</if>
            <if test="custPhone != null">cust_phone,</if>
            <if test="custAddr != null">cust_addr,</if>
            <if test="salesper != null">salesper,</if>
            <if test="quaranNo != null">quaran_no,</if>
            <if test="apprNo != null">appr_no,</if>
            <if test="price != null">price,</if>
            <if test="tech != null and tech != ''">tech,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="saleDate != null">#{saleDate},</if>
            <if test="custName != null and custName != ''">#{custName},</if>
            <if test="custPhone != null">#{custPhone},</if>
            <if test="custAddr != null">#{custAddr},</if>
            <if test="salesper != null">#{salesper},</if>
            <if test="quaranNo != null">#{quaranNo},</if>
            <if test="apprNo != null">#{apprNo},</if>
            <if test="price != null">#{price},</if>
            <if test="tech != null and tech != ''">#{tech},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateDdSale" parameterType="DdSale">
        update dd_sl
        <trim prefix="SET" suffixOverrides=",">
            <if test="saleDate != null">sale_date = #{saleDate},</if>
            <if test="custName != null and custName != ''">cust_name = #{custName},</if>
            <if test="custPhone != null">cust_phone = #{custPhone},</if>
            <if test="custAddr != null">cust_addr = #{custAddr},</if>
            <if test="salesper != null">salesper = #{salesper},</if>
            <if test="quaranNo != null">quaran_no = #{quaranNo},</if>
            <if test="apprNo != null">appr_no = #{apprNo},</if>
            <if test="price != null">price = #{price},</if>
            <if test="tech != null and tech != ''">tech = #{tech},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDdSaleById" parameterType="Long">
        delete from dd_sl where id = #{id}
    </delete>

    <delete id="deleteDdSaleByIds" parameterType="String">
        delete from dd_sl where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteDdSaleItemBySaleIds" parameterType="String">
        delete from dd_sl_item where sale_id in 
        <foreach item="saleId" collection="array" open="(" separator="," close=")">
            #{saleId}
        </foreach>
    </delete>

    <delete id="deleteDdSaleItemBySaleId" parameterType="Long">
        delete from dd_sl_item where sale_id = #{id}
    </delete>

    <insert id="batchDdSaleItem">
        insert into dd_sl_item( id, sale_id, item_type, item_code, qty, unit_price, tank_id, bucket_id, rack_id, create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.saleId}, #{item.itemType}, #{item.itemCode}, #{item.qty}, #{item.unitPrice}, #{item.tankId}, #{item.bucketId}, #{item.rackId}, #{item.createTime})
        </foreach>
    </insert>
</mapper>