<?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.zfire.jiasm.syncdata.dao.mapper.GDOrderBaseMapper">

    <select id="getAzOrderBase" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfTblAzAssignLcLsUpload">
        select a.id as worker_order_no,a.pgguid,'guangfo' as created_by,a.create_time as created_date,'guangfo' as last_modified_by,
               a.update_time as last_modified_date,a.pgid,a.user_name as yhmc,a.user_mobile as yddh,a.user_mobile2 as yddh2,
               a.area_code as quhao,a.tel_phone as dhhm,if(length(a.address)>200,left(a.address,200),a.address) as dizi,
               a.info_channel as xxqd,a.info_source as xxly,
               a.remark as beiz,a.user_attr as yhsx,c.ref_dict_code as spid,c.ref_dict_name as spmc,
               a.worker_name as azren,a.websit_id as azwdno,a.rece_websit_id as jspgwdno,a.rece_time as jspgwdsj,
               a.external_sys as qqlyno,a.external_name2 as qqlymc,a.external_no as qqlyxh,a.external_pk as qqlyzj,
               a.expect_start_time as yhqwkssj,a.expect_end_time as yhqwjssj,m.brand_order_state as stat,
               if(length(a.gps_address)>130,left(a.gps_address,130),a.gps_address)  as gpsdzxx,
               a.create_by as cjren,a.create_name as cjrmc,a.create_time as cjdt,
               a.last_operator_time as zjczsj,a.sale_type as xslx,a.appointment_time as yyazsj,
               case a.is_delivery_over when 'NO' then '0'
                                       when 'YES' then '1' else '0' end as sfwcps,
               a.sale_no as xsdh,j.engin_no as gcbh,j.engin_name as gcmc,
               a.total_num as azsl,a.undone_num as wwsl,a.link_name as lxren,a.sale_websit_id as xsdwno,
               a.invoice_no as fphm,a.buy_time as gmsj,
               a.cross_no as kqbh,a.data_push_attr as xsorsh,a.order_status_time as dqjdsj,l.brand_order_state as syjd,
               m.brand_order_state as dqjd,
               case when length(a.lng) >12  then left(a.lng,12) else ifnull(a.lng,'') end as jindu,
               ifnull(a.lat,'') as weidu,a.delivery_time as shsj,ifnull(a.sale_type_id,1) as xslxid,
               ifnull(a.user_attr_id,1) as yhsxid,ifnull(a.info_source_id,1) as xxlyid,a.over_time as wcsj,0 as extendfiled1,
               case when a.vip = '1' then 'VIP1' when a.vip = '2' then 'VIP2' when a.vip = '3' then 'VIP3'
                    when a.vip = '4' then 'VIP4' when a.vip = '5' then 'VIP5'
                    else a.vip end as extendfiled2,
               '1' as extendfiled5,a.dispatch_time as pgsj,
               a.province as sfen,a.province_id as sfenid,a.city as cshi,a.city_id as cshiid,
               a.area as xian,a.area_id as xianid,a.street as xzhen,a.street_id as xzhenid
        from order_base a
            left join sys_dict_ref c on c.dict_code=a.main_id and c.brand_id = #{request.brandId} and c.dict_type='MAIN_TYPE'
            left join order_install j on j.order_base_id=a.id and a.id = #{request.orderNo}
            left join order_state_contrast l on l.order_state=a.last_order_status and l.state_type=10 and l.brand_id = #{request.brandId} and l.flag=1
            left join order_state_contrast m on m.order_state=a.order_status and m.state_type=10 and m.brand_id = #{request.brandId} and m.flag=1
        where a.id = #{request.orderNo}
    </select>

    <select id="getItfTblAssignUpload" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfTblAssignUpload">
        select a.id as worker_order_no,a.pgid,'guangfo' as created_by,a.create_time as created_date,'guangfo' as last_modified_by,
          a.update_time as last_modified_date,IFNULL(a.user_name,'') as yhmc,IFNULL(a.user_mobile,'') as yddh,
          IFNULL(a.area_code,'') as quhao,IFNULL(a.tel_phone,'') as dhhm,
          a.fjhm,IFNULL(a.email,''),a.province as sfen,a.province_id as sfenid,a.city as cshi,a.city_id as cshiid,a.area as xian,
          a.area_id as xianid,a.street as xzhen,a.street_id as xzhenid,
          if(length(a.address)>100,left(a.address,100),a.address) as dizi,
          if(length(a.gps_address)>130,left(a.gps_address,130),a.gps_address) as gpsdzxx,
          IFNULL(a.info_channel,'') as xxqd,IFNULL(a.info_source,'总部') as xxly,q.demand_type_name as xxlb,
          IFNULL(a.remark,'') as beiz,a.file_no as gdhao,
          m.brand_order_state as stat,a.expect_start_time as yhqwsmsj,IFNULL(c.ref_dict_code,0) as spid,IFNULL(c.ref_dict_name,'') as spmc,
          IFNULL(a.create_by,'') as cjren,IFNULL(a.create_name,'') as cjrmc,a.create_time as cjdt,
          IFNULL(a.worker_name,'') as wxren,a.over_time as wcsj,a.websit_id as zjczwd,
          a.websit_id as wxwdno,IFNULL(a.priority,1) as yxji,0 as ldcs,IFNULL(a.user_mobile2,'') as yddh2,'' as cxyzm,
          IFNULL(a.sale_no,'') as xsdh,a.rece_websit_id as xjwdno,
          '' as chaoshiqe,IFNULL(q.zbby,'') as zbby,a.over_time as fwrybwgsj,
          a.external_name2 as qqlymc,a.external_no as qqlyxh,IFNULL(a.user_attr,'') as yhsx,
          a.sale_websit_id as xswdno,a.dispatch_time as xjwdsj,a.last_operator_time as zjczsj,
          0 as yhgyhf,p.brand_order_state as auth_state,o.brand_order_state as qystat,'' as zptype,'' as yhif,
          case a.complaint when 'FIRST' then '一级投诉' when 'SECOND' then '二级投诉' when 'THIRD' then '三级投诉'
                           when 'SUPER' then '特级投诉' else ' ' end as tsdengji,
          a.expect_end_time as qwsmjssj,
          a.seat_no as zxhao,a.id as qqlyzj,a.order_status_time as dqjdsj,
          case when length(a.lng) >12  then left(a.lng,12) else IFNULL(a.lng,'') end as jindu,
          case when length(a.lat) >12  then left(a.lat,12) else IFNULL(a.lat,'') end as weidu,
          IFNULL(a.pgguid, '') as pgguid,q.demand_type_id as xxlbid,a.info_channel_id as xxqdid,
          IFNULL(a.info_source_id, 102) as xxlyid,IFNULL(a.user_attr_id, 1) as yhsxid,IFNULL(a.out_wxcount,1) as wxcount,
          '1' as extjson5,IFNULL(a.vip,'')  as vip
        from order_base a
            left join sys_dict_ref c on c.dict_code=a.main_id and c.brand_id = #{request.brandId} and c.dict_type='MAIN_TYPE'
            left join order_state_contrast m on m.order_state=a.order_status and m.state_type=60 and m.brand_id = #{request.brandId} and m.flag=1
            left join order_state_contrast o on o.order_state=a.last_order_status and o.state_type=60 and o.brand_id = #{request.brandId} and o.flag=1
            left join order_state_contrast p on p.order_state=a.order_status and p.state_type=60 and p.brand_id = #{request.brandId} and p.flag=1
            left join order_repair q on q.order_base_id=a.id
        where a.id = #{request.orderNo}
    </select>

    <select id="getItfMessagehuifangentity" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfMessagehuifangentity">
        select id as order_base_id, 2 as sendtype,2 as azorwx,now() as crdt,pgid as fjid,
               if(hour(now())>20,concat(date(DATE_ADD(NOW(),INTERVAL 1 day)),' 07:00:00'),DATE_ADD(NOW(),INTERVAL 2 HOUR)) as plansendtime,
               websit_id as fjwd,replace(trim(user_mobile),'	','') as sjid
        from order_base
        where id = #{request.orderNo} and (timestampdiff(DAY,FROM_UNIXTIME(over_time),now()) &lt; 7
            or timestampdiff(DAY,FROM_UNIXTIME(websit_over_time),now()) &lt; 7
            or timestampdiff(DAY,FROM_UNIXTIME(worker_sort_time),now()) &lt; 7)
    </select>

    <select id="getItfMessagehuifangentityT" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfMessagehuifangentity">
        select id as order_base_id, 2 as azorwx,now() as crdt,pgid as fjid,websit_id as fjwd,replace(trim(user_mobile),'	','') as sjid
        from order_base
        where id = #{request.orderNo} and (timestampdiff(DAY,FROM_UNIXTIME(over_time),now()) &lt; 7
              or timestampdiff(DAY,FROM_UNIXTIME(websit_over_time),now()) &lt; 7
              or timestampdiff(DAY,FROM_UNIXTIME(worker_sort_time),now()) &lt; 7)
    </select>

    <select id="getAZItfMessagehuifangentity" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfMessagehuifangentity">
        select id as order_base_id, 2 as sendtype,1 as azorwx,now() as crdt,pgguid as fjid,
               if(hour(now())>20,concat(date(DATE_ADD(NOW(),INTERVAL 1 day)),' 07:00:00'),DATE_ADD(NOW(),INTERVAL 2 HOUR)) as plansendtime,
               websit_id as fjwd,replace(trim(user_mobile),'	','') as sjid
        from order_base
        where id = #{request.orderNo}  and (timestampdiff(DAY,FROM_UNIXTIME(over_time),now()) &lt; 7
            or timestampdiff(DAY,FROM_UNIXTIME(websit_over_time),now()) &lt; 7
            or timestampdiff(DAY,FROM_UNIXTIME(worker_sort_time),now()) &lt; 7)
    </select>

    <select id="getAZItfMessagehuifangentityT" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfMessagehuifangentity">
        select id as order_base_id, 1 as sendtype,1 as azorwx,now() as crdt,pgguid as fjid,
               replace(trim(user_mobile),'	','') as sjid
        from order_base
        where id = #{request.orderNo}  and (timestampdiff(DAY,FROM_UNIXTIME(over_time),now()) &lt; 7
            or timestampdiff(DAY,FROM_UNIXTIME(websit_over_time),now()) &lt; 7
            or timestampdiff(DAY,FROM_UNIXTIME(worker_sort_time),now()) &lt; 7)
    </select>

    <select id="getAzOrderBaseCj" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.ItfTblAzAssignLcLsUpload">
        select 44 as syn_status,a.id as worker_order_no,a.pgguid,'guangfo' as created_by,a.create_time as created_date,'guangfo' as last_modified_by,
               a.update_time as last_modified_date,a.pgid,a.user_name as yhmc,a.user_mobile as yddh,a.user_mobile2 as yddh2,
               a.area_code as quhao,a.tel_phone as dhhm,if(length(a.address)>200,left(a.address,200),a.address) as dizi,
               a.info_channel as xxqd,a.info_source as xxly,
               a.remark as beiz,a.user_attr as yhsx,c.ref_dict_code as spid,c.ref_dict_name as spmc,a.worker_name as azren,
               a.websit_id as azwdno,a.rece_websit_id as jspgwdno,a.rece_time as jspgwdsj,a.rece_websit_id as ssqy,
               a.external_sys as qqlyno,a.external_name2 as qqlymc,a.external_no as qqlyxh,a.external_pk as qqlyzj,
               a.expect_start_time as yhqwkssj,a.expect_end_time as yhqwjssj,m.brand_order_state as stat,
               if(length(a.gps_address)>130,left(a.gps_address,130),a.gps_address) as gpsdzxx,
               a.create_by as cjren,a.create_name as cjrmc,a.create_time as cjdt,
               a.last_operator_time as zjczsj,a.sale_type as xslx,a.appointment_time as yyazsj,
               case a.is_delivery_over when 'NO' then '0' when 'YES' then '1' else '0' end as sfwcps,
               a.sale_no as xsdh,j.engin_no as gcbh,j.engin_name as gcmc,
               a.total_num as azsl,a.undone_num as wwsl,a.link_name as lxren,a.sale_websit_id as xsdwno,
               a.invoice_no as fphm,a.buy_time as gmsj,
               a.cross_no as kqbh,a.data_push_attr as xsorsh,a.order_status_time as dqjdsj,l.brand_order_state as syjd,
               m.brand_order_state as dqjd,
               case when length(a.lng) >12  then left(a.lng,12) else a.lng end as jindu,
               ifnull(a.lat,'') as weidu,a.delivery_time as shsj,ifnull(a.sale_type_id,1) as xslxid,
               ifnull(a.user_attr_id,1) as yhsxid,ifnull(a.info_source_id,1) as xxlyid,a.over_time as wcsj,
               case when a.vip = '1' then 'VIP1' when a.vip = '2' then 'VIP2' when a.vip = '3' then 'VIP3'
                    when a.vip = '4' then 'VIP4' when a.vip = '5' then 'VIP5'
                    else a.vip end as extendfiled2,
               '1' as extendfiled5,a.dispatch_time as pgsj,
               a.province as sfen,a.province_id as sfenid,a.city as cshi,a.city_id as cshiid,
               a.area as xian,a.area_id as xianid,a.street as xzhen,a.street_id as xzhenid
        from order_base a
             left join sys_dict_ref c on c.dict_code=a.main_id and c.brand_id = #{request.brandId} and c.dict_type='MAIN_TYPE'
             left join order_install j on j.order_base_id=a.id and a.id = #{request.orderNo}
             left join order_state_contrast l on l.order_state=a.last_order_status and l.state_type=10 and l.brand_id = #{request.brandId} and l.flag=1
             left join order_state_contrast m on m.order_state=a.order_status and m.state_type=10 and m.brand_id = #{request.brandId} and m.flag=1
        where a.id = #{request.orderNo}
    </select>

    <select id="getOrderBaseLog" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.OrderRepairOperatingLog">
        select create_websit_id as websit_id,create_websit_id as websit_name, id as order_base_id,user_name as customer_name,
               user_mobile as customer_phone,'指派服务商' as type,'[JSM](家用空调)派工服务商' as content,'指派服务商' as feedback,
               create_websit_id as operator,create_websit_name as operate_websit,last_order_status as before_order_status,order_status,
               last_dispatch_status as before_dispatch_status,dispatch_status,pgguid,pgid,2 as data_source,1 as upload_notify_flag,
               create_time,update_time,create_by,update_by
        from order_base
        where a.id = #{request.orderNo}
    </select>

    <select id="getOrderBaseLogT" parameterType="com.zfire.jiasm.syncdata.parameter.OrderPar" resultType="com.zfire.jiasm.syncdata.plus.entity.OrderRepairOperatingLog">
        select sale_websit_id as websit_id,sale_websit_name as websit_name, id as order_base_id,user_name as customer_name,
               user_mobile as customer_phone,'工单派网点' as type,'[JSM](家用空调)派工服务商' as content,'工单派网点' as feedback,
               create_websit_id as operator,create_websit_name as operate_websit,last_order_status as before_order_status,order_status,
               last_dispatch_status as before_dispatch_status,dispatch_status,pgguid,pgid,2 as data_source,1 as upload_notify_flag,
               create_time,update_time,create_by,update_by
        from order_base
        where a.id = #{request.orderNo}
    </select>


</mapper>
