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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.base.WorkerCertificateInfo;
import com.zfire.jiasm.syncdata.base.WorkerCertificateOther;
import com.zfire.jiasm.syncdata.constant.DictTypeEnum;
import com.zfire.jiasm.syncdata.gree.GreeLogic;
import com.zfire.jiasm.syncdata.helper.GreeResponseHelper;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.request.SignSplb;
import com.zfire.jiasm.syncdata.request.WorkUploadInfo;
import com.zfire.jiasm.syncdata.request.ZJInfo;
import com.zfire.jiasm.syncdata.response.Geographicallocation;
import com.zfire.jiasm.syncdata.response.WebsitQXLB;
import com.zfire.jiasm.syncdata.response.WorkerUploadResponse;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.service.UploadWorkeCertificateService;
import com.zfire.jiasm.syncdata.service.UploadWorkerTOGreeService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Slf4j
@Service
public class UploadWorkerTOGreeServiceImpl implements UploadWorkerTOGreeService {
    @Resource
    BaseService baseService;
    @Resource
    OutWorkerUploadService outWorkerUploadService;
    @Resource
    WorkerService workerService;
    @Resource
    WorkerBrandAccountService workerBrandAccountService;
    @Resource
    WorkerStreetService workerStreetService;
    @Resource
    WorkerCertService workerCertService;
    @Resource
    WorkerServiceCategoryDetailService workerServiceCategoryDetailService;
    @Resource
    SysDictRefService sysDictRefService;
    @Resource
    SysDictService sysDictService;
    @Resource
    GreeLogic greeLogic;
    @Resource
    ItfTblWangdianPositionDownloadService itfTblWangdianPositionDownloadService;
    @Resource
    PublicService publicService;
    @Resource
    WebsitWorkerService websitWorkerService;
    @Resource
    InsureWorkerCommercialService insureWorkerCommercialService;
    @Resource
    InsureWorkerCommercialFileService insureWorkerCommercialFileService;
    @Resource
    BrandWebsitService brandWebsitService;
    @Resource
    InsureWorkerRecordService insureWorkerRecordService;
    @Resource
    WorkerCertGreeService workerCertGreeService;
    @Autowired
    UploadWorkeCertificateService uploadWorkeCertificateService;
    @Resource
    BrandLbsRelateService brandLbsRelateService;





