<?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.boco.nbd.wios.flow.mapper.def.FlowOrderMapper">

    <sql id="Base_Column_List">
        id
        ,process_ins_id,node_flag,
        oem_id,oem_name,oem_agent_id,
        oem_agent_name,oem_agent_code,oem_agent_info,
        oem_agent_contact_name,oem_agent_contact_phone,out_order_id,
        contract_id,supplier_contract_id,user_name,
        user_phone,user_sex,contact_name,
        contact_phone,region_id,city_name,
        city_id,region_name,region_level,
        province_name,province_id,area_name,
        area_id,country_name,country_id,
        warehouse_id,address,refuse_order_reason,
        is_pick_car,vehicle_model,vehicle_chassis_no,
        vehicle_vin,vehicle_parking_no,vehicle_parking_status,
        survey_before_sale_enable,manu_dispatch_enable,wallbox_model,
        pack_type,status,type,
        expand_status,expand_fail_reason,expand_fail_operate,
        expand_remark,survey_staff_id,survey_check_fail_num,
        survey_staff_phone,survey_staff_name,survey_finish_time,
        survey_reserve_time,survey_client_name,survey_client_phone,
        survey_cams_phone,survey_reserve_remark,install_supplier_id,
        install_supplier_name,install_staff_id,install_check_fail_num,
        install_staff_phone,install_staff_name,install_finish_time,
        install_reserve_time,install_client_name,install_client_phone,
        install_cams_phone,install_reserve_remark,survey_finish_fail_reason,
        survey_rectify_fail_reason,install_change_fail_reason,install_finish_fail_reason,
        install_rectify_fail_reason,close_order_reason,install_change_attachment_files,
        survey_reserve_fail_info,install_reserve_fail_info,files,
        description,score,visit_content,
        invoice_time,estate_type,has_independent_parking,
        install_position,property_agree,electric_expand,
        need_erect_pile,dispatch_time,survey_submit_examine_time,
        survey_approve_time,install_submit_examine_time,install_approve_time,
        return_visit_time,close_time,create_account,
        create_account_name,create_time,modify_account,
        modify_time,item_model,survey_wallbox_model,
        survey_pack_type,survey_item_model,install_wallbox_model,
        install_pack_type,install_item_model,address_lat,
        address_lng,survey_follow_time,install_follow_time,
        connect_person,connect_person_name,want_home_time,
        install_emergency_level,remark,is_connect,
        is_need_survey,shipno
    </sql>

    <select id="getOrderData" resultType="com.boco.nbd.wios.flow.entity.bo.OrderBO">
        select a.id,
        case a.expand_status
        when 1 then '通过'
        when 2 then '不通过'
        else '' end expand_status,
        b.name oem_name,
        b.code oem_code,
        c.name oem_agent_name,
        c.code oem_agent_code,
        c.organization_code oem_agent_code,
        d.name supplier_name,
        e.name survey_staff_name,
        e.phone survey_staff_phone,
        f.name install_staff_name,
        f.phone install_staff_phone,
        g.name region_name,
        h.secret_no virtual_user_phone,
        i.wallbox_no,
        i.real_cable_meter,
        i.predict_total_fee
        from t_order a
        left join t_oem b on a.oem_id = b.id
        left join t_oem c on a.oem_agent_id = c.id
        left join t_supplier d on a.install_supplier_id = d.id
        left join t_supplier_staff e on a.survey_staff_id = e.id
        left join t_supplier_staff f on a.install_staff_id = f.id
        left join t_region g on a.region_id = g.id
        left join t_secret_phone h on h.phone = a.user_phone and h.status = 1
        left join t_order_install i on i.order_id = a.id
        where 1=1
        <if test="ids !=null and ids.size>0">
            and a.id in
            <foreach item="id" collection="ids" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="getUserMoreInfo" resultType="com.ihidea.core.support.session.SessionInfo">
        select id userId,account loginname,name userName from
        wb_account
        where 1=1
        <if test="userIds !=null and userIds.size>0">
            and id in
            <foreach item="userId" collection="userIds" open="(" close=")" separator=",">
                #{userId}
            </foreach>
        </if>
    </select>


    <select id="getOrderByNodeFlagAndCreateTime" resultType="com.boco.nbd.wios.flow.entity.po.OrderPO">
        select
        <include refid="Base_Column_List"/>
        from t_order
        where type=1 and process_ins_id is not null
        <if test="nodeFlag != null and nodeFlag != ''">
            and node_flag &gt; #{nodeFlag}
        </if>
        <if test="createTime != null and createTime != ''">
            and date_format(create_time,'%Y-%m')=#{createTime}
        </if>
    </select>


    <select id="getSendMessageData" resultType="com.boco.nbd.wios.flow.entity.po.SendMessagePO">
        select node_flag,
               id,
               survey_client_phone user_phone,
               survey_staff_phone  staff_phone,
               survey_staff_name   staff_name,
               survey_reserve_time reserve_time
        from t_order
        where node_flag = 105
          and status!=64
        and survey_reserve_time <![CDATA[>=]]>#{startTime}
          and survey_reserve_time <![CDATA[<=]]>#{endTime}
        union all
        select node_flag,
               id,
               install_client_phone user_phone,
               install_staff_phone  staff_phone,
               install_staff_name   staff_name,
               install_reserve_time reserve_time
        from t_order
        where node_flag = 110
          and status!=64
        and install_reserve_time <![CDATA[>=]]>#{startTime}
          and install_reserve_time <![CDATA[<=]]>#{endTime}
    </select>


    <select id="getPmUser" resultType="com.boco.nbd.wios.flow.entity.po.PmUserPO">
        select a.account_id, a.region_id
        from t_cams_account a
                 inner join wb_account_role b on a.account_id = b.account_id
        where a.status = 1
          and b.role_id = 5
    </select>

    <select id="getUsableSupplier" resultType="com.boco.nbd.wios.flow.entity.po.UsableSupplierPO">
        select a.id supplier_id, a.name supplier_name, a.region_id, a.oem_code, a.pack_type, a.level, max_order_quantity
        from t_supplier a
                 inner join t_supplier_contract b on a.id = b.supplier_id and b.status = 2
        where a.status = 1
          and a.check_status = 1
    </select>

    <select id="getSupplierReceiveInfo" resultType="com.boco.nbd.wios.flow.entity.po.SupplierReceivePO">
        select install_supplier_id supplier_id, region_id, count(*) receive_num
        from t_order
        where 1=1
        <if test="supplierId != null">
            and install_supplier_id = #{supplierId}
        </if>
        <if test="regionId != null and regionId != ''">
            and region_id = #{regionId}
        </if>
    </select>

    <select id="selectOrderPO" resultType="com.boco.nbd.wios.flow.entity.po.OrderPO">
        select *
        from t_order a
        where 1 = 1
        <if test="outOrderId != null and outOrderId != ''">
            and a.out_order_id=#{outOrderId}
        </if>
        <if test="userName != null and userName != ''">
            and a.user_name=#{userName}
        </if>
        <if test="userPhone != null and userPhone != ''">
            and a.user_phone=#{userPhone}
        </if>
        <if test="oemAgentInfo != null and oemAgentInfo != ''">
            and a.oem_agent_info like concat(#{oemAgentInfo}, '%')
        </if>
        <if test="oemAgentName != null and oemAgentName != ''">
            and a.oem_agent_name like concat(#{oemAgentName}, '%')
        </if>
        <if test="id != null and id != ''">
            and a.id =#{id}
        </if>
        <if test="oemId != null">
            and a.oem_id =#{oemId}
        </if>
        <if test="status != null and status != ''">
            and a.status =#{status}
        </if>
        <if test="countryId != null and countryId != ''">
            a.country_id in (#{countryId})
        </if>
        <if test="areaId != null and areaId != ''">
            a.area_id in (#{areaId})
        </if>
        <if test="provinceId != null and provinceId != ''">
            a.province_id in (#{provinceId})
        </if>
        <if test="regionId != null and regionId != ''">
            a.region_id in (#{regionId})
        </if>
        <if test="cityId != null and cityId != ''">
            a.city_id in (#{cityId})
        </if>
        <if test="wallboxNo != null and wallboxNo != ''">
            and a.status =#{status}
        </if>
        <if test="supplierName != null and supplierName != ''">
            and a.install_supplier_name like concat(#{supplierName}, '%')
        </if>
        <if test="supplierId != null and supplierId != ''">
            and a.install_supplier_id in (#{supplierId})
        </if>
        <if test="type != null">
            and a.type in (#{type})
        </if>
        <if test="surveyFinishTimeStart != null">
            and a.survey_finish_time <![CDATA[<=]]>#{surveyFinishTimeStart}
        </if>
        <if test="surveyFinishTimeEnd != null">
            and a.survey_finish_time <![CDATA[<=]]>#{surveyFinishTimeEnd}
        </if>
        <if test="installFinishTimeStart != null">
            and a.install_finish_time <![CDATA[>=]]>#{installFinishTimeStart}
        </if>
        <if test="installFinishTimeEnd != null">
            and a.install_finish_time <![CDATA[<=]]>#{installFinishTimeEnd}
        </if>
        <if test="overtimeType != null">
            and a.overtime_type =#{overtimeType}
        </if>
        <if test="scoreGt != null">
            and a.client_rate <![CDATA[>=]]>#{scoreGt}
        </if>
        <if test="scoreLt != null">
            and a.client_rate <![CDATA[<=]]>#{scoreLt}
        </if>
        order by a.create_time desc
    </select>

    <select id="getVisitOrderScore" resultType="com.boco.nbd.wios.flow.entity.po.VisitOrderPO">
        select a.out_order_id,
        'cams APP' source,
        user_name,
        user_phone,
        a.score,
        a.know_after_sales_channel,
        a.install_length,
        c.score_data
        from t_order a
        left join (select b.order_id,
        group_concat(replace(b.code, 'customer_item', ''), ',', b.name, ',', b.score,',', b.description, ';') score_data
        from t_order_score b
        where b.status = 1
        group by b.order_id) c
        on a.id = c.order_id
        where 1=1
        <if test="status != null">
            and a.status = #{status}
        </if>
        <if test="id != null and id != ''">
            and a.id =#{id}
        </if>
        <if test="userName != null and userName != ''">
            and a.user_name=#{userName}
        </if>
        <if test="userPhone != null and userPhone != ''">
            and a.user_phone=#{userPhone}
        </if>
        <if test="oemName != null and oemName != ''">
            and a.oem_name like concat(#{oemName}, '%')
        </if>
        <if test="supplierName != null and supplierName != ''">
            and a.install_supplier_name like concat(#{supplierName}, '%')
        </if>
        <if test="surveyFinishStartTime != null and surveyFinishStartTime != ''">
            and a.survey_finish_time &gt;= #{surveyFinishStartTime}
        </if>
        <if test="surveyFinishEndTime != null and surveyFinishEndTime != ''">
            and a.survey_finish_time &lt;= #{surveyFinishEndTime}
        </if>
        <if test="installFinishStartTime != null and installFinishStartTime != ''">
            and a.install_finish_time &gt;= #{installFinishStartTime}
        </if>
        <if test="installFinishEndTime != null and installFinishEndTime != ''">
            and a.install_finish_time &lt;= #{installFinishEndTime}
        </if>
    </select>
</mapper>