package com.cool.store.service.impl;

import com.cool.store.dao.*;
import com.cool.store.dto.AccountAuditDTO;
import com.cool.store.dto.AccountEntryStatusAuditDTO;
import com.cool.store.dto.ModifyPasswordDTO;
import com.cool.store.dto.ShopAccount.ShopAccountDTO;
import com.cool.store.dto.AccountEntryStatusChangeDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.GetPasswordDTO;
import com.cool.store.request.PostAndOrderRequest;
import com.cool.store.request.ZxjpApiRequest;
import com.cool.store.response.MiniShopsResponse;
import com.cool.store.service.*;
import com.cool.store.utils.PasswordUtil;
import com.cool.store.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/4/7 15:51
 * @Version 1.0
 */
@Slf4j
@Service
public class ShopAccountServiceImpl implements ShopAccountService {
    @Resource
    QualificationsInfoDAO qualificationsInfoDAO;
    @Resource
    ShopAccountDAO shopAccountDAO;
    @Resource
    ShopInfoDAO shopInfoDAO;
    @Resource
    ShopService shopService;
    @Resource
    HyPartnerUserInfoDAO hyPartnerUserInfoDAO;
    @Resource
    SyncDataService syncDataService;
    @Resource
    PushService pushService;
    @Resource
    ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    PosAndOrderInfoService posAndOrderInfoService;
    @Resource
    OldShopDAO oldShopDAO;
    @Resource
    ThirdXinGuanJiaService thirdXinGuanJiaService;
    @Resource
    private LineInfoDAO lineInfoDAO;
    @Resource
    private StoreDao storeDao;

    @Override
    public List<ShopAccountDTO> getShopAccountByShopId(Long shopId) {
        if (Objects.isNull(shopId)) {
            throw new ServiceException(ErrorCodeEnum.ERROR_MESSAGE);
        }
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        if (Objects.isNull(shopInfo)) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        List<ShopAccountDO> shopAccountDOS = shopAccountDAO.selectByShopId(shopId);
        List<ShopAccountDTO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(shopAccountDOS)) {
            return result;
        }

        shopAccountDOS.forEach(x -> {
            ShopAccountDTO shopAccountDTO = new ShopAccountDTO();
            shopAccountDTO.setBoundPhone(x.getBoundPhone());
            shopAccountDTO.setShopId(x.getShopId());
            shopAccountDTO.setSystemName(x.getSystemName());
            shopAccountDTO.setAccount(x.getAccount());
            if (ShopAccountEnum.getSpecialList().contains(x.getSystemName())) {
                shopAccountDTO.setAccount(shopInfo.getShopCode());
            }
            shopAccountDTO.setPassword(x.getPassword());
            shopAccountDTO.setStatus(x.getStatus());
            shopAccountDTO.setEntryStatus(x.getEntryStatus());
            shopAccountDTO.setLastSyncTime(x.getLastSyncTime());
            shopAccountDTO.setRemark(x.getRemark());
            result.add(shopAccountDTO);
        });
        return result;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyPassword(ModifyPasswordDTO modifyPasswordDTO) {
        //查询
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(modifyPasswordDTO.getShopId());
        if (Objects.isNull(shopInfo)) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByPartnerId(shopInfo.getPartnerId());
        hyPartnerUserInfoDO.setDownstreamSystemPassword(modifyPasswordDTO.getPassword());
        hyPartnerUserInfoDO.setDownstreamSystemSecondaryPassword(modifyPasswordDTO.getSecondaryPassword());
        hyPartnerUserInfoDO.setDownstreamSystemSalting(modifyPasswordDTO.getPasswordSalt());
        hyPartnerUserInfoDO.setUpdateTime(new Date());
        hyPartnerUserInfoDAO.updatePasswordByPartnerId(hyPartnerUserInfoDO);

        List<Long> shopIdList = shopInfoDAO.getShopIdList(shopInfo.getLineId());

        //批量修改密码 密码盐
        shopAccountDAO.batchUpdatePasswordByShopIds(shopIdList, modifyPasswordDTO.getPassword(), modifyPasswordDTO.getSecondaryPassword(), modifyPasswordDTO.getPasswordSalt(), new Date());

        //当前加盟商所有开店成功的CODE
        List<String> shopCodeList = shopInfoDAO.getShopCodeList(shopInfo.getLineId());
        modifyPasswordDTO.setShopCode(shopCodeList);
        modifyPasswordDTO.setMobile(hyPartnerUserInfoDO.getMobile());

        //推送下游系统
        pushService.modifyDownstreamSystem(modifyPasswordDTO);

        return Boolean.TRUE;

    }

    @Override
    public ZxjpApiRequest getData(Long shopId, DownSystemTypeEnum systemType) {
        return syncDataService.getData(shopId, systemType);
    }

    @Override
    public Boolean auditAccount(AccountAuditDTO accountAuditDTO) {
        if (accountAuditDTO.getShopId() == null || accountAuditDTO.getAuditFlag() == null || accountAuditDTO.getShopAccountEnum() == null) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        List<ShopAccountDO> accountDOS = shopAccountDAO.selectByShopId(accountAuditDTO.getShopId());
        if (CollectionUtils.isEmpty(accountDOS)) {
            throw new ServiceException(ErrorCodeEnum.SYSTEM_DATA_ERROR);
        }
        Map<String, ShopAccountDO> map = accountDOS.stream().collect(Collectors.toMap(ShopAccountDO::getSystemName, data -> data));
        ShopAccountDO shopAccountDO = map.get(accountAuditDTO.getShopAccountEnum().getSystemName());
        if (Objects.isNull(shopAccountDO)) {
            throw new ServiceException(ErrorCodeEnum.SYSTEM_DATA_ERROR);
        }
        if (!(OpenStatusEnum.OPENSTATUSENUM_2.getCode().equals(shopAccountDO.getStatus())||OpenStatusEnum.OPENSTATUSENUM_3.getCode().equals(shopAccountDO.getStatus()))) {
            throw new ServiceException(ErrorCodeEnum.CURRENT_STATUS_NOT_OPERATION);
        }
        //同一份数据到新管家
        List<String> list = new ArrayList<>(Arrays.asList(shopAccountDO.getSystemName()));
        if (ShopAccountEnum.YLS.equals(accountAuditDTO.getShopAccountEnum())) {
            list.add(ShopAccountEnum.XZG.getSystemName());
        }
        if (ShopAccountEnum.XZG.equals(accountAuditDTO.getShopAccountEnum())) {
            list.add(ShopAccountEnum.YLS.getSystemName());
        }
        OpenStatusEnum openStatusEnum = accountAuditDTO.getAuditFlag() == 1 ? OpenStatusEnum.OPENSTATUSENUM_4 : OpenStatusEnum.OPENSTATUSENUM_3;
        shopAccountDAO.updateStatusByShopIdAndSystemName(accountAuditDTO.getShopId(), list, openStatusEnum.getCode(), null, null);
        return Boolean.TRUE;
    }

    @Override
    public Boolean accountEntryStatusAudit(AccountEntryStatusAuditDTO dto) {
        if (dto.getShopId() == null || dto.getAuditFlag() == null) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        List<ShopAccountDO> accountDOS = shopAccountDAO.selectByShopId(dto.getShopId());
        if (CollectionUtils.isEmpty(accountDOS)) {
            return Boolean.FALSE;
        }
        Map<String, ShopAccountDO> map = accountDOS.stream().collect(Collectors.toMap(ShopAccountDO::getSystemName, data -> data));
        ShopAccountDO shopAccountDO = map.get(ShopAccountEnum.HUOMA.getSystemName());
        if (Objects.isNull(shopAccountDO)) {
            throw new ServiceException(ErrorCodeEnum.SYSTEM_DATA_ERROR);
        }
        if (!(OpenStatusEnum.OPENSTATUSENUM_2.getCode().equals(shopAccountDO.getStatus())||OpenStatusEnum.OPENSTATUSENUM_3.getCode().equals(shopAccountDO.getStatus()))) {
            throw new ServiceException(ErrorCodeEnum.CURRENT_STATUS_NOT_OPERATION);
        }
        OpenStatusEnum openStatusEnum = dto.getAuditFlag() == 1 ? OpenStatusEnum.OPENSTATUSENUM_4 : OpenStatusEnum.OPENSTATUSENUM_3;
        //修改进件状态
        shopAccountDAO.updateEntryStatusByShopIdAndSystemName(dto.getShopId(), Arrays.asList(shopAccountDO.getSystemName()), openStatusEnum.getCode());
        return Boolean.TRUE;
    }