    @Override
    public void procesUpdateWorker() throws Exception {
        List<OutWorkerUpload> recs = new ArrayList<>();
        Worker worker = new Worker();
        List<WorkerBrandAccount> workerBrandAccount = new ArrayList<>();
        List<SignSplb> jySignSplbList = new ArrayList<>();
        List<SignSplb> sySignSplbList = new ArrayList<>();
        List<SignSplb> jyList = new ArrayList<>();
        List<SignSplb> syList = new ArrayList<>();
        WebsitQXLB websitqypl = new WebsitQXLB();
        List<ItfTblWangdianPositionDownload> wdwxgGl = new ArrayList<>();
        Map<String,List<ItfTblWangdianPositionDownload>> wdwxgGlMap = new HashMap<>();
        Map<String,List<Worker>> workersMap = new HashMap<>();

        String wxgaddapi = "/outapp/wxg/add";
        String wdqyapi = "/outapp/wxg/base";

        // 提取品牌信息
        Brand brand = baseService.getBrand();
        if (brand == null) {
            return;
        }

        wdwxgGl = itfTblWangdianPositionDownloadService.lambdaQuery().list();
        wdwxgGlMap = wdwxgGl.stream().
                collect(Collectors.groupingBy(ItfTblWangdianPositionDownload::getWxgno));

        // 只提取已入驻的师傅信息
        List<Worker> workers = workerService.lambdaQuery()
                .eq(Worker::getStatus,"ON").list();
        if (CollectionUtils.isNotEmpty(workers)){
            workersMap = workers.stream().
                    collect(Collectors.groupingBy(Worker::getWorkerId));
        }

        // 获取待同步数据
        recs = getWorkerUploads(0);

        // 测试用
        // recs = getWorkerUploadstest();

        for (OutWorkerUpload item:recs){
            try{
                WorkUploadInfo req = new WorkUploadInfo();
                WorkerCertificateInfo workerCertificateInfo = new WorkerCertificateInfo();


                // 获取师傅信息  需要同步的师傅必须在平台系统中存在
                if (!workersMap.containsKey(item.getWorkerId())){
                    // 写入表备注信息
                    updateWorkerUploadLag(item,"师傅尚未在平台中入驻");
                    continue;
                }
                worker = workersMap.get(item.getWorkerId()).get(0);

                // 查看师傅是否已选中品牌商 同步状态为有效状态
                // 这里存在一个商户网点对应平台多个网点
                workerBrandAccount = getWorkerBrandAccount(brand, item, worker);
                if (CollectionUtils.isEmpty(workerBrandAccount)) {
                    continue;
                }

                // 格力一个师傅最多三个有效网点超3个网点不能同步
                if (checkWebsitSum(workerBrandAccount,item)){
                    continue;
                }

                // 这里需要从对应的网点信息中提取师傅服务区域
                // 查看师傅服务区域 规定随便取一个地址即可
                /*workerStreet = getWorkerStreet(item, worker);
                if (workerStreet == null) {
                    continue;
                }*/

                /*// 验证师傅的保险证信息 所有网点都验证 这里只验证是否成功上传了图片
                if (checkBXOutInsureCert(workerBrandAccount)) {
                    continue;
                }*/

                // 按网点同步
                for (WorkerBrandAccount dot: workerBrandAccount){

                    // 看要同步的师傅的证件上否已更新到总部 只有更新到总部才可以执行 有问题但不影响下一个师傅
                    if (checkWaitCert(item, dot)){
                        continue;
                    }

                    // 必填参数 省份-城市-区县-乡镇 找一个即可
                    if (getaddres(item, req, dot)) {
                        continue;
                    }

                    // 提取师傅证件信息，未同步时不处理
                    if (getCertificateInfo(workerCertificateInfo, worker.getWorkerId(),dot)){
                        continue;
                    }

                    /**
                     * 必要条件必须满足，否则不进行数据上传动作 身份证、高空证、保险证
                     * 师傅信息同步到总部的必要条件：
                     * 1：身份证、2：保险，3：高空证(复审时间必填）  这三个条件必备否则无法同步
                     * 制冷证可以没有，如果填了复审时间则必填
                     */
                    if (checkNecessary(item, workerCertificateInfo,worker)){
                        continue;
                    }

                    // 获取签约品类
                    if (getCategory(brand, jySignSplbList, sySignSplbList, worker, item, dot.getWebsitId())){
                        continue;
                    }

                    // 生成数据对象 证件
                    getreq(workerCertificateInfo, req, worker,dot.getWebsitWorkerId());

                    // 师傅所在网点状态
                    req.setFlag("有效");
                    if (worker.getStatus().equals("OFF") || dot.getBrandWorkerStatus().equals("OFF")){
                        req.setFlag("无效");
                    }

                    //网点未签约品类时该网点师傅信息不同步
                    GreeResponseHelper response = getwdqylbxx(wdqyapi,dot.getBrandWebsitId());
                    if (response.getStatus() == 200){
                        // 获取网点签约的品类信息
                        websitqypl = JSONObject.parseObject(response.getData().toString(), WebsitQXLB.class);
                        log.info("总部网点签约的品类信息{}-{}",dot.getBrandWebsitId(),JSONObject.toJSONString(websitqypl));
                    } else {
                        // 写入表备注信息
                        updateWorkerUploadLag(item,"获取网点签约的品类信息失败["+
                                dot.getBrandWebsitId()+"]-"+
                                (StringUtils.isEmpty(response.getMsg()) ? "":response.getMsg()));
                        continue;
                    }

                    log.info("平台师傅签约的品类信息[{}-{}] 家用:{} 商用：{}",
                            worker.getWorkerId(),dot.getWebsitId(),
                            JSONObject.toJSONString(jySignSplbList),JSONObject.toJSONString(sySignSplbList));

                    // 从网点签约的品类信息中过滤师傅的品类信息
                    String result = extracted(jySignSplbList, sySignSplbList, jyList, syList, websitqypl,dot.getBrandWebsitId());
                    if (result.length()>0){
                        // 写入表备注信息
                        updateWorkerUploadLag(item,result);
                        continue;
                    }

                    // 过滤后无品类，不能上传
                    if (jyList.size()<1 && syList.size()<1){
                        // 写入表备注信息
                        updateWorkerUploadLag(item,"师傅签约品类过滤后不能同时为空");
                        continue;
                    }

                    // 按网点过滤师傅的类别信息
                    req.setJySignSplbList(jyList);
                    req.setSySignSplbList(syList);

                    // 服务人员编号(不填值为新增，填值为修改)
                    if (StringUtils.isNotEmpty(dot.getBrandWorkerNumber())){
                        req.setWxgno(dot.getBrandWorkerNumber());
                        // 重置部分数据 以格力门店数据为准
                        resetParameter(wdwxgGlMap, req);
                    }

                    // 所属网点
                    req.setWdno(dot.getBrandWebsitId());

                    // 日志
                    log.info("创建或修改师傅信息request：[{}] {}",dot.getBrandWebsitId(),JSONObject.toJSONString(req));

                    // 同步
                    syncData(dot.getId(), wxgaddapi, item, req,dot.getBrandWebsitId());

                    jyList.clear();
                    syList.clear();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                String msg ="向总部创建师傅同步失败";
                if (ex.getMessage() != null){
                    msg = ex.getMessage();
                }
                if (ex.getMessage() != null && ex.getMessage().trim().length() > 255) {
                    msg = ex.getMessage().substring(0, 255);
                }
                updateWorkerUploadLag(item,msg);
            }
        }
    }

    private boolean checkWaitCert(OutWorkerUpload item, WorkerBrandAccount dot) {
        //先看保险
        WebsitWorker wws = websitWorkerService.lambdaQuery()
                .eq(WebsitWorker::getWorkerId, item.getWorkerId())
                .eq(WebsitWorker::getWebsitId, dot.getWebsitId()).one();
        // 若未更新保险不同步
        if (wws != null) {
            if (StringUtils.isEmpty(wws.getOutInsureCert())){
                if (StringUtils.isEmpty(wws.getInsureFiles())){
                    updateWorkerUploadLag(item,"师傅尚未购买保险或保险未生效【"+item.getWebsitId()+"】");
                } else {
                    updateWorkerUploadLag(item,"等待保险同步【"+item.getWebsitId()+"】");
                }
                return true;
            }
        }
        // 看其他证件是否有待同步
        List<WorkerCert> workerwcs = workerCertService.lambdaQuery()
                .eq(WorkerCert::getWorkerId, item.getWorkerId())
                .list();
        for (WorkerCert w : workerwcs){
            if ((StringUtils.isNotEmpty(w.getFileUrl()) && (StringUtils.isEmpty(w.getOutFileUrl()))) ||
                    (StringUtils.isNotEmpty(w.getFileUrl2()) && (StringUtils.isEmpty(w.getOutFileUrl2())))){
                updateWorkerUploadLag(item,"等待证件同步");
                return true;
            }
        }

        // 查看人脸面像
        Worker worker = workerService.lambdaQuery()
                .eq(Worker::getWorkerId, item.getWorkerId()).one();
        if (!"YES".equals(worker.getIsReal())){
            updateWorkerUploadLag(item,"未实名认证");
            return false;
        }
        if (StringUtils.isEmpty(worker.getAuthHeadImage())){
            updateWorkerUploadLag(item,"未上传实名认证头像");
            return false;
        }

        // 已认证就需要同步
        if (StringUtils.isEmpty(worker.getOutAuthHeadImage())){
            updateWorkerUploadLag(item,"等待实名认证头像同步");
            return true;
        }

        return false;
    }

    @Override
    public void procesAddCertWorker(){
        List<OutWorkerUpload> recs = new ArrayList<>();

        // 获取待入驻的新网点师傅数据
        recs = getWorkerUploads(1);

        // 测试用
        // recs = getWorkerUploadstest();

        for (OutWorkerUpload item:recs){
            try{
                // 先同步保险数据
                if (!uploadWorkeCertificateService.uploadWorkeBx(item)){
                    continue;
                }

                // 再同步其他证件数据
                if (!uploadWorkeCertificateService.UploadWorkeCert(item)){
                    continue;
                }

                // 再同步人脸面像 非必备条件
                uploadWorkeCertificateService.uploadWorkeFace(item);

            } catch (Exception ex) {
                ex.printStackTrace();
                String msg ="向总部同步师傅入驻指定网点证件失败";
                if (ex.getMessage() != null){
                    msg = ex.getMessage();
                }
                if (ex.getMessage() != null && ex.getMessage().trim().length() > 255) {
                    msg = ex.getMessage().substring(0, 255);
                }
                updateWorkerUploadLag(item,msg);
            }
        }
    }

    @Override
    public void procesAddWorker() throws Exception {
        List<OutWorkerUpload> recs = new ArrayList<>();
        Worker worker = new Worker();
        List<WorkerBrandAccount> workerBrandAccount = new ArrayList<>();
        List<SignSplb> jySignSplbList = new ArrayList<>();
        List<SignSplb> sySignSplbList = new ArrayList<>();
        List<SignSplb> jyList = new ArrayList<>();
        List<SignSplb> syList = new ArrayList<>();
        WebsitQXLB websitqypl = new WebsitQXLB();
        List<ItfTblWangdianPositionDownload> wdwxgGl = new ArrayList<>();
        Map<String,List<ItfTblWangdianPositionDownload>> wdwxgGlMap = new HashMap<>();
        Map<String,List<Worker>> workersMap = new HashMap<>();

        String wxgaddapi = "/outapp/wxg/add";
        String wdqyapi = "/outapp/wxg/base";

        // 提取品牌信息
        Brand brand = baseService.getBrand();
        if (brand == null) {
            return;
        }

        wdwxgGl = itfTblWangdianPositionDownloadService.lambdaQuery().list();
        wdwxgGlMap = wdwxgGl.stream().
                collect(Collectors.groupingBy(ItfTblWangdianPositionDownload::getWxgno));

        // 只提取已入驻的师傅信息
        List<Worker> workers = workerService.lambdaQuery()
                .eq(Worker::getStatus,"ON").list();
        if (CollectionUtils.isNotEmpty(workers)){
            workersMap = workers.stream().
                    collect(Collectors.groupingBy(Worker::getWorkerId));
        }

        // 获取待入驻的新网点师傅数据
        recs = getWorkerUploads(1);

        // 测试用
        // recs = getWorkerUploadstest();

        for (OutWorkerUpload item:recs){
            try{
                WorkUploadInfo req = new WorkUploadInfo();
                WorkerCertificateInfo workerCertificateInfo = new WorkerCertificateInfo();

                // 获取师傅信息  需要同步的师傅必须在平台系统中存在
                if (!workersMap.containsKey(item.getWorkerId())){
                    // 写入表备注信息
                    updateWorkerUploadLag(item,"师傅尚未在平台中入驻");
                    continue;
                }
                worker = workersMap.get(item.getWorkerId()).get(0);

                // 查看师傅是否已选中品牌商 同步状态为有效状态
                // 这里存在一个商户网点对应平台多个网点
                workerBrandAccount = getWorkerBrandAccount(brand, item, worker);
                if (CollectionUtils.isEmpty(workerBrandAccount)) {
                    continue;
                }

                // 格力一个师傅最多三个有效网点超3个网点不能同步
                if (checkWebsitSum(workerBrandAccount,item)){
                    continue;
                }

                // 按网点同步
                for (WorkerBrandAccount dot: workerBrandAccount){
                    // 找出指定网点 三方网点与平台网点
                    if (!(dot.getBrandWebsitId().equals(item.getBrandWebsitId()) &&
                            dot.getWebsitId().equals(item.getWebsitId()))){
                        continue;
                    }

                    // 必填参数 省份-城市-区县-乡镇 找一个即可
                    if (getaddres(item, req, dot)) {
                        continue;
                    }

                    // 提取师傅证件信息，未同步时不处理
                    if (getCertificateInfo(workerCertificateInfo, worker.getWorkerId(),dot)){
                        continue;
                    }

                    /**
                     * 必要条件必须满足，否则不进行数据上传动作 身份证、高空证、保险证
                     * 师傅信息同步到总部的必要条件：
                     * 1：身份证、2：保险，3：高空证(复审时间必填）  这三个条件必备否则无法同步
                     * 制冷证可以没有，如果填了复审时间则必填
                     */
                    if (checkNecessary(item, workerCertificateInfo,worker)){
                        continue;
                    }

                    // 获取签约品类
                    if (getCategory(brand, jySignSplbList, sySignSplbList, worker, item, dot.getWebsitId())){
                        continue;
                    }

                    // 生成数据对象 证件 是否在黑名单需修改成以平台状态为准
                    getreq(workerCertificateInfo, req, worker,dot.getWebsitWorkerId());

                    // 师傅所在网点状态
                    req.setFlag("有效");
                    if (worker.getStatus().equals("OFF") || dot.getBrandWorkerStatus().equals("OFF")){
                        req.setFlag("无效");
                    }

                    //网点未签约品类时该网点师傅信息不同步
                    GreeResponseHelper response = getwdqylbxx(wdqyapi,dot.getBrandWebsitId());
                    if (response.getStatus() == 200){
                        // 获取网点签约的品类信息
                        websitqypl = JSONObject.parseObject(response.getData().toString(), WebsitQXLB.class);
                        log.info("总部网点签约的品类信息{}-{}",dot.getBrandWebsitId(),JSONObject.toJSONString(websitqypl));
                    } else {
                        // 写入表备注信息
                        updateWorkerUploadLag(item,"获取网点签约的品类信息失败["+
                                dot.getBrandWebsitId()+"]-"+
                                (StringUtils.isEmpty(response.getMsg()) ? "":response.getMsg()));
                        continue;
                    }

                    log.info("平台师傅签约的品类信息[{}-{}] 家用:{} 商用：{}",
                            worker.getWorkerId(),dot.getWebsitId(),
                            JSONObject.toJSONString(jySignSplbList),JSONObject.toJSONString(sySignSplbList));

                    // 从网点签约的品类信息中过滤师傅的品类信息
                    String result = extracted(jySignSplbList, sySignSplbList, jyList, syList, websitqypl,dot.getBrandWebsitId());
                    if (result.length()>0){
                        // 写入表备注信息
                        updateWorkerUploadLag(item,result);
                        continue;
                    }

                    // 过滤后无品类，不能上传
                    if (jyList.size()<1 && syList.size()<1){
                        // 写入表备注信息
                        updateWorkerUploadLag(item,"师傅签约品类过滤后不能同时为空");
                        continue;
                    }

                    // 按网点过滤师傅的类别信息
                    req.setJySignSplbList(jyList);
                    req.setSySignSplbList(syList);

                    // 服务人员编号(不填值为新增，填值为修改)
                    if (StringUtils.isNotEmpty(dot.getBrandWorkerNumber())){
                        req.setWxgno(dot.getBrandWorkerNumber());
                        // 重置部分数据 以格力门店数据为准
                        resetParameter(wdwxgGlMap, req);
                    }

                    // 所属网点
                    req.setWdno(dot.getBrandWebsitId());

                    // 日志
                    log.info("创建或修改师傅信息request：{}",JSONObject.toJSONString(req));

                    // 同步
                    syncData(dot.getId(), wxgaddapi, item, req,dot.getBrandWebsitId());

                    jyList.clear();
                    syList.clear();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                String msg ="师傅入驻新网点、向总部创建师傅同步失败";
                if (ex.getMessage() != null){
                    msg = ex.getMessage();
                }
                if (ex.getMessage() != null && ex.getMessage().trim().length() > 255) {
                    msg = ex.getMessage().substring(0, 255);
                }
                updateWorkerUploadLag(item,msg);
            }
        }
    }

    private boolean getaddresbak(OutWorkerUpload item, WorkUploadInfo req, @NotNull WorkerBrandAccount dot) {
        WebsitWorker wwone = websitWorkerService.lambdaQuery()
                .eq(WebsitWorker::getWebsitWorkerId, dot.getWebsitWorkerId()).one();
        if (StringUtils.isNotEmpty(wwone.getLng()) && StringUtils.isNotEmpty(wwone.getLat())){
            Geographicallocation ps = publicService.getRegionByAddress(wwone.getLng(),wwone.getLat());
            if (ps != null){
                req.setSfen(ps.getProvince());
                req.setCshi(ps.getCity());
                req.setCounty(ps.getArea());
                req.setXzhen(ps.getStreet());
                return Boolean.FALSE;
            }
        }

        // 使用商户网点的位置 一般是存在的
        BrandWebsit bw = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, dot.getBrandWebsitId())
                .last("limit 1").one();
        if (bw ==null){
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(bw.getSfen()) &&
                StringUtils.isNotEmpty(bw.getCshi()) &&
                StringUtils.isNotEmpty(bw.getXian()) &&
                StringUtils.isNotEmpty(bw.getXzhen())){
            req.setSfen(bw.getSfen());
            req.setCshi(bw.getCshi());
            req.setCounty(bw.getXian());
            req.setXzhen(bw.getXzhen());
            return Boolean.FALSE;
        }

        if (bw.getJingdu() !=null && bw.getWeidu() !=null){
            Geographicallocation gs = publicService.getRegionByAddress(String.valueOf(bw.getJingdu()),String.valueOf(bw.getWeidu()));
            if (gs != null){
                req.setSfen(gs.getProvince());
                req.setCshi(gs.getCity());
                req.setCounty(gs.getArea());
                req.setXzhen(gs.getStreet());
                return Boolean.FALSE;
            }
        }

        // 写入表备注信息
        updateWorkerUploadLag(item,"师傅服务区域信息不存在");
        return Boolean.TRUE;
    }

    private boolean getaddres(OutWorkerUpload item, WorkUploadInfo req, @NotNull WorkerBrandAccount dot) {
        // 使用商户网点的位置 一般是存在的
        BrandWebsit bw = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, dot.getBrandWebsitId())
                .last("limit 1").one();
        if (bw == null){
            return Boolean.TRUE;
        }
        if (bw.getAddressSuccess().equals(-1)){
            updateWorkerUploadLag(item,"省市区街道信息有误,请及时维护【"+dot.getBrandWebsitId()+"】");
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(bw.getSfen()) &&
                StringUtils.isNotEmpty(bw.getCshi()) &&
                StringUtils.isNotEmpty(bw.getXian()) &&
                StringUtils.isNotEmpty(bw.getXzhen())){
            req.setSfen(bw.getSfen());
            req.setCshi(bw.getCshi());
            req.setCounty(bw.getXian());
            req.setXzhen(bw.getXzhen());
            return Boolean.FALSE;
        }

        if (bw.getJingdu() !=null && bw.getWeidu() !=null){
            Geographicallocation gs = publicService.getRegionByAddress(String.valueOf(bw.getJingdu()),String.valueOf(bw.getWeidu()));
            if (gs != null){
                req.setSfen(gs.getProvince());
                req.setCshi(gs.getCity());
                req.setCounty(gs.getArea());
                req.setXzhen(gs.getStreet());
                return Boolean.FALSE;
            }
        }

        // 写入表备注信息
        updateWorkerUploadLag(item,"省市区街道信息不存在,请及时维护【"+dot.getBrandWebsitId()+"】");
        return Boolean.TRUE;
    }


