package com.gic.haoban.manage.service.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSONArray;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanWelcomeMediaMapper;
import com.gic.haoban.manage.service.entity.*;
import com.gic.haoban.manage.service.pojo.WelcomeUserInfoBO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.JSONResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.dubbo.entity.ProviderLocalTag;
import com.gic.enterprise.api.dto.QRcodeDTO;
import com.gic.enterprise.api.service.ImageService;
import com.gic.haoban.manage.api.constants.Manage3Constants;
import com.gic.haoban.manage.api.dto.QwFrientNoticeDTO;
import com.gic.haoban.manage.api.dto.QywxErrorLogDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.dto.hm.HmClerkRelationDTO;
import com.gic.haoban.manage.api.dto.hm.HmQrcodeDTO;
import com.gic.haoban.manage.api.enums.WelcomeMediaTypeEnum;
import com.gic.haoban.manage.api.enums.hm.HmWelcomeReferType;
import com.gic.haoban.manage.api.service.hm.HmClerkRelationApiService;
import com.gic.haoban.manage.api.service.hm.HmQrcodeApiService;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.dao.mapper.StaffMapper;
import com.gic.haoban.manage.service.pojo.bo.hm.KeyDataLinkBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeDetailBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeMediaBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeReferBO;
import com.gic.haoban.manage.service.service.ClerkMainStoreRelatedService;
import com.gic.haoban.manage.service.service.KeyDataService;
import com.gic.haoban.manage.service.service.MiniprogramSettingService;
import com.gic.haoban.manage.service.service.StaffClerkRelationService;
import com.gic.haoban.manage.service.service.WelcomeSendService;
import com.gic.haoban.manage.service.service.WelcomeService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.haoban.manage.service.util.EmojiFilterUtil;
import com.gic.mq.sdk.GicMQClient;
import com.gic.wechat.api.dto.qywx.welcome.QywxExternalMessageBaseDTO;
import com.gic.wechat.api.dto.qywx.welcome.QywxFileExternalBaseDTO;
import com.gic.wechat.api.dto.qywx.welcome.QywxImageExternalBaseDTO;
import com.gic.wechat.api.dto.qywx.welcome.QywxLinkExternalBaseDTO;
import com.gic.wechat.api.dto.qywx.welcome.QywxMiniprogramExternalBaseDTO;
import com.gic.wechat.api.dto.qywx.welcome.QywxVideoExternalBaseDTO;
import com.gic.wechat.api.dto.qywx.welcome.QywxWelcomeMsgDTO;
import com.gic.wechat.api.enums.QywxMediaTypeEnum;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import com.gic.wechat.api.service.qywx.QywxUserApiService;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.SecureUtil;

@Service
public class WelcomeSendServiceImpl implements WelcomeSendService {

    private static final Logger log = LogManager.getLogger(WelcomeSendServiceImpl.class);
    @Autowired
    private QywxUserApiService qywxUserApiService;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private ClerkMainStoreRelatedService clerkMainStoreRelatedService;
    @Autowired
    private Config config;
    @Autowired
    private QywxSuiteApiService qywxSuiteApiService;
    @Autowired
    private StaffClerkRelationService staffClerkRelationService;
    @Autowired
    private MiniprogramSettingService miniprogramSettingService;
    @Autowired
    private WelcomeService welcomeService;
    @Autowired
    private ImageService imageService;
    @Autowired
    private HmQrcodeApiService hmQrcodeApiService;
    @Autowired
    private KeyDataService keyDataService;
    @Autowired
    private HmClerkRelationApiService hmClerkRelationApiService;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private TabHaobanWelcomeMediaMapper welcomeMediaMapper ;

    private static GicMQClient clientInstance = GICMQClientUtil.getClientInstance();

    @Override
    public void sendWelcome(String params) {
        log.info("发送欢迎语={}", params);
        QwFrientNoticeDTO dto = JSON.parseObject(params, QwFrientNoticeDTO.class);
        this.sendWelcome(dto.getCorpid(), dto.getExternalUserid(), dto.getWelcomeCode(), dto.getWxUserId(),
                dto.getState());
    }