    @Override
    public Boolean accountEntryStatusChange(AccountEntryStatusChangeDTO dto) {
        if (dto.getShopId() == null || dto.getEntryStatus() == null) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        List<ShopAccountDO> accountDOS = shopAccountDAO.selectByShopId(dto.getShopId());
        if (CollectionUtils.isEmpty(accountDOS)) {
            return Boolean.FALSE;
        }
        Map<String, ShopAccountDO> map = accountDOS.stream().collect(Collectors.toMap(ShopAccountDO::getSystemName, data -> data));
        ShopAccountDO shopAccountDO = map.get(ShopAccountEnum.HUOMA.getSystemName());
        if (Objects.isNull(shopAccountDO)) {
            throw new ServiceException(ErrorCodeEnum.SYSTEM_DATA_ERROR);
        }
        //不是审核中 已进件 进件失败状态不支持修改
        if (!OpenStatusEnum.getSpecialList().contains(shopAccountDO.getEntryStatus())) {
            throw new ServiceException(ErrorCodeEnum.CURRENT_ENTRY_STATUS_NOT_OPERATION);
        }
        //修改进件状态
        shopAccountDAO.updateEntryStatusByShopIdAndSystemName(dto.getShopId(), Arrays.asList(shopAccountDO.getSystemName()), dto.getEntryStatus());
        return Boolean.TRUE;
    }

    @Override
    public Boolean pushData(AccountAuditDTO accountAuditDTO) {
        if (accountAuditDTO.getShopId() == null || accountAuditDTO.getShopAccountEnum() == null) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        DownSystemTypeEnum downSystemTypeEnum = DownSystemTypeEnum.XGJ;
        if (ShopAccountEnum.HUOMA.equals(accountAuditDTO.getShopAccountEnum())) {
            downSystemTypeEnum = DownSystemTypeEnum.POS;
        }
        ZxjpApiRequest data = this.getData(accountAuditDTO.getShopId(), downSystemTypeEnum);
        if (DownSystemTypeEnum.POS.equals(downSystemTypeEnum)) {
            Boolean successFlag = pushService.pushDataToPOS(data);
            OpenStatusEnum openStatusEnum = successFlag ? OpenStatusEnum.OPENSTATUSENUM_5 : OpenStatusEnum.OPENSTATUSENUM_6;
            shopAccountDAO.updateStatusByShopIdAndSystemName(accountAuditDTO.getShopId(), Arrays.asList(ShopAccountEnum.HUOMA.getSystemName()), openStatusEnum.getCode(), new Date(), null);
            //阶段完成
            PostAndOrderRequest postAndOrderRequest = new PostAndOrderRequest();
            postAndOrderRequest.setShopId(accountAuditDTO.getShopId());
            postAndOrderRequest.setType(PosAndOrderEnum.POS.getCode());
            postAndOrderRequest.setRemark("已为您开通系统账号和密码，密码已加密，初始密码为身份证后6位，可通过加盟小程序修改密码！后续由运营经理服务，请您联系您督导或者经理添加我们运营经理微信，感谢您的配合！");
            postAndOrderRequest.setAccount(data.getPartnerMobile());
            postAndOrderRequest.setPassword(data.getDownstreamSystemPassword());
            posAndOrderInfoService.submitOrUpdate(postAndOrderRequest, null);
        } else {
            pushService.pushDataToXGJ(data);
        }
        return Boolean.TRUE;
    }

    @Override
    public String shopIdToYlsCode(Long shopId) {
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        if (Objects.isNull(shopInfo)) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        List<ShopAccountDO> accountDOS = shopAccountDAO.selectByShopId(shopId);
        if (CollectionUtils.isEmpty(accountDOS)) {
            return "ZXA8_"+shopInfo.getShopCode();
        }
        Map<String, ShopAccountDO> map = accountDOS.stream().collect(Collectors.toMap(ShopAccountDO::getSystemName, data -> data));
        ShopAccountDO shopAccountDO = map.get(ShopAccountEnum.YLS.getSystemName());
        if (Objects.isNull(shopAccountDO)) {
            return "ZXA8_"+shopInfo.getShopCode();
        }
        return StringUtil.isEmpty(shopAccountDO.getAccount()) ? shopInfo.getShopCode() : shopAccountDO.getAccount();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean handleOldData() {
        List<ShopInfoDO> shopList = shopInfoDAO.getListByTime();
        if (CollectionUtils.isEmpty(shopList)) {
            return false;
        }
        Map<String, List<ShopInfoDO>> shopMap = shopList.stream().collect(Collectors.groupingBy(ShopInfoDO::getPartnerId));
        Set<String> partnerIds = shopList.stream().map(ShopInfoDO::getPartnerId).collect(Collectors.toSet());
        Set<Long> lineIdSet = shopList.stream().map(ShopInfoDO::getLineId).collect(Collectors.toSet());
        List<HyPartnerUserInfoDO> hyPartnerUserInfoDOS = hyPartnerUserInfoDAO.selectByPartnerIds(new ArrayList<>(partnerIds));
        List<QualificationsInfoDO> qualificationsInfoDOList = qualificationsInfoDAO.getByLineIds(new ArrayList<>(lineIdSet));
        Map<String, QualificationsInfoDO> qualificationsInfoDOMap = qualificationsInfoDOList.stream().collect(Collectors.toMap(QualificationsInfoDO::getPartnerId, Function.identity()));
        List<ShopAccountDO> updateList = new ArrayList<>();
        for (HyPartnerUserInfoDO hyPartnerUserInfoDO : hyPartnerUserInfoDOS) {
            QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDOMap.get(hyPartnerUserInfoDO.getPartnerId());
            List<ShopInfoDO> shopInfos = shopMap.get(hyPartnerUserInfoDO.getPartnerId());
            List<Long> shopIds = shopInfos.stream().map(ShopInfoDO::getId).collect(Collectors.toList());
            List<ShopAccountDO> accountDOList = shopAccountDAO.getByShopIds(shopIds);
            String substring = qualificationsInfoDO.getIdCardNo().substring(qualificationsInfoDO.getIdCardNo().length() - 6);
            //生成密码和盐值
            byte[] saltBytes = PasswordUtil.generateSalt();
            String salt = PasswordUtil.bytesToHex(saltBytes);
            String password = PasswordUtil.encryptPassword(substring, salt);
            GetPasswordDTO getPasswordDTO = new GetPasswordDTO();
            getPasswordDTO.setPassword(substring);
            String downstreamSystemSecondaryPassword = thirdXinGuanJiaService.getPassword(getPasswordDTO);
            // 修改用户表
            hyPartnerUserInfoDO.setDownstreamSystemPassword(password);
            hyPartnerUserInfoDO.setDownstreamSystemSalting(salt);
            hyPartnerUserInfoDO.setDownstreamSystemSecondaryPassword(downstreamSystemSecondaryPassword);
            //循环修改 门店账户表
            for (ShopAccountDO shopAccountDO : accountDOList) {
                if (shopAccountDO.getSystemName().equals(ShopAccountEnum.YLS.getSystemName())
                        || shopAccountDO.getSystemName().equals(ShopAccountEnum.XZG.getSystemName())
                        || shopAccountDO.getSystemName().equals(ShopAccountEnum.HUOMA.getSystemName())) {
                    shopAccountDO.setPassword(password);
                    shopAccountDO.setPasswordSalt(salt);
                    shopAccountDO.setSecondaryPassword(downstreamSystemSecondaryPassword);
                    updateList.add(shopAccountDO);
                }
            }
        }
        shopAccountDAO.dateHandle(updateList);
        hyPartnerUserInfoDAO.dataHandle(hyPartnerUserInfoDOS);
        return true;
    }

    @Override
    public String shopCodeToYlsCode(String shopCode) {
        ShopInfoDO shopInfoDO = shopInfoDAO.selectByStoreCode(shopCode);
        if (!Objects.isNull(shopInfoDO)) {
            return this.shopIdToYlsCode(shopInfoDO.getId());
        }
        //查询老店关联表数据
        OldShopDO oldShopDO = oldShopDAO.getByCode(shopCode);
        if (Objects.isNull(oldShopDO)) {
            StoreDO storeDO = storeDao.getByStoreNum(shopCode);
            if (Objects.isNull(storeDO)) {
                throw new ServiceException(ErrorCodeEnum.GET_YLS_CODE_FAIL);
            }
            oldShopDO = OldShopDO.builder()
                    .shopCode(shopCode)
                    .shopName(storeDO.getStoreName())
                    .mobile(storeDO.getTelephone())
                    .ylsShopCode("ZXA8_" + shopCode)
                    .build();
            oldShopDAO.insertSelective(oldShopDO);
        }
        return oldShopDO.getYlsShopCode();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean handleAccountPassword() {
        List<Long> shopIdList = shopAccountDAO.getPasswordIsNull();
        if (CollectionUtils.isEmpty(shopIdList)){
            return false;
        }
        List<ShopInfoDO> shopList = shopInfoDAO.getShopListByIds(shopIdList);
        Map<String, List<ShopInfoDO>> shopMap = shopList.stream().collect(Collectors.groupingBy(ShopInfoDO::getPartnerId));
        Set<String> partnerIds = shopList.stream().map(ShopInfoDO::getPartnerId).collect(Collectors.toSet());
        Set<Long> lineIdSet = shopList.stream().map(ShopInfoDO::getLineId).collect(Collectors.toSet());
        List<HyPartnerUserInfoDO> hyPartnerUserInfoDOS = hyPartnerUserInfoDAO.selectByPartnerIds(new ArrayList<>(partnerIds));
        List<QualificationsInfoDO> qualificationsInfoDOList = qualificationsInfoDAO.getByLineIds(new ArrayList<>(lineIdSet));
        Map<String, QualificationsInfoDO> qualificationsInfoDOMap = qualificationsInfoDOList.stream().collect(Collectors.toMap(QualificationsInfoDO::getPartnerId, Function.identity()));
        List<ShopAccountDO> updateList = new ArrayList<>();
        for (HyPartnerUserInfoDO hyPartnerUserInfoDO : hyPartnerUserInfoDOS) {
            QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDOMap.get(hyPartnerUserInfoDO.getPartnerId());
            List<ShopInfoDO> shopInfos = shopMap.get(hyPartnerUserInfoDO.getPartnerId());
            List<Long> shopIds = shopInfos.stream().map(ShopInfoDO::getId).collect(Collectors.toList());
            List<ShopAccountDO> accountDOList = shopAccountDAO.getByShopIds(shopIds);
            String substring = qualificationsInfoDO.getIdCardNo().substring(qualificationsInfoDO.getIdCardNo().length() - 6);
            //生成密码和盐值
            byte[] saltBytes = PasswordUtil.generateSalt();
            String salt = PasswordUtil.bytesToHex(saltBytes);
            String password = PasswordUtil.encryptPassword(substring, salt);
            GetPasswordDTO getPasswordDTO = new GetPasswordDTO();
            getPasswordDTO.setPassword(substring);
            String downstreamSystemSecondaryPassword = thirdXinGuanJiaService.getPassword(getPasswordDTO);
            // 修改用户表
            hyPartnerUserInfoDO.setDownstreamSystemPassword(password);
            hyPartnerUserInfoDO.setDownstreamSystemSalting(salt);
            hyPartnerUserInfoDO.setDownstreamSystemSecondaryPassword(downstreamSystemSecondaryPassword);
            //循环修改 门店账户表
            for (ShopAccountDO shopAccountDO : accountDOList) {
                if (shopAccountDO.getSystemName().equals(ShopAccountEnum.YLS.getSystemName())
                        || shopAccountDO.getSystemName().equals(ShopAccountEnum.XZG.getSystemName())
                        || shopAccountDO.getSystemName().equals(ShopAccountEnum.HUOMA.getSystemName())) {
                    shopAccountDO.setPassword(password);
                    shopAccountDO.setPasswordSalt(salt);
                    shopAccountDO.setSecondaryPassword(downstreamSystemSecondaryPassword);
                    updateList.add(shopAccountDO);
                }
            }
        }
        shopAccountDAO.dateHandle(updateList);
        hyPartnerUserInfoDAO.dataHandle(hyPartnerUserInfoDOS);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean handleOldDataAccount() {
        List<HyPartnerUserInfoDO> hyPartnerUserInfoDOS = hyPartnerUserInfoDAO.selectPasswordIsNull();
        if (CollectionUtils.isEmpty(hyPartnerUserInfoDOS)){
            return true;
        }
        List<String> partnerIds = hyPartnerUserInfoDOS.stream().map(HyPartnerUserInfoDO::getPartnerId).collect(Collectors.toList());

        List<QualificationsInfoDO> qualificationsInfoDOList = qualificationsInfoDAO.getByPartnerIds(partnerIds);
        Map<String, QualificationsInfoDO> qualificationsInfoDOMap = qualificationsInfoDOList.stream().collect(Collectors.toMap(QualificationsInfoDO::getPartnerId, Function.identity()));

        List<ShopInfoDO> shopInfoDOS = shopInfoDAO.selectByPartnerIds(partnerIds);
        Map<String,List<Long>> shopIdByPartnerIdMap = shopInfoDOS.stream().collect(Collectors.groupingBy(ShopInfoDO::getPartnerId,Collectors.mapping(ShopInfoDO::getId,Collectors.toList())));
        List<Long> shopIds = shopInfoDOS.stream().map(ShopInfoDO::getId).collect(Collectors.toList());
        // 获取密码为空的门店账户新管家，yls，pos
        List<ShopAccountDO> passwordIsNullByShopIds = shopAccountDAO.getPasswordIsNullByShopIds(shopIds);
        Map<Long, List<ShopAccountDO>> shopAccountDOMap = passwordIsNullByShopIds.stream().collect(Collectors.groupingBy(ShopAccountDO::getShopId));
        for (HyPartnerUserInfoDO hyPartnerUserInfoDO : hyPartnerUserInfoDOS){
            QualificationsInfoDO qualificationsInfoDO = qualificationsInfoDOMap.get(hyPartnerUserInfoDO.getPartnerId());
            if (qualificationsInfoDO == null ){
                log.info("获取证件信息失败，手机号：{}",hyPartnerUserInfoDO.getMobile());
                continue;
            }
            String substring = qualificationsInfoDO.getIdCardNo().substring(qualificationsInfoDO.getIdCardNo().length() - 6);
            if (StringUtil.isBlank(substring)){
                log.info("身份证截取失败，手机号：{}",hyPartnerUserInfoDO.getMobile());
                continue;
            }
            //生成密码和盐值
            byte[] saltBytes = PasswordUtil.generateSalt();
            String salt = PasswordUtil.bytesToHex(saltBytes);
            if (StringUtil.isBlank(salt)){
                log.info("盐值生成失败，手机号：{}",hyPartnerUserInfoDO.getMobile());
                continue;
            }
            String password = PasswordUtil.encryptPassword(substring, salt);
            if (StringUtil.isBlank(password)){
                log.info("密码生成失败，手机号：{}",hyPartnerUserInfoDO.getMobile());
                continue;
            }
            GetPasswordDTO getPasswordDTO = new GetPasswordDTO();
            getPasswordDTO.setPassword(substring);
            String downstreamSystemSecondaryPassword = thirdXinGuanJiaService.getPassword(getPasswordDTO);
            if (StringUtil.isBlank(downstreamSystemSecondaryPassword)){
                log.info("第二密码密码生成失败，手机号：{}",hyPartnerUserInfoDO.getMobile());
                continue;
            }
            hyPartnerUserInfoDO.setDownstreamSystemPassword(password);
            hyPartnerUserInfoDO.setDownstreamSystemSalting(salt);
            hyPartnerUserInfoDO.setDownstreamSystemSecondaryPassword(downstreamSystemSecondaryPassword);
            List<Long> shopIdList = shopIdByPartnerIdMap.get(hyPartnerUserInfoDO.getPartnerId());
            if (CollectionUtils.isEmpty(shopIdList)){
                log.info("没有门店，手机号：{}",hyPartnerUserInfoDO.getMobile());
                continue;
            }
            for (Long shopId : shopIdList){
                if (shopAccountDOMap.containsKey(shopId)){
                    List<ShopAccountDO> shopAccountDOList = shopAccountDOMap.get(shopId);
                    if (CollectionUtils.isNotEmpty(shopAccountDOList)){
                        for (ShopAccountDO shopAccountDO : shopAccountDOList) {
                            shopAccountDO.setPassword(password);
                            shopAccountDO.setPasswordSalt(salt);
                            shopAccountDO.setSecondaryPassword(downstreamSystemSecondaryPassword);
                        }
                    }
                }
            }
        }
        hyPartnerUserInfoDAO.dataHandle(hyPartnerUserInfoDOS);
        shopAccountDAO.dateHandle(passwordIsNullByShopIds);
        return true;
    }


}
