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

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.constant.*;
import com.zfire.jiasm.syncdata.dao.mapper.OJTGUploadTaskListMapper;
import com.zfire.jiasm.syncdata.parameter.InstallUpdatePar;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.response.*;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.service.UploadService;
import com.zfire.jiasm.syncdata.utils.MyBeanUtils;
import com.zfire.jiasm.syncdata.utils.RedisUtil;
import com.zfire.jiasm.syncdata.utils.ResponseHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static jdk.nashorn.internal.objects.NativeString.substr;

@Slf4j
@Service
public class PublicServiceImpl implements PublicService {
    @Value("${lbs.web.key}")
    private String lbsWebKey;

    private static String ALISMS_NOHEARD = "尊敬的顾客您好，我司于%s接到您的工单信息，期间联系您的电话无人接听，如有需要可致电网点%s，网点服务热线%s，我司将竭诚为您服务，祝您生活愉快！";
    private static String ALISMS_RESERVE = "尊敬的客户，您好！师傅上门时间为%s，服务网点电话：%s，如本次上门时间与您的安排有冲突，请拨打服务网点电话联系，我们将竭诚为您服务！";


    @Resource
    OrderWorkerService orderWorkerService;
    @Resource
    SysDictRefService sysDictRefService;
    @Resource
    WebsitOrderSignService websitOrderSignService;
    @Resource
    CustomerInformationWebsitService customerInformationWebsitService;
    @Resource
    BrandWebsitService brandWebsitService;
    @Resource
    WebsitService websitService;
    @Resource
    OutJiasmToGreeUploadTaskListService outJiasmToGreeUploadTaskListService;
    @Resource
    OJTGUploadTaskListMapper ojtgUploadTaskListMapper;
    @Resource
    OrderRepairOperatingLogService orderRepairOperatingLogService;
    @Resource
    SysDictService sysDictService;
    @Resource
    WebsitServiceCategoryDetailService websitServiceCategoryDetailService;
    @Resource
    BaseService baseService;
    @Resource
    ItfMessagehuifangentityService itfMessagehuifangentityService;
    @Resource
    SmsRecordService  smsRecordService;
    @Resource
    FaOrderSmsService faOrderSmsService;
    @Resource
    OrderBaseService orderbaseService;
    @Resource
    WorkerBrandAccountService workerBrandAccountService;
    @Resource
    WorkerService workerService;
    @Autowired
    SysVariableConfigService sysVariableConfigService;
    @Autowired
    SatisfactionListService satisfactionListService;
    @Autowired
    RedisUtil redis;
    @Resource
    ItfTblWangdianWxgDownloadService itfTblWangdianWxgDownloadService;
    @Resource
    StreetService streetService;
    @Resource
    AreaService areaService;
    @Resource
    BrandLbsRelateService brandLbsRelateService;
    @Autowired
    LbsAmapService lbsAmapService;
    @Autowired
    AdminWebsitService adminWebsitService;
    @Resource
    BelongcompanyService belongcompanyService;
    @Resource
    private OrderBaseService orderBaseService;
    @Resource
    private PublicService publicService;
    @Resource
    private SynUploadWebsitService synUploadWebsitService;
    @Resource
    private UploadService uploadService;
    @Resource
    WebsitWorkerService websitWorkerService;
    @Resource
    InsureService insureService;
    @Resource
    OrderDetailFileService orderDetailFileService;
    @Resource
    ProductService productService;
    @Resource
    OrderAppraiseService orderAppraiseService;





    @Override
    public OrderWorker getOrderWorker(OrderBase orderBase){
        if (StringUtils.isNotEmpty(orderBase.getWorkerName())){
            OrderWorker orderWorker = getWorkerInfo(orderBase);
            if (orderWorker !=null){
                orderWorkerService.save(orderWorker);
                return orderWorker;
            }
        }
        return null;
    }

    @Override
    public OrderWorker getOrderWorkerApi(OrderBase orderBase){
        if (StringUtils.isNotEmpty(orderBase.getWorkerName())){
            return getWorkerInfo(orderBase);
        }

        return null;
    }

    private OrderWorker getWorkerInfo(OrderBase orderBase) {
        OrderWorker orderWorker = new OrderWorker();
        orderWorker.setOrderBaseId(orderBase.getId());
        orderWorker.setWorkerId(orderBase.getWorkerId());
        orderWorker.setWorkerName(orderBase.getWorkerName());
        orderWorker.setWorkerMobile(orderBase.getWorkerMobile());
        orderWorker.setWorkerIdcard(orderBase.getWorkerIdcard());
        orderWorker.setIsMaster(Boolean.TRUE);
        orderWorker.setBrandPgid(orderBase.getPgid());
        orderWorker.setPgguid(orderBase.getPgguid());
        orderWorker.setCreateTime(orderBase.getCreateTime());
        orderWorker.setCreateBy(orderBase.getCreateBy());
        orderWorker.setOutWorkerSource(Boolean.FALSE);
        return orderWorker;
    }

    @Override
    public List<OrderWorker> getWorker(String workerId,String orderBaseId) {
        List<OrderWorker> ows = orderWorkerService.lambdaQuery()
                .eq(OrderWorker::getOrderBaseId,orderBaseId)
                .eq(OrderWorker::getOutWorkerSource,Boolean.FALSE)
                .eq(OrderWorker::getIsMaster,Boolean.TRUE).list();
        return ows;
    }

    @Override
    public void deleteWorker(String orderBaseId){
        QueryWrapper del = new QueryWrapper<OrderWorker>()
                .eq("order_base_id", orderBaseId);

        orderWorkerService.remove(del);
    }


    @Override
    public Map<String, List<SysDictRef>> getStringListMap(Brand brand){
        List<SysDictRef> sysDictRef;
        Map<String, List<SysDictRef>> sysDictRefDLMap = new HashMap<>();
        sysDictRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .list();
        sysDictRefDLMap = sysDictRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefDLMap;
    }