    // 商户网点与平台网点存在一对多，确保正确，所有平台网点的图片都必须生成
    private boolean checkBXOutInsureCert(List<WorkerBrandAccount> workerBrandAccount) {
        List<String> websitworkerids = new ArrayList<>();
        workerBrandAccount.stream().forEach(wba->{
            websitworkerids.add(wba.getWebsitId());
        });
        List<WebsitWorker> websitWorkers = websitWorkerService.lambdaQuery()
                .in(WebsitWorker::getWebsitWorkerId,websitworkerids)
                .eq(WebsitWorker::getOutInsureCert,"").list();
        if (CollectionUtils.isNotEmpty(websitWorkers)){
            return true;
        }
        return false;
    }

    private void resetParameter(Map<String, List<ItfTblWangdianPositionDownload>> wdwxgGlMap,
                                WorkUploadInfo req) {
        if (wdwxgGlMap.containsKey(req.getWxgno())){
            ItfTblWangdianPositionDownload i = wdwxgGlMap.get(req.getWxgno()).get(0);
            req.setFlag(i.getFlag());
            // req.setFwryzpfj(i.getFwryzpfj());
            req.setGzkkh(i.getGzkkh());
            req.setHmdbz(i.getHmdbz());
            req.setIfmessageFlag(i.getIfmessageFlag());
            req.setPxhgkssj(i.getPxhgkssj());
            req.setQbaoid(i.getQbaoid());
            req.setQbcheackFlag(i.getQbcheackFlag());
            req.setSfdswxgFlag(i.getSfdswxgFlag());
            req.setVipflag(i.getVipflag());
            req.setMaxaznum(i.getMaxaznum());
            req.setMaxwxnum(i.getMaxwxnum());
        } else {
            // extractedPCAS(req, workerStreet);
        }
    }

    private String extractedBak(List<SignSplb> jySignSplbList, List<SignSplb> sySignSplbList,
                             List<SignSplb> jyList, List<SignSplb> syList, WebsitQXLB websitqypl,String brandWebsitNumber) {
        String result = "";
        List<String> syfwlbList = new ArrayList<>();
        List<String> jyfwlbList = new ArrayList<>();

        if (websitqypl.getJySignSplbList().size()<1 && websitqypl.getSySignSplbList().size()<1){
            result="网点【"+brandWebsitNumber+"】暂未签约品类";
            return result;
        }

        // 过滤商用品类
        if (websitqypl.getSySignSplbList().size()>0 && sySignSplbList.size()>0){
            // 大类名称 取交集
            websitqypl.getSySignSplbList().get(0).getFwlbList().retainAll(sySignSplbList.get(0).getFwlbList());
            syfwlbList.addAll(websitqypl.getSySignSplbList().get(0).getFwlbList());
            // 品类
            for (SignSplb sy : sySignSplbList){
                for (SignSplb wdsy: websitqypl.getSySignSplbList()){
                    if (wdsy.getSpdl().equals(sy.getSpdl())){
                        /*if (wdsy.getSpxl()==null){
                            result="网点:【"+brandWebsitNumber+"】商用大类【"+wdsy.getSpdl()+"】下未签约小类";
                            return result;
                        }*/
                        SignSplb one = new SignSplb();
                        one.setFwlbList(syfwlbList);
                        one.setSpdl(wdsy.getSpdl());
                        one.setSpdlName(wdsy.getSpdlName());
                        if (wdsy.getSpxl()!=null){
                            one.setSpxl(wdsy.getSpxl());
                            one.setSpxlName(wdsy.getSpxlName());
                        }
                        syList.add(one);
                    }
                }
            }
        }

        // 过滤家用品类
        if (websitqypl.getJySignSplbList().size()>0 && jySignSplbList.size()>0){
            // 大类名称 取交集
            websitqypl.getJySignSplbList().get(0).getFwlbList().retainAll(jySignSplbList.get(0).getFwlbList());
            jyfwlbList.addAll(websitqypl.getJySignSplbList().get(0).getFwlbList());
            // 品类
            for (SignSplb sy : jySignSplbList){
                for (SignSplb wdjy: websitqypl.getJySignSplbList()){
                    if (wdjy.getSpdl().equals(sy.getSpdl()) ){
                        /*if (wdjy.getSpxl()==null){
                            result="网点:【"+brandWebsitNumber+"】家用大类【"+wdjy.getSpdl()+"】下未签约小类";
                            return result;
                        }*/
                        SignSplb one = new SignSplb();
                        one.setFwlbList(jyfwlbList);
                        one.setSpdl(wdjy.getSpdl());
                        one.setSpdlName(wdjy.getSpdlName());
                        if (wdjy.getSpxl()==null){
                            one.setSpxl(wdjy.getSpxl());
                            one.setSpxlName(wdjy.getSpxlName());
                        }
                        jyList.add(one);
                    }
                }
            }
        }

        return result;
    }

    private String extractedBakt(List<SignSplb> jySignSplbList, List<SignSplb> sySignSplbList,
                             List<SignSplb> jyList, List<SignSplb> syList, WebsitQXLB websitqypl,String brandWebsitNumber) {
        String result = "";

        if (websitqypl.getJySignSplbList().size()<1 && websitqypl.getSySignSplbList().size()<1){
            result="网点【"+brandWebsitNumber+"】暂未签约品类";
            return result;
        }

        // 过滤商用品类
        if (websitqypl.getSySignSplbList().size()>0 && sySignSplbList.size()>0){
            for (SignSplb sy : sySignSplbList) {
                for (SignSplb wdsy : websitqypl.getSySignSplbList()) {
                    if (wdsy.getSpdl().equals(sy.getSpdl())){
                        List<String> syfwlbList = new ArrayList<>();
                        // 大类名称 取交集
                        wdsy.getFwlbList().retainAll(sy.getFwlbList());
                        syfwlbList.addAll(wdsy.getFwlbList());
                        SignSplb one = new SignSplb();
                        one.setFwlbList(syfwlbList);
                        one.setSpdl(wdsy.getSpdl());
                        one.setSpdlName(wdsy.getSpdlName());
                        // 以总部的为准
                        if (wdsy.getSpxl()!=null){
                            one.setSpxl(wdsy.getSpxl());
                            one.setSpxlName(wdsy.getSpxlName());
                        }
                        syList.add(one);
                        break;
                    }
                }
            }
        }

        // 过滤家用品类
        if (websitqypl.getJySignSplbList().size()>0 && jySignSplbList.size()>0){
            for (SignSplb jy : jySignSplbList) {
                for (SignSplb wdjy : websitqypl.getJySignSplbList()) {
                    if (wdjy.getSpdl().equals(jy.getSpdl()) ){
                        List<String> jyfwlbList = new ArrayList<>();
                        // 大类名称 取交集
                        wdjy.getFwlbList().retainAll(jy.getFwlbList());
                        jyfwlbList.addAll(wdjy.getFwlbList());
                        SignSplb one = new SignSplb();
                        one.setFwlbList(jyfwlbList);
                        one.setSpdl(wdjy.getSpdl());
                        one.setSpdlName(wdjy.getSpdlName());
                        if (wdjy.getSpxl()!=null){
                            one.setSpxl(wdjy.getSpxl());
                            one.setSpxlName(wdjy.getSpxlName());
                        }
                        jyList.add(one);
                        break;
                    }
                }
            }
        }

        return result;
    }

    private String extracted(List<SignSplb> jySignSplbList, List<SignSplb> sySignSplbList,
                             List<SignSplb> jyList, List<SignSplb> syList, WebsitQXLB websitqypl,String brandWebsitNumber) {
        String result = "";

        if (websitqypl.getJySignSplbList().size()<1 && websitqypl.getSySignSplbList().size()<1){
            result="网点【"+brandWebsitNumber+"】暂未签约品类";
            return result;
        }

        // 过滤商用品类
        if (websitqypl.getSySignSplbList().size()>0 && sySignSplbList.size()>0){
            for (SignSplb wdsy : websitqypl.getSySignSplbList()) {
                for (SignSplb sy : sySignSplbList) {
                    if (sy.getSpdl().equals(wdsy.getSpdl())){
                        List<String> syfwlbList = new ArrayList<>();
                        // 大类名称 取交集
                        wdsy.getFwlbList().retainAll(sy.getFwlbList());
                        syfwlbList.addAll(wdsy.getFwlbList());
                        SignSplb one = new SignSplb();
                        one.setFwlbList(syfwlbList);
                        one.setSpdl(wdsy.getSpdl());
                        one.setSpdlName(wdsy.getSpdlName());
                        // 以总部的为准
                        if (wdsy.getSpxl()!=null){
                            one.setSpxl(wdsy.getSpxl());
                            one.setSpxlName(wdsy.getSpxlName());
                        }
                        syList.add(one);
                        break;
                    }
                }
            }
        }

        // 过滤家用品类
        if (websitqypl.getJySignSplbList().size()>0 && jySignSplbList.size()>0){
            for (SignSplb wdjy : websitqypl.getJySignSplbList()) {
                for (SignSplb jy : jySignSplbList) {
                    if (jy.getSpdl().equals(wdjy.getSpdl()) ){
                        List<String> jyfwlbList = new ArrayList<>();
                        // 大类名称 取交集
                        wdjy.getFwlbList().retainAll(jy.getFwlbList());
                        jyfwlbList.addAll(wdjy.getFwlbList());
                        SignSplb one = new SignSplb();
                        one.setFwlbList(jyfwlbList);
                        one.setSpdl(wdjy.getSpdl());
                        one.setSpdlName(wdjy.getSpdlName());
                        // 小类存在取总部小类数据
                        if (wdjy.getSpxl()!=null){
                            one.setSpxl(wdjy.getSpxl());
                            one.setSpxlName(wdjy.getSpxlName());
                        }
                        jyList.add(one);
                        break;
                    }
                }
            }
        }

        return result;
    }

    private Boolean checkNecessary(OutWorkerUpload item, WorkerCertificateInfo workerCertificateInfo,Worker worker) {
        if (StringUtils.isEmpty(workerCertificateInfo.getSfzbh())){
            updateWorkerUploadLag(item,"师傅缺失身份证信息，不上传");
            return Boolean.TRUE;
        }

        if (StringUtils.isEmpty(workerCertificateInfo.getBxz())){
            updateWorkerUploadLag(item,"师傅缺失保险信息，不上传");
            return Boolean.TRUE;
        }
        if (StringUtils.isEmpty(workerCertificateInfo.getGkzbh())){
            updateWorkerUploadLag(item,"师傅缺失高空证信息，不上传");
            return Boolean.TRUE;
        } else {
            if (StringUtils.isEmpty(workerCertificateInfo.getGkzfsrq())){
                updateWorkerUploadLag(item,"师傅高空证缺失证件复审时间:，不上传");
                return Boolean.TRUE;
            }
        }

        if (StringUtils.isNotEmpty(workerCertificateInfo.getZlgbh())){
            if (StringUtils.isEmpty(workerCertificateInfo.getZlgfsrq())){
                updateWorkerUploadLag(item,"师傅制冷证缺失证件复审时间:，不上传");
                return Boolean.TRUE;
            }
        }

        // 已进行人脸面像认证未上传不同步
        if (StringUtils.isNotEmpty(worker.getAuthHeadImage()) &&
                StringUtils.isEmpty(workerCertificateInfo.getFacepath())){
            updateWorkerUploadLag(item,"师傅人脸面像待同步中");
            return Boolean.TRUE;
        }


        return Boolean.FALSE;
    }

    // 1：身份证、2：保险，3：高空证(复审时间必填）  这三个条件必备否则无法同步
    private Boolean getCertificateInfo(WorkerCertificateInfo workerCertificateInfo, String workerId, WorkerBrandAccount dot) throws ParseException {
        // 提取师傅所在网点的证件信息
        List<WorkerCertGree> wcgs = workerCertGreeService.lambdaQuery()
                .eq(WorkerCertGree::getWorkerId,workerId)
                .eq(WorkerCertGree::getBrandWebsitId,dot.getBrandWebsitId()).list();

        if (CollectionUtils.isEmpty(wcgs)){
            return Boolean.FALSE;
        }

        // 身份证
        if (getsfz(workerCertificateInfo, workerId, wcgs)){
            return Boolean.FALSE;
        }
        // 保险证
        if (getbxz(workerCertificateInfo, dot, wcgs)){
            return Boolean.FALSE;
        }
        // 高空作业证
        if (getgkz(workerCertificateInfo, workerId, wcgs)){
            return Boolean.FALSE;
        }
        // 上岗资格证
        getsgz(workerCertificateInfo, workerId, wcgs);

        // 制冷工证
        getzlz(workerCertificateInfo, workerId, wcgs);

        // 驾驶证
        getjsz(workerCertificateInfo, workerId,wcgs);

        // 电工证
        getdgz(workerCertificateInfo, workerId,wcgs);

        // 焊工证
        gethgz(workerCertificateInfo, workerId,wcgs);

        // 人脸面像
        gethFace(workerCertificateInfo, workerId,wcgs);

        // 其他资格证
        getotherz(workerCertificateInfo, workerId,wcgs);


        return Boolean.FALSE;
    }


