<?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.dgtly.wxportal.mapper.OrderFileMapper">
    
    <resultMap type="OrderFile" id="OrderFileResult">
        <result property="orderId"    column="order_id"    />
        <result property="deliveryNumber"    column="delivery_number"    />
        <result property="shipmentNumber"    column="shipment_number"    />
        <result property="wareHouseCode"    column="ware_house_code"    />
        <result property="wareHouseName"    column="ware_house_name"    />
        <result property="code"    column="code"    />
        <result property="message"    column="message"    />
        <result property="flowId"    column="flow_id"    />
        <result property="chainsCode"    column="chains_code"    />
        <result property="chainsName"    column="chainsName"    />
        <result property="customersCode"    column="customers_code"    />
        <result property="signerId"    column="signer_id"    />
        <result property="signerName"    column="signer_name"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="entrustName"    column="entrust_Name"    />
        <result property="beentrustName"    column="beentrust_Name"    />
        <result property="bId"    column="b_Id"    />
        <result property="carNumber"    column="car_Number"    />
        <result property="types"    column="types"    />
    </resultMap>

    <sql id="selectOrderFileVo">
        select order_id,delivery_number,shipment_number,ware_house_code,ware_house_name,code,message, flow_id,chains_code,customers_code,signer_id,signer_name, file_url, create_by, create_time, update_by, update_time, is_delete ,entrust_Name,beentrust_Name,b_Id,car_Number,types from ssb_order_file
    </sql>

    <select id="selectOrderFileList" parameterType="OrderFile" resultMap="OrderFileResult">
        SELECT
        o.order_id,
        o.flow_id,
        o.chains_code,
        o.customers_code,
        o.signer_id,
        o.signer_name,
        o.file_url,
        o.create_by,
        o.create_time,
        o.update_by,
        o.update_time,
        o.is_delete,
        o.entrust_Name,
        o.beentrust_Name,
        o.b_Id,
        o.car_Number,
        c.chains_name AS chainsName
        FROM
        ssb_order_file AS o
        LEFT JOIN (select chains_name,chains_code from customers GROUP BY chains_code ) AS c ON c.chains_code = o.chains_code
        where o.file_url is not NULL
            <if test="orderId != null  and orderId != ''"> and c.chains_name = #{orderId}</if>
            <if test="chainsName != null  and chainsName != ''"> and  c.chains_name like concat('%', #{chainsName}, '%')</if>
            <if test="signerName != null  and signerName != ''"> and  o.signer_name like concat('%', #{signerName}, '%')</if>
            <if test="flowId != null  and flowId != ''"> and o.flow_id = #{flowId}</if>
            <if test="fileUrl != null  and fileUrl != ''"> and o.file_url = #{fileUrl}</if>
            <if test="isDelete != null  and isDelete != ''"> and o.is_delete = #{isDelete}</if>
            <if test="types != null  and types != '' "> and o.types =#{types}</if>
        <if test="authType != null  and authType != '' "> and o.auth_type =#{authType}</if>
        order by o.create_time desc
    </select>
    
    <select id="selectOrderFileById" parameterType="String" resultMap="OrderFileResult">
        <include refid="selectOrderFileVo"/>
        where order_id = #{orderId}
    </select>

    <select id="selectOrderFileByFlowId" parameterType="String" resultMap="OrderFileResult">
        <include refid="selectOrderFileVo"/>
        where flow_id = #{flowId}
    </select>
    <select id="selectOrderFileByParam" resultMap="OrderFileResult">
        <include refid="selectOrderFileVo"/>
        where shipment_number = #{shipmentNumber}
            and chains_code = #{customersCode}
            and file_url is not null
            ORDER BY create_time desc
            limit 1
    </select>

    <insert id="insertOrderFile" parameterType="OrderFile">
        insert into ssb_order_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="deliveryNumber != null">delivery_number,</if>
            <if test="shipmentNumber != null">shipment_number,</if>
            <if test="wareHouseCode != null">ware_house_code,</if>
            <if test="wareHouseName != null">ware_house_name,</if>
            <if test="code != null">code,</if>
            <if test="message != null">message,</if>
            <if test="flowId != null">flow_id,</if>
            <if test="chainsCode != null">chains_code,</if>
            <if test="customersCode != null">customers_code,</if>
            <if test="signerId != null">signer_id,</if>
            <if test="signerName != null">signer_name,</if>
            <if test="fileUrl != null">file_url,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="entrustName != null">entrust_Name,</if>
            <if test="beentrustName != null">beentrust_Name,</if>
            <if test="bId != null">b_Id,</if>
            <if test="carNumber != null">car_Number,</if>
            <if test="types !=null">types,</if>
            <if test="beentrustPost !=null">beentrust_post,</if>
            <if test="entrustPost !=null">entrust_post,</if>
            <if test="authType !=null">auth_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="deliveryNumber != null">#{deliveryNumber},</if>
            <if test="shipmentNumber != null">#{shipmentNumber},</if>
            <if test="wareHouseCode != null">#{wareHouseCode},</if>
            <if test="wareHouseName != null">#{wareHouseName},</if>
            <if test="code != null">#{code},</if>
            <if test="message != null">#{message},</if>
            <if test="flowId != null">#{flowId},</if>
            <if test="chainsCode != null">#{chainsCode},</if>
            <if test="customersCode != null">#{customersCode},</if>
            <if test="signerId != null">#{signerId},</if>
            <if test="signerName != null">#{signerName},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="entrustName != null">#{entrustName},</if>
            <if test="beentrustName != null">#{beentrustName},</if>
            <if test="bId != null">#{bId},</if>
            <if test="carNumber != null">#{carNumber},</if>
            <if test="types !=null">#{types},</if>
            <if test="beentrustPost !=null">#{beentrustPost},</if>
            <if test="entrustPost !=null">#{entrustPost},</if>
            <if test="authType !=null">#{authType},</if>
         </trim>
    </insert>

    <update id="updateOrderFile" parameterType="OrderFile">
        update ssb_order_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="flowId != null">flow_id = #{flowId},</if>
            <if test="chainsCode != null">chains_code = #{chainsCode},</if>
            <if test="customersCode != null">customers_code = #{customersCode},</if>
            <if test="signerId != null">signer_id = #{signerId},</if>
            <if test="signerName != null">signer_name = #{signerName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="entrustName != null">entrust_Name = #{entrustName},</if>
            <if test="beentrustName != null">beentrust_Name = #{beentrustName},</if>
            <if test="bId != null">b_Id = #{bId},</if>
            <if test="carNumber != null">car_Number = #{carNumber},</if>
            <if test="beentrustPost !=null">beentrust_post =#{beentrustPost},</if>
            <if test="entrustPost !=null">entrust_post =#{entrustPost},</if>
            <if test="authType !=null">auth_type =#{authType},</if>
        </trim>
        where order_id = #{orderId}
    </update>
    <update id="updateOrderFileByFlowId">
        update ssb_order_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="chainsCode != null">chains_code = #{chainsCode},</if>
            <if test="customersCode != null">customers_code = #{customersCode},</if>
            <if test="signerId != null">signer_id = #{signerId},</if>
            <if test="signerName != null">signer_name = #{signerName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="code != null">code = #{code},</if>
            <if test="message != null">message = #{message},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="entrustName != null">entrust_Name = #{entrustName},</if>
            <if test="beentrustName != null">beentrust_Name = #{beentrustName},</if>
            <if test="bId != null">b_Id = #{bId},</if>
            <if test="carNumber != null">car_Number = #{carNumber},</if>
            <if test="bId != null">b_Id = #{bId},</if>
            <if test="beentrustPost !=null">beentrust_post =#{beentrustPost},</if>
            <if test="entrustPost !=null">entrust_post =#{entrustPost},</if>
            <if test="authType !=null">auth_type =#{authType},</if>
        </trim>
        where flow_id = #{flowId}
    </update>

    <delete id="deleteOrderFileById" parameterType="String">
        delete from ssb_order_file where order_id = #{orderId}
    </delete>

    <delete id="deleteOrderFileByIds" parameterType="String">
        delete from ssb_order_file where order_id in 
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>

</mapper>