<?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.casic.missiles.mapper.business.BusinessInterchangeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessInterchange">
        <id column="id" property="id"/>
        <result column="interchange_code" property="interchangeCode"/>
        <result column="order_id" property="orderId"/>
        <result column="certification_company" property="certificationCompany"/>
        <result column="certification_company_fex" property="certificationCompanyFex"/>
        <result column="certification_company_email" property="certificationCompanyEmail"/>
        <result column="reciever" property="reciever"/>
        <result column="reciever_phone" property="recieverPhone"/>
        <result column="certification_company_address" property="certificationCompanyAddress"/>
        <result column="customer_id" property="customerId"/>
        <result column="require_over_time" property="requireOverTime"/>
        <result column="deliver_time" property="deliverTime"/>
        <result column="measure_category" property="measureCategory"/>
        <result column="secrecy" property="secrecy"/>
        <result column="appearance" property="appearance"/>
        <result column="rely_file" property="relyFile"/>
        <result column="deliverer_sign_file" property="delivererSignFile"/>
        <result column="deliverer_sign_time" property="delivererSignTime"/>
        <result column="reciever_sign_file" property="recieverSignFile"/>
        <result column="remark" property="remark"/>
        <result column="is_del" property="isDel"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, interchange_code, order_id, certification_company, certification_company_fex, certification_company_email, reciever, reciever_phone, certification_company_address, customer_id, require_over_time, deliver_time, mesure_category, secrecy, appearance, rely_file, deliverer_sign_file, deliverer_sign_time, reciever_sign_file, remark, is_del, create_user, create_time, update_time
    </sql>

    <select id="selectInterchangeListPage"
            resultType="com.casic.missiles.dto.business.interchange.InterchangeListResponse">
        SELECT
        bif.id,deliverer,customerNo,customerName,delivererTel,orderId,id,interchangeCode,remark,deliverTime,bo.orderId,isUrgent,orderNo,requireOverTime,reciever
        FROM (
            SELECT id,interchange_code AS "interchangeCode",remark,order_id,deliver_time AS
            "deliverTime",reciever,deliverer,deliverer_tel AS "delivererTel"
            FROM business_interchange
            WHERE is_del = 0
            <if test="request.interchangeCode != null and request.interchangeCode !=''">
                AND interchange_Code=#{request.interchangeCode}
            </if>
            <if test="request.reciever != null and request.reciever !=''">
                AND reciever=#{request.reciever}
            </if>
            <if test="request.startTime != null and request.startTime !=''">
                AND #{request.startTime}  <![CDATA[ <= ]]> DATE_FORMAT(deliver_time,'%Y-%m-%d')
            </if>
            <if test="request.endTime != null and request.endTime !=''">
                AND #{request.endTime}  <![CDATA[ >= ]]> DATE_FORMAT(deliver_time,'%Y-%m-%d')
            </if>
            <if test="request.ids != null ">
                and id in
                <foreach collection="request.ids" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        ) bif
        JOIN (
        SELECT customer_no AS "customerNo",customer_name AS "customerName",id as "orderId",is_urgent AS
        "isUrgent",order_code AS "orderNo",require_over_time AS "requireOverTime"
        FROM business_order
        WHERE is_del = 0
        <if test="request.deliverer != null and request.deliverer !=''">
            AND deliverer=#{request.deliverer}
        </if>
        <if test="request.customerNo != null and request.customerNo !=''">
            AND customer_no=#{request.customerNo}
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            AND customer_name=#{request.customerName}
        </if>
        ) bo ON bo.orderId = bif.order_id
        ORDER BY bif.deliverTime DESC
    </select>

    <select id="selectMaxExchangeNo" resultType="Long">
        SELECT IFNULL(max(RIGHT(interchange_code, 12)), 0)
        FROM business_interchange
    </select>

    <select id="getInfoById" resultType="com.casic.missiles.dto.business.interchange.BusinessInterchangeDetailResponse">
    SELECT
        bi.id,
        interchange_code AS "interchangeCode",
        bo.id AS "orderId",
        order_code AS "orderCode",
        bi.certification_company AS "certificationCompany",
        certification_company_fex AS "certificationCompanyFex",
        certification_company_email AS "certificationCompanyEmail",
        reciever,
        reciever_phone AS "recieverPhone",
        bi.certification_company_address AS "certificationCompanyAddress",
        bo.customer_id AS "customerId",
        customer_no AS "customerNo",
        customer_name AS "customerName",
        customer_address AS "customerAddress",
        order_time AS "orderTime",
        bo.require_over_time AS "requireOverTime",
        bo.deliver_time AS "deliverTime",
        measure_category AS "measureCategory",
        secrecy,
        appearance,
        rely_file AS "relyFile",
        bo.deliverer,
        bo.deliver_time AS "deliverTime",
        bo.deliverer_tel AS "delivererTel",
        deliverer_sign_file AS "delivererSignFile",
        deliverer_sign_time AS "delivererSignTime",
        reciever_sign_file AS "recieverSignFile",
        bi.remark,
        bi.create_user AS "createUser",
        bi.create_Time AS createTime,
        bi.update_time AS "updateTime"
    FROM
        ( SELECT * FROM business_interchange WHERE is_del = 0 AND id=#{id} ) bi
        JOIN ( SELECT * FROM business_order WHERE is_del = 0 ) bo ON bi.order_id = bo.id
    </select>


</mapper>