    /**
     * 发送欢迎语
     *
     * {"data":{"welcomeCode":"J0GjdrsF-nZNac3Xc6V2K2p8OokQZcY4g0jxljdHc7A","wxUserId":"016903","suiteid":"selfSuiteId","corpid":"wxbde984c3488b837e","externalUserid":"wmSe9FEAAAy39F1NI6Z-THUUd9mkqOIw","changeType":"add_external_contact"},"type":1,"mqTraceId":"278473818-1-1679557069.126"}
     */
    @Override
    public void sendWelcome(String corpid, String externalUserId, String welcomeCode, String wxUserId, String state) {
        if (StringUtils.isEmpty(welcomeCode)) {
            log.error("welcomeCode为空,externalUserId={}", externalUserId);
            return;
        }
        TabHaobanWxEnterprise wxEnterprise = this.wxEnterpriseService.getEnterpriseBycorpId(corpid);
        if (wxEnterprise == null) {
            log.info("企业不存在,corpid={}", corpid);
            return;
        }
        String wxEnterpriseId = wxEnterprise.getWxEnterpriseId();
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId);
        if (qwDTO == null) {
            log.info("企业不存在,wxEnterpriseId={}", wxEnterpriseId);
            return;
        }
        TabHaobanStaff staff = this.staffMapper.selectByUserIdAndEnterpriseId(wxUserId, wxEnterpriseId);
        if (null == staff) {
            log.info("成员不存在,wxUserid={}", wxUserId);
            return;
        }
        String staffId = staff.getStaffId();
        log.info("发送欢迎语,state={}", state);
        String relationKey = SecureUtil.md5(staffId + externalUserId);
        WelcomeDetailBO welcomeBO = null;
        WelcomeUserInfoBO userBO = this.getExternalUserName(qwDTO, externalUserId , wxUserId);
        int addWay = userBO.getAddWay() ;
        int welcomeScene = 0 ;
        if(addWay==202) {
            // 离职继承、在职继承
            welcomeScene = 202 ;
        }
        if (StringUtils.isNotBlank(state)
                && (state.startsWith(Manage3Constants.HM) || state.startsWith(Manage3Constants.DT))) {
            WelcomeReferBO welcomeReferBo = getWelcomeReferBo(state);
            welcomeBO = welcomeService.getMatchWelcomeWithReferId(staffId, wxEnterpriseId, welcomeReferBo);
            //单人活码欢迎语未配置时，取欢迎语的配置进行发送
            if(state.startsWith(Manage3Constants.HM) && welcomeBO == null){
                welcomeBO = welcomeService.getMatchWelcome(staffId, wxEnterpriseId,welcomeScene);
            }
        } else {
            welcomeBO = welcomeService.getMatchWelcome(staffId, wxEnterpriseId,welcomeScene);
        }
        if (Objects.isNull(welcomeBO)) {
            log.info("欢迎语未配置wxEnterpriseId:{}", wxEnterpriseId);
            return;
        }
        // 内容
        log.info("欢迎语发送{}={}", welcomeBO.getWelcomeId(), JSON.toJSONString(welcomeBO));
        String content = welcomeBO.getWelcomeContent();
        if (content.contains("<微信昵称>")) {
            String nickName = userBO.getName() ;
            //这里对“$”符号和”\\“符号进行转义处理
            nickName = java.util.regex.Matcher.quoteReplacement(nickName);
            content = content.replaceAll("<微信昵称>", nickName);
        }
        QywxWelcomeMsgDTO qywxWelcomeMsgDTO = new QywxWelcomeMsgDTO();
        // 欢迎语内容
        qywxWelcomeMsgDTO.setText(content);
        // 欢迎语类型
        List<QywxExternalMessageBaseDTO> attachments = new ArrayList<>();
        // 开始设置欢迎语附件
        List<WelcomeMediaBO> mediaList = welcomeBO.getWelcomeMediaList();
        mediaList.forEach(one -> {
            Integer mediaType = one.getMediaType();
            if (WelcomeMediaTypeEnum.IMAGE.getCode().equals(mediaType)) {
                attachmentAddImage(attachments, one, corpid,qwDTO);
            }
            if (WelcomeMediaTypeEnum.VIDEO.getCode().equals(mediaType)) {
                attachmentAddVideo(attachments, one, corpid,qwDTO);
            }
            if (WelcomeMediaTypeEnum.FILE.getCode().equals(mediaType)) {
                attachmentAddFile(attachments, one, corpid,qwDTO);
            }
            if (WelcomeMediaTypeEnum.LINK.getCode().equals(mediaType)) {
                attachmentAddLink(attachments, one);
            }
            if (WelcomeMediaTypeEnum.PROGRAM.getCode().equals(mediaType)) {
                attachmentAddProgram(attachments, one, wxEnterpriseId);
            }
            if (WelcomeMediaTypeEnum.PARAM_PROGRAM.getCode().equals(mediaType)
                    || WelcomeMediaTypeEnum.PARAM_QRCODE.getCode().equals(mediaType)) {
                String clerkId = this.getClerkId(wxEnterpriseId, staffId, state);
                String enterpriseId = null;
                if (null != clerkId) {
                    ClerkDTO clerk = this.clerkService.getclerkById(clerkId);
                    if (null != clerk) {
                        enterpriseId = clerk.getEnterpriseId();
                    }
                }
                if (WelcomeMediaTypeEnum.PARAM_QRCODE.getCode().equals(mediaType)) {
                    attachmentAddParamQrCode(attachments, enterpriseId, clerkId, corpid, relationKey,qwDTO);
                }
                if (WelcomeMediaTypeEnum.PARAM_PROGRAM.getCode().equals(mediaType)) {
                    attachmentAddParamProgram(attachments, enterpriseId, wxEnterpriseId, relationKey);
                }
            }
        });
        if (CollectionUtils.isNotEmpty(attachments)) {
            qywxWelcomeMsgDTO.setAttachments(attachments);
        }
        qywxWelcomeMsgDTO.setWelcomeCode(welcomeCode);
        String result = null ;
        if(qwDTO.isSelf()) {
            log.info("自建={}",corpid,qwDTO.getSelfSecret());
            result = this.qywxUserApiService.sendSelfWelcomeMsgByExternal(corpid, qwDTO.getSelfSecret(),
                    qywxWelcomeMsgDTO , qwDTO.getUrlHost());
        }else {
            result = this.qywxUserApiService.sendWelcomeMsgByExternal(corpid, config.getWxSuiteid(),
                    qywxWelcomeMsgDTO);
        }
        log.info("发送欢迎语 params:{},返回={}", JSON.toJSONString(qywxWelcomeMsgDTO), result);
        if (StringUtils.isNotBlank(result)) {
            JSONObject json = JSON.parseObject(result);
            int errcode = json.getIntValue("errcode");
            if (0 != errcode) {
                try {
                    QywxErrorLogDTO errLog = new QywxErrorLogDTO();
                    errLog.setWxEnterpriseId(wxEnterpriseId);
                    errLog.setExternalUserId(externalUserId);
                    errLog.setTraceId(ProviderLocalTag.tag.get().traceId);
                    errLog.setQywxErrorType(1);
                    errLog.setScenario("欢迎语");
                    errLog.setErrorContent(result);
                    clientInstance.sendMessage(Manage3Constants.QYWX_ERROR_MSG, JSON.toJSONString(errLog));
                } catch (Exception e) {
                    log.error("发送消息失败：{}", e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 获取链接或者活码id
     */
    private WelcomeReferBO getWelcomeReferBo(String state) {
        WelcomeReferBO welcomeReferBo = new WelcomeReferBO();
        String id = state.substring(2);
        if (state.contains(Manage3Constants.HM)) {
            welcomeReferBo.setType(HmWelcomeReferType.HM.getCode());
            welcomeReferBo.setReferId(id);
        } else if (state.contains(Manage3Constants.DT)) {
            KeyDataLinkBO dataForHmLink = keyDataService.getDataForHmLink(id);
            if (dataForHmLink != null) {
                welcomeReferBo.setType(HmWelcomeReferType.LINK.getCode());
                welcomeReferBo.setReferId(Convert.toStr(dataForHmLink.getLinkId()));
            }
        }
        return welcomeReferBo;
    }

    private void attachmentAddImage(List<QywxExternalMessageBaseDTO> attachments, WelcomeMediaBO media, String corpid , WxEnterpriseQwDTO qwDTO) {
        String mediaUrl = media.getMediaUrl();
        String[] arr = mediaUrl.split("/");
        boolean selfFlag = qwDTO.isSelf() ;
        JSONResponse jp = qywxSuiteApiService.uploadMedia(corpid, qwDTO.getSelf3thSecret(),
                mediaUrl, arr[arr.length - 1], QywxMediaTypeEnum.IMAGE.getCode(),selfFlag,qwDTO.getUrlHost());
        log.info("欢迎语发送图片返回：{}", JSON.toJSONString(jp));
        if (jp.getErrorCode() == 0) {
            QywxImageExternalBaseDTO qywxImageExternalBaseDTO = new QywxImageExternalBaseDTO();
            qywxImageExternalBaseDTO.setMediaId(jp.getResult().toString());
            attachments.add(qywxImageExternalBaseDTO);
        }
    }

    private void attachmentAddVideo(List<QywxExternalMessageBaseDTO> attachments, WelcomeMediaBO media, String corpid, WxEnterpriseQwDTO qwDTO) {
        String mediaUrl = media.getMediaUrl();
        String[] arr = mediaUrl.split("/");
        boolean selfFlag = qwDTO.isSelf() ;
        JSONResponse jp = qywxSuiteApiService.uploadMedia(corpid, qwDTO.getSelf3thSecret(),
                mediaUrl, arr[arr.length - 1], QywxMediaTypeEnum.VIDEO.getCode(),selfFlag,qwDTO.getUrlHost());
        log.info("欢迎语发送视频返回：{}", JSON.toJSONString(jp));
        if (jp.getErrorCode() == 0) {
            QywxVideoExternalBaseDTO qywxVideoExternalBaseDTO = new QywxVideoExternalBaseDTO();
            qywxVideoExternalBaseDTO.setMediaId(jp.getResult().toString());
            attachments.add(qywxVideoExternalBaseDTO);
        }
    }

    private void attachmentAddFile(List<QywxExternalMessageBaseDTO> attachments, WelcomeMediaBO media, String corpid, WxEnterpriseQwDTO qwDTO) {
        String mediaUrl = media.getMediaUrl();
        String[] arr = mediaUrl.split("/");
        boolean selfFlag = qwDTO.isSelf() ;
        JSONResponse jp = qywxSuiteApiService.uploadMedia(corpid, qwDTO.getSelf3thSecret(),
                mediaUrl, arr[arr.length - 1], QywxMediaTypeEnum.FILE.getCode(),selfFlag,qwDTO.getUrlHost());
        log.info("欢迎语发送文件返回：{}", JSON.toJSONString(jp));
        if (jp.getErrorCode() == 0) {
            QywxFileExternalBaseDTO qywxFileExternalBaseDTO = new QywxFileExternalBaseDTO();
            qywxFileExternalBaseDTO.setMediaId(jp.getResult().toString());
            attachments.add(qywxFileExternalBaseDTO);
        }
    }

    private void attachmentAddLink(List<QywxExternalMessageBaseDTO> attachments, WelcomeMediaBO media) {
        String mediaTitle = media.getMediaTitle();
        String mediaUrl = media.getMediaUrl();
        QywxLinkExternalBaseDTO qywxLinkExternalBaseDTO = new QywxLinkExternalBaseDTO();
        qywxLinkExternalBaseDTO.setTitle(mediaTitle);
        qywxLinkExternalBaseDTO.setUrl(mediaUrl);
        attachments.add(qywxLinkExternalBaseDTO);
    }

    private void attachmentAddProgram(List<QywxExternalMessageBaseDTO> attachments, WelcomeMediaBO media,
            String wxEnterpriseId) {
        log.info("发送小程序：wxEnterpriseId:{}", wxEnterpriseId);
        String miniprogramSettingId = media.getMiniprogramSettingId();
        String mediaId = null ;
        Long welcomeMediaId = media.getWelcomeMediaId() ;
        TabHaobanWelcomeMedia wxa = this.welcomeMediaMapper.queryById(welcomeMediaId) ;
        if(StringUtils.isNotBlank(wxa.getMediaId()) && null != wxa.getMediaTime()) {
            Date date = new Date();
            Date compareDate = DateUtils.addDays(date, -3);
            if (wxa.getMediaTime().after(compareDate)) {
                mediaId = wxa.getMediaId() ;
            }else {
                WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxa.getWxEnterpriseId()) ;
                boolean selfFlag = qwDTO.isSelf() ;
                JSONResponse jp = qywxSuiteApiService.uploadMedia(qwDTO.getThirdCorpid(), qwDTO.getSelf3thSecret(), wxa.getMediaUrl(), "image", QywxMediaTypeEnum.IMAGE.getCode(),selfFlag,qwDTO.getUrlHost());
                if (jp.getErrorCode() == 0) {
                    mediaId = jp.getResult().toString() ;
                    this.welcomeMediaMapper.updateByMiniProgramId(mediaId,welcomeMediaId);
                } else {
                    throw new RuntimeException("处理素材异常");
                }
            }
        }
        TabMiniprogramSetting miniprogramSetting = miniprogramSettingService.getById(miniprogramSettingId);
        if (Objects.isNull(miniprogramSetting) || miniprogramSetting.getStatusFlag() == 0) {
            log.info("小程序不存在或已被删除, miniprogramSettingId:{}", miniprogramSettingId);
            return;
        }else {
            if(null == mediaId) {
                mediaId = miniprogramSetting.getMediaId();
            }
        }
        QywxMiniprogramExternalBaseDTO messageBaseDTO = new QywxMiniprogramExternalBaseDTO();
        messageBaseDTO.setAppid(miniprogramSetting.getAppId());
        messageBaseDTO.setPicMediaId(mediaId);
        messageBaseDTO.setTitle(miniprogramSetting.getTitle());
        messageBaseDTO.setPage(media.getMiniProgramPage());
        attachments.add(messageBaseDTO);
    }

    private void attachmentAddParamProgram(List<QywxExternalMessageBaseDTO> attachments, String enterpriseId,
            String wxEnterpriseId, String relationKey) {
        if (StringUtils.isEmpty(enterpriseId) || StringUtils.isEmpty(relationKey)) {
            log.info("带参二维码无法发送");
            return;
        }
        log.info("发送带参小程序：wxEnterpriseId:{}", wxEnterpriseId);
        // 发送小程序
        TabMiniprogramSetting miniprogramSetting = miniprogramSettingService.getMiniprogramSetting(wxEnterpriseId,
                enterpriseId);
        if (null == miniprogramSetting) {
            log.info("需要配置小程序配置：wxEnterpriseId:{},enterpriseId:{}", wxEnterpriseId, enterpriseId);
            return;
        }
        QywxMiniprogramExternalBaseDTO messageBaseDTO = new QywxMiniprogramExternalBaseDTO();
        messageBaseDTO.setAppid(miniprogramSetting.getAppId());
        messageBaseDTO.setPicMediaId(miniprogramSetting.getMediaId());
        messageBaseDTO.setTitle(miniprogramSetting.getTitle());
        messageBaseDTO.setPage(config.getMemberOpenPage() + relationKey);
        attachments.add(messageBaseDTO);
    }

    private void attachmentAddParamQrCode(List<QywxExternalMessageBaseDTO> attachments, String enterpriseId,
            String clerkId, String corpid, String relationKey,WxEnterpriseQwDTO qwDTO) {
        if (StringUtils.isEmpty(enterpriseId) || StringUtils.isEmpty(clerkId)) {
            log.info("带参小程序无法发送");
            return;
        }
        // 带参数的公众号二维码
        String sceneStr = "QWHY_" + relationKey + "_" + clerkId;
        // 30天失效
        QRcodeDTO qRcodePic = imageService.getQRcodePic(enterpriseId, sceneStr, 2592000);
        log.info("参数的公众号二维码返回：{}", JSON.toJSONString(qRcodePic));
        if (qRcodePic != null) {
            String downloadUrl = qRcodePic.getPicUploadResDTO().downloadUrl;
            String[] arr = downloadUrl.split("/");
            boolean selfFlag = qwDTO.isSelf() ;
            JSONResponse jp = qywxSuiteApiService.uploadMedia(corpid, qwDTO.getSelf3thSecret(),
                    downloadUrl, arr[arr.length - 1], QywxMediaTypeEnum.IMAGE.getCode(),selfFlag,qwDTO.getUrlHost());
            log.info("欢迎语发送带参数的公众号企微二维码返回：{}", JSON.toJSONString(jp));
            if (jp.getErrorCode() == 0) {
                QywxImageExternalBaseDTO qywxImageExternalBaseDTO = new QywxImageExternalBaseDTO();
                qywxImageExternalBaseDTO.setMediaId(jp.getResult().toString());
                attachments.add(qywxImageExternalBaseDTO);
            }
        }
    }

    private WelcomeUserInfoBO getExternalUserName(WxEnterpriseQwDTO qwDTO, String externalUserId , String addUserId) {
        log.info("查询好友详情,externalUserId={},{}", externalUserId,qwDTO.getWxSecurityType());
        String userInfo = null ;
        if(qwDTO.isSelf()) {
            userInfo =qywxUserApiService.getCorpSelfExternalUseridInfo(qwDTO.getDkCorpid(), qwDTO.getSelfSecret(), externalUserId,qwDTO.getUrlHost());
        }else {
            userInfo =qywxUserApiService.getExternalUseridInfo(qwDTO.getThirdCorpid(), config.getWxSuiteid(), externalUserId);
        }
        String name = "";
        Integer addWay = 0 ;
        if (StringUtils.isBlank(userInfo)) {
            log.info("好友不存在");
            return new WelcomeUserInfoBO(name,0);
        }
        if ("1".equals(userInfo)) {
            log.info("限制次数");
            return new WelcomeUserInfoBO(name,0);
        }
        if (StringUtils.isNotBlank(userInfo)) {
            JSONObject userJson = JSON.parseObject(userInfo);
            String external_contact = userJson.getString("external_contact");
            JSONObject jt = null;
            if (StringUtils.isNotBlank(external_contact)) {
                jt = JSON.parseObject(external_contact);
                name = EmojiFilterUtil.filterEmojiLast(jt.getString("name"), true);
            }
            String follow_user = "";
            follow_user = userJson.getString("follow_user");
            if (StringUtils.isNotBlank(follow_user)) {
                JSONArray j = JSON.parseArray(follow_user);
                for (Object object : j) {
                    String followJson = JSON.toJSONString(object);
                    JSONObject follow = JSON.parseObject(followJson);
                    String userId = follow.getString("userid");
                    if (userId.equals(addUserId)) {
                        addWay = follow.getIntValue("add_way") ;
                    }
                }
            }
        }
        log.info("addWay={}",addWay);
        return new WelcomeUserInfoBO(name,addWay);
    }

    private String getClerkId(String wxEnterpriseId, String staffId, String state) {
        String hyClerkId = null;
        Long hmId = null;
        if (StringUtils.isNotBlank(state)
                && (state.startsWith(Manage3Constants.HM) || state.startsWith(Manage3Constants.DT))) {
            String id = state.substring(2);
            if (state.startsWith(Manage3Constants.HM)) {
                hmId = Convert.toLong(id);
            } else if (state.startsWith(Manage3Constants.DT)) {
                KeyDataLinkBO linkData = keyDataService.getDataForHmLink(id);
                if (null != linkData) {
                    hmId = linkData.getHmId();
                }
            }
            if (null != hmId) {
                HmQrcodeDTO hmDTO = hmQrcodeApiService.getSimpleById(hmId);
                if (hmDTO != null) {
                    hyClerkId = this.getHmClerk(hmDTO, wxEnterpriseId, staffId);
                    log.info("获取到活码导购={},hmId={}", hyClerkId, hmId);
                }
            }
        }
        if (StringUtils.isEmpty(hyClerkId)) {
            TabHaobanClerkMainStoreRelated mainStoreRelated = clerkMainStoreRelatedService
                    .getMainStoreByStaffId(staffId, wxEnterpriseId);
            if (mainStoreRelated != null) {
                StaffClerkRelationDTO staffClerkRelationDTO = staffClerkRelationService.getOneBindByStoreId(staffId,
                        mainStoreRelated.getStoreId());
                if (null != staffClerkRelationDTO) {
                    hyClerkId = staffClerkRelationDTO.getClerkId();
                    log.info("获取成员关联导购={}", hyClerkId);
                }
            }
        }
        return hyClerkId;
    }

    @Override
    public String getHmClerk(HmQrcodeDTO hmDTO, String wxEnterpriseId, String staffId) {
        Long hmId = hmDTO.getHmId();
        if (hmDTO.getHmType() == 1) {
            return hmDTO.getClerkId();
        } else if (hmDTO.getHmType() == 2) {
            // 有主门店导购绑定主导购，没有主门店导购绑定第一个导购，其余随机
            HmClerkRelationDTO hmClerkRelationDTO = null;
            TabHaobanClerkMainStoreRelated mainStoreRelated = clerkMainStoreRelatedService
                    .getMainStoreByStaffId(staffId, wxEnterpriseId);
            if (mainStoreRelated != null) {
                StaffClerkRelationDTO relationDTO = staffClerkRelationService.getOneBindByStoreId(staffId,
                        mainStoreRelated.getStoreId());
                hmClerkRelationDTO = hmClerkRelationApiService.getHmClerkByHmIdAndClerkId(hmId,
                        relationDTO.getClerkId());
                if (hmClerkRelationDTO == null) {
                    hmClerkRelationDTO = hmClerkRelationApiService.getOneHmClerkByHmIdAndStaffId(hmId, staffId);
                    if (hmClerkRelationDTO == null) {
                        hmClerkRelationDTO = hmClerkRelationApiService.getOneHmClerkByHmId(hmId);
                    }
                }
            } else {
                hmClerkRelationDTO = hmClerkRelationApiService.getOneHmClerkByHmId(hmId);
            }
            return hmClerkRelationDTO.getClerkId();
        }
        return null;
    }
}
