<?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.njzscloud.supervisory.order.mapper.OrderInfoMapper">

    <sql id="base_select">
        SELECT a.id,
               a.sn,
               a.station_id,
               a.certificate_sn,
               a.lane,
               a.project_id,
               a.trans_company_id,
               a.user_id,
               a.audit_status,
               a.audit_memo,
               a.audit_time,
               a.qu_audit_user_id,
               a.shi_audit_user_id,
               n.nickname           qu_audit_user_name,
               o.nickname           shi_audit_user_name,
               a.order_category,
               a.order_status,
               a.checker_id,
               a.check_status,
               a.truck_id,
               a.driver_id,
               a.goods_id,
               a.discount_money,
               a.revise_money,
               a.settle_money,
               a.total_money,
               a.refund_money,
               a.payment_status,
               a.customer_memo,
               a.checker_memo,
               a.cargo_photo,
               a.check_photo,
               a.creator_id,
               a.modifier_id,
               a.create_time,
               a.modify_time,
               a.pay_time,
               a.refund_time,
               a.expect_time,
               a.estimated_quantity,
               a.deleted,
               a.cargo_place_id,
               a.car_in_out_id,
               a.site_photos,
               b.province,
               b.city,
               b.area,
               b.town,
               b.province_name,
               b.city_name,
               b.area_name,
               b.town_name,
               b.address,
               b.lng,
               b.lat,
               c.goods_category_id,
               c.origin_goods_id,
               c.goods_name,
               c.unit_price,
               c.unit,
               c.money_strategy,
               c.money_config_id,
               c.picture,
               c.tax_rate,
               i.category_name,
               d.rough_weight,
               d.tare_weight,
               d.settle_weight,
               d.in_front_photo,
               d.in_body_photo,
               d.out_front_photo,
               d.out_body_photo,
               d.in_time,
               d.out_time,
               e.license_plate,
               e.truck_license,
               e.vn_code,
               e.qualification,
               e.carrying_capacity,
               e.tare_weight          history_tare_weight,
               e.truck_category,
               e.picture            truck_picture,
               f.driver_name,
               f.phone                driver_phone,
               f.driving_licence,
               f.licence_start_time,
               f.licence_end_time,
               f.driving_licence_no,
               g.uscc                 trans_uscc,
               g.company_name         trans_company_name,
               g.business_license     trans_business_license,
               g.license_start_time   trans_license_start_time,
               g.license_end_time     trans_license_end_time,
               g.legal_representative trans_legal_representative,
               g.province             trans_province,
               g.city                 trans_city,
               g.area                 trans_area,
               g.town                 trans_town,
               g.province_name        trans_province_name,
               g.city_name            trans_city_name,
               g.area_name            trans_area_name,
               g.town_name            trans_town_name,
               g.address              trans_address,
               g.lng                  trans_lng,
               g.lat                  trans_lat,
               g.contacts             trans_contacts,
               g.phone                trans_phone,
               h.nickname,
               h.biz_obj,
               h.phone              user_phone,
               j.company_name,
               j.uscc,
               k.nickname           checker_name,
               l.project_name,
               m.station_name,
               m.company_name       station_company_name,
               m.address            station_address,
               m.uscc               station_uscc,
               m.contacts           station_contacts,
               m.phone              station_phone,
               a.contacts,
               a.phone,
               a.expect_time,
               a.estimated_quantity,
               e.truck_license_date,
               e.qualification_date truck_qualification_date,
               f.licence_start_time driver_licence_start_time,
               f.licence_end_time   driver_licence_end_time,
               j.license_start_time trans_license_start_time,
               j.license_end_time   trans_license_start_time,
               g.idcard             trans_idcard,
               g.idcard_start_time  trans_idcard_start_time,
               g.idcard_end_time    trans_idcard_end_time
        FROM order_info a
                 LEFT JOIN order_cargo_place b ON b.id = a.cargo_place_id
                 LEFT JOIN order_goods c ON c.id = a.goods_id
                 LEFT JOIN order_car_in_out d ON d.id = a.car_in_out_id
                 LEFT JOIN biz_truck e ON e.id = a.truck_id
                 LEFT JOIN biz_driver f ON f.id = a.driver_id
                 LEFT JOIN biz_company g ON g.id = a.trans_company_id
                 LEFT JOIN sys_user h ON h.id = a.user_id
                 LEFT JOIN goods_category i ON i.id = c.goods_category_id
                 LEFT JOIN biz_company j ON j.user_id = a.user_id
                 LEFT JOIN sys_user k ON k.id = a.checker_id
                 LEFT JOIN biz_project l ON l.id = a.project_id
                 LEFT JOIN biz_company m ON m.id = a.station_id AND m.station = 1
                 LEFT JOIN sys_user n ON n.id = a.qu_audit_user_id
                 LEFT JOIN sys_user o ON o.id = a.shi_audit_user_id
    </sql>
    <update id="busyDriver">
        UPDATE biz_driver
        SET busy = #{busy}
        WHERE id = #{driverId}
    </update>
    <update id="busyTruck">
        UPDATE biz_truck
        SET busy = #{busy}
        WHERE id = #{truckId}
    </update>

    <resultMap id="OrderPagingResultMap" autoMapping="true" type="com.njzscloud.supervisory.order.pojo.result.OrderPagingResult">
        <result property="truckLicenseDate" column="truck_license_date" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="truckQualificationDate" column="truck_qualification_date" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="cargoPhoto" column="cargo_photo" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="checkPhoto" column="check_photo" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="sitePhotos" column="site_photos" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="truckLicense" column="truck_license" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="qualification" column="qualification" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
        <result property="auditStatus" column="audit_status" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result property="orderCategory" column="order_category" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result property="orderStatus" column="order_status" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result property="checkStatus" column="check_status" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result property="payerCategory" column="payer_category" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result property="paymentStatus" column="payment_status" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
        <result property="moneyStrategy" column="money_strategy" typeHandler="com.njzscloud.common.mp.support.handler.e.EnumTypeHandlerDealer"/>
    </resultMap>
    <select id="paging" resultMap="OrderPagingResultMap">
        <include refid="base_select"/>
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="detail" resultMap="OrderPagingResultMap">
        <include refid="base_select"/>
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="pendingOrder" resultMap="OrderPagingResultMap">
        <include refid="base_select"/>
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
        LIMIT 1
    </select>
    <select id="obtainWeighBill" resultMap="OrderPagingResultMap">
        <include refid="base_select"/>
        WHERE a.id = #{orderId}
    </select>
    <select id="getDriverId" resultType="java.lang.Long">
        SELECT IFNULL(id, 0) id
        FROM biz_driver
        WHERE user_id = #{userId}
    </select>
    <select id="paymentContext" resultType="com.njzscloud.supervisory.order.pojo.result.PaymentContextResult">
        SELECT
            a.id AS orderId,
            a.trans_company_id AS transCompanyId,
            a.sn,
            a.payment_category,
            a.out_trade_no,
            bc.user_id AS companyUserId,
            ma1.id AS companyAccountId,
            ma1.money AS companyBalance,
            bc.settlement_way,
            sua.wechat_openid,
            a.refund_money,
            a.settle_money
        FROM order_info a
             LEFT JOIN biz_company bc ON bc.id = a.trans_company_id
             LEFT JOIN money_account ma1 ON ma1.station_id = a.trans_company_id
             LEFT JOIN biz_driver bd ON a.driver_id = bd.id
             LEFT JOIN sys_user_account sua ON bd.user_id = sua.user_id
        WHERE a.id = #{orderId}
    </select>
    <select id="getTruckInfo" resultType="com.njzscloud.supervisory.biz.pojo.entity.BizTruckEntity">
        SELECT *
        FROM biz_truck
        WHERE id = #{truckId}
    </select>
</mapper>