    // true为有信息
    private Boolean getjsz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("驾驶证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }


        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setJszz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setJszf(wcg.getOutFileUrl2());
        }

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setJszyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setJszfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    private Boolean getdgz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("电工证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }


        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setDgzz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setDgzf(wcg.getOutFileUrl2());
        }

        workerCertificateInfo.setDgbh(wcg.getCertNo());

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setDgyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setDgfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    private Boolean gethgz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("焊工证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }


        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setHgzz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setHgzf(wcg.getOutFileUrl2());
        }

        workerCertificateInfo.setHgbh(wcg.getCertNo());

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setHgyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setHgfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    private Boolean gethFace(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("人脸面像".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        workerCertificateInfo.setFacecheck(0);
        workerCertificateInfo.setHasfaceFlag(Boolean.FALSE);

        // 无不处理
        if (wcg == null){
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setFacecheck(1);
            workerCertificateInfo.setFacepath(wcg.getOutFileUrl());
            workerCertificateInfo.setHasfaceFlag(Boolean.TRUE);
        }

        return Boolean.FALSE;
    }

    private Boolean getotherz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;
        List<WorkerCertificateOther> others = new ArrayList<>();

        for (WorkerCertGree item :wcgs){
            if (!("身份证".equals(item.getCertName())) &&
                    !("保险证".equals(item.getCertName())) &&
                    !("高空作业证".equals(item.getCertName())) &&
                    !("上岗资格证".equals(item.getCertName())) &&
                    !("制冷工证".equals(item.getCertName())) &&
                    !("驾驶证".equals(item.getCertName())) &&
                    !("电工证".equals(item.getCertName())) &&
                    !("焊工证".equals(item.getCertName())) &&
                    !("人脸面像".equals(item.getCertName()))){
                wcg= item;
                WorkerCertificateOther other = new WorkerCertificateOther();

                other.setName(wcg.getCertName());

                if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
                    other.setZz(wcg.getOutFileUrl());
                }
                if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
                    other.setZf(wcg.getOutFileUrl2());
                }

                if (StringUtils.isNotEmpty(wcg.getCertNo())){
                    other.setBh(wcg.getCertNo());
                }

                if (wcg.getExpireTime() !=null){
                    other.setYxq(datesub(wcg.getExpireTime()));
                }
                if (wcg.getReviewTime() !=null){
                    other.setFsrq(datesub(wcg.getReviewTime()));
                }

                others.add(other);
            }
        }

        if (others.size()>0){
            workerCertificateInfo.getOther().addAll(others);
        }

        return Boolean.FALSE;
    }

    // true为有信息
    private Boolean getzlz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("制冷工证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setZlgz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setZlgf(wcg.getOutFileUrl2());
        }
        workerCertificateInfo.setZlgbh(wcg.getCertNo());

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setZlgyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setZlgfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    // true为有证件
    private Boolean getsgz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("上岗资格证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setSgzgzz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setSgzgzf(wcg.getOutFileUrl2());
        }
        workerCertificateInfo.setSgzgzbh(wcg.getCertNo());

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setSgzgzyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setSgzgzfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    // true为有信息
    private Boolean getgkzBak(WorkerCertificateInfo workerCertificateInfo, String workerId) throws ParseException {
        WorkerCert certificate = getCertificateInfo(workerId,"高空作业证");
        // 无证件不处理
        if (certificate == null){
            return Boolean.TRUE;
        }
        // 身份证信息未同步，不操作
        if (!certificate.getOutSyncStatus().equals(1)){
            return Boolean.TRUE;
        }
        if (StringUtils.isNotEmpty(certificate.getOutFileUrl())){
            workerCertificateInfo.setGkzz(certificate.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(certificate.getOutFileUrl2())){
            workerCertificateInfo.setGkzf(certificate.getOutFileUrl2());
        }
        workerCertificateInfo.setGkzbh(certificate.getCertNo());

        if (certificate.getExpireTime() !=null){
            workerCertificateInfo.setGkzyxq(datesub(certificate.getExpireTime()));
        }
        if (certificate.getReviewTime() !=null){
            workerCertificateInfo.setGkzfsrq(datesub(certificate.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    private Boolean getgkz(WorkerCertificateInfo workerCertificateInfo, String workerId,List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("高空作业证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setGkzz(wcg.getOutFileUrl());
            workerCertificateInfo.setGkzyztp(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setGkzf(wcg.getOutFileUrl2());
            workerCertificateInfo.setGkzyztp(wcg.getOutFileUrl2());
        }
        workerCertificateInfo.setGkzbh(wcg.getCertNo());


        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setGkzyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setGkzfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    // true为有信息
    private Boolean getbxzBak(WorkerCertificateInfo workerCertificateInfo,WorkerBrandAccount dot) {
        // 查家用保险
        WebsitWorker wk = websitWorkerService.lambdaQuery()
                .eq(WebsitWorker::getWorkerId, dot.getWorkerId())
                .eq(WebsitWorker::getWebsitId, dot.getWebsitId())
                .last("limit 1").one();
        if (wk !=null){
            if (org.apache.commons.lang3.StringUtils.isEmpty(wk.getOutInsureCert())){
                // 证件信息未同步，不操作
                return Boolean.TRUE;
            }
            workerCertificateInfo.setBxz(wk.getOutInsureCert());
            if (wk.getGmbxyxq() !=null){
                workerCertificateInfo.setBxyxq(datesub(wk.getGmbxyxq()));
            }
        }

        // 师傅必买保险，所以不会不存在
        return Boolean.FALSE;
    }


    private Boolean getbxz(WorkerCertificateInfo workerCertificateInfo,WorkerBrandAccount dot, List<WorkerCertGree> wcgs) {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("保险证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }


        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setBxz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setBxf(wcg.getOutFileUrl2());
        }
        workerCertificateInfo.setSafeNo(wcg.getSafeCompany() + "/" + wcg.getCertNo());

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setBxyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setBxfsrq(datesub(wcg.getReviewTime()));
        }

        // 师傅必买保险，所以不会不存在
        return Boolean.FALSE;
    }

    // true为有信息
    private Boolean getbxzInsBak(WorkerCertificateInfo workerCertificateInfo,WorkerBrandAccount dot) {
        InsureWorkerCommercial iwc = insureWorkerCommercialService.lambdaQuery()
                .eq(InsureWorkerCommercial::getWorkerId, dot.getWorkerId())
                // .eq(InsureWorkerCommercial::getWebsitId, dot.getWebsitId())
                .orderByDesc(InsureWorkerCommercial::getEndTime)
                .last("limit 1").one();
        if (iwc != null){
            InsureWorkerCommercialFile iwcf = insureWorkerCommercialFileService.lambdaQuery()
                    .eq(InsureWorkerCommercialFile::getInsureWorkerCommercialId,iwc.getId())
                    .last("limit 1").one();
            if (iwcf !=null){
                if (org.apache.commons.lang3.StringUtils.isEmpty(iwcf.getOutZjlj())){
                    // 证件信息未同步，不操作
                    return Boolean.TRUE;
                }
                workerCertificateInfo.setBxz(iwcf.getOutZjlj());
                if (iwc.getEndTime() !=null){
                    workerCertificateInfo.setBxyxq(datesub(iwc.getEndTime()));
                }
                return Boolean.FALSE;
            }
        }

        // 查家用保险
        InsureWorkerRecord wk = insureWorkerRecordService.lambdaQuery()
                .eq(InsureWorkerRecord::getWorkerId, dot.getWorkerId())
                //.eq(InsureWorkerRecord::getWebsitId, dot.getWebsitId())
                .orderByDesc(InsureWorkerRecord::getEndTime)
                .last("limit 1").one();
        if (wk !=null){
            if (org.apache.commons.lang3.StringUtils.isEmpty(wk.getOutVoucher())){
                // 证件信息未同步，不操作
                return Boolean.TRUE;
            }
            workerCertificateInfo.setBxz(wk.getOutVoucher());
            if (wk.getEndTime() !=null){
                workerCertificateInfo.setBxyxq(datesub(wk.getEndTime()));
            }
        }

        // 师傅必买保险，所以不会不存在
        return Boolean.FALSE;
    }

    private Boolean getbxzBak(WorkerCertificateInfo workerCertificateInfo, String workerId) throws ParseException {
        WorkerCert certificate = getCertificateInfo(workerId,"保险证");
        // 无证件不处理
        if (certificate == null){
            return Boolean.TRUE;
        }
        // 身份证信息未同步，不操作
        if (!certificate.getOutSyncStatus().equals(1)){
            return Boolean.TRUE;
        }
        if (StringUtils.isNotEmpty(certificate.getOutFileUrl())){
            workerCertificateInfo.setBxz(certificate.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(certificate.getOutFileUrl2())){
            workerCertificateInfo.setBxf(certificate.getOutFileUrl2());
        }

        if (certificate.getExpireTime() !=null){
            workerCertificateInfo.setBxyxq(datesub(certificate.getExpireTime()));
        }
        if (certificate.getReviewTime() !=null){
            workerCertificateInfo.setBxfsrq(datesub(certificate.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    private Boolean getsfzBak(WorkerCertificateInfo workerCertificateInfo, String workerId) throws ParseException {
        WorkerCert certificate = getCertificateInfo(workerId,"身份证");

        // 无证件不处理
        if (certificate == null){
            return Boolean.TRUE;
        }

        // 身份证信息未同步，不操作
        if (!certificate.getOutSyncStatus().equals(1)){
            return Boolean.TRUE;
        }
        if (StringUtils.isNotEmpty(certificate.getOutFileUrl())){
            workerCertificateInfo.setSfzz(certificate.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(certificate.getOutFileUrl2())){
            workerCertificateInfo.setSfzf(certificate.getOutFileUrl2());
        }
        workerCertificateInfo.setSfzbh(certificate.getCertNo());

        if (certificate.getExpireTime() !=null){
            workerCertificateInfo.setSfzyxq(datesub(certificate.getExpireTime()));
        }
        if (certificate.getReviewTime() !=null){
            workerCertificateInfo.setSfzfsrq(datesub(certificate.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    private Boolean getsfz(WorkerCertificateInfo workerCertificateInfo, String workerId, List<WorkerCertGree> wcgs) throws ParseException {
        WorkerCertGree wcg = null;

        for (WorkerCertGree item :wcgs){
            if ("身份证".equals(item.getCertName())){
                wcg= item;
                break;
            }
        }

        // 无证件不处理
        if (wcg == null){
            return Boolean.TRUE;
        }

        if (StringUtils.isNotEmpty(wcg.getOutFileUrl())){
            workerCertificateInfo.setSfzz(wcg.getOutFileUrl());
        }
        if (StringUtils.isNotEmpty(wcg.getOutFileUrl2())){
            workerCertificateInfo.setSfzf(wcg.getOutFileUrl2());
        }
        workerCertificateInfo.setSfzbh(wcg.getCertNo());

        if (wcg.getExpireTime() !=null){
            workerCertificateInfo.setSfzyxq(datesub(wcg.getExpireTime()));
        }
        if (wcg.getReviewTime() !=null){
            workerCertificateInfo.setSfzfsrq(datesub(wcg.getReviewTime()));
        }

        return Boolean.FALSE;
    }

    public String datesub(@NotNull Date d) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        long localTime = d.getTime()-8*60*60*1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(localTime);
        Date utcDate = new Date(calendar.getTimeInMillis());
        return sf.format(utcDate);
    }

    public String dateToUTC(Date d) throws ParseException {
        SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = "yyyy/MM/dd HH:mm:ss";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        SimpleDateFormat sdf = new SimpleDateFormat (format);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date gmtTime = new Date(sdf.format(d));
        return sf.format(gmtTime);
    }

    private WorkerCert getCertificateInfo(String workerId, String certName){
        List<WorkerCert> recs = new ArrayList<>();
        recs = workerCertService.lambdaQuery()
                .eq(WorkerCert::getWorkerId, workerId)
                .eq(WorkerCert::getCertName, certName)
                .list();

        // 无证件不处理
        if (recs.size()<1){
            return null;
        }
        return recs.get(0);
    }

    // 获取网点签约的品类信息 并过滤师傅的品类信息
    private GreeResponseHelper getwdqylbxx(String apiPath, String wdno) throws Exception {
        Map<String,String> req = new HashMap<>();

        req.put("wdno",wdno);
        GreeResponseHelper response = greeLogic.greePost(apiPath, JSONObject.toJSONString(req), publicService.getCompanyId(wdno));
        return response;
    }

    private void syncData(String id, String apiPath, OutWorkerUpload item, WorkUploadInfo req,String brandWebsitId) throws Exception {
        GreeResponseHelper response = greeLogic.greePost(apiPath, JSONObject.toJSONString(req), publicService.getCompanyId(brandWebsitId));

        BrandWebsit bw = brandWebsitService.lambdaQuery()
                .eq(BrandWebsit::getBrandWebsitNumber, brandWebsitId)
                .last("limit 1").one();

        if (response.getStatus() == 200){
            // 成功 修改同步状态
            updateWorkerUploadSuccess(item);
            // 修改师傅品牌网点帐号
            updataWorker(id, response);

            // 修改总部网点的地址正确性标识
            if (bw !=null && !bw.getAddressSuccess().equals(1)){
                bw.setAddressSuccess(1);
                bw.updateById();
            }
        } else {
            // 失败 修改同步状态
            updateWorkerUploadError(item, response, brandWebsitId);
            // 修改总部网点的地址正确性标识
            if (response.getMsg().contains("获取不到乡镇信息")){
                if (bw !=null){
                    bw.setAddressSuccess(-1);
                    bw.updateById();
                }
            }
        }
    }

    private void updateWorkerUploadError(OutWorkerUpload item, GreeResponseHelper response,String brandWebsitId) {
        String msg = "["+brandWebsitId+"]";
        OutWorkerUpload v = new OutWorkerUpload();
        v.setSynReqId(item.getSynReqId());
        v.setSyncStatus(99);
        v.setSyncTime(new Date());
        v.setSyncTimes(item.getSyncTimes()+1);
        if (StringUtils.isNotEmpty(response.getMsg())){
            msg = msg + response.getMsg();
        } else {
            msg = msg + "网点证件信息同步出错";
        }
        if (msg.length()>255){
            v.setSyncErrorMsg(msg.substring(0,255));
        } else {
            v.setSyncErrorMsg(msg);
        }

        outWorkerUploadService.updateById(v);
    }

    // 这里只有修改
    private void updataWorker(String id, GreeResponseHelper response) {
        WorkerBrandAccount w = new WorkerBrandAccount();
        w.setId(id);
        w.setOutSyncTime(new Date());
        WorkerUploadResponse resultData = JSONObject.parseObject(response.getData().toString(), WorkerUploadResponse.class);
        w.setBrandWorkerNumber(resultData.getWxgno());
        w.setBrandWorkerId(String.valueOf(resultData.getId()));
        workerBrandAccountService.updateById(w);
    }

    private void updateWorkerUploadSuccess(OutWorkerUpload item) {
        OutWorkerUpload v = new OutWorkerUpload();
        v.setSynReqId(item.getSynReqId());
        v.setSyncStatus(1);
        v.setSyncTime(new Date());
        v.setSyncTimes(item.getSyncTimes()+1);
        v.setSyncErrorMsg("成功");
        outWorkerUploadService.updateById(v);
    }

    private void getreq(WorkerCertificateInfo workerCertificateInfo, WorkUploadInfo req, Worker worker,String websitWorkerId) throws Exception {
        List<ZJInfo> zjInfoList = new ArrayList<>();

        // 证件信息
        getcert(workerCertificateInfo, zjInfoList, worker, websitWorkerId);

        // 生成数据对象
        setReq(zjInfoList, req, worker,websitWorkerId);

        // 人脸面像赋值 服务人员员工照片保存路径必填
        setReqFace(workerCertificateInfo, req);

    }

    private void setReqFace(WorkerCertificateInfo workerCertificateInfo, WorkUploadInfo req) {

        // 必填
        req.setFwryzpfj("www");

        req.setFacecheck(workerCertificateInfo.getFacecheck());
        req.setHasfaceFlag(workerCertificateInfo.getHasfaceFlag());
        if (workerCertificateInfo.getFacecheck().equals(1)){
            req.setFwryzpfj(workerCertificateInfo.getFacepath());
        }

    }

    private Boolean extractedPCAS(WorkUploadInfo req, WorkerStreet workerStreet) {
        req.setSfen(workerStreet.getProvince());
        req.setCshi(workerStreet.getCity());
        req.setCounty(workerStreet.getArea());
        req.setXzhen(workerStreet.getStreet());
        return Boolean.TRUE;
    }

    private void setReq(List<ZJInfo> zjInfoList, WorkUploadInfo req, Worker worker, String websitWorkerId) {
        WebsitWorker wb = websitWorkerService.lambdaQuery()
                .eq(WebsitWorker::getWebsitWorkerId,websitWorkerId)
                .last("limit 1")
                .one();
        req.setZjInfoList(zjInfoList);

        req.setCsgzns(worker.getJobAge());
        // 必填
        if (StringUtils.isNotEmpty(worker.getAddress())){
            req.setDizhi(worker.getAddress());
        } else {
            req.setDizhi(req.getSfen() + req.getCshi() + req.getCounty() + req.getXzhen());
        }


        req.setGzkkh("");
        if (StringUtils.isNotEmpty(worker.getBankAccount())){
            req.setGzkkh(worker.getBankAccount());
        }
        req.setGzkssyh("");
        if (StringUtils.isNotEmpty(worker.getBankDeposit())){
            req.setGzkssyh(worker.getBankDeposit());
        }
        req.setHasfaceFlag(Boolean.FALSE);
        if (wb.getHasFace()==1){
            req.setHasfaceFlag(Boolean.TRUE);
        }
        req.setHmdbz("");
        if (StringUtils.isNotEmpty(worker.getBlackListRemark())){
            req.setHmdbz(worker.getBlackListRemark());
        }
        // 必填
        req.setIfmessageFlag(Boolean.FALSE);
        if (worker.getIsSmsNotice().equals("YES")){
            req.setIfmessageFlag(Boolean.TRUE);
        }
        req.setNianl(worker.getAge());
        req.setPxhgkssj("");
        /*if (worker.getTrainPassTime() !=null ){
            req.setPxhgkssj(datesub(worker.getTrainPassTime()));
        }*/
        req.setQbaoid("");
        req.setQbcheackFlag(Boolean.FALSE);
        req.setSfdswxgFlag(Boolean.FALSE);

        // 必填
        req.setSfhmdFlag(Boolean.FALSE);
        if (worker.getIsBlackList().equals("YES")){
            req.setSfhmdFlag(Boolean.TRUE);
        }
        req.setSfpxhgFlag(Boolean.TRUE);
        /*if (worker.getTrainStatus()){
            req.setSfpxhgFlag(Boolean.TRUE);
        }*/
        // 必填
        req.setSfzstFlag(Boolean.FALSE);
        if (worker.getIsZst().equals("YES")){
            req.setSfzstFlag(Boolean.TRUE);
        }
        req.setVipflag(worker.getVipFlag());

        req.setWxfensu(wb.getScore());
        // 必填
        req.setWxgmc(worker.getWorkerName());
        req.setXcsgz("");
        if (StringUtils.isNotEmpty(worker.getJob())){
            req.setXcsgz(worker.getJob());
        }
        // 必填
        req.setXingb(worker.getGender());

        // 必填 必须初中以下
        req.setXueli("中专");
        if (StringUtils.isNotEmpty(worker.getEducation())){
            req.setXueli(worker.getEducation());
        }
        // 必填
        if (StringUtils.isNotEmpty(worker.getMobile())){
            req.setYddh(worker.getMobile());
        }
        req.setMaxwxnum(wb.getRepairDayLimit());
        req.setMaxaznum(wb.getInstallDayLimit());
    }

    private void getcert(WorkerCertificateInfo workerCertificateInfo,
                         List<ZJInfo> zjInfoList, Worker worker,String websitWorkerId) throws Exception {
        // 身份证  必要条件
        if (StringUtils.isNotEmpty(workerCertificateInfo.getSfzbh())) {
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getSfzbh());
            zjInfo.setZjmc("身份证");
            zjInfo.setZjyxq("");

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getSfzz())) {
                zjInfo.setZjlj(workerCertificateInfo.getSfzz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getSfzf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getSfzz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getSfzf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getSfzf());
                }

            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getSfzyxq())) {
                zjInfo.setZjyxq(workerCertificateInfo.getSfzyxq());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getSfzfsrq())) {
                zjInfo.setZjfssj(workerCertificateInfo.getSfzfsrq());
            }

            zjInfoList.add(zjInfo);
        }

        // 保险  必要条件
        if (StringUtils.isNotEmpty(workerCertificateInfo.getBxz())){
            ZJInfo zjInfo = new ZJInfo();

            zjInfo.setZjbh(workerCertificateInfo.getSafeNo());
            zjInfo.setZjmc("保险证");

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getBxz())) {
                zjInfo.setZjlj(workerCertificateInfo.getBxz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getBxf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getBxz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getBxf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getBxf());
                }
            }

            if (StringUtils.isNotEmpty(workerCertificateInfo.getBxyxq())) {
                zjInfo.setZjyxq(workerCertificateInfo.getBxyxq());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getBxfsrq())) {
                zjInfo.setZjfssj(workerCertificateInfo.getBxfsrq());
            }

            zjInfoList.add(zjInfo);
        }

        // 高空作业证  必要条件
        if (StringUtils.isNotEmpty(workerCertificateInfo.getGkzbh())
                && StringUtils.isNotEmpty(workerCertificateInfo.getGkzyxq())){
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getGkzbh());
            zjInfo.setZjmc("高空作业证");
            zjInfo.setZjfssj(workerCertificateInfo.getGkzfsrq());

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getGkzz())) {
                zjInfo.setZjlj(workerCertificateInfo.getGkzz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getGkzf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getGkzz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getGkzf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getGkzf());
                }
            }

            zjInfo.setZjfssj(workerCertificateInfo.getGkzfsrq());
            zjInfo.setZjyxq(workerCertificateInfo.getGkzyxq());
            zjInfoList.add(zjInfo);
        }

        // 上岗资格证
        if (StringUtils.isNotEmpty(workerCertificateInfo.getSgzgzbh())){
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getSgzgzbh());
            zjInfo.setZjmc("上岗资格证");

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getSgzgzz())) {
                zjInfo.setZjlj(workerCertificateInfo.getSgzgzz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getSgzgzf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getSgzgzz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getSgzgzf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getSgzgzf());
                }
            }

            zjInfo.setZjfssj(workerCertificateInfo.getSgzgzfsrq());
            zjInfo.setZjyxq(workerCertificateInfo.getSgzgzyxq());

            zjInfoList.add(zjInfo);
        }

        // 制冷工证
        if (StringUtils.isNotEmpty(workerCertificateInfo.getZlgbh())){
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getZlgbh());
            zjInfo.setZjmc("制冷工证");

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getZlgz())) {
                zjInfo.setZjlj(workerCertificateInfo.getZlgz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getZlgf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getZlgz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getZlgf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getZlgf());
                }
            }

            zjInfo.setZjfssj(workerCertificateInfo.getZlgfsrq());
            zjInfo.setZjyxq(workerCertificateInfo.getZlgyxq());

            zjInfoList.add(zjInfo);
        }

        // 驾驶证
        if (StringUtils.isNotEmpty(workerCertificateInfo.getCph())){
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getCph());
            zjInfo.setZjmc("驾驶证");

            if (StringUtils.isNotEmpty(worker.getGpsMachineId())){
                zjInfo.setZjbh(workerCertificateInfo.getCph()+"/"+ worker.getGpsMachineId());
            }

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getJszz())) {
                zjInfo.setZjlj(workerCertificateInfo.getJszz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getJszf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getJszz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getJszf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getJszf());
                }
            }

            zjInfo.setZjfssj(workerCertificateInfo.getJszfsrq());
            zjInfo.setZjyxq(workerCertificateInfo.getJszyxq());

            zjInfoList.add(zjInfo);
        }

        // 电工证
        if (StringUtils.isNotEmpty(workerCertificateInfo.getDgbh())){
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getDgbh());
            zjInfo.setZjmc("电工证");

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getDgzz())) {
                zjInfo.setZjlj(workerCertificateInfo.getDgzz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getDgzf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getDgzz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getDgzf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getDgzf());
                }
            }

            zjInfo.setZjfssj(workerCertificateInfo.getDgfsrq());
            zjInfo.setZjyxq(workerCertificateInfo.getDgyxq());

            zjInfoList.add(zjInfo);
        }

        // 焊工证
        if (StringUtils.isNotEmpty(workerCertificateInfo.getHgbh())){
            ZJInfo zjInfo = new ZJInfo();
            zjInfo.setZjbh(workerCertificateInfo.getHgbh());
            zjInfo.setZjmc("焊工证");

            // 证件路径 必填
            if (StringUtils.isNotEmpty(workerCertificateInfo.getHgzz())) {
                zjInfo.setZjlj(workerCertificateInfo.getHgzz());
            }
            if (StringUtils.isNotEmpty(workerCertificateInfo.getHgzf())) {
                if (StringUtils.isNotEmpty(workerCertificateInfo.getHgzz())) {
                    zjInfo.setZjlj(zjInfo.getZjlj() + "," + workerCertificateInfo.getHgzf());
                } else {
                    zjInfo.setZjlj(workerCertificateInfo.getHgzf());
                }
            }

            zjInfo.setZjfssj(workerCertificateInfo.getHgfsrq());
            zjInfo.setZjyxq(workerCertificateInfo.getHgyxq());

            zjInfoList.add(zjInfo);
        }

        // 其他证件
        for (WorkerCertificateOther item :workerCertificateInfo.getOther()){
            if (StringUtils.isNotEmpty(item.getBh())){
                ZJInfo zjInfo = new ZJInfo();
                zjInfo.setZjbh(item.getBh());
                zjInfo.setZjmc(item.getName());

                // 证件路径 必填
                if (StringUtils.isNotEmpty(item.getZz())) {
                    zjInfo.setZjlj(item.getZz());
                }
                if (StringUtils.isNotEmpty(item.getZf())) {
                    if (StringUtils.isNotEmpty(item.getZz())) {
                        zjInfo.setZjlj(zjInfo.getZjlj() + "," + item.getZf());
                    } else {
                        zjInfo.setZjlj(item.getZf());
                    }
                }

                zjInfo.setZjfssj(item.getFsrq());
                zjInfo.setZjyxq(item.getYxq());

                zjInfoList.add(zjInfo);
            }
        }









    }

    /**
     * 签约大类，必须有对应的小类信息，否则无法上传(获取所有的大类对应的小类信息，再从网点本身过滤掉)
     */
    private Boolean getCategory(Brand brand, List<SignSplb> jySignSplbList, List<SignSplb> sySignSplbList,
                                Worker worker, OutWorkerUpload itemi, String websitId) {
        List<String> SYDictDaleiCode = new ArrayList<>();

        // 提取品牌商所有的商用大类信息
        List<SysDict> sysDicts = sysDictService.lambdaQuery()
                .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .eq(SysDict::getFlag, DictTypeEnum.DICT_TYPE_FLAG_SY.getCode()).list();
        if (sysDicts !=null && sysDicts.size()>0){
            SYDictDaleiCode = sysDicts.stream().map(SysDict::getDictCode).collect(Collectors.toList());
        }

        // 提取师傅的服务大类信息
        List<WorkerServiceCategoryDetail> wxgCategoryRecs = workerServiceCategoryDetailService.lambdaQuery()
                .eq(WorkerServiceCategoryDetail::getWorkerId, worker.getWorkerId())
                .eq(WorkerServiceCategoryDetail::getWebsitId, websitId).list();

        // 组装类别参数
        if (wxgCategoryRecs !=null && wxgCategoryRecs.size()>0){
            getCategoryt(brand, jySignSplbList, sySignSplbList, SYDictDaleiCode, wxgCategoryRecs);
        }

        if (jySignSplbList.size()<1 && sySignSplbList.size()<1){
            // 写入表备注信息
            updateWorkerUploadLag(itemi,worker.getWorkerId() + "-"+websitId+"平台师傅签约的品类不能同时为空");
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }

    // 获取类别信息
    // 师傅的服务类型分，安装、维修、配送等
    // 格力网点答约的服务类目也要限制师傅的品数参数
    private void getCategoryt(Brand brand, List<SignSplb> jySignSplbList, List<SignSplb> sySignSplbList,
                              List<String> SYDictDaleiCode, List<WorkerServiceCategoryDetail> wxgCategoryRecs) {
        List<SysDictRef> sysDictRefs = new ArrayList<>();

        // 组装参数类别名称信息  服务类型 分，安装、维修、配送等
        List<String> fwlbList = getCategoryName(wxgCategoryRecs);

        // 获取师傅在品牌商品对应的类别信息 这里的类别为师傅服务的有效的大类别信息 sysDictRefs
        getDictRefs(brand, wxgCategoryRecs, sysDictRefs);

        // 组装品类参数 大类下的所有小类都放在一起
        for (SysDictRef item:sysDictRefs){
            // 大类对应的小类必填
            // 获取品牌商的大类对应的所有小类信息
            List<SysDict> categorys = sysDictService.lambdaQuery()
                    .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode())
                    .eq(SysDict::getParentDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                    .eq(SysDict::getStatus, DictTypeEnum.DICT_TYPE_ON.getCode())
                    // .eq(SysDict::getDictCode, "120300") 测试用
                    .eq(SysDict::getParentDictCode, item.getRefDictCode()).list();
            if (categorys !=null && categorys.size()>0){
                for (SysDict category :categorys){
                    SignSplb v = new SignSplb();
                    v.setSpdl(Integer.valueOf(item.getRefDictCode()));
                    v.setSpdlName(item.getRefDictName());
                    v.setSpxl(Integer.valueOf(category.getDictCode()));
                    v.setSpxlName(category.getDictValue());
                    v.setFwlbList(fwlbList);
                    // 查看是否为商用
                    if (SYDictDaleiCode.contains(item.getDictCode())){
                        sySignSplbList.add(v);
                    } else {
                        jySignSplbList.add(v);
                    }
                }
            }
        }
    }

    @NotNull
    private List<String> getCategoryName(List<WorkerServiceCategoryDetail> wxgCategoryRecs) {
        // 服务类型 INSTALL=安装 REPAIR=维修  DELIVERY=配送
        Map<String,String> serviceFwlbList = new HashMap<>();
        for (WorkerServiceCategoryDetail w: wxgCategoryRecs){
            if (w.getType().equals("INSTALL")){
                serviceFwlbList.put("INSTALL","安装");
            } else if (w.getType().equals("REPAIR")){
                serviceFwlbList.put("REPAIR\"","维修");
            } else if (w.getType().equals("DELIVERY")){
                serviceFwlbList.put("DELIVERY","配送");
            }
        }
        List<String> fwlbList = new ArrayList<>();
        Set<String> keySet = serviceFwlbList.keySet();
        for (String key : keySet){
            String val = serviceFwlbList.get(key);
            fwlbList.add(val);
        }
        return fwlbList;
    }

    private void getDictRefs(Brand brand, List<WorkerServiceCategoryDetail> wxgCategoryRecs, List<SysDictRef> sysDictRefs) {
        List<String> wxgCaregoryIds = wxgCategoryRecs.stream().map(WorkerServiceCategoryDetail::getMainId).collect(Collectors.toList());

        // 提取师傅所在品牌商对应的大类信息
        List<SysDictRef> sysDictRefRecs =  sysDictRefService.lambdaQuery()
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .in(SysDictRef::getDictCode,wxgCaregoryIds).list();

        // sysDictRefs 内的大类信息需去除无效的大类信息
        // 提取品牌商所有无效的大类信息
        List<SysDict> daleis = sysDictService.lambdaQuery()
                .eq(SysDict::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .eq(SysDict::getStatus, DictTypeEnum.DICT_TYPE_OFF.getCode())
                .list();
        if ((daleis !=null && daleis.size()>0) && (sysDictRefRecs!=null && sysDictRefRecs.size()>0)){
            List<String> daleiCodes = new ArrayList<>();
            daleiCodes = daleis.stream().map(SysDict::getDictCode).collect(Collectors.toList());
            for (SysDictRef s: sysDictRefRecs){
                if (!daleiCodes.contains(s.getDictCode())){
                    sysDictRefs.add(s);
                }
            }
        } else {
            sysDictRefs.addAll(sysDictRefRecs);
        }

    }


    @Nullable
    private WorkerStreet getWorkerStreet(OutWorkerUpload item, Worker worker) {
        List<WorkerStreet> wss = workerStreetService.lambdaQuery()
                .eq(WorkerStreet::getWorkerId, worker.getWorkerId()).list();
        if (wss == null || wss.size()<1){
            // 写入表备注信息
            updateWorkerUploadLag(item,"师傅服务区域信息不存在");
            return null;
        }
        WorkerStreet ws = wss.get(0);
        // 必填参数 省份-城市-区县-乡镇
        /*if (StringUtils.isEmpty(ws.getProvince()) || StringUtils.isEmpty(ws.getCity())
        || StringUtils.isEmpty(ws.getArea()) || StringUtils.isEmpty(ws.getStreet())){
            updateWorkerUploadLag(item,"师傅所在省市区镇信息缺失");
            return null;
        }
*/
        return ws;
    }

    private void updateWorkerUploadLag(OutWorkerUpload item, String msg) {
        OutWorkerUpload v = new OutWorkerUpload();
        v.setSynReqId(item.getSynReqId());
        v.setSyncTime(new Date());
        v.setSyncErrorMsg(msg);
        if (msg.length()>255){
            v.setSyncErrorMsg(msg.substring(0,255));
        }
        outWorkerUploadService.updateById(v);
    }

    @Nullable
    private List<WorkerBrandAccount> getWorkerBrandAccount(Brand brand, OutWorkerUpload item, Worker worker) {
        List<WorkerBrandAccount> workerBrandAccount = workerBrandAccountService.lambdaQuery()
                .eq(WorkerBrandAccount::getWorkerId, worker.getWorkerId())
                .eq(WorkerBrandAccount::getGroupCompanyId, brand.getBrandId())
                .eq(WorkerBrandAccount::getBrandWorkerStatus,"ON")
                .eq(WorkerBrandAccount::getDel, 0)
                .orderByAsc(WorkerBrandAccount::getCreateTime)
                .list();

        if (CollectionUtils.isEmpty(workerBrandAccount)){
            // 写入表备注信息
            updateWorkerUploadLag(item,"师傅未选择品牌商");
            return null;
        }
        return workerBrandAccount;
    }

    private Boolean checkWebsitSum(List<WorkerBrandAccount> workerBrandAccount, OutWorkerUpload item){
        // 格力一个师傅最多三个有效网点超3个网点不能同步
        Integer sum = 0;
        for (WorkerBrandAccount wba :workerBrandAccount){
            if (wba.getBrandWorkerStatus().equals("ON")){
                sum ++;
            }
        }
        if (sum>3) {
            // 写入表备注信息
            updateWorkerUploadLag(item,"师傅所在有效网点数超过3个无法同步");
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }


    @Nullable
    private Worker getWorker(OutWorkerUpload item) {
        Worker worker = workerService.lambdaQuery()
                .eq(Worker::getWorkerId, item.getWorkerId())
                .eq(Worker::getStatus,"ON").one();
        if (worker ==null){
            return null;
        }
        return worker;
    }

    private List<OutWorkerUpload> getWorkerUploads(Integer syncType) {
        // 同步标志-synStatus(0:待同步;1:已同步;99:同步失败)
        List<OutWorkerUpload> recs;
        LambdaQueryWrapper<OutWorkerUpload> queryWrapper = new LambdaQueryWrapper<OutWorkerUpload>()
                .eq(OutWorkerUpload::getSyncStatus,0)
                .eq(OutWorkerUpload::getSyncType,syncType)
                .or(wrapper->wrapper.eq(OutWorkerUpload::getSyncStatus,99)
                        .le(OutWorkerUpload::getSyncTimes,100));
        recs = outWorkerUploadService.list(queryWrapper);
        return recs;
    }

    private List<OutWorkerUpload> getWorkerUploadstest() {
        // 同步标志-synStatus(0:待同步;1:已同步;99:同步失败)
        List<OutWorkerUpload> recs = outWorkerUploadService.lambdaQuery()
                .eq(OutWorkerUpload::getSynReqId,147).list();
        return recs;
    }
}