    @Override
    public Map<String, List<SysDictRef>> getListMap(Brand brand){
        List<SysDictRef> sysDictxlRef;
        Map<String, List<SysDictRef>> sysDictRefXLMap = new HashMap<>();
        sysDictxlRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode())
                .list();
        sysDictRefXLMap = sysDictxlRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefXLMap;
    }

    @Override
    public Map<String, List<SysDictRef>> getMap(Brand brand){
        List<SysDictRef> sysDictxilieRef;
        Map<String, List<SysDictRef>> sysDictRefXiLieMap = new HashMap<>();
        sysDictxilieRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_SERIES.getCode())
                .list();
        sysDictRefXiLieMap = sysDictxilieRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefXiLieMap;
    }


    @Override
    public Map<String, List<SysDictRef>> getStringListMapXq(Brand brand){
        List<SysDictRef> sysDictRef;
        Map<String, List<SysDictRef>> sysDictRefDLMap = new HashMap<>();
        sysDictRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_DEMAND_TYPE.getCode())
                .list();
        sysDictRefDLMap = sysDictRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefDLMap;
    }

    @Override
    public Map<String, List<SysDictRef>> getListMapXq(Brand brand){
        List<SysDictRef> sysDictxlRef;
        Map<String, List<SysDictRef>> sysDictRefXLMap = new HashMap<>();
        sysDictxlRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_DEMAND_SMALL_TYPE.getCode())
                .list();
        sysDictRefXLMap = sysDictxlRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefXLMap;
    }

    // 提取品牌商对应的需求小类信息
    @Override
    public Map<String, List<SysDictRef>> getListMapXqtoPP(Brand brand){
        List<SysDictRef> sysDictxlRef;
        Map<String, List<SysDictRef>> sysDictRefXLMap = new HashMap<>();
        sysDictxlRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_DEMAND_SMALL_TYPE.getCode())
                .list();
        sysDictRefXLMap = sysDictxlRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getDictCode));
        return sysDictRefXLMap;
    }

    // 提取品牌商对应的需求类别信息
    @Override
    public Map<String, List<SysDict>> getStringListMapXqLb(){
        Map<String, List<SysDict>> result = new HashMap<>();

        List<SysDict> sysDictxl = sysDictService.lambdaQuery()
                .select(SysDict::getDictCode,SysDict::getDictValue)
                .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_DEMAND_ASK_CATEGORY.getCode())
                .list();

        result = sysDictxl.stream().
                collect(Collectors.groupingBy(SysDict::getDictCode));
        return result;
    }



    @Override
    public String getSHWDPhone(String websitNo){
        Websit websit = websitService.lambdaQuery()
                .eq(Websit::getWebsitId,websitNo)
                .last("limit 1")
                .one();
        if (websit != null){
            if (StringUtils.isNotEmpty(websit.getHotline())){
                return websit.getHotline();
            } else if (StringUtils.isNotEmpty(websit.getOfficePhone()))
                return websit.getOfficePhone();
        }
        return "";
    }

    private WebsitFWS WebsitToSH(String websitNo, String brandId){
        WebsitFWS resultData = new WebsitFWS();

        resultData.setWebsitId("");
        resultData.setWdmc("");
        resultData.setWdlxdh("");

        // 先从售后网点查
        WebsitOrderSign wos = websitOrderSignService.lambdaQuery()
                .eq(WebsitOrderSign::getBrandWebsitId, websitNo)
                .eq(WebsitOrderSign::getBrandId, brandId)
                .last("limit 1")
                .one();
        if (wos != null){
            resultData.setWebsitId(wos.getWebsitId());
        } else {
            CustomerInformationWebsit ciwx = customerInformationWebsitService.lambdaQuery()
                    .eq(CustomerInformationWebsit::getRefWebsitId, websitNo)
                    .eq(CustomerInformationWebsit::getBrandId, brandId)
                    .last("limit 1")
                    .one();
            if (ciwx !=null){
                resultData.setWebsitId(ciwx.getWebsitId());
            }
        }

        // 提取网点名称
        if (StringUtils.isNotEmpty(resultData.getWebsitId())){
            Websit websit = websitService.lambdaQuery()
                    .eq(Websit::getWebsitId,resultData.getWebsitId())
                    .last("limit 1")
                    .one();
            if (websit != null){
                resultData.setWdmc(websit.getWebsitName());
                if (StringUtils.isNotEmpty(websit.getHotline())){
                    resultData.setWdlxdh(websit.getHotline());
                } else if (StringUtils.isNotEmpty(websit.getOfficePhone()))
                    resultData.setWdlxdh(websit.getOfficePhone());
            }
        }

        return resultData;
    }

    // 商户网点对应品牌网点
    private WebsitPP WebsitToPP(String websitId, String brandId){
        WebsitPP resultData = new WebsitPP();

        resultData.setXtwdno("");
        resultData.setWdno("");
        resultData.setWdmc("");

        // 先从售后网点查
        WebsitOrderSign wos = websitOrderSignService.lambdaQuery()
                .eq(WebsitOrderSign::getWebsitId, websitId)
                .eq(WebsitOrderSign::getBrandId, brandId)
                .last("limit 1")
                .one();
        if (wos != null){
            resultData.setWdno(wos.getBrandWebsitId());
            resultData.setWdmc(wos.getBrandWebsitName());
        } else {
            // 查看销售网点
            CustomerInformationWebsit ciwx = customerInformationWebsitService.lambdaQuery()
                    .eq(CustomerInformationWebsit::getWebsitId, websitId)
                    .eq(CustomerInformationWebsit::getBrandId, brandId)
                    .last("limit 1")
                    .one();
            if (ciwx !=null){
                resultData.setWdno(ciwx.getRefWebsitId());
                resultData.setWdmc(ciwx.getRefWebsitName());
            }
        }

        // 提取系统网点编号
        if (StringUtils.isNotEmpty(resultData.getWdno())){
            BrandWebsit bw = brandWebsitService.lambdaQuery()
                    .eq(BrandWebsit::getBrandWebsitNumber, resultData.getWdno())
                    .eq(BrandWebsit::getGroupCompanyId, brandId)
                    .last("limit 1")
                    .one();
            if (bw != null){
                resultData.setXtwdno(bw.getSysWebsitNumber());
            }

        }

        return resultData;
    }


    @Override
    public OutJiasmToGreeUploadTaskList getUploadTaskListLog(String orderNo, String id, Date updateTime,
                                                             String itemTableName, String itemOrderType, int taskType) {
        // 提取目志的类型
        OrderRepairOperatingLog rlog = orderRepairOperatingLogService.lambdaQuery()
                .eq(OrderRepairOperatingLog::getId,id)
                .last("limit 1")
                .one();

        OutJiasmToGreeUploadTaskList add = new OutJiasmToGreeUploadTaskList();
        add.setUploadTaskType(taskType);
        add.setOrderNo(orderNo);
        add.setId(id);
        add.setTaskExtraInfo(rlog.getType());
        add.setTableName(itemTableName);
        add.setOrderType(itemOrderType);
        add.setSynReqTime(new Date());
        add.setUpdatetime(updateTime);
        return add;
    }

    @Override
    public OutJiasmToGreeUploadTaskList getUploadTaskList(String orderNo, String id, Date updateTime,
                                                          String itemTableName, String itemOrderType, int taskType) {
        OutJiasmToGreeUploadTaskList add = new OutJiasmToGreeUploadTaskList();
        add.setUploadTaskType(taskType);
        add.setOrderNo(orderNo);
        add.setId(id);
        add.setTableName(itemTableName);
        add.setOrderType(itemOrderType);
        add.setSynReqTime(new Date());
        add.setUpdatetime(updateTime);
        return add;
    }

    @Override
    public OutJiasmToGreeUploadTaskList getUploadTaskList(String orderNo, String id,
                                                          String itemTableName, String itemOrderType, int taskType) {
        OutJiasmToGreeUploadTaskList add = new OutJiasmToGreeUploadTaskList();
        add.setUploadTaskType(taskType);
        add.setOrderNo(orderNo);
        add.setId(id);
        add.setTableName(itemTableName);
        add.setOrderType(itemOrderType);
        add.setSynReqTime(new Date());
        return add;
    }

    @Override
    public String getfsma(String phone) {
        Random random = new Random(System.currentTimeMillis());
        String fsma="";
        List<ItfMessagehuifangentity> r = new ArrayList<>();


        // 同一用户最近两个月内产生的发送码不能相同 itfMessagehuifangentityService
        while (Boolean.TRUE){
            r.clear();
            fsma = String.valueOf(random.nextInt(10000));
            if (fsma.length()<4){
                if (fsma.length()==3){
                    fsma = fsma + "9";
                } else if (fsma.length()==2){
                    fsma = fsma + "99";
                } else {
                    fsma = fsma + "`999";
                }
            }
            if (StringUtils.isEmpty(phone.trim())){
                break;
            }
            r = itfMessagehuifangentityService.lambdaQuery()
                    .eq(ItfMessagehuifangentity::getSjid,phone)
                    .eq(ItfMessagehuifangentity::getFsma,fsma)
                    .ge(ItfMessagehuifangentity::getCrdt,stepMonth(new Date(),-2)).list();
            if (CollectionUtils.isEmpty(r)){
                r.clear();
                break;
            }
        }

        return fsma;
    }

    @Override
    public Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }

    @Override
    public void updateUploadTaskListStatus(String orderNo, Long synReqId,Integer synStatus,Integer uploadTaskType){
        OutJiasmToGreeUploadTaskList upoutgultl = new OutJiasmToGreeUploadTaskList();
        upoutgultl.setSynReqId(synReqId);
        upoutgultl.setSynStatus(synStatus);
        upoutgultl.setSynTime(new Date());
        outJiasmToGreeUploadTaskListService.updateById(upoutgultl);

        // 批量修改
        InstallUpdatePar request = new InstallUpdatePar();
        request.setOrderNo(orderNo);
        request.setUploadTaskType(uploadTaskType);
        request.setSynReqId(synReqId);
        ojtgUploadTaskListMapper.installUpdateuploadTaskList(request);
    }

    @Override
    public void updateUploadTaskListInfo(Long synReqId,String msg){
        OutJiasmToGreeUploadTaskList upoutgultl = new OutJiasmToGreeUploadTaskList();
        upoutgultl.setSynReqId(synReqId);
        upoutgultl.setSynTime(new Date());
        upoutgultl.setSynErrMsg(msg);
        outJiasmToGreeUploadTaskListService.updateById(upoutgultl);
    }

    @Override
    public void updateUploadTaskLisError(OutJiasmToGreeUploadTaskList ojtutl, String msg){
        ojtutl.setSynTime(new Date());
        ojtutl.setSynErrMsg("other");
        ojtutl.setSynTimes(ojtutl.getSynTimes()+1);

        if (msg!=null){
            if (msg.length()>255){
                ojtutl.setSynErrMsg(msg.substring(0,255));
            } else {
                ojtutl.setSynErrMsg(msg);
            }
        }
        ojtutl.updateById();
    }

    /**
     * 商户网点信息
     * @param websitNo  商户网点编号
     * @param type      商户网点类型(销售或售后)
     */
    @Override
    public WebsitSH merchantWebsitInfoX(String websitNo,String type){
        WebsitSH resultData = new WebsitSH();

        if (StringUtils.isEmpty(websitNo)){
            return resultData;
        }

        // 提取格力集团信息
        Brand brandRec = baseService.getBrand();

        BrandWebsit brandWebsit = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getGroupCompanyId,brandRec.getBrandId())
                .eq(BrandWebsit::getType,type)
                .eq(BrandWebsit::getBrandWebsitNumber,websitNo)
                .last("limit 1")
                .one();

        if (brandWebsit !=null){
            resultData.setWdId(brandWebsit.getBrandWebsitNumber());
            resultData.setWdmc(brandWebsit.getBrandWebsitName());
            resultData.setBrandSysWebsitNumber(brandWebsit.getSysWebsitNumber());
        }

        return resultData;
    }


    /**
     * 商户销售网点对应平台销售网点
     * @param websitNo  商户销售网点编号
     * @param order  商户工单类别、商户工单类型
     * @return
     */
    @Override
    public WebsitFWS shxswdToptxswd(String websitNo, OrderBase order){
        WebsitFWS resultData = new WebsitFWS();
        if (StringUtils.isEmpty(websitNo)){
            return resultData;
        }
        // 提取平台的销售网点信息 无信息不处理
        List<CustomerInformationWebsit> cIWebsitList = customerInformationWebsitService.lambdaQuery()
                .eq(CustomerInformationWebsit::getRefWebsitId,websitNo).list();
        if (CollectionUtils.isNotEmpty(cIWebsitList)){
            if (cIWebsitList.size()==1){
                // 这里的网点编号也是平台的商户编号
                resultData.setWebsitId(cIWebsitList.get(0).getWebsitId());
                resultData.setBrandSysWebsitNumber(cIWebsitList.get(0).getRefSysWebsitNumber());
                getNameAndHotline(resultData);
                resultData.setBrandId(cIWebsitList.get(0).getBrandId());
                resultData.setBrandName(cIWebsitList.get(0).getBrandName());
                return resultData;
            }

            List<CustomerInformationWebsit> ciws = new ArrayList<>();
            // 看服务商销售网点是否服务工单大类
            for (CustomerInformationWebsit item:cIWebsitList){
                WebsitServiceCategoryDetail wosinfo = getWebCategory(item.getBrandId(),item.getWebsitId(),order.getMainId(),order.getOrderType());
                if (wosinfo !=null){
                    ciws.add(item);
                }
            }

            if (CollectionUtils.isEmpty(ciws)){
                return resultData;
            }
            if (ciws.size()==1){
                resultData.setWebsitId(ciws.get(0).getWebsitId());
                getNameAndHotline(resultData);
                resultData.setBrandId(ciws.get(0).getBrandId());
                resultData.setBrandName(ciws.get(0).getBrandName());
                resultData.setBrandSysWebsitNumber(ciws.get(0).getRefSysWebsitNumber());
                return resultData;
            }

            // 多网点时再进行位置过滤
            CustomerInformationWebsit ci = getCustomerInformationWebsit(websitNo,ciws);
            if (ci!=null){
                resultData.setWebsitId(ci.getWebsitId());
                getNameAndHotline(resultData);
                resultData.setBrandId(ci.getBrandId());
                resultData.setBrandName(ci.getBrandName());
                resultData.setBrandSysWebsitNumber(ci.getRefSysWebsitNumber());
                return resultData;
            }

        }
        return resultData;
    }

    /**
     * 商户售后中心网点对应平台总包商户网点
     * @param websitNo  商户售后中心网点编号
     * @param order    商户工单类别
     * @return
     */
    public WebsitFWS shshzxwdToptzbwd(String websitNo, OrderBase order){
        WebsitFWS resultData = new WebsitFWS();
        if (StringUtils.isEmpty(websitNo)){
            return resultData;
        }
        // 提取平台的销售网点信息 无信息不处理
        List<CustomerInformationWebsit> cIWebsitList = customerInformationWebsitService.lambdaQuery()
                .eq(CustomerInformationWebsit::getRefAfterSaleWebsitId,websitNo).list();
        if (CollectionUtils.isNotEmpty(cIWebsitList)){
            if (cIWebsitList.size()==1){
                // 这里的网点编号也是平台的商户编号
                resultData.setWebsitId(cIWebsitList.get(0).getWebsitId());
                resultData.setBrandSysWebsitNumber(cIWebsitList.get(0).getRefSysWebsitNumber());
                getNameAndHotline(resultData);
                resultData.setBrandId(cIWebsitList.get(0).getBrandId());
                resultData.setBrandName(cIWebsitList.get(0).getBrandName());
                return resultData;
            }

            List<CustomerInformationWebsit> ciws = new ArrayList<>();
            // 看服务商总包商户网点是否服务工单大类
            for (CustomerInformationWebsit item:cIWebsitList){
                WebsitServiceCategoryDetail wosinfo = getWebCategory(item.getBrandId(),item.getWebsitId(),order.getMainId(),order.getOrderType());
                if (wosinfo !=null){
                    ciws.add(item);
                }
            }

            if (CollectionUtils.isEmpty(ciws)){
                return resultData;
            }
            if (ciws.size()==1){
                resultData.setWebsitId(ciws.get(0).getWebsitId());
                getNameAndHotline(resultData);
                resultData.setBrandId(ciws.get(0).getBrandId());
                resultData.setBrandName(ciws.get(0).getBrandName());
                resultData.setBrandSysWebsitNumber(ciws.get(0).getRefSysWebsitNumber());
                return resultData;
            }

            // 多网点时再进行位置过滤
            CustomerInformationWebsit ci = getCustomerInformationWebsit(websitNo,ciws);
            if (ci!=null){
                resultData.setWebsitId(ci.getWebsitId());
                getNameAndHotline(resultData);
                resultData.setBrandId(ci.getBrandId());
                resultData.setBrandName(ci.getBrandName());
                resultData.setBrandSysWebsitNumber(ci.getRefSysWebsitNumber());
                return resultData;
            }

        }
        return resultData;
    }

    /**
     * 商户售后网点对应平台售后网点
     * @param websitNo  商户售后网点编号
     * @param order 商户工单大类、商户工单类型
     * @return
     */
   @Override
   public WebsitFWS shshwdToptshwd(String websitNo, OrderBase order){
       WebsitFWS resultData = new WebsitFWS();
       long tid = Thread.currentThread().getId();

       log.info("商户售后网点到平台服务商网点：["+tid+"]["+websitNo+"]["+order.getMainId()+"]");

       if (StringUtils.isEmpty(websitNo)){
           return resultData;
       }

       // 提取商户售后网点对应的平台售后网点信息 存在一对多的关系
       List<WebsitOrderSign> wos = getWebsitOrderSign(websitNo);
       if (CollectionUtils.isNotEmpty(wos)){
           // 找到一条记录时取网点名称直接返回
           if (wos.size() == 1){
               MyBeanUtils.copyProperties(wos.get(0),resultData);
               /*resultData.setWebsitId(wos.get(0).getWebsitId());
               // 网点名称及服务热线
               getNameAndHotline(resultData);
               resultData.setBrandSysWebsitNumber(wos.get(0).getBrandSysWebsitNumber());
               // 结算主体
               resultData.setBrandId(wos.get(0).getBrandId());
               resultData.setBrandName(wos.get(0).getBrandName());*/
               MyBeanUtils.copyProperties(wos.get(0),resultData);
               getNameAndHotline(resultData);

               log.info("商户售后网点到平台服务商网点锁定结果：["+tid+"]["+resultData.getWebsitId()+"]");
               return resultData;
           }

           // 多记录时根据网点服务的产品大类是否满足订单的大类(category) 进行取舍
           WebsitOrderSign wost = websitCategory(websitNo,wos, order.getMainId(), tid,order.getOrderType());
           // 无对应关系先工单落表，不再对应
           if (wost != null){
               /*resultData.setWebsitId(wost.getWebsitId());
               getNameAndHotline(resultData);
               resultData.setBrandSysWebsitNumber(wost.getBrandSysWebsitNumber());
               resultData.setBrandId(wost.getBrandId());
               resultData.setBrandName(wost.getBrandName());*/
               MyBeanUtils.copyProperties(wos.get(0),resultData);
               getNameAndHotline(resultData);
           }
       } else {
           log.info("商户售后网点到平台服务商网点："+tid+"]["+websitNo+"]无对应关系");
       }
       return resultData;
   }

    /**
     * 平台销售网点对应商户销售网点
     * @param websitNo  平台销售网点编号
     * @param category  工单类别
     * @return
     */
    @Override
    public WebsitSH ptxswdToshxswd(String websitNo, String category){
        WebsitSH resultData = new WebsitSH();
        if (StringUtils.isEmpty(websitNo)){
            return resultData;
        }

        // 提取平台的销售网点信息 无信息不处理
        List<CustomerInformationWebsit> cIWebsitList = customerInformationWebsitService.lambdaQuery()
                .eq(CustomerInformationWebsit::getWebsitId,websitNo).list();
        if (CollectionUtils.isNotEmpty(cIWebsitList)){
            if (cIWebsitList.size()==1){
                return merchantWebsitInfoX(cIWebsitList.get(0).getRefWebsitId(),
                        InterFaceTypeEnum.XS_WEBSIT_TYPE.getCode());
            }
            // 看商户销售网点是否服务工单大类,找到即返回
            for (CustomerInformationWebsit item:cIWebsitList) {
                BrandWebsit bw = brandWebsitService.lambdaQuery()
                        .eq(BrandWebsit::getGroupCompanyId, item.getGroupCompanyId())
                        .eq(BrandWebsit::getBrandWebsitNumber, item.getRefWebsitId())
                        .like(BrandWebsit::getSplb,"%,"+category+",%")
                        .last("limit 1")
                        .one();
                if (bw != null) {
                    return merchantWebsitInfoX(bw.getBrandWebsitNumber(),
                            InterFaceTypeEnum.XS_WEBSIT_TYPE.getCode());
                }
            }
        }

        return resultData;
    }

    /**
     * 平台售后网点对应商户售后网点
     * @param websitNo  平台售后网点编号
     * @param category  工单
     * @return
     */
    @Override
    public WebsitSH  ptshwdToshshwd(String websitNo, String category) {
        WebsitSH resultData = new WebsitSH();
        List<BrandWebsit> wosn = new ArrayList<>();

        if (StringUtils.isEmpty(websitNo)) {
            return resultData;
        }

        // 提取平台售后网点对应的商户售后网点信息 存在一对多的关系
        List<WebsitOrderSign> wos = new ArrayList<>();
        wos = websitOrderSignService.lambdaQuery()
                .eq(WebsitOrderSign::getWebsitId, websitNo)
                .list();

        if (CollectionUtils.isNotEmpty(wos)) {
            // 找到一条记录时直接返回
            if (wos.size() == 1) {
                return merchantWebsitInfoX(wos.get(0).getBrandWebsitId(),
                        InterFaceTypeEnum.SH_WEBSIT_TYPE.getCode());
            }

            // 多记录时根据商户网点服务的产品大类是否满足订单的大类(category) 进行取舍
            for (WebsitOrderSign item : wos) {
                BrandWebsit bw = brandWebsitService.lambdaQuery()
                        .eq(BrandWebsit::getBrandWebsitNumber, item.getBrandWebsitId())
                        .like(BrandWebsit::getSplb,"%,"+category+",%")
                        .last("limit 1")
                        .one();
                if (bw != null) {
                    wosn.add(bw);
                }
            }
            // 这里一般是存在的
            if (wosn.size() == 0) {
                return resultData;
            }

            // 已做限制 同一个大类不会归属两个以上服务网点
            return merchantWebsitInfoX(wosn.get(0).getBrandWebsitNumber(),
                    InterFaceTypeEnum.SH_WEBSIT_TYPE.getCode());
        }

        return resultData;
    }


    /**
     * 向发送短信表添加数据 工单类型，短信类型
     * @return true写入成功，false写入失败
     */
    @Override
    public Boolean addSmsRecord(ItfMessagehuifangentity addms,String orderType,int type){
        SmsRecord record = new SmsRecord();
        // 插入前查看是否已存在，存在不再插入
        List<SmsRecord> sr = smsRecordService.lambdaQuery()
                .eq(SmsRecord::getOrderBaseId,addms.getOrderBaseId())
                .eq(SmsRecord::getOrderType,orderType)
                .eq(SmsRecord::getType,type)
                .eq(SmsRecord::getPhone,addms.getSjid())
                .eq(SmsRecord::getFsma,addms.getFsma()).list();
        if (CollectionUtils.isNotEmpty(sr)){
            return Boolean.TRUE;
        }

        // 生成实体对像
        getSmsRecord(addms, orderType, type, record);

        return smsRecordService.save(record);
    }

    // ID同预约改约表ID相同，方便查找
    private Boolean addSRecord(OrderReAppointment rea){
        // 插入前查看是否已存在，存在不再插入
        SmsRecord sr = smsRecordService.lambdaQuery()
                .eq(SmsRecord::getId,rea.getId())
                .last("limit 1")
                .one();
        if (sr !=null){
            return Boolean.TRUE;
        }

        // 新增发短信数据
        return addSmsRecord(rea);

    }

    @Override
    public Boolean addFaOrderSms(OrderReAppointment rea){
        // 改约需要审核通过才可以发短信(平台直接写表，这里只操作预约发送写表)
        if (rea.getFlag().equals(0)){
            return Boolean.TRUE;
        }
        OrderBase orderBase = orderbaseService.lambdaQuery()
                .eq(OrderBase::getId,rea.getOrderBaseId())
                .last("limit 1")
                .one();
        if (orderBase == null){
            return Boolean.TRUE;
        }
        // 一个工单只能有一次预约信息，改约可以多个
        if (rea.getFlag().equals(1)){
            FaOrderSms r = faOrderSmsService.lambdaQuery()
                    .eq(FaOrderSms::getSynTaskNo,rea.getId())
                    .last("limit 1")
                    .one();
            if (r !=null){
                return Boolean.TRUE;
            }
        }

        // 生成预约改约短信实体对像
        FaOrderSms add = getFaOrderSms(orderBase, rea);
        // 插入发短信数据表
        if (faOrderSmsService.save(add)){
            // 预约改约同时向短信记录表同步数据
            return addSRecord(rea);
        }
        return Boolean.FALSE;
    }

    private FaOrderSms getFaOrderSms(OrderBase orderBase, OrderReAppointment rea) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        FaOrderSms add = new FaOrderSms();

        // add.setSynTaskNo(rea.getId());
        add.setWorkerOrderNo(orderBase.getId());
        // 工单类型 (1:安装,2:维修,3:回收工单,4:以旧换新)
        add.setAzorwx(4);
        // 工单类型 INSTALL=安装单  REPAIR=维修单  RETURN_EXCHANGE=退换货  TRADE_IN=以旧换新 MAINTENANCE=维保工单
        if (orderBase.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_INSTALL.getCode())){
            add.setAzorwx(1);
        } else if (orderBase.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_REPAIR.getCode())){
            add.setAzorwx(2);
        } else if (orderBase.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_RECOVERY.getCode())){
            add.setAzorwx(3);
        }
        // 维修工单pgid 其他工单pggid
        if (orderBase.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_REPAIR.getCode())){
            add.setFjid(orderBase.getPgid());
        } else {
            add.setFjid(orderBase.getPgguid());
        }
        add.setSjid(orderBase.getUserMobile());
        add.setFjwd(orderBase.getWebsitId());
        add.setFsma(getFaOrderSmsfsma(orderBase.getUserMobile()));
        add.setFsnr(String.format(ALISMS_RESERVE,
                rea.getNewAppointmentTime()==null ? sdf.format(rea.getOldAppointmentTime()):sdf.format(rea.getNewAppointmentTime()),
                getSHWDPhone(rea.getWebsitId())));
        add.setSjid(orderBase.getUserMobile());
        // 1:预约上门,4:改约
        add.setYhlb(4);
        if (rea.getFlag()==1){
            add.setYhlb(1);
        }

        return add;
    }

    /**
     * 根据经纬度反查省市区街道
     * @Param lng,lat
     * @Return {"area":"番禺区","province":"广东省","city":"广州市","street":"大龙街道"}
     */
    @Override
    public Geographicallocation getRegionByAddress(String lng, String lat){
        Geographicallocation result = new Geographicallocation();
        String lnglat = lng + "," + lat;
        String url = "https://restapi.amap.com/v3/geocode/regeo?key="+lbsWebKey+"&location="+lnglat;
        String s = HttpUtil.get(url);
        JSONObject jsonObject = new JSONObject(s);
        String status = jsonObject.getStr("status");
        if(!status.equals("1")){
            return null;
        }
        JSONObject regeocode = jsonObject.getJSONObject("regeocode");
        JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
        Map<String,String> map = new HashMap<>();
        result.setProvince(addressComponent.getStr("province"));
        if(addressComponent.getStr("city").equals("[]")) {
            result.setCity(addressComponent.getStr("province"));
        }else{
            result.setCity(addressComponent.getStr("city"));
        }
        if(addressComponent.getStr("district").equals("[]")) {
            result.setArea(addressComponent.getStr("city"));
        }else{
            result.setArea(addressComponent.getStr("district"));
        }
        result.setStreet(addressComponent.getStr("township"));
        return result;
    }

    // 根据地址反查经纬度
    @Override
    public String getLocationByAddress(String address){
        String url = "https://restapi.amap.com/v3/geocode/geo?key="+lbsWebKey+"&address="+address;
        String s = HttpUtil.get(url);
        JSONObject jsonObject = new JSONObject(s);
        String status = jsonObject.getStr("status");
        if(!status.equals("1")){
            return null;
        }
        JSONArray geocodes = jsonObject.getJSONArray("geocodes");
        if(org.springframework.util.CollectionUtils.isEmpty(geocodes))
            return null;
        Map<String, Object> map = (Map<String, Object>) geocodes.get(0);
        String location = (String)map.get("location");
        return location;
    }

    // 获取gpsaddress
    @Override
    public String getGpsaddress(String lng, String lat){
        String gps="";
        if (StringUtils.isNotEmpty(lng) && StringUtils.isNotEmpty(lat)){
            Geographicallocation result = getRegionByAddress(lng, lat);
            if (result !=null){
                gps=result.getProvince()+
                        result.getCity()+
                        result.getArea()+
                        result.getStreet();
            }
        }

        return gps;
    }

    // 商户网点编号    belongCompanyId(1:佛山,2:广州,3:天猫)
    @Override
    public Integer getCompanyId(String brandWebsitNumber){
        BrandWebsit bw = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, brandWebsitNumber)
                .last("limit 1")
                .one();
        if (bw != null){
            return bw.getBelongCompanyId();
        }
        return 1;
    }

    @Override
    public void getExpireTime(OrderBase orderBase, Date ssDate, Date jzDate) {
        int hour = 24;
        int hourjz = 24;
        Date d = new Date();

        // 提取配置参数 首次申诉-服务人员+网点审核截止时间
        SysVariableConfig config = sysVariableConfigService.lambdaQuery()
                .eq(SysVariableConfig::getVariableType,"APPEAL_TIME")
                .eq(SysVariableConfig::getSmallType,3)
                .last("limit 1")
                .one();
        if (config != null){
            hour = Integer.valueOf(config.getVariableValue());
        }

        hourjz = hour;
        // 应家盛茂平台要求只要短信发送时间，这里hour改成0；
        hour=0;
        // 提取短信发送时间 目的配合配置参数生成申诉单的截止申诉时间
        ItfMessagehuifangentity message = itfMessagehuifangentityService.lambdaQuery()
                .eq(ItfMessagehuifangentity::getOrderBaseId, orderBase.getId())
                .eq(ItfMessagehuifangentity::getFjid, orderBase.getPgguid())
                .last("limit 1")
                .one();


        // 测试时存在无message的情况
        if (message == null){
            if (orderBase.getOverTime()!=null){
                d = orderBase.getOverTime();
            }
            if (orderBase.getWebsitOverTime() !=null){
                d = orderBase.getWebsitOverTime();
            }
            ssDate.setTime(d.getTime()+ hour * 60 * 60 * 1000);
            jzDate.setTime(d.getTime()+ hourjz * 60 * 60 * 1000);
        } else {
            ssDate.setTime(message.getSendtime().getTime()+ hour * 60 * 60 * 1000);
            jzDate.setTime(message.getSendtime().getTime()+ hourjz * 60 * 60 * 1000);
        }
    }

    @Override
    public void getExpireTimeRepair(OrderBase orderBase, Date ssDate, Date jzDate) {
        int hour = 24;
        int hourjz = 24;
        Date d = new Date();

        // 提取配置参数 首次申诉-服务人员+网点审核截止时间
        SysVariableConfig config = sysVariableConfigService.lambdaQuery()
                .eq(SysVariableConfig::getVariableType,"APPEAL_TIME")
                .eq(SysVariableConfig::getSmallType,3)
                .last("limit 1")
                .one();
        if (config != null){
            hour = Integer.valueOf(config.getVariableValue());
        }

        hourjz = hour;
        // 应家盛茂平台要求只要短信发送时间，这里hour改成0；
        hour=0;
        // 提取短信发送时间 目的配合配置参数生成申诉单的截止申诉时间
        ItfMessagehuifangentity message = itfMessagehuifangentityService.lambdaQuery()
                .eq(ItfMessagehuifangentity::getOrderBaseId, orderBase.getId())
                .eq(ItfMessagehuifangentity::getFjid, orderBase.getPgid())
                .last("limit 1")
                .one();


        // 测试时存在无message的情况
        if (message == null){
            if (orderBase.getOverTime()!=null){
                d = orderBase.getOverTime();
            }
            if (orderBase.getWebsitOverTime() !=null){
                d = orderBase.getWebsitOverTime();
            }
            ssDate.setTime(d.getTime()+ hour * 60 * 60 * 1000);
            jzDate.setTime(d.getTime()+ hourjz * 60 * 60 * 1000);
        } else {
            ssDate.setTime(message.getSendtime().getTime()+ hour * 60 * 60 * 1000);
            jzDate.setTime(message.getSendtime().getTime()+ hourjz * 60 * 60 * 1000);
        }
    }

    // 返回差评标识
    // SATISFACTION=好GENERAL=中DISPLEASURE=差 ELIMINATE=剔除 OTHER_APPRISE=其他
    @Override
    public Boolean  checkEvaluatemessage(String pjly){
        SatisfactionList sfl = satisfactionListService.lambdaQuery()
                .eq(SatisfactionList::getPjly,pjly.trim())
                .last("limit 1")
                .one();
        if (sfl==null){
            return Boolean.TRUE;
        }
        if (sfl.getAppraiseType().equals("SATISFACTION")){
            return Boolean.FALSE;
        } else {
            return Boolean.TRUE;
        }
    }

    @Override
    public SatisfactionList  getAppraiseStatus(String pjly){
        List<SatisfactionList> rec = satisfactionListService.lambdaQuery()
                .eq(SatisfactionList::getPjly,pjly.trim()).list();
        if (CollectionUtils.isNotEmpty(rec)){
            return rec.get(0);
        }

        return null;
    }

    // 获取安装工单对应的师傅信息
    @Override
    public Worker getInstallOrderWorker(Long azrenid){
        List<Worker> worker = new ArrayList<>();
        ItfTblWangdianWxgDownload wdwxg = itfTblWangdianWxgDownloadService.lambdaQuery()
                .eq(ItfTblWangdianWxgDownload::getWxgid,azrenid)
                .last("limit 1")
                .one();
        // 根据身份证取师傅信息
        if (wdwxg !=null){
            worker = workerService.lambdaQuery()
                    .eq(Worker::getIdcard,wdwxg.getSfzen()).list();
            if (CollectionUtils.isNotEmpty(worker)){
                return worker.get(0);
            }
        }
        return null;
    }

    // 获取平台网点师傅信息
    @Override
    public WebsitWorker getOrderWebsitWorker(String workerId,String websitId){
        WebsitWorker wws = websitWorkerService.lambdaQuery()
                .eq(WebsitWorker::getWorkerId,workerId)
                .eq(WebsitWorker::getWebsitId,websitId)
                .last("limit 1").one();
        return wws;
    }


    // SATISFACTION=好GENERAL=中DISPLEASURE=差 ELIMINATE=剔除 OTHER_APPRISE=其他
    // 评价状态 A=满意  B=一般  C=不满意
    @Override
    public String checkAppraiseType(String pjly){
        String result ="C";
        SatisfactionList sfl = satisfactionListService.lambdaQuery()
                .eq(SatisfactionList::getPjly,pjly.trim())
                .last("limit 1")
                .one();
        if (sfl==null){
            result = "C";
        } else {
            if (sfl.getAppraiseType().equals("SATISFACTION")){
                result = "A";
            } else if (sfl.getAppraiseType().equals("GENERAL")){
                result = "B";
            }
        }

        return result;
    }

    @NotNull
    @Override
    public String getErrormessage(ResponseHelper respone) {
        String redisKey = Constant.RedisPrefix.TOKEN_JIASM_ORDER;
        // 签名验证失败处理
        if (respone.getCode()==1001){
            redis.del(redisKey);
        } else if (respone.getMessage()!=null && respone.getMessage().indexOf("签名验证失败")>=0){
            redis.del(redisKey);
        }

        String msg ="";
        if (respone.getMessage()!=null){
            msg="错误代码:("+respone.getCode()+")"+ respone.getMessage();
        }
        return msg;
    }


    // 获取师傅在商户网点账号 平台师傅账号，商户网点编号，集团ID
    @Override
    public Long getBrandWorkerId(String workerId,String brandWebsitId,String groupCompanyId, String websitId){
        long brandWorkerId =0;

        WorkerBrandAccount wba = workerBrandAccountService.lambdaQuery()
                .eq(WorkerBrandAccount::getGroupCompanyId,groupCompanyId)
                // .eq(WorkerBrandAccount::getBrandWorkerStatus,"ON") 王总要求去掉在总部状态的判断
                .eq(WorkerBrandAccount::getDel,0)
                .eq(WorkerBrandAccount::getWorkerId,workerId)
                .eq(WorkerBrandAccount::getBrandWebsitId,brandWebsitId)
                .eq(WorkerBrandAccount::getWebsitId,websitId)
                .last("limit 1")
                .one();
        if (wba !=null){
            if (StringUtils.isNotEmpty(wba.getBrandWorkerId())){
                return Long.valueOf(wba.getBrandWorkerId());
            }
        }

        return brandWorkerId;
    }


    @Override
    public String getBrandWorkerNo(String workerId,String brandWebsitId,String groupCompanyId, String websitId){
        String brandWorkerNo ="";

        WorkerBrandAccount wba = workerBrandAccountService.lambdaQuery()
                .eq(WorkerBrandAccount::getGroupCompanyId,groupCompanyId)
                .eq(WorkerBrandAccount::getDel,0)
                .eq(WorkerBrandAccount::getWorkerId,workerId)
                .eq(WorkerBrandAccount::getBrandWebsitId,brandWebsitId)
                .eq(WorkerBrandAccount::getWebsitId,websitId)
                .last("limit 1")
                .one();
        if (wba !=null){
            if (StringUtils.isNotEmpty(wba.getBrandWorkerNumber())){
                return wba.getBrandWorkerNumber();
            }
        }

        return brandWorkerNo;
    }

    @Override
    public WorderInfo getWorkerInfo(String workerId,String brandWebsitId,String groupCompanyId){
        WorderInfo info = new WorderInfo();
        WorkerBrandAccount wba = workerBrandAccountService.lambdaQuery()
                .eq(WorkerBrandAccount::getGroupCompanyId,groupCompanyId)
                .eq(WorkerBrandAccount::getBrandWorkerStatus,"ON")
                .eq(WorkerBrandAccount::getDel,0)
                .eq(WorkerBrandAccount::getWorkerId,workerId)
                .eq(WorkerBrandAccount::getBrandWebsitId,brandWebsitId)
                .last("limit 1")
                .one();
        if (wba !=null){
            MyBeanUtils.copyProperties(wba,info);
            Worker wk = workerService.lambdaQuery()
                    .eq(Worker::getWorkerId,workerId)
                    .last("limit 1")
                    .one();

            MyBeanUtils.copyProperties(wk,info);
        }

        return info;
    }

    @Override
    public List<WebsitOrderSign> SyncshshwdToptshwd(){
        // 提取商户售后网点对应的平台售后网点信息 存在一对多的关系
        List<WebsitOrderSign> wos = new ArrayList<>();
        wos = websitOrderSignService.lambdaQuery().list();
        return wos;
    }

    @Override
    public List<WebsitOrderSign> shShwdToptshwdByBrandWebsitNo(String wdno){
        // 提取商户售后网点对应的平台售后网点信息 存在一对多的关系
        List<WebsitOrderSign> wos = new ArrayList<>();
        wos = websitOrderSignService.lambdaQuery()
                .eq(WebsitOrderSign::getBrandWebsitId,wdno).list();
        return wos;
    }


    // 安装工单的省市区对应关系
    @Override
    public void installOrderSSQJ(OrderBase orderBase){
        // 先查 lbs_amap表 1省，2市，3区，4街道 找到直接返回
        Integer flag = getLbsAmap(orderBase);
        if (flag<4){
            // 再查 BrandLbsRelate 表
            getBrandLbsRelate(orderBase,flag);
        }

        // 根据地址设置经纬度
        setLocationByAddress(orderBase);
    }

    /**
     * 商户售后中心网点对应平台售后网点
     * @param websitNo  商户售后网点编号
     * @param order 商户工单大类、商户工单类型
     * @return
     */
    @Override
    public WebsitFWS shshwdToptshwdZX(String websitNo, OrderBase order){
        WebsitFWS resultData = new WebsitFWS();
        long tid = Thread.currentThread().getId();

        log.info("商户售后中心网点到平台部门网点：["+tid+"]["+websitNo+"]["+order.getMainId()+"]");

        if (StringUtils.isEmpty(websitNo)){
            return resultData;
        }

        List<AdminWebsit> aws = adminWebsitService.lambdaQuery()
                .eq(AdminWebsit::getBrandWebsitId,websitNo)
                .eq(AdminWebsit::getIsCenter,Boolean.TRUE)
                .list();
        if (CollectionUtils.isEmpty(aws)){
            return resultData;
        }

        if(aws.size()==1){
            resultData.setWebsitId(aws.get(0).getWebsitNumber());
            resultData.setWdmc(aws.get(0).getName());
            return resultData;
        }

        // 多网点里找下级网点
        LinkedHashSet<String> websitNumbers = new LinkedHashSet<>();
        aws.forEach(o->{
            websitNumbers.add(o.getWebsitNumber());
        });
        List<AdminWebsit> awst = adminWebsitService.lambdaQuery()
                .in(AdminWebsit::getParentId,websitNumbers)
                .list();

        // 多网点时用大类过滤
        AdminWebsit adminWebsit = websitCategoryZX(awst, order.getMainId(), tid,order.getOrderType());
        resultData.setWebsitId(adminWebsit.getWebsitNumber());
        resultData.setWdmc(adminWebsit.getName());
        return resultData;
    }

    @Override
    public Belongcompany getBelongCompany(String bcp) {
        Belongcompany bcs = null;

        if (redis.hasKey(RedisConstant.CENTER_CODE + bcp)) {
            bcs = (Belongcompany) redis.get(RedisConstant.CENTER_CODE + bcp);
        } else {
            bcs = belongcompanyService.lambdaQuery()
                    .eq(Belongcompany::getBelongCompanyProfession,bcp).last("limit 1").one();

            if (bcs != null){
                redis.set(RedisConstant.CENTER_CODE + bcp, bcs,RedisConstant.CENTER_CODE_TIME_LENGTH);
            }
        }

        return bcs;
    }

    // 中心下沉到总包商户 格力中心网点编号，工单
    @Override
    public void getZXINFO(String websitNo, OrderBase order){
        WebsitFWS websitFWS = null;
        // websitFWS = shshwdToptshwd(websitNo, order);
        websitFWS = shshzxwdToptzbwd(websitNo, order);
        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = shxswdToptxswd(websitNo, order);
        }
        order.setReceWebsitId(websitFWS.getWebsitId());
        order.setReceWebsitName(websitFWS.getWdmc());
        order.setWebsitId(websitFWS.getWebsitId());
        order.setWebsitName(websitFWS.getWdmc());

        // 结算主体重置
        Belongcompany bc = getBelongCompany(websitNo);
        websitFWS = shxswdToptxswd(bc.getBelongCompanySale(),order);
        order.setBrandId(websitFWS.getBrandId());
        order.setBrandName(websitFWS.getBrandName());
    }

    // 中心下沉到总包服务商 格力中心网点编号，工单
    @Override
    public void getZXPTZBINFO(String websitNo, OrderBase order){
        WebsitFWS websitFWS = shshwdToptshwd(websitNo, order);

        if (StringUtils.isEmpty(websitFWS.getWebsitId())){
            websitFWS = shxswdToptxswd(websitNo, order);
        }
        order.setReceWebsitId(websitFWS.getWebsitId());
        order.setReceWebsitName(websitFWS.getWdmc());
        order.setWebsitId(websitFWS.getWebsitId());
        order.setWebsitName(websitFWS.getWdmc());

        // 结算主体重置
        Belongcompany bc = getBelongCompany(websitNo);
        websitFWS = shxswdToptxswd(bc.getBelongCompanySale(),order);
        order.setBrandId(websitFWS.getBrandId());
        order.setBrandName(websitFWS.getBrandName());
    }

    /**
     *   1：网点不存在需要回传的网点表内，改回传同步状态字段为99
     *   2：回传网点表为空时 一样不回传
     * @param orderNo 工单ID
     * @param tableName 表名
     * @param recordId 记录ID
     * @return false回传， true 不回传
     */
    @Override
    public Boolean checkWebsitSyncFlag(String orderNo,String tableName,Long recordId){
        WebsitSH websit = new WebsitSH();
        String msg="";
        OrderBase order = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, orderNo)
                .last("limit 1")
                .one();

        if (order ==null) {
            // 不回传 不会存在工单主表无数据的情况 修改无需同步
            msg = "【"+orderNo + "】工单信息不存在不需要回传";
            uploadService.updateNotSync(tableName, recordId,msg);
            return Boolean.TRUE;
        }

        // 售后网点处理
        websit = publicService.ptshwdToshshwd(order.getWebsitId(), order.getMainId());
        SynUploadWebsit websitinfo = synUploadWebsitService.lambdaQuery()
                .eq(SynUploadWebsit::getBrandWebsitNo,websit.getWdId()).one();
        if (websitinfo != null){
            // 回传  找到相应的网点说明该网点工单需要回传
            return Boolean.FALSE;
        }
        msg = "【"+order.getWebsitId() + ">"+websit.getWdId()+"】网点工单信息不需要回传";

        // 不回传 修改无需同步
        uploadService.updateNotSync(tableName, recordId,msg);
        return Boolean.TRUE;
    }

    /**
     *   1：网点不存在需要回传的网点表内，改回传同步状态字段为99
     *   2：回传网点表为空时 一样不回传
     * @param orderNo 工单ID
     * @param tableName 表名
     * @param recordId 记录ID
     * @return false回传， true 不回传
     */
    @Override
    public Boolean checkWebsitSyncFlagT(String orderNo,String tableName,String recordId){
        WebsitSH websit = new WebsitSH();
        String msg="";
        OrderBase order = orderBaseService.lambdaQuery()
                .eq(OrderBase::getId, orderNo)
                .last("limit 1")
                .one();

        if (order ==null) {
            // 不回传 不会存在工单主表无数据的情况 修改无需同步
            msg = "【"+orderNo + "】工单信息不存在不需要回传";
            uploadService.updateNotSyncT(tableName, recordId,msg);
            return Boolean.TRUE;
        }

        // 售后网点处理
        websit = publicService.ptshwdToshshwd(order.getWebsitId(), order.getMainId());
        SynUploadWebsit websitinfo = synUploadWebsitService.lambdaQuery()
                .eq(SynUploadWebsit::getBrandWebsitNo,websit.getWdId()).one();
        if (websitinfo != null){
            // 回传  找到相应的网点说明该网点需要回传
            return Boolean.FALSE;
        }
        msg = "【"+order.getWebsitId() + ">"+websit.getWdId()+"】网点工单信息不需要回传";

        // 不回传 修改无需同步
        uploadService.updateNotSyncT(tableName, recordId,msg);
        return Boolean.TRUE;
    }

    // 获取平台网点师傅信息
    @Override
    public List<WebsitWorker> getWebsitWorkerByWorkerAndWebsit(){
        List<WebsitWorker> wws = websitWorkerService.lambdaQuery()
                .select(WebsitWorker::getWorkerId,WebsitWorker::getWebsitId,WebsitWorker::getWebsitWorkerId)
                .list();
        return wws;
    }

    @Override
    public Insure getWebsitWorkerByWorkerAndWebsit(String type){
        Insure insure = insureService.lambdaQuery()
                .eq(Insure::getTypeName,type).last("limit 1").one();
        return insure;
    }

    @Override
    public void creatMkdir(){
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyyMM/");
        File file =new File(sdfs.format(new Date()));
        if  (!file .exists()  && !file .isDirectory()){
            file .mkdir();
        }
    }

    @Override
    public Date subOneYear(Date dt){
        // 创建一个Calendar对象
        Calendar calendar = Calendar.getInstance();
        // 设置日期
        calendar.setTime(dt);
        // 减去一年
        calendar.add(Calendar.YEAR, -1);
        // 获取减去一年后的日期
        return calendar.getTime();
    }

    @Override
    public String path(String f){
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyyMM/");

        String procf = f.trim();
        if (substr(procf,procf.length()-1,procf.length()).equals(".")){
            f = procf+"jpeg";
        }

        String filedir = "syncdata/" + sdfs.format(new Date()) + f;
        return filedir;
    }

    @Override
    public String pathToGree(String f){
        String filedir = "g/" + f;
        return filedir;
    }

    @Override
    public String uploadPath(String f){
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy/");
        String filedir = sdfs.format(new Date()) + f;
        return filedir;
    }

    @Override
    public File createf(String name) throws IOException {
        File newFile = new File(name);
        while (newFile.exists()){
            newFile.delete();
        }


        File dir = newFile.getParentFile();
        if (!dir.exists()){
            dir.mkdirs();
        }
        newFile.createNewFile();

        return newFile;
    }

    // 修改附件序
    @Override
    public void upOrderDetailFileOutId(String orderBaseId){
        List<OrderDetailFile> upList = new ArrayList<>();
        // 查看是否有需要修改的数据
        List<OrderDetailFile> oids = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, orderBaseId)
                .eq(OrderDetailFile::getOutId, 0)
                .list();
        if (CollectionUtils.isEmpty(oids)){
            return;
        }

        List<OrderDetailFile> detaillist = orderDetailFileService.lambdaQuery()
                .eq(OrderDetailFile::getOrderBaseId, orderBaseId)
                .list();
        // 找出最大的序 默认值为0
        int outId=0;
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(detaillist)){
            for (OrderDetailFile item:detaillist){
                if (outId<item.getOutId()){
                    outId = item.getOutId();
                }
            }
        }

        for (OrderDetailFile ite:detaillist){
            if (ite.getOutId() == 0){
                outId++;
                ite.setOutId(outId);
                upList.add(ite);
            }
        }
        // 修改序
        if (upList.size()>0){
            orderDetailFileService.updateBatchById(upList);
            upList.clear();
        }
        detaillist.clear();
    }

    @Override
    public void revertFun(OrderBase order,ItfTblAzAssignLcLsDownload lcls){
        // 安装网点
        order.setWebsitId("");
        order.setWebsitName("");
        if (StringUtils.isNotEmpty(lcls.getAzwdno())){
            order.setWebsitId(lcls.getAzwdno());
        }
        if (StringUtils.isNotEmpty(lcls.getAzwdmc())){
            order.setWebsitName(lcls.getAzwdmc());
        }

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

        // 接收派工网点
        order.setReceWebsitId("");
        order.setReceWebsitName("");
        if (StringUtils.isNotEmpty(lcls.getJspgwdno())){
            order.setReceWebsitId(lcls.getJspgwdno());
        }
        if (StringUtils.isNotEmpty(lcls.getJspgwdmc())){
            order.setReceWebsitName(lcls.getJspgwdmc());
        }

        // 销售网点
        order.setSaleWebsitId("");
        order.setSaleWebsitName("");
        order.setSaleWebsitSysNumber("");
        if (StringUtils.isNotEmpty(lcls.getXsdwno())){
            order.setSaleWebsitId(lcls.getXsdwno());
        }
        if (StringUtils.isNotEmpty(lcls.getXswdmc())){
            order.setSaleWebsitName(lcls.getXswdmc());
        }
        if (StringUtils.isNotEmpty(lcls.getXsdwxtbh())){
            order.setSaleWebsitSysNumber(lcls.getXsdwxtbh());
        }

        // 创建网点
        order.setCreateWebsitId("");
        order.setCreateWebsitName("");
        if (StringUtils.isNotEmpty(lcls.getCjwdno())){
            order.setCreateWebsitId(lcls.getCjwdno());
        }
        if (StringUtils.isNotEmpty(lcls.getCjrmc())){
            order.setCreateWebsitName(lcls.getCjrmc());
        }

        order.setLastOperatorWebsit("");
        order.setLastOperatorBy("");
        if (StringUtils.isNotEmpty(lcls.getZjczwd())){
            order.setLastOperatorWebsit(lcls.getZjczwd());
            order.setLastOperatorBy(lcls.getZjczwd());
        }
    }

    @Override
    public void revertRepairFun(OrderBase order,ItfTblAssignDownload lcls){
        // 安装网点
        order.setWebsitId("");
        order.setWebsitName("");

        if (StringUtils.isNotEmpty(lcls.getWxwdno())){
            order.setWebsitId(lcls.getWxwdno());
        } else if (StringUtils.isNotEmpty(lcls.getXjwdno())){
            order.setWebsitId(lcls.getXjwdno());
        }
        if (StringUtils.isNotEmpty(lcls.getWxwdmc())){
            order.setWebsitName(lcls.getWxwdmc());
        }

        // 接收派工网点
        order.setReceWebsitId("");
        order.setReceWebsitName("");
        if (StringUtils.isNotEmpty(lcls.getXjwdno())){
            order.setReceWebsitId(lcls.getXjwdno());
        }
        if (StringUtils.isNotEmpty(lcls.getXjwdmc())){
            order.setReceWebsitName(lcls.getXjwdmc());
        }

        // 销售网点
        order.setSaleWebsitId("");
        order.setSaleWebsitName("");
        order.setSaleWebsitSysNumber("");
        if (StringUtils.isNotEmpty(lcls.getXswdno())){
            order.setSaleWebsitId(lcls.getXswdno());
        }
        if (StringUtils.isNotEmpty(lcls.getXswdmc())){
            order.setSaleWebsitName(lcls.getXswdmc());
        }


        // 创建网点
        order.setCreateWebsitId("");
        order.setCreateWebsitName("");
        if (StringUtils.isNotEmpty(lcls.getCjwdno())){
            order.setCreateWebsitId(lcls.getCjwdno());
        }

        order.setLastOperatorWebsit("");
        order.setLastOperatorBy("");
        if (StringUtils.isNotEmpty(lcls.getZjczwd())){
            order.setLastOperatorWebsit(lcls.getZjczwd());
            order.setLastOperatorBy(lcls.getZjczwd());
        }
    }

    @Override
    public String getUUID(){
        String uuId = UUID.randomUUID().toString().replace("-","").toUpperCase();
        return uuId;
    }

    @Override
    public Date getYearFirstDay(Date date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DATE, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(sdf.format(calendar.getTime()));
    }
    @Override
    public Date getYearLastDay(Date date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DATE, 31);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(sdf.format(calendar.getTime()));
    }

    @Override
    public void setWorkerCertGreeId(WorkerCertGree awc, Map<String,String> wcgids){
        if (wcgids.containsKey(awc.getWorkerId() + awc.getBrandWebsitId() + awc.getCertName())){
            awc.setWorkerCertGreeId(wcgids.get(awc.getWorkerId() + awc.getBrandWebsitId() + awc.getCertName()));
        } else {
            wcgids.put(awc.getWorkerId() + awc.getBrandWebsitId() + awc.getCertName(),awc.getWorkerCertGreeId());
        }
    }


    @Override
    public void setCertFlag(String certName, WorkerCertGree awc){
        if ("身份证".equals(certName)) {
            awc.setCertFlag(1);
        } else if ("保险证".equals(certName)) {
            awc.setCertFlag(2);
        } else if ("高空作业证".equals(certName)) {
            awc.setCertFlag(3);
            awc.setCertName("高空作业证");
        } else if ("上岗资格证".equals(certName)) {
            awc.setCertFlag(4);
        } else if ("制冷工证".equals(certName)) {
            awc.setCertFlag(5);
        } else if ("驾驶证".equals(certName)) {
            awc.setCertFlag(6);
        } else if ("电工证".equals(certName)) {
            awc.setCertFlag(7);
        } else if ("焊工证".equals(certName)) {
            awc.setCertFlag(8);
        } else if ("人脸面像".equals(certName)) {
            awc.setCertFlag(9);
        }
    }

    @Override
    public void setUserLevel(OrderBase order){
        Map<String,String> userLevel = new HashMap<>();
        if (StringUtils.isEmpty(order.getVip())){
            return;
        }

        List<SysDict> sysDicts = sysDictService.lambdaQuery()
                .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_USER_LEVEL.getCode()).list();
        if (CollectionUtils.isNotEmpty(sysDicts)){
            sysDicts.stream().forEach(event->{
                userLevel.put(event.getDictValue(),event.getDictCode());
            });
            sysDicts.clear();
        }

        if (userLevel.containsKey(order.getVip())){
            order.setVip(userLevel.get(order.getVip()));
        }
    }

    @Override
    public void getProductId(Map<String,String> productMap){
        Brand brand = baseService.getBrand();
        // 提取现有数据
        List<Product> oldSysProduct = productService.lambdaQuery()
                .eq(Product::getProductBrandId, brand.getBrandId()).list();
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(oldSysProduct)){
            oldSysProduct.stream().forEach(event->{
                productMap.put(event.getJxid(), event.getProductId());
            });
            oldSysProduct.clear();
        }
    }

    @Override
    public void upSmsRecordSendTime(String synTaskNo,String phone,Date date){
        // 提取信息，有则更新
        SmsRecord sr = smsRecordService.lambdaQuery()
                .eq(SmsRecord::getSynTaskNo,synTaskNo)
                .eq(SmsRecord::getPhone,phone)
                .isNull(SmsRecord::getSendTime)
                .orderByDesc(SmsRecord::getCreateTime)
                .last("limit 1").one();
        if (sr != null){
            sr.setSendTime(date);
            sr.updateById();
        }
    }

    @Override
    public Boolean getCppjly(String pjly){
        Boolean cppjly;
        if (pjly.equals("1") || pjly.indexOf("满意")>=0){
            cppjly = Boolean.FALSE;
        } else if (pjly.equals("2") || pjly.indexOf("一般")>=0){
            cppjly = Boolean.TRUE;
        } else if (pjly.equals("3") || pjly.indexOf("不满意")>=0){
            cppjly = Boolean.TRUE;
        } else {
            cppjly = checkEvaluatemessage(pjly);
        }
        return cppjly;
    }

    // 首次评价(A:好评,B:中评,C:差评,N:未评价,O:其他
    @Override
    public String getAppraiseStatusT(String pjly){
        String appraiseStatus="";
        if (pjly.equals("1") || pjly.indexOf("满意")>=0){
            appraiseStatus="A";
        } else if (pjly.equals("2") || pjly.indexOf("一般")>=0){
            appraiseStatus="B";
        } else if (pjly.equals("3") || pjly.indexOf("不满意")>=0){
            appraiseStatus="C";
        } else {
            // 这里需要增加一个表用来查询确认是否为不满意
            appraiseStatus=getAppraiseStatusr(pjly);
        }

        return appraiseStatus;
    }

    @Override
    public String getSpmessage(OrderBase orderBase, String spmessage){
        OrderAppraise oaOne = orderAppraiseService.lambdaQuery()
                .eq(OrderAppraise::getOrderBaseId, orderBase.getId())
                .eq(OrderAppraise::getType,1)
                .last("limit 1").one();
        if (oaOne !=null){
            spmessage = oaOne.getAppraiseContent();
        }
        return spmessage;
    }

    private String getAppraiseStatusr(String pjly) {
        String appraiseStatus = "";
        SatisfactionList sfs = getAppraiseStatus(pjly);
        if (sfs == null){
            appraiseStatus="O";
            return appraiseStatus;
        }
        if (sfs.getAppraiseType().equals(AppraiseTypeEnum.SATISFACTION.getCode())){
            appraiseStatus="A";
        } else if (sfs.getAppraiseType().equals(AppraiseTypeEnum.GENERAL.getCode())){
            appraiseStatus="B";
        } else if (sfs.getAppraiseType().equals(AppraiseTypeEnum.DISPLEASURE.getCode())){
            appraiseStatus="C";
        } else {
            appraiseStatus="O";
        }

        return appraiseStatus;
    }


    private void setLocationByAddress(OrderBase orderBase) {
        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 = getLocationByAddress(add);
            if (StringUtils.isEmpty(lngLat)) {
                return;
            }
            List<String> result = Arrays.asList(lngLat.split(","));

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

    private void getBrandLbsRelate(OrderBase orderBase, Integer flag) {
        // 格力总部的省市区为必填，可能无街道
        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, flag);
            } 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, flag);
                    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, flag);
                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 = getLocationByAddress(add);
            if (StringUtils.isEmpty(lngLat)){
                return;
            }
            List<String> result = Arrays.asList(lngLat.split(","));

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

    // 1省，2市，3区，4街道
    private Integer getLbsAmap(OrderBase orderBase){
        LbsAmap lbsAmap = new LbsAmap();
        // 先查 lbs_amap表
        if (redis.hasKey(RedisConstant.PROVINCE_ADDRE+orderBase.getProvince())){
            lbsAmap = (LbsAmap) redis.get(RedisConstant.PROVINCE_ADDRE+orderBase.getProvince());
        } else {
            lbsAmap = lbsAmapService.lambdaQuery()
                    .eq(LbsAmap::getName,orderBase.getProvince())
                    .eq(LbsAmap::getLevel,"province")
                    .last("limit 1").one();
            if (lbsAmap == null){
                return 0;
            }
            redis.set(RedisConstant.PROVINCE_ADDRE + orderBase.getProvince(), lbsAmap,RedisConstant.ADDRES_TIME_LENGTH);
        }
        //省
        orderBase.setProvinceId(lbsAmap.getProvinceId());
        orderBase.setProvince(lbsAmap.getProvinceName());

        if (redis.hasKey(RedisConstant.CITY_ADDRE+lbsAmap.getProvinceId()+orderBase.getCity())){
            lbsAmap = (LbsAmap) redis.get(RedisConstant.CITY_ADDRE+lbsAmap.getProvinceId()+orderBase.getCity());
        } else {
            lbsAmap = lbsAmapService.lambdaQuery()
                    .eq(LbsAmap::getProvinceId,lbsAmap.getProvinceId())
                    .eq(LbsAmap::getName,orderBase.getCity())
                    .eq(LbsAmap::getLevel,"city")
                    .last("limit 1").one();
            if (lbsAmap == null){
                return 1;
            }
            redis.set(RedisConstant.CITY_ADDRE+lbsAmap.getProvinceId()+orderBase.getCity(), lbsAmap,RedisConstant.ADDRES_TIME_LENGTH);
        }
        //市
        orderBase.setCityId(lbsAmap.getCityId());
        orderBase.setCity(lbsAmap.getCityName());

        lbsAmap = lbsAmapService.lambdaQuery()
                .eq(LbsAmap::getProvinceId,lbsAmap.getProvinceId())
                .eq(LbsAmap::getCityId,lbsAmap.getCityId())
                .eq(LbsAmap::getName,orderBase.getArea())
                .eq(LbsAmap::getLevel,"district")
                .last("limit 1").one();
        if (lbsAmap == null){
            return 2;
        }
        //区
        orderBase.setAreaId(lbsAmap.getAreaId());
        orderBase.setArea(lbsAmap.getAreaName());

        lbsAmap = lbsAmapService.lambdaQuery()
                .eq(LbsAmap::getProvinceId,lbsAmap.getProvinceId())
                .eq(LbsAmap::getCityId,lbsAmap.getCityId())
                .eq(LbsAmap::getAreaId,lbsAmap.getAreaId())
                .eq(LbsAmap::getName,orderBase.getStreet())
                .eq(LbsAmap::getLevel,"street")
                .last("limit 1").one();
        if (lbsAmap == null){
            return 3;
        }
        //街道
        orderBase.setStreetId(lbsAmap.getLbsId());
        orderBase.setStreet(lbsAmap.getName());

        return 4;
    }

    private void getPosition(OrderBase orderBase, Street street, Integer flag) {
        //省市区，有时无街道，这里不提取街道信息
        List<BrandLbsRelate> 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){
            if (flag==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 (flag==1){
                //市
                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 (flag==2){
                //区
                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());
            }
            if (StringUtils.isEmpty(orderBase.getStreet()) &&
                    StringUtils.isEmpty(street.getStreet())){
                orderBase.setStreet(street.getStreet());
            }
            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());
            }
        } else {
            // 先街道时依总部为准
            if (StringUtils.isEmpty(orderBase.getStreetId()) &&
                    StringUtils.isNotEmpty(street.getStreetId())){
                orderBase.setStreetId(street.getStreetId());
            }
            if (StringUtils.isEmpty(orderBase.getStreet()) &&
                    StringUtils.isNotEmpty(street.getStreet())){
                orderBase.setStreet(street.getStreet());
            }
        }
    }

    private String getFaOrderSmsfsma(String phone) {
        Random random = new Random(System.currentTimeMillis());
        String fsma="";
        List<FaOrderSms> fos = new ArrayList<>();
        // 同一用户最近两个月内产生的发送码不能相同
        while (Boolean.TRUE){
            fos.clear();
            fsma = String.valueOf(random.nextInt(10000));
            if (fsma.length()<4){
                if (fsma.length()==3){
                    fsma = fsma + "9";
                } else if (fsma.length()==2){
                    fsma = fsma + "99";
                } else {
                    fsma = fsma + "`999";
                }
            }
            fos = faOrderSmsService.lambdaQuery()
                    .eq(FaOrderSms::getSjid,phone)
                    .eq(FaOrderSms::getFsma,fsma)
                    .ge(FaOrderSms::getCrdt,stepMonth(new Date(),-2)).list();
            if (CollectionUtils.isEmpty(fos)){
                fos.clear();
                break;
            }
        }

        return fsma;
    }

    private Boolean addSmsRecord(OrderReAppointment rea) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SmsRecord record = new SmsRecord();

        record.setId(rea.getId());
        record.setOrderBaseId(rea.getOrderBaseId());
        record.setOrderType(rea.getOrderType());
        record.setType(4);
        if (rea.getFlag() == 1){
            record.setType(rea.getFlag());
        }

        record.setPhone(rea.getUserMobile());
        if (record.getType()==1){
            record.setContent(String.format(ALISMS_RESERVE,
                    rea.getNewAppointmentTime()==null ? sdf.format(rea.getOldAppointmentTime()):sdf.format(rea.getNewAppointmentTime()),
                    getSHWDPhone(rea.getWebsitId())));
        } else {
            record.setContent(String.format(ALISMS_RESERVE,
                    sdf.format(rea.getNewAppointmentTime()),getSHWDPhone(rea.getWebsitId())));
        }
        record.setFsma(getfsma(record.getPhone()));
        record.setCreateTime(new Date());

        return smsRecordService.save(record);
    }

    private void getSmsRecord(ItfMessagehuifangentity addms, String orderType, int type, SmsRecord record) {
        record.setId(publicService.getUUID());
        record.setOrderBaseId(addms.getOrderBaseId());
        record.setOrderType(orderType);
        record.setType(type);
        record.setPhone(addms.getSjid());
        record.setContent(addms.getFsnr());
        record.setFsma(addms.getFsma());
        record.setCreateTime(new Date());
    }

    // 生成短信评价数据 主程序本身已做了重复过滤，这里无需再操作
    @Override
    public Boolean addItfMessagehuifangentity(ItfMessagehuifangentity add,String orderType,int type){
        if (itfMessagehuifangentityService.save(add)){
            // 向发送短信表插入数据
            return addSmsRecord(add,orderType,type);
        }
        return Boolean.FALSE;
    }


    // 平台网点服务的大类信息
    private WebsitOrderSign websitCategory(String websitNo,List<WebsitOrderSign> wos, String categoryId,long tid,String type){
        String wd="";
        String wdh="";
        List<WebsitOrderSign> wosn = new ArrayList<>();

        // 对应多总包服务商 处理方式与一个总包服务商相同
        // 一个总包服务商多网点：从平台网点服务的类目取对应的网点信息 一般在这里就可以区分开
        for (WebsitOrderSign item:wos){
            WebsitServiceCategoryDetail wosinfo = getWebCategory(item.getBrandId(),item.getWebsitId(), categoryId,type);
            wd=wd+item.getWebsitId()+",";
            if (wosinfo !=null){
                wosn.add(item);
                wdh=wdh+item.getWebsitId()+",";
            }
        }
        log.info("网点一对多关系进行分类锁定：[{}][{}]",tid,wd.substring(0,wd.length()-1));

        if (wosn.size() ==0 ){
            log.info("分类锁定无对应的网点：{}",tid);
            // 这里返回一个至少可以正常下单
            return wos.get(0);
            // return null;
        }
        if (wosn.size() ==1){
            log.info("分类锁定网点结果：[{}][{}]",tid,wosn.get(0).getWebsitId());
            return wosn.get(0);
        }

        log.info("分类锁定后网点一对多关系：[{}][{}]",tid,wdh.substring(0,wdh.length()-1));

        // 类别无法锁定时再进行位置过滤
        return getWebsitOrderSign(websitNo, wosn, tid);
    }

    // 平台网点服务的大类信息
    private AdminWebsit websitCategoryZX(List<AdminWebsit> aws,String categoryId,long tid,String type){
        String wd="";
        String wdh="";
        List<AdminWebsit> wosn = new ArrayList<>();


        // 提取格力集团信息
        Brand brandRec = baseService.getBrand();

        // 对应多总包服务商 处理方式与一个总包服务商相同
        // 一个总包服务商多网点：从平台网点服务的类目取对应的网点信息 一般在这里就可以区分开
        for (AdminWebsit item:aws){
            WebsitServiceCategoryDetail wosinfo = getWebCategory(brandRec.getBrandId(),item.getWebsitNumber(), categoryId,type);
            wd=wd+item.getWebsitNumber()+",";
            if (wosinfo !=null){
                wosn.add(item);
                wdh=wdh+item.getWebsitNumber()+",";
            }
        }
        log.info("网点一对多关系进行分类锁定：[{}][{}]",tid,wd.substring(0,wd.length()-1));

        if (wosn.size() ==0 ){
            log.info("分类锁定无对应的网点：{}",tid);
            // 这里返回一个至少可以正常下单
            return aws.get(0);
            // return null;
        }
        if (wosn.size() ==1){
            log.info("分类锁定网点结果：[{}][{}]",tid,wosn.get(0).getWebsitNumber());
            return wosn.get(0);
        }

        log.info("分类锁定后网点一对多关系：[{}][{}]",tid,wdh.substring(0,wdh.length()-1));

        return wosn.get(0);
    }

    @Nullable
    private WebsitOrderSign getWebsitOrderSign(String websitNo, List<WebsitOrderSign> wosn, long tid) {
        Map<String, List<WebsitOrderSign>> wosMap;
        String s = "";
        wosMap = wosn.stream().collect(Collectors.groupingBy(WebsitOrderSign::getWebsitId));
        // 依然有多个时按位置再次过滤 这里指网点的位置
        // 商户网点位置
        BrandWebsit bw = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, websitNo)
                .last("limit 1")
                .one();
        List<String> wdno = new ArrayList<>();
        // 有为空的按经纬度重新提取位置
        if (StringUtils.isEmpty(bw.getSfen()) ||
                StringUtils.isEmpty(bw.getCshi()) ||
                StringUtils.isEmpty(bw.getXian()) ||
                StringUtils.isEmpty(bw.getXzhen()) ||
                StringUtils.isEmpty(bw.getXzhen())){
            Geographicallocation jwd = getRegionByAddress(String.valueOf(bw.getJingdu()),String.valueOf(bw.getWeidu()));
            if (jwd ==null){
                log.info("重新获取商户网点位置失败：{}",tid);
                // throw new RemoteServiceException("重新获取商户网点位置失败！");
                // 这里返回一个至少可以正常下单
                return wosn.get(0);
            }
            bw.setSfen(jwd.getProvince());
            bw.setCshi(jwd.getCity());
            bw.setXian(jwd.getArea());
            bw.setXzhen(jwd.getStreet());
        }

        log.info("商户网点位置：[{}][{}-{}|{}|{}|{}]",tid,websitNo,bw.getSfen(),bw.getCshi(),bw.getXian(),bw.getXzhen());

        // 提取平台网点编号
        wosn.stream().forEach(wd->{
            wdno.add(wd.getWebsitId());
        });
        // 提取平台网点信息
        List<Websit> websits = websitService.lambdaQuery()
                .in(Websit::getWebsitId,wdno)
                .list();
        List<WebsitOrderSign> wosns = new ArrayList<>();
        // 网点的位置信息过滤 最低区级必有值
        for (Websit item:websits){
            // 全址比较
            if (item.getProvince().equals(bw.getSfen()) &&
                    item.getCity().equals(bw.getCshi()) &&
                    item.getArea().equals(bw.getXian()) &&
                    item.getStreet().equals(bw.getXzhen())){
                wosns.add(wosMap.get(item.getWebsitId()).get(0));
            }
            s=s+"["+item.getWebsitId()+"-"+
                    item.getProvince()+"|"+
                    item.getCity()+"|"+
                    item.getArea()+"|"+
                    item.getStreet()+"],";
        }
        log.info("平台服务商网点位置：[{}][{}]",tid,s.substring(0,s.length()-1));

        if (CollectionUtils.isEmpty(wosns)){
            log.info("网点位置销定无对应的网点：{}",tid);
            // return null;
            // 取其一至少可以下派网点
            return wosn.get(0);
        }

        // 全址对应只取其一即可
        log.info("网点位置销定结果：[{}][{}]",tid,wosn.get(0).getWebsitId());
        return wosn.get(0);
    }


    @Nullable
    private CustomerInformationWebsit getCustomerInformationWebsit(String websitNo, List<CustomerInformationWebsit> ciws) {
        Map<String, List<CustomerInformationWebsit>> wosMap;
        wosMap = ciws.stream().collect(Collectors.groupingBy(CustomerInformationWebsit::getWebsitId));
        // 依然有多个时按位置再次过滤 这里指网点的位置
        // 商户网点位置
        BrandWebsit bw = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, websitNo)
                .last("limit 1")
                .one();
        List<String> wdno = new ArrayList<>();
        // 有为空的按经纬度重新提取位置
        if (StringUtils.isNotEmpty(bw.getSfen()) ||
                StringUtils.isNotEmpty(bw.getCshi()) ||
                StringUtils.isNotEmpty(bw.getXian()) ||
                StringUtils.isNotEmpty(bw.getXzhen()) ||
                bw.getXzhen().length()<1){
            Geographicallocation jwd = getRegionByAddress(String.valueOf(bw.getJingdu()),String.valueOf(bw.getWeidu()));
            if (jwd ==null){
                return null;
            }
            bw.setSfen(jwd.getProvince());
            bw.setCshi(jwd.getCity());
            bw.setXian(jwd.getArea());
            bw.setXzhen(jwd.getStreet());
        }

        // 提取平台网点编号
        ciws.stream().forEach(wd->{
            wdno.add(wd.getWebsitId());
        });
        // 提取平台网点信息
        List<Websit> websits = websitService.lambdaQuery()
                .in(Websit::getWebsitId,wdno)
                .list();
        List<CustomerInformationWebsit> wosns = new ArrayList<>();
        // 网点的位置信息过滤 最低区级必有值
        for (Websit item:websits){
            // 全址比较
            if (item.getProvince().equals(bw.getSfen()) &&
                    item.getCity().equals(bw.getCshi()) &&
                    item.getArea().equals(bw.getXian()) &&
                    item.getStreet().equals(bw.getXzhen())){
                wosns.add(wosMap.get(item.getWebsitId()).get(0));
            }
        }
        if (CollectionUtils.isEmpty(wosns)){
            return null;
        }

        // 全址对应只取其一即可
        return ciws.get(0);
    }




    // 平台网点与其服务的大类 商户ID，商户网点，网点服务的大类
    private WebsitServiceCategoryDetail getWebCategory(String brandId,String websitId,String categoryId,String type){
        // 查看平台网点是否服务该类目 不存在即找到的网点不接该服务类型的工单
        return websitServiceCategoryDetailService.lambdaQuery()
                .eq(WebsitServiceCategoryDetail::getBrandId,brandId)
                .eq(WebsitServiceCategoryDetail::getWebsitId,websitId)
                .eq(WebsitServiceCategoryDetail::getType,type)
                .eq(WebsitServiceCategoryDetail::getMainId,categoryId)
                .last("limit 1")
                .one();
    }

    // 提取平台网点名称及服务热线
    private  void getNameAndHotline(WebsitFWS req){
        Websit websit = websitService.lambdaQuery()
                .eq(Websit::getWebsitId,req.getWebsitId())
                .last("limit 1")
                .one();

        if (websit != null){
            if (StringUtils.isNotEmpty(websit.getHotline())){
                req.setWdlxdh(websit.getHotline());
            } else if (StringUtils.isNotEmpty(websit.getOfficePhone())){
                req.setWdlxdh(websit.getOfficePhone());
            }
            if (StringUtils.isNotEmpty(websit.getWebsitName())){
                req.setWdmc(websit.getWebsitName());
            }
        }
    }

    private List<WebsitOrderSign> getWebsitOrderSign(String ppWebsitNo){
        List<WebsitOrderSign> wos = new ArrayList<>();
        wos = websitOrderSignService.lambdaQuery()
                .eq(WebsitOrderSign::getBrandWebsitId, ppWebsitNo)
                .list();
        return wos;
    }


}
