package com.zfire.jiasm.syncdata.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.constant.GDOrderStatusEnum;
import com.zfire.jiasm.syncdata.constant.OrderStatusEnum;
import com.zfire.jiasm.syncdata.constant.ServiceCategoryEnum;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.mapper.OrderBaseMapper;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.response.WebsitFWS;
import com.zfire.jiasm.syncdata.response.WebsitSH;
import com.zfire.jiasm.syncdata.service.OrderService;
import com.zfire.jiasm.syncdata.service.PublicService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderBaseMapper, OrderBase> implements OrderService {
    @Resource
    StreetService streetService;
    @Resource
    BrandLbsRelateService brandLbsRelateService;
    @Resource
    AreaService areaService;
    @Resource
    PublicService publicService;
    @Resource
    OrderBaseService orderBaseService;
    @Resource
    OrderInstallDetailService orderInstallDetailService;
    @Resource
    BaseService baseService;


    @Override
    public  void setPar(ItfTblAzAssignLcLsDownload item, OrderBase orderBase,
                        List<ItfGreeToFaDownloadTaskList> its){
        WebsitFWS websitFWS = new WebsitFWS();

        if (StringUtils.isEmpty(orderBase.getGpsAddress())){
            orderBase.setGpsAddress(publicService.getGpsaddress(orderBase.getLng(),orderBase.getLat()));
        }

        // 设置网点信息
        setWebsit(item, orderBase);

        // 接收派工网点为中心且当前节点是1129或1301，更新为2待分部派工
        if ((item.getJspgwdno().equals("S9219801") || item.getJspgwdno().equals("S9219807")) &&
                (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1129.getCode()) ||
                        orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1301.getCode()))){
            orderBase.setOrderStatus(GDOrderStatusEnum.INSTALL_STATUS_G2_0.getCode());
        }
        // 分销商城订单需要写日志表这里不再处理

        // 省市区单独处理
        // shPositionTopt(orderBase);
        publicService.installOrderSSQJ(orderBase);
        // 派工方式，新创建订单时 azren 名称不为空时为手动派工  （dispatch_flag派工方式 A=自动派工 B=手动派工）
        if (StringUtils.isNotEmpty(orderBase.getDispatchFlag())){
            orderBase.setDispatchFlag("B");
        } else {
            orderBase.setDispatchFlag("A");
        }

        // create_sys - 创建网点系统  cjwdno
        // last_operator_websit:最近操作网点 创建单时也是创建网点 可能是销售网点，也可能是售后网点  zjczwd
        // sys_websit_number 系统网点编号 zjczwd 同上
        orderBase.setCreateSys(orderBase.getCreateWebsitId());

        // 先从销售网点判断
        websitFWS = publicService.shxswdToptxswd(item.getXsdwno(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(item.getXsdwno(),orderBase);
        }
        orderBase.setLastOperatorWebsit(websitFWS.getWebsitId());
        orderBase.setSysWebsitNumber(websitFWS.getBrandSysWebsitNumber());

        // 采集数据下载
        extracted(item, orderBase, its);
    }

    @Override
    public  void setParApi(ItfTblAzAssignLcLsDownload item, OrderBase orderBase,
                        List<ItfGreeToFaDownloadTaskList> its){
        WebsitFWS websitFWS = new WebsitFWS();

        if (StringUtils.isEmpty(orderBase.getGpsAddress())){
            orderBase.setGpsAddress(publicService.getGpsaddress(orderBase.getLng(),orderBase.getLat()));
        }

        // 消息来源不能为空，为空时默认电话
        if (StringUtils.isEmpty(orderBase.getInfoSource())){
            orderBase.setInfoSource("电话");
        }
        orderBase.setInfoSourceId(baseService.getxxly(orderBase.getInfoSource()));

        // 设置网点信息
        setWebsit(item, orderBase);

        // 接收派工网点为中心且当前节点是1129或1301，更新为2待分部派工
        if ((item.getJspgwdno().equals("S9219801") || item.getJspgwdno().equals("S9219807")) &&
                (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1129.getCode()) ||
                        orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1301.getCode()))){
            orderBase.setOrderStatus(GDOrderStatusEnum.INSTALL_STATUS_G2_0.getCode());
        }
        // 分销商城订单需要写日志表这里不再处理

        // 省市区单独处理
        // shPositionTopt(orderBase);
        publicService.installOrderSSQJ(orderBase);
        // 派工方式，新创建订单时 azren 名称不为空时为手动派工  （dispatch_flag派工方式 A=自动派工 B=手动派工）
        if (StringUtils.isNotEmpty(orderBase.getDispatchFlag())){
            orderBase.setDispatchFlag("B");
        } else {
            orderBase.setDispatchFlag("A");
        }

        // create_sys - 创建网点系统  cjwdno
        // last_operator_websit:最近操作网点 创建单时也是创建网点 可能是销售网点，也可能是售后网点  zjczwd
        // sys_websit_number 系统网点编号 zjczwd 同上
        orderBase.setCreateSys(orderBase.getCreateWebsitId());

        // 先从销售网点判断
        websitFWS = publicService.shxswdToptxswd(item.getXsdwno(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(item.getXsdwno(),orderBase);
        }
        orderBase.setLastOperatorWebsit(websitFWS.getWebsitId());
        orderBase.setSysWebsitNumber(websitFWS.getBrandSysWebsitNumber());

        // 采集数据下载
        extractedApi(item, orderBase, its);
    }

    private void setWebsit(ItfTblAzAssignLcLsDownload item, OrderBase orderBase) {
        // 售后网点处理-安装或维修网点
        WebsitFWS websitFWS = null;

        // 判断安装网点编号是否为空或者为公司，如果为空或为公司，就写入接收派工网点
        if (StringUtils.isEmpty(item.getAzwdno()) ||
                item.getAzwdno().equals("S9219801") ||
                item.getAzwdno().equals("S9219807")){
            orderBase.setWebsitId(item.getJspgwdno());
            orderBase.setWebsitName(item.getJspgwdmc());
        }

        websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(), orderBase);
        orderBase.setWebsitId(websitFWS.getWebsitId());
        orderBase.setWebsitName(websitFWS.getWdmc());
        orderBase.setGroupCompanyId(websitFWS.getGroupCompanyId());
        orderBase.setGroupCompanyName(websitFWS.getGroupCompanyName());
        orderBase.setProductBrandId("A");
        orderBase.setProductBrandName("格力(机型品牌)");

        // 接收派工网点处理 格力总部下来的接收网点信息不为空
        if (StringUtils.isNotEmpty(item.getAzwdno()) && item.getAzwdno().equals(orderBase.getReceWebsitId())) {
            orderBase.setReceWebsitId(orderBase.getWebsitId());
            orderBase.setReceWebsitName(orderBase.getWebsitName());
            // 结算主体 销售网点对应的接算主休息
            // orderBase.setBrandId(websitFWS.getBrandId());
            // orderBase.setBrandName(websitFWS.getBrandName());

        } else {
            websitFWS = publicService.shshwdToptshwd(orderBase.getReceWebsitId(), orderBase);
            orderBase.setReceWebsitId(websitFWS.getWebsitId());
            orderBase.setReceWebsitName(websitFWS.getWdmc());
            // 结算主体
            // orderBase.setBrandId(websitFWS.getBrandId());
            // orderBase.setBrandName(websitFWS.getBrandName());
        }
        // 创建网点处理
        if (StringUtils.isNotEmpty(item.getAzwdno()) && item.getAzwdno().equals(orderBase.getCreateWebsitId())) {
            orderBase.setCreateWebsitId(orderBase.getWebsitId());
            orderBase.setCreateWebsitName(orderBase.getWebsitName());
        } else {
            websitFWS = publicService.shshwdToptshwd(orderBase.getCreateWebsitId(), orderBase);
            orderBase.setCreateWebsitId(websitFWS.getWebsitId());
            orderBase.setCreateWebsitName(websitFWS.getWdmc());
        }
        // 销售网点处理
        websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(), orderBase);
        orderBase.setSaleWebsitId(websitFWS.getWebsitId());
        orderBase.setSaleWebsitName(websitFWS.getWdmc());
        // 结算主体 销售网点对应的接算主休息
        orderBase.setBrandId(websitFWS.getBrandId());
        orderBase.setBrandName(websitFWS.getBrandName());

        // 这里为商户的系统网点编号
        orderBase.setSaleWebsitSysNumber(websitFWS.getBrandSysWebsitNumber());

        // jspgwdno 引入中心网点 S9219807,S9219801,S9329829
        /*if (item.getJspgwdno().equals("S9219807") ||
                item.getJspgwdno().equals("S9219801") ||
                item.getJspgwdno().equals("S9329829")){
            websitFWS = publicService.shshwdToptshwdZX(item.getJspgwdno(), orderBase);
            orderBase.setCreateWebsitId(websitFWS.getWebsitId());
            orderBase.setCreateWebsitName(websitFWS.getWdmc());
            orderBase.setReceWebsitId(websitFWS.getWebsitId());
            orderBase.setReceWebsitName(websitFWS.getWdmc());
            orderBase.setWebsitId(websitFWS.getWebsitId());
            orderBase.setWebsitName(websitFWS.getWdmc());

            // 结算主体重置
            Belongcompany bc = publicService.getBelongCompany(item.getJspgwdno());
            websitFWS = publicService.shxswdToptxswd(bc.getBelongCompanySale(),orderBase);
            orderBase.setBrandId(websitFWS.getBrandId());
            orderBase.setBrandName(websitFWS.getBrandName());
        }*/

        // 格力中心的单直接下沉到总包商户(先查WebsitOrderSign 无数据再查CustomerInformationWebsit)
        /*if (item.getJspgwdno().equals("S9219807") ||
                item.getJspgwdno().equals("S9219801") ||
                item.getJspgwdno().equals("S9329829")){
            publicService.getZXINFO(item.getJspgwdno(), orderBase);
        }*/
        // 格力中心的单直接下沉到总包商户 改成下沉到总包服务商(websit_order_sign)
        if (item.getJspgwdno().equals("S9219807") ||
                item.getJspgwdno().equals("S9219801") ||
                item.getJspgwdno().equals("S9329829")){
            publicService.getZXPTZBINFO(item.getJspgwdno(), orderBase);
        }
    }

    private void extracted(ItfTblAzAssignLcLsDownload item, OrderBase orderBase, List<ItfGreeToFaDownloadTaskList> its) {
        // 修改工单状态
        setOrderStatus(item, orderBase);

        // 生成改派申请数据 todo 先不做 20230818
        // getOrderApplyDispatch(orderBase, oads);

        // 接收派工网点与所属区域有差异时 以接收网点为准 本身就是按网点来的，这里无需处理

        // 99999999 待验证要不要处理 todo
        // 采集数据下载
        getOrderDetailRec(item, orderBase, its);
    }

    private void extractedApi(ItfTblAzAssignLcLsDownload item, OrderBase orderBase,
                              List<ItfGreeToFaDownloadTaskList> its) {
        // 修改工单状态
        setOrderStatus(item, orderBase);

        // 生成改派申请数据 todo 先不做 20230818
        // getOrderApplyDispatch(orderBase, oads);

        // 接收派工网点与所属区域有差异时 以接收网点为准 本身就是按网点来的，这里无需处理

        // 99999999 待验证要不要处理 todo
        // 采集数据下载
        getOrderDetailRec(item, orderBase, its);
    }

    private void getOrderDetailRec(ItfTblAzAssignLcLsDownload item, OrderBase orderBase, List<ItfGreeToFaDownloadTaskList> its) {
        if (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1304.getCode()) ||
                orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1309.getCode())){
            // 提取本地订单
            OrderBase old = orderBaseService.lambdaQuery()
                    .eq(OrderBase::getPgguid, item.getPgguid())
                    .last("limit 1")
                    .one();
            if ((old !=null && old.getOutCjDownloadFlag().equals(0)) || (old ==null)){
                List<OrderInstallDetail> orderDetailRec = orderInstallDetailService.lambdaQuery()
                                .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                                .eq(OrderInstallDetail::getStatus,1).list();
                if (CollectionUtils.isNotEmpty(orderDetailRec)){
                    // 存在已采集的数据忽略下载
                } else {
                    // 清除本地的采集数据
                    List<OrderInstallDetail> delorderDetailRec = orderInstallDetailService.lambdaQuery()
                            .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                            .eq(OrderInstallDetail::getStatus,0).list();
                    if (CollectionUtils.isNotEmpty(delorderDetailRec)){
                        orderInstallDetailService.removeByIds(delorderDetailRec);
                    }
                    if (orderBase.getMainName().equals("家用空调") ||
                            orderBase.getMainName().equals("家用空气能热水器") ||
                            orderBase.getMainName().equals("驻车空调")){
                        ItfGreeToFaDownloadTaskList tl = new ItfGreeToFaDownloadTaskList();
                        tl.setDownloadTaskNo(IdWorker.getIdStr());
                        tl.setDownloadKeyValue(orderBase.getPgguid());
                        tl.setDownloadTaskType(2201);
                        tl.setDownloadStatus(0);
                        its.add(tl);
                    } else if (orderBase.getMainName().equals("商用空调")){
                        ItfGreeToFaDownloadTaskList tl = new ItfGreeToFaDownloadTaskList();
                        tl.setDownloadTaskNo(IdWorker.getIdStr());
                        tl.setDownloadKeyValue(orderBase.getPgguid());
                        tl.setDownloadTaskType(2202);
                        tl.setDownloadStatus(0);
                        its.add(tl);
                    } else {
                        ItfGreeToFaDownloadTaskList tl = new ItfGreeToFaDownloadTaskList();
                        tl.setDownloadTaskNo(IdWorker.getIdStr());
                        tl.setDownloadKeyValue(orderBase.getPgguid());
                        tl.setDownloadTaskType(2203);
                        tl.setDownloadStatus(0);
                        its.add(tl);
                    }
                    orderBase.setOutCjDownloadFlag(1);
                }
            }
        }
    }

    private void setOrderStatus(ItfTblAzAssignLcLsDownload item, OrderBase orderBase) {
        WebsitSH websitSH = publicService.ptxswdToshxswd(orderBase.getReceWebsitId(), orderBase.getMainId());

        if (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1121.getCode()) ||
                orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1125.getCode())){
            if (!(websitSH.getWdId().equals("S9219801") || websitSH.getWdId().equals("S9219807")) &&
                    (!orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G1_0.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G1_0.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G1_1.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G2_0.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G2_1.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G3.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G4_0.getCode()) &&
                            !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G4_1.getCode())) &&
                    !orderBase.getOrderStatus().equals(GDOrderStatusEnum.INSTALL_STATUS_G5.getCode())){
                orderBase.setOrderStatus(GDOrderStatusEnum.INSTALL_STATUS_G3.getCode());
                orderBase.setDispatchStatus("DJS");
            }
            if (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1125.getCode())){
                // 售后网点=接收派工网点
                orderBase.setWebsitId(orderBase.getReceWebsitId());
                orderBase.setWebsitName(orderBase.getReceWebsitName());
            }
        }

        if (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1129.getCode()) ||
                orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1135.getCode())){
            if (websitSH.getWdId().equals("S9219801") || websitSH.getWdId().equals("S9219807")){
                orderBase.setOrderStatus(GDOrderStatusEnum.INSTALL_STATUS_G2_0.getCode());
                orderBase.setDispatchStatus("DJS");
            }
        }

        // 关闭工单修改
        if (orderBase.getOrderStatus().equals(OrderStatusEnum.INSTALL_STATUS_A1309.getCode())){
            List<OrderInstallDetail> oid = orderInstallDetailService.lambdaQuery()
                    .eq(OrderInstallDetail::getOrderBaseId, orderBase.getId())
                    .eq(OrderInstallDetail::getStatus,1).list();
            // 提取本地订单
            OrderBase old = orderBaseService.lambdaQuery()
                    .eq(OrderBase::getPgguid, item.getPgguid())
                    .last("limit 1")
                    .one();

            if (CollectionUtils.isNotEmpty(oid) && oid.size()==old.getTotalNum()){
                orderBase.setOrderStatus(GDOrderStatusEnum.INSTALL_STATUS_G10.getCode());
                orderBase.setDispatchStatus("YBWG");
            }
        }
    }



    @Override
    public void setParUp(ItfTblAzAssignLcLsDownload item, OrderBase orderBase,List<ItfGreeToFaDownloadTaskList> its){
        WebsitFWS websitFWS = new WebsitFWS();

        if (StringUtils.isEmpty(orderBase.getGpsAddress())){
            orderBase.setGpsAddress(publicService.getGpsaddress(orderBase.getLng(),orderBase.getLat()));
        }

        // 师傅信息可能对应不到
        if (StringUtils.isEmpty(orderBase.getWorkerIdcard())){
            orderBase.setWorkerId("");
            orderBase.setWorkerName("");
            orderBase.setWorkerMobile("");
            orderBase.setWorkerIdcard("");
        }

        // 设置网点信息
        setWebsit(item, orderBase);

        // 省市区单独处理
        // shPositionTopt(orderBase);
        publicService.installOrderSSQJ(orderBase);

        // 派工方式，新创建订单时 azren 名称不为空时为手动派工  （dispatch_flag派工方式 A=自动派工 B=手动派工）
        if (StringUtils.isNotEmpty(orderBase.getDispatchFlag())){
            orderBase.setDispatchFlag("B");
        } else {
            orderBase.setDispatchFlag("A");
        }

        // create_sys - 创建网点系统  cjwdno
        // last_operator_websit:最近操作网点 创建单时也是创建网点 可能是销售网点，也可能是售后网点  zjczwd
        // sys_websit_number 系统网点编号 zjczwd 同上
        orderBase.setCreateSys(orderBase.getCreateWebsitId());

        // 先从销售网点判断
        websitFWS = publicService.shxswdToptxswd(item.getXsdwno(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(item.getXsdwno(),orderBase);
        }
        orderBase.setLastOperatorWebsit(websitFWS.getWebsitId());
        orderBase.setSysWebsitNumber(websitFWS.getBrandSysWebsitNumber());

        extracted(item, orderBase, its);
    }

    @Override
    public void setParUpApi(ItfTblAzAssignLcLsDownload item, OrderBase orderBase, List<ItfGreeToFaDownloadTaskList> its){
        WebsitFWS websitFWS = new WebsitFWS();

        if (StringUtils.isEmpty(orderBase.getGpsAddress())){
            orderBase.setGpsAddress(publicService.getGpsaddress(orderBase.getLng(),orderBase.getLat()));
        }

        // 师傅信息可能对应不到
        if (StringUtils.isEmpty(orderBase.getWorkerIdcard())){
            orderBase.setWorkerId("");
            orderBase.setWorkerName("");
            orderBase.setWorkerMobile("");
            orderBase.setWorkerIdcard("");
        }

        // 消息来源不能为空，为空时默认电话
        if (StringUtils.isEmpty(orderBase.getInfoSource())){
            orderBase.setInfoSource("电话");
        }
        orderBase.setInfoSourceId(baseService.getxxly(orderBase.getInfoSource()));

        // 设置网点信息
        setWebsit(item, orderBase);

        // 省市区单独处理
        // shPositionTopt(orderBase);
        publicService.installOrderSSQJ(orderBase);
        // 派工方式，新创建订单时 azren 名称不为空时为手动派工  （dispatch_flag派工方式 A=自动派工 B=手动派工）
        if (StringUtils.isNotEmpty(orderBase.getDispatchFlag())){
            orderBase.setDispatchFlag("B");
        } else {
            orderBase.setDispatchFlag("A");
        }

        // create_sys - 创建网点系统  cjwdno
        // last_operator_websit:最近操作网点 创建单时也是创建网点 可能是销售网点，也可能是售后网点  zjczwd
        // sys_websit_number 系统网点编号 zjczwd 同上
        orderBase.setCreateSys(orderBase.getCreateWebsitId());

        // 先从销售网点判断
        websitFWS = publicService.shxswdToptxswd(item.getXsdwno(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(item.getXsdwno(),orderBase);
        }
        orderBase.setLastOperatorWebsit(websitFWS.getWebsitId());
        orderBase.setSysWebsitNumber(websitFWS.getBrandSysWebsitNumber());

        extractedApi(item, orderBase, its);
    }



    @Override
    public void setParame(Brand brand, OrderBase orderBase, ItfTblAssignDownload item) {
        WebsitFWS websitFWS = new WebsitFWS();

        // gpsaddress
        if (StringUtils.isEmpty(orderBase.getGpsAddress())){
            orderBase.setGpsAddress(publicService.getGpsaddress(orderBase.getLng(),orderBase.getLat()));
        }

        // 结算主体
        // (wxwdno、xjwdno)维修网点与派下级网点同时为空时  (cjwdno、xswdno)创建网点与销售网点都有值
        /*if (StringUtils.isNotEmpty(orderBase.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
        } else {
            websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(),orderBase);
        }*/

        websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(),orderBase);

        orderBase.setBrandId(websitFWS.getBrandId());
        orderBase.setBrandName(websitFWS.getBrandName());
        orderBase.setGroupCompanyId(websitFWS.getGroupCompanyId());
        orderBase.setGroupCompanyName(websitFWS.getGroupCompanyName());
        orderBase.setProductBrandId("A");
        orderBase.setProductBrandName("格力(机型品牌)");

        // 总部下来的工单统一为待接收状态-派工状态
        orderBase.setDispatchStatus("DJS");

        // 省市区单独处理
        // shPositionTopt(orderBase);
        publicService.installOrderSSQJ(orderBase);

        // 派工方式，新创建订单时 azren 名称不为空时为手动派工  （dispatch_flag派工方式 A=自动派工 B=手动派工）
        if (StringUtils.isNotEmpty(orderBase.getDispatchFlag())){
            orderBase.setDispatchFlag("B");
        } else {
            orderBase.setDispatchFlag("A");
        }

        // 售后网点处理
        websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
        orderBase.setWebsitId(websitFWS.getWebsitId());
        orderBase.setWebsitName(websitFWS.getWdmc());
        // 接收派工网点处理
        websitFWS = publicService.shshwdToptshwd(orderBase.getReceWebsitId(),orderBase);
        orderBase.setReceWebsitId(websitFWS.getWebsitId());
        orderBase.setReceWebsitName(websitFWS.getWdmc());

        // 创建网点处理
        websitFWS = publicService.shshwdToptshwd(orderBase.getCreateWebsitId(),orderBase);
        orderBase.setCreateWebsitId(websitFWS.getWebsitId());
        orderBase.setCreateWebsitName(websitFWS.getWdmc());

        // 销售网点处理
        websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(),orderBase);
        orderBase.setSaleWebsitId(websitFWS.getWebsitId());
        orderBase.setSaleWebsitName(websitFWS.getWdmc());
        // 这里为商户的系统网点编号
        orderBase.setSaleWebsitSysNumber(websitFWS.getBrandSysWebsitNumber());

        // create_sys - 创建网点系统  cjwdno
        // last_operator_websit:最近操作网点 创建单时也是创建网点 可能是销售网点，也可能是售后网点  zjczwd
        // sys_websit_number 系统网点编号 zjczwd 同上
        // 先从销售网点查
        websitFWS = publicService.shxswdToptxswd(orderBase.getCreateSys(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getCreateSys(),orderBase);
        }
        orderBase.setCreateSys(websitFWS.getWebsitId());

        websitFWS = publicService.shxswdToptxswd(orderBase.getLastOperatorWebsit(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getLastOperatorWebsit(),orderBase);
        }
        orderBase.setLastOperatorWebsit(websitFWS.getWebsitId());
        orderBase.setSysWebsitNumber(websitFWS.getBrandSysWebsitNumber());

        // jspgwdno 引入中心网点 S9219807,S9219801,S9329829
        /*if (item.getXjwdno().equals("S9219807") ||
                item.getXjwdno().equals("S9219801") ||
                item.getXjwdno().equals("S9329829")){
            websitFWS = publicService.shshwdToptshwdZX(item.getXjwdno(), orderBase);
            orderBase.setCreateWebsitId(websitFWS.getWebsitId());
            orderBase.setCreateWebsitName(websitFWS.getWdmc());
            orderBase.setReceWebsitId(websitFWS.getWebsitId());
            orderBase.setReceWebsitName(websitFWS.getWdmc());
            orderBase.setWebsitId(websitFWS.getWebsitId());
            orderBase.setWebsitName(websitFWS.getWdmc());

            Belongcompany bc = publicService.getBelongCompany(item.getXjwdno());
            websitFWS = publicService.shxswdToptxswd(bc.getBelongCompanySale(),orderBase);
            orderBase.setBrandId(websitFWS.getBrandId());
            orderBase.setBrandName(websitFWS.getBrandName());
        }

        // 结算主体重置 维修网点一般是不填销售单位的通过售后网点来查结算主体   (a.xjwdno,''),a.wxwdno) as websit_id
        if (StringUtils.isEmpty(orderBase.getBrandId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
            orderBase.setBrandId(websitFWS.getBrandId());
            orderBase.setBrandName(websitFWS.getBrandName());
        }*/

        // 格力中心的单直接下沉到总包商户(先查WebsitOrderSign 无数据再查CustomerInformationWebsit)
        /*if (item.getXjwdno().equals("S9219807") ||
                item.getXjwdno().equals("S9219801") ||
                item.getXjwdno().equals("S9329829")){
            publicService.getZXINFO(item.getXjwdno(), orderBase);
        }*/

        // 格力中心的单直接下沉到总包商户 改成下沉到总包服务商(websit_order_sign)
        if (item.getXjwdno().equals("S9219807") ||
                item.getXjwdno().equals("S9219801") ||
                item.getXjwdno().equals("S9329829")){
            publicService.getZXPTZBINFO(item.getXjwdno(), orderBase);
        }

        // 结算主体重置 维修网点一般是不填销售单位的通过售后网点来查结算主体   (a.xjwdno,''),a.wxwdno) as websit_id
        if (orderBase.getOrderType().equals(ServiceCategoryEnum.REPAIR_TYPE.getCode()) &&
                StringUtils.isEmpty(orderBase.getBrandId())){

            String wdnoq = item.getXjwdno();
            if (StringUtils.isEmpty(wdnoq)){
                wdnoq = item.getWxwdno();
            }
            websitFWS = publicService.shshwdToptshwd(wdnoq,orderBase);
            orderBase.setBrandId(websitFWS.getBrandId());
            orderBase.setBrandName(websitFWS.getBrandName());
        }
    }

    @Override
    public void setParameUpdate(Brand brand, OrderBase orderBase, ItfTblAssignDownload item) {
        WebsitFWS websitFWS = new WebsitFWS();

        // gpsaddress
        if (StringUtils.isEmpty(orderBase.getGpsAddress())){
            orderBase.setGpsAddress(publicService.getGpsaddress(orderBase.getLng(),orderBase.getLat()));
        }

        // 结算主体
        // (wxwdno、xjwdno)维修网点与派下级网点同时为空时  (cjwdno、xswdno)创建网点与销售网点都有值
        /*if (StringUtils.isNotEmpty(orderBase.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
        } else {
            websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(),orderBase);
        }*/
        websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(),orderBase);
        orderBase.setBrandId(websitFWS.getBrandId());
        orderBase.setBrandName(websitFWS.getBrandName());
        orderBase.setGroupCompanyId(websitFWS.getGroupCompanyId());
        orderBase.setGroupCompanyName(websitFWS.getGroupCompanyName());

        // 省市区单独处理
        // shPositionTopt(orderBase);
        publicService.installOrderSSQJ(orderBase);

        // 售后网点处理
        websitFWS = publicService.shshwdToptshwd(orderBase.getWebsitId(),orderBase);
        orderBase.setWebsitId(websitFWS.getWebsitId());
        orderBase.setWebsitName(websitFWS.getWdmc());
        // 接收派工网点处理
        websitFWS = publicService.shshwdToptshwd(orderBase.getReceWebsitId(),orderBase);
        orderBase.setReceWebsitId(websitFWS.getWebsitId());
        orderBase.setReceWebsitName(websitFWS.getWdmc());

        // 创建网点处理
        websitFWS = publicService.shshwdToptshwd(orderBase.getCreateWebsitId(),orderBase);
        orderBase.setCreateWebsitId(websitFWS.getWebsitId());
        orderBase.setCreateWebsitName(websitFWS.getWdmc());

        // 销售网点处理
        websitFWS = publicService.shxswdToptxswd(orderBase.getSaleWebsitId(),orderBase);
        orderBase.setSaleWebsitId(websitFWS.getWebsitId());
        orderBase.setSaleWebsitName(websitFWS.getWdmc());
        // 这里为商户的系统网点编号
        orderBase.setSaleWebsitSysNumber(websitFWS.getBrandSysWebsitNumber());

        // create_sys - 创建网点系统  cjwdno
        // last_operator_websit:最近操作网点 创建单时也是创建网点 可能是销售网点，也可能是售后网点  zjczwd
        // sys_websit_number 系统网点编号 zjczwd 同上
        // 先从销售网点查
        websitFWS = publicService.shxswdToptxswd(orderBase.getCreateSys(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getCreateSys(),orderBase);
        }
        orderBase.setCreateSys(websitFWS.getWebsitId());

        websitFWS = publicService.shxswdToptxswd(orderBase.getLastOperatorWebsit(),orderBase);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = publicService.shshwdToptshwd(orderBase.getLastOperatorWebsit(),orderBase);
        }
        orderBase.setLastOperatorWebsit(websitFWS.getWebsitId());
        orderBase.setSysWebsitNumber(websitFWS.getBrandSysWebsitNumber());
    }

    @Override
    public void shPositionTopbak(OrderBase orderBase) {
        List<BrandLbsRelate> br;
        if (StringUtils.isNotEmpty(orderBase.getStreet())){
            // 提取格力总部的省市区街道信息
            Street street = streetService.lambdaQuery()
                    .eq(Street::getStatus, 1)
                    .eq(Street::getProvince, orderBase.getProvince())
                    .eq(Street::getCity, orderBase.getCity())
                    .eq(Street::getArea, orderBase.getArea())
                    .eq(Street::getStreet, orderBase.getStreet())
                    .last("limit 1")
                    .one();
            // 对应出平台的省市区街道信息 无对应关系时依总部的为准
            if (street!=null){
                getPosition(orderBase, street);
            } else {
                // 总部区必选项 按区再次查询
                List<Area> area = areaService.lambdaQuery()
                        .eq(Area::getProvince, orderBase.getProvince())
                        .eq(Area::getCity, orderBase.getCity())
                        .eq(Area::getArea, orderBase.getArea()).list();

                Street st = new Street();
                if (CollectionUtils.isNotEmpty(area)){
                    BeanUtils.copyProperties(area.get(0),st);
                    getPosition(orderBase, st);
                    orderBase.setStreet("");
                }
            }
        } else {
            // 总部区必选项
            List<Area> area = areaService.lambdaQuery()
                    .eq(Area::getProvince, orderBase.getProvince())
                    .eq(Area::getCity, orderBase.getCity())
                    .eq(Area::getArea, orderBase.getArea()).list();
            Street st = new Street();
            if (CollectionUtils.isNotEmpty(area)){
                BeanUtils.copyProperties(area.get(0),st);
                getPosition(orderBase, st);
                orderBase.setStreet("");
            }
        }


        // getLocationByAddress
        if (StringUtils.isEmpty(orderBase.getLng()) || StringUtils.isEmpty(orderBase.getLat())){
            String add = "";
            if (StringUtils.isNotEmpty(orderBase.getProvince())){
                add = add + orderBase.getProvince();
            }
            if (StringUtils.isNotEmpty(orderBase.getCity())){
                add = add + orderBase.getCity();
            }
            if (StringUtils.isNotEmpty(orderBase.getArea())){
                add = add + orderBase.getArea();
            }
            if (StringUtils.isNotEmpty(orderBase.getStreet())){
                add = add + orderBase.getStreet();
            }
            if (StringUtils.isEmpty(add)){
                return;
            }
            String lngLat = publicService.getLocationByAddress(add);
            if (StringUtils.isEmpty(lngLat)){
                return;
            }
            List<String> result = Arrays.asList(lngLat.split(","));

            orderBase.setLng(result.get(0));
            orderBase.setLat(result.get(1));
        }
    }

    // 平台工单地理位置转成商户工单地理位置
    @Override
    public void tpPositionTosh(ItfTblAzAssignLcLsUpload lcls){
        List<BrandLbsRelate> br;
        // 街道处理
        if (StringUtils.isNotEmpty(lcls.getXzhenid())){
            // 提取街道信息
            br = brandLbsRelateService.lambdaQuery()
                    .eq(BrandLbsRelate::getLbsStreetId, lcls.getXzhenid())
                    .eq(BrandLbsRelate::getDel,0)
                    .isNotNull(BrandLbsRelate::getStreetId).list();
            // 存在 即找到了省市区街道ID
            if (br !=null && br.size()>0){
                lcls.setSfenid(Long.valueOf(br.get(0).getProvinceId()));
                lcls.setCshiid(Long.valueOf(br.get(0).getCityId()));
                lcls.setXianid(Long.valueOf(br.get(0).getAreaId()));
                lcls.setXzhenid(br.get(0).getStreetId());

                Street street = streetService.lambdaQuery()
                        .eq(Street::getStatus, 1)
                        .eq(Street::getProvince, lcls.getSfenid())
                        .eq(Street::getCity, lcls.getCshiid())
                        .eq(Street::getArea, lcls.getXianid())
                        .eq(Street::getStreet, lcls.getXzhenid())
                        .last("limit 1")
                        .one();
                // 这里一定有
                if (street !=null){
                    lcls.setSfen(street.getProvince());
                    lcls.setCshi(street.getCity());
                    lcls.setXian(street.getArea());
                    lcls.setXzhen(street.getStreet());
                    return;
                }
            }
        }

        // 换街道信息时单独处理
        // 区为必选
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getLbsProvinceId, lcls.getSfenid())
                .eq(BrandLbsRelate::getDel,0)
                .isNotNull(BrandLbsRelate::getProvinceId).list();
        if (br !=null && br.size()>0){
            lcls.setSfenid(Long.valueOf(br.get(0).getProvinceId()));
        } else {
            // 省信息无时不再处理，以平台的为准
            return;
        }
        //市
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getLbsCityId, lcls.getCshiid())
                .eq(BrandLbsRelate::getDel,0)
                .isNotNull(BrandLbsRelate::getCityId).list();
        if (br !=null && br.size()>0){
            lcls.setCshiid(Long.valueOf(br.get(0).getCityId()));
        }
        //区
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getLbsDistrictId, lcls.getXianid())
                .eq(BrandLbsRelate::getDel,0)
                .isNotNull(BrandLbsRelate::getAreaId).list();
        if (br !=null && br.size()>0){
            lcls.setXianid(Long.valueOf(br.get(0).getAreaId()));
        }

        // 总部区必选
        Area area = areaService.lambdaQuery()
                .eq(Area::getAreaId, lcls.getXianid())
                .last("limit 1")
                .one();
        if (area !=null){
            lcls.setSfen(area.getProvince());
            lcls.setCshi(area.getCity());
            lcls.setXian(area.getArea());
        }
    }

    private void getPosition(OrderBase orderBase, Street street) {
        List<BrandLbsRelate> br;
        //省市区，有时无街道，这里不提取街道信息
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getDel,0)
                .eq(BrandLbsRelate::getProvinceId, street.getProvinceId())
                .eq(BrandLbsRelate::getCityId, street.getCityId())
                .eq(BrandLbsRelate::getAreaId, street.getAreaId())
                .isNotNull(BrandLbsRelate::getLbsProvinceId)
                .isNotNull(BrandLbsRelate::getLbsCityId)
                .isNotNull(BrandLbsRelate::getLbsDistrictId).list();
        if (br !=null && br.size()>0){
            //省
            orderBase.setProvinceId(br.get(0).getLbsProvinceId());
            orderBase.setProvince(br.get(0).getLbsProvinceName());
            //市
            orderBase.setCityId(br.get(0).getLbsCityId());
            orderBase.setCity(br.get(0).getLbsCityName());
            //区
            orderBase.setAreaId(br.get(0).getLbsDistrictId());
            orderBase.setArea(br.get(0).getLbsDistrictName());
        } else {
            // 找不到依总部的为准
            if (StringUtils.isEmpty(orderBase.getProvinceId())){
                orderBase.setProvinceId(street.getProvinceId());
            }
            if (StringUtils.isEmpty(orderBase.getCityId())){
                orderBase.setCityId(street.getCityId());
            }
            if (StringUtils.isEmpty(orderBase.getAreaId())){
                orderBase.setAreaId(street.getAreaId());
            }
            if (StringUtils.isEmpty(orderBase.getStreetId())){
                orderBase.setStreetId(street.getStreetId());
            }
            return;
        }

        if (StringUtils.isNotEmpty(orderBase.getStreet())){
            //街道单独处理
            br = brandLbsRelateService.lambdaQuery()
                    .eq(BrandLbsRelate::getDel,0)
                    .eq(BrandLbsRelate::getProvinceId, street.getProvinceId())
                    .eq(BrandLbsRelate::getCityId, street.getCityId())
                    .eq(BrandLbsRelate::getAreaId, street.getAreaId())
                    .eq(BrandLbsRelate::getStreetId,street.getStreetId())
                    .isNotNull(BrandLbsRelate::getLbsProvinceId)
                    .isNotNull(BrandLbsRelate::getLbsCityId)
                    .isNotNull(BrandLbsRelate::getLbsDistrictId)
                    .isNotNull(BrandLbsRelate::getLbsStreetId).list();
            if (br !=null && br.size()>0){
                orderBase.setStreetId(br.get(0).getLbsStreetId());
                orderBase.setStreet(br.get(0).getLbsStreetName());
            }
        }
    }

    private void getPCABack(OrderBase orderBase, Street street) {
        List<BrandLbsRelate> br;
        //省
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getProvinceId, street.getProvinceId())
                .eq(BrandLbsRelate::getDel,0)
                .isNotNull(BrandLbsRelate::getLbsProvinceId).list();
        if (br !=null && br.size()>0){
            orderBase.setProvinceId(br.get(0).getLbsProvinceId());
            orderBase.setProvince(br.get(0).getLbsProvinceName());
        }
        //市
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getCityId, street.getCityId())
                .eq(BrandLbsRelate::getDel,0)
                .isNotNull(BrandLbsRelate::getLbsCityId).list();
        if (br !=null && br.size()>0){
            orderBase.setCityId(br.get(0).getLbsCityId());
            orderBase.setProvince(br.get(0).getLbsCityName());
        }
        //区
        br = brandLbsRelateService.lambdaQuery()
                .eq(BrandLbsRelate::getAreaId, street.getAreaId())
                .eq(BrandLbsRelate::getDel,0)
                .isNotNull(BrandLbsRelate::getLbsDistrictId).list();
        if (br !=null && br.size()>0){
            orderBase.setAreaId(br.get(0).getLbsDistrictId());
            orderBase.setArea(br.get(0).getLbsDistrictName());
        }
    }

}
