package com.ydd.app.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ydd.api.DeliveryService; import com.ydd.app.QueryAmountEntityUtils; import com.ydd.app.ShopEntityUtils; import com.ydd.app.service.ApiDeliveryBindService; import com.ydd.common.enums.DeliveryTypeEnums; import com.ydd.common.utils.StringUtils; import com.ydd.ecloud.core.utils.JsonMapper; import com.ydd.module.domain.*; import com.ydd.module.dto.DeliveryInfo; import com.ydd.module.dto.ShopBindDto; import com.ydd.module.dto.ShopDeliveryBindDto; import com.ydd.module.dto.UpdateDadaShopNoDto; import com.ydd.module.enums.*; import com.ydd.module.expection.CustomAppException; import com.ydd.module.service.IDspDeliveryService; import com.ydd.module.service.IMerchantService; import com.ydd.module.service.IShopDeliveryBindService; import com.ydd.module.service.IShopService; import com.ydd.system.service.ISysConfigService; import com.ydd.third.common.dto.*; import com.ydd.third.common.vo.ResObject; import com.ydd.third.common.vo.callback.ShansongTokenVo; import com.ydd.third.common.vo.shop.AmountResultVo; import com.ydd.third.common.vo.shop.QueryAmount; import com.ydd.third.common.vo.shop.ShopResultVo; import com.ydd.third.common.vo.shop.ShopVo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 门店绑定运力Service * @author 叶君翔 * @date 2021/12/05 09:53 */ @Slf4j @Service @RequiredArgsConstructor(onConstructor_ = @Autowired) public class ApiDeliveryBindServiceImpl implements ApiDeliveryBindService { private final ISysConfigService iSysConfigService; private final IDspDeliveryService iDspDeliveryService; private final IShopDeliveryBindService iShopDeliveryBindService; private final IMerchantService iMerchantService; private final DeliveryService client; private final IShopService iShopService; @Override public List list(Member member) { List bindList = iShopDeliveryBindService.findBindList(member.getMerchantId(), member.getShopId(), member.getMemberType()); if (CollectionUtils.isNotEmpty(bindList)) { bindList = bindList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(ShopDeliveryBindDto::getDeliveryId))), ArrayList::new)); } // 查询余额 for (ShopDeliveryBindDto bindDto : bindList){ DeliveryInfo delivery = new DeliveryInfo(); delivery.setThirdShopId(bindDto.getThirdShopId()); delivery.setShopId(bindDto.getThirdShopId()); delivery.setAuthToken(bindDto.getAuthToken()); delivery.setPhone(bindDto.getAccount()); delivery.setType(bindDto.getDeliveryType()); DspDelivery dspDelivery = iDspDeliveryService.getById(bindDto.getDeliveryId()); QueryAmount queryAmount = QueryAmountEntityUtils.queryAmount(delivery, member.getMerchantId(),1); if(StringUtils.isBlank(queryAmount.getLbClient())){ continue; } ResObject amountRes = client.queryAmount(queryAmount); if (amountRes.getCode() == 0) { // 更新余额 AmountResultVo vo = amountRes.getData(); this.updateAmount(dspDelivery, member.getMerchantId(), bindDto.getShopId(), vo.getBalanceAmount()); bindDto.setBalance(vo.getBalanceAmount()); } // 达达、达达优质是否已绑定开放平台模式 if (DeliveryTypeEnums.DADA.getType().equals(dspDelivery.getType()) || DeliveryTypeEnums.DADA_YZ.getType().equals(dspDelivery.getType())) { int count = iShopDeliveryBindService.count(new QueryWrapper() .eq("delivery_id", bindDto.getDeliveryId()) .eq("merchant_id", bindDto.getMerchantId()) .eq("dada_bind_type", DadaBindTypeEnum.OPEN.type) .eq("deleted", 0)); bindDto.setDadaBindType(count > 0 ? DadaBindTypeEnum.OPEN.type : null); } // 同一猎豹商户的同一运力有多个运力商户编号,则不展示商户号和余额 if (DeliveryTypeEnums.DADA.getType().equals(dspDelivery.getType()) || DeliveryTypeEnums.DADA_YZ.getType().equals(dspDelivery.getType()) || DeliveryTypeEnums.SHUN_FENG.getType().equals(dspDelivery.getType()) || DeliveryTypeEnums.MEI_TUAN_ZB.getType().equals(dspDelivery.getType()) || DeliveryTypeEnums.FENG_NIAO.getType().equals(dspDelivery.getType())) { int count = iShopDeliveryBindService.count(new QueryWrapper() .eq("delivery_id", bindDto.getDeliveryId()) .eq("merchant_id", bindDto.getMerchantId()) .eq(MemberTypeEnum.SHOP.type.equals(member.getMemberType()),"shop_id", member.getShopId()) .ne("third_shop_id", bindDto.getThirdShopId()) .eq("deleted", 0)); if (count > 0) { bindDto.setThirdShopId(null); bindDto.setBalance(null); } } } List bindDeliveryIds = bindList.stream().map(ShopDeliveryBindDto::getDeliveryId).collect(Collectors.toList()); List unbindList = iShopDeliveryBindService.findUnbindList(member.getMerchantId(), bindDeliveryIds, member.getShopId(), member.getMemberType()); if (CollectionUtils.isNotEmpty(unbindList)) { unbindList = unbindList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(ShopDeliveryBindDto::getDeliveryId))), ArrayList::new)); for (ShopDeliveryBindDto unbindDto : unbindList) { if (unbindDto.getDeliveryType().equals(DeliveryTypeEnums.DADA.getType()) || unbindDto.getDeliveryType().equals(DeliveryTypeEnums.DADA_YZ.getType()) || unbindDto.getDeliveryType().equals(DeliveryTypeEnums.SHUN_FENG.getType())) { bindList.add(unbindDto); } } } List deliveryList = iDspDeliveryService.getDeliveryList(); deliveryList.removeAll(bindList); for (ShopDeliveryBindDto dto : deliveryList) { if (!dto.getDeliveryType().equals(DeliveryTypeEnums.MEI_TUAN.getType()) && !dto.getDeliveryType().equals(DeliveryTypeEnums.FENG_NIAO.getType()) && !dto.getDeliveryType().equals(DeliveryTypeEnums.FENG_NIAO_KA.getType()) && !dto.getDeliveryType().equals(DeliveryTypeEnums.FENG_NIAO_PT.getType()) && !dto.getDeliveryType().equals(DeliveryTypeEnums.HUO_LA_LA.getType())) { dto.setBindStatus(BindStatusEnum.UNBIND.getStatus()); bindList.add(dto); } } return bindList.stream().sorted(Comparator.comparing(ShopDeliveryBindDto::getDeliveryType)).collect(Collectors.toList()); } private void updateAmount(DspDelivery delivery, Long merchantId, Long shopId, BigDecimal balanceAmount) { if (DeliveryTypeEnums.DADA.getType().equals(delivery.getType()) || DeliveryTypeEnums.DADA_YZ.getType().equals(delivery.getType()) || DeliveryTypeEnums.FENG_NIAO.getType().equals(delivery.getType()) || DeliveryTypeEnums.FENG_NIAO_PT.getType().equals(delivery.getType()) || DeliveryTypeEnums.SHUN_FENG.getType().equals(delivery.getType()) || DeliveryTypeEnums.MEI_TUAN_ZB.getType().equals(delivery.getType())) { ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper() .eq("merchant_id", merchantId).eq("shop_id", shopId).eq("delivery_id", delivery.getId()) .eq("deleted", IsDeleteEnum.NORMAL.status).eq("bind_status", 1)); if (shopDeliveryBind != null) { // 门店模式变更余额 iShopDeliveryBindService.updateShopAmount(shopDeliveryBind.getMerchantId(), shopDeliveryBind.getDeliveryId(), shopDeliveryBind.getThirdShopId(), balanceAmount); } } else { ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper() .eq("merchant_id", merchantId).eq("delivery_id", delivery.getId()) .eq("deleted", IsDeleteEnum.NORMAL.status).isNull("shop_id") .eq("bind_status", 1)); // 商户模式变更余额 if (shopDeliveryBind != null) { iShopDeliveryBindService.updateMerchantAmount(shopDeliveryBind.getId(), balanceAmount); } } } @Override @Transactional(rollbackFor = Exception.class) public String bind(Member member, Long shopId, Long deliveryId, String thirdShopId, String dadaName) { if (member.getMerchantId() == null) { throw new CustomAppException("请先申请成为商家!"); } Merchant merchant = iMerchantService.getById(member.getMerchantId()); if (merchant == null) { throw new CustomAppException("商家信息异常,请联系管理员!"); } DspDelivery delivery = iDspDeliveryService.getById(deliveryId); if (delivery == null) { throw new CustomAppException("暂不支持该运力绑定!"); } Integer deliveryType = delivery.getType(); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getShopDeliveryBindInfo(member.getMerchantId(), shopId, deliveryId, deliveryType); if (!DeliveryTypeEnums.DADA.getType().equals(deliveryType) && !DeliveryTypeEnums.DADA_YZ.getType().equals(deliveryType) && shopDeliveryBind != null && BindStatusEnum.BIND.status.equals(shopDeliveryBind.getBindStatus())) { throw new CustomAppException("已绑定成功!"); } String url = null; if (delivery.getType().equals(DeliveryTypeEnums.SHAN_SONG.getType())){ url = client.shansongAuthUrl(merchant.getCode()); } if (delivery.getType().equals(DeliveryTypeEnums.FENG_NIAO.getType())){ url = client.fengniaoAuthUrl(); } if (delivery.getType().equals(DeliveryTypeEnums.FENG_NIAO_PT.getType())){ url = client.fengniaoAuthUrl(); } if (delivery.getType().equals(DeliveryTypeEnums.AIPT.getType())){ url = client.aptAuthUrl(merchant.getCode()); } if (delivery.getType().equals(DeliveryTypeEnums.HUO_LA_LA.getType())){ url = client.hllAuthUrl(); } if (delivery.getType().equals(DeliveryTypeEnums.KUAI_FU.getType())){ url = client.kfAuthUrl(merchant.getCode()); } if (DeliveryTypeEnums.MEI_TUAN_ZB.getType().equals(deliveryType)) { shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setAccount("sssss"); shopDeliveryBind.setMerchantId(merchant.getId()); shopDeliveryBind.setShopId(shopId); shopDeliveryBind.setDeliveryId(delivery.getId()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); iShopDeliveryBindService.save(shopDeliveryBind); } if (StringUtils.isBlank(thirdShopId) && (DeliveryTypeEnums.DADA.getType().equals(deliveryType)) || DeliveryTypeEnums.DADA_YZ.getType().equals(deliveryType)) { int type = 0; if (DeliveryTypeEnums.DADA_YZ.getType().equals(deliveryType)) { type = 1; } JSONObject jsonObject = client.partyTicket(type); if (jsonObject != null && jsonObject.getString("status").equals("success")) { String state = merchant.getCode(); if (shopId != null) { state = state + "_" + shopId; } url = client.getPartyOauth(jsonObject.getString("result"), type, state); } else { throw new CustomAppException(jsonObject == null ? "获取渠道授权码异常!" : jsonObject.getString("msg")); } } if (DeliveryTypeEnums.SHUN_FENG.getType().equals(deliveryType) && StringUtils.isNotBlank(thirdShopId)) { if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setAccount("sssss"); if (StringUtils.isNotBlank(dadaName)) { shopDeliveryBind.setAccount(dadaName); } shopDeliveryBind.setShopId(shopId); shopDeliveryBind.setBalance(new BigDecimal("0")); shopDeliveryBind.setThirdShopId(thirdShopId); shopDeliveryBind.setMerchantId(merchant.getId()); shopDeliveryBind.setDeliveryId(delivery.getId()); shopDeliveryBind.setBindStatus(BindStatusEnum.CHECK.status); iShopDeliveryBindService.save(shopDeliveryBind); } else { // 更新 shopDeliveryBind.setShopId(shopId); shopDeliveryBind.setThirdShopId(thirdShopId); shopDeliveryBind.setBindStatus(BindStatusEnum.CHECK.status); shopDeliveryBind.setAccount("sssss"); if (StringUtils.isNotBlank(dadaName)) { shopDeliveryBind.setAccount(dadaName); } iShopDeliveryBindService.updateById(shopDeliveryBind); } } return url; } @Override @Transactional(rollbackFor = Exception.class) public void bindDaDa(String sourceId, String state, Integer type, String shopNo) { log.info("绑定达达回调: sourceId: {}, state: {}, type: {}, shopNo: {}", sourceId, state, type, shopNo); String merchantCode = state; Long shopId = null; if (StringUtils.isNotEmpty(state) && state.contains("_")) { String[] split = state.split("_"); if (split.length == 2) { merchantCode = split[0]; shopId = Long.valueOf(split[1]); } } Merchant merchant = iMerchantService.getOne(new QueryWrapper().eq("code", merchantCode)); if (merchant == null) { log.info("达达绑定商家为空"); return; } DspDelivery delivery = iDspDeliveryService.findOne(type); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper().eq("merchant_id", merchant.getId()). eq("delivery_id", delivery.getId()).eq("shop_id", shopId).eq("deleted",0)); if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); } shopDeliveryBind.setThirdShopId(sourceId); shopDeliveryBind.setMerchantId(merchant.getId()); shopDeliveryBind.setShopId(shopId); shopDeliveryBind.setBindStatus(BindStatusEnum.WAIT_INFO.status); shopDeliveryBind.setDeliveryId(delivery.getId()); shopDeliveryBind.setDadaBindType(DadaBindTypeEnum.OPEN.type); iShopDeliveryBindService.saveOrUpdate(shopDeliveryBind); if (StringUtils.isNotBlank(shopNo)) { shopDeliveryBind.setDadaBindType(DadaBindTypeEnum.APP.type); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); if (DeliveryTypeEnums.DADA.getType().equals(delivery.getType())) { shopDeliveryBind.setDadaShopNo(shopNo); } else if (DeliveryTypeEnums.DADA_YZ.getType().equals(delivery.getType())) { shopDeliveryBind.setDadaYzShopNo(shopNo); } // 更新门店编号 iShopDeliveryBindService.updateById(shopDeliveryBind); // iShopDeliveryBindService.updateDaDaShopNo(type, shopDeliveryBind.getDeliveryId(), shopNo, merchant.getId(), shopId); // 更新余额 this.changeDadaBalance(shopDeliveryBind, delivery.getType()); } } @Override @Transactional(rollbackFor = Exception.class) public void bindShunfeng(String outShopId, Integer shopId, Integer type) { log.info("绑定顺丰回调: outShopId: {}, shopId: {}, type: {}", outShopId, shopId, type); Shop shop = iShopService.getOne(new QueryWrapper().eq("deleted", 0).eq("code", outShopId)); Merchant merchant = null; if (shop.getMerchantId() != null) { merchant = iMerchantService.getById(shop.getMerchantId()); } if (merchant == null) { log.info("顺丰绑定商家为空"); return; } DspDelivery delivery = iDspDeliveryService.findOne(type); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper().eq("merchant_id", merchant.getId()). eq("delivery_id", delivery.getId()).eq("shop_id", shop.getId()).eq("deleted",0)); if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); } shopDeliveryBind.setThirdShopId(shopId.toString()); shopDeliveryBind.setMerchantId(merchant.getId()); shopDeliveryBind.setShopId(shop.getId()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); shopDeliveryBind.setDeliveryId(delivery.getId()); iShopDeliveryBindService.saveOrUpdate(shopDeliveryBind); // 更新余额 this.changeShunfengBalance(shopDeliveryBind, delivery.getType()); } private void changeDadaBalance(ShopDeliveryBind shopDeliveryBind, Integer deliveryType){ QueryAmount queryAmount = new QueryAmount(); queryAmount.setIsDirectDelivery(0); queryAmount.setLbClient(DeliveryTypeEnums.DADA.getName()); if (DeliveryTypeEnums.DADA_YZ.getType().equals(deliveryType)) { queryAmount.setIsDirectDelivery(1); queryAmount.setLbClient(DeliveryTypeEnums.DADA_YZ.getName()); } // 查询余额 queryAmount.setMerchantId(shopDeliveryBind.getThirdShopId()); ResObject res = client.queryAmount(queryAmount); if(res.getCode() == 0) { BigDecimal balanceAmount = res.getData().getBalanceAmount(); // 变更余额 iShopDeliveryBindService.updateShopAmount(shopDeliveryBind.getMerchantId(), shopDeliveryBind.getDeliveryId(), shopDeliveryBind.getThirdShopId(), balanceAmount); } } private void changeShunfengBalance(ShopDeliveryBind shopDeliveryBind, Integer deliveryType){ QueryAmount queryAmount = new QueryAmount(); // 查询余额 queryAmount.setMerchantId(shopDeliveryBind.getThirdShopId()); queryAmount.setShopType(1); ResObject res = client.queryAmount(queryAmount); if(res.getCode() == 0) { BigDecimal balanceAmount = res.getData().getBalanceAmount(); // 变更余额 iShopDeliveryBindService.updateShopAmount(shopDeliveryBind.getMerchantId(), shopDeliveryBind.getDeliveryId(), shopDeliveryBind.getThirdShopId(), balanceAmount); } } @Override public void updateDadaShopNo(Member member, UpdateDadaShopNoDto updateDadaShopNoDto) { int merchantCount = iShopDeliveryBindService.count(new QueryWrapper() .eq("merchant_id", member.getMerchantId()) .eq("third_shop_id", updateDadaShopNoDto.getMerchantNo()) .eq("delivery_id", updateDadaShopNoDto.getDeliveryId()) .in("bind_status", Arrays.asList(BindStatusEnum.BIND.status, BindStatusEnum.WAIT_INFO.status)) .eq("deleted",0)); if (merchantCount == 0) { throw new CustomAppException("当前商户未授权猎豹AI,请重新绑定"); } DspDelivery dspDelivery = iDspDeliveryService.findById(updateDadaShopNoDto.getDeliveryId()); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getById(updateDadaShopNoDto.getBindId()); if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setMerchantId(member.getMerchantId()); shopDeliveryBind.setShopId(updateDadaShopNoDto.getShopId()); shopDeliveryBind.setDeliveryId(dspDelivery.getId()); shopDeliveryBind.setAccount(updateDadaShopNoDto.getMobile()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); shopDeliveryBind.setThirdShopId(updateDadaShopNoDto.getMerchantNo()); if (DeliveryTypeEnums.DADA.getType().equals(dspDelivery.getType())) { shopDeliveryBind.setDadaShopNo(updateDadaShopNoDto.getShopNo()); } else if (DeliveryTypeEnums.DADA_YZ.getType().equals(dspDelivery.getType())) { shopDeliveryBind.setDadaYzShopNo(updateDadaShopNoDto.getShopNo()); } shopDeliveryBind.setDadaBindType(DadaBindTypeEnum.OPEN.type); iShopDeliveryBindService.save(shopDeliveryBind); } else { if (DeliveryTypeEnums.DADA.getType().equals(dspDelivery.getType())) { shopDeliveryBind.setDadaShopNo(updateDadaShopNoDto.getShopNo()); } else if (DeliveryTypeEnums.DADA_YZ.getType().equals(dspDelivery.getType())) { shopDeliveryBind.setDadaYzShopNo(updateDadaShopNoDto.getShopNo()); } shopDeliveryBind.setThirdShopId(updateDadaShopNoDto.getMerchantNo()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); shopDeliveryBind.setAccount(updateDadaShopNoDto.getMobile()); // 更新门店编号 iShopDeliveryBindService.updateById(shopDeliveryBind); } // 变更余额 this.changeDadaBalance(shopDeliveryBind, dspDelivery.getType()); } @Override @Transactional(rollbackFor = Exception.class) public void release(Long bindId) { ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getById(bindId); if (shopDeliveryBind == null) { throw new CustomAppException("解除绑定失败, 请联系客服人员处理!"); } DspDelivery delivery =iDspDeliveryService.getById(shopDeliveryBind.getDeliveryId()); Integer deliveryType = delivery.getType(); if (DeliveryTypeEnums.SHUN_FENG.getType().equals(deliveryType) || DeliveryTypeEnums.DADA.getType().equals(deliveryType) || DeliveryTypeEnums.DADA_YZ.getType().equals(deliveryType) || DeliveryTypeEnums.MEI_TUAN_ZB.getType().equals(deliveryType) || DeliveryTypeEnums.KUAI_FU.getType().equals(deliveryType)) { iShopDeliveryBindService.removeById(bindId); } if (DeliveryTypeEnums.AIPT.getType().equals(deliveryType)) { AiptUnBindDto aiptUnBindDto = this.unBindAipt(bindId); if ("Fail".equals(aiptUnBindDto.getState())) { throw new CustomAppException("解除绑定失败, 请联系客服人员处理!"); } } else if (DeliveryTypeEnums.SHAN_SONG.getType().equals(deliveryType)) { ResObject res = client.cancelToken(shopDeliveryBind.getAuthToken()); log.info("===闪送解除绑定返回: {}", JsonMapper.nonEmptyMapper().toJson(res)); if (res.getCode() == 0) { iShopDeliveryBindService.removeById(bindId); } } } @Override @Transactional(rollbackFor = Exception.class) public AiptUnBindDto unBindAipt(Long bindId) { ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getById(bindId); if (shopDeliveryBind == null) { AiptUnBindDto bindDto = new AiptUnBindDto(); bindDto.setState("Fail"); bindDto.setInfo("该运力不存在"); return bindDto; } // 解除绑定 AiptUnBindDto bindDto = client.unBind(shopDeliveryBind.getAccount()); log.info("===Aipt解除绑定返回: {}", JsonMapper.nonEmptyMapper().toJson(bindDto)); if (bindDto != null && bindDto.getCode() != null && bindDto.getCode() == 1 && "Success".equals(bindDto.getState())) { iShopDeliveryBindService.removeById(bindId); } else { bindDto = new AiptUnBindDto(); bindDto.setState("Fail"); bindDto.setInfo("解除绑定失败, 请联系客服进行处理!"); } return bindDto; } @Override public String explain() { return iSysConfigService.selectConfigByKey("BINDING_PROCESS"); } @Override @Transactional(rollbackFor = Exception.class) public void bindShansong(ShansongTokenVo tokenVo) { Merchant merchant = iMerchantService.getOne(new QueryWrapper().eq("code", tokenVo.getShopId())); if (merchant == null) { throw new CustomAppException("商户不存在!"); } DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.SHAN_SONG.getType()); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper().eq("merchant_id", merchant.getId()). eq("delivery_id", delivery.getId()).eq("deleted",0).isNull("shop_id")); if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setAccount("sssss"); shopDeliveryBind.setMerchantId(merchant.getId()); shopDeliveryBind.setDeliveryId(delivery.getId()); } shopDeliveryBind.setThirdShopId(tokenVo.getShopId()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); // 获取余额 try { QueryAmount queryAmount = new QueryAmount(); queryAmount.setAccessToken(tokenVo.getAccessToken()); queryAmount.setIsShansongSH(true); queryAmount.setLbClient(DeliveryTypeEnums.SHAN_SONG.getName()); ResObject res = client.queryAmount(queryAmount); if (res.getCode() == 0) { AmountResultVo vo = res.getData(); shopDeliveryBind.setBalance(vo.getBalanceAmount()); } } catch (Exception e) { e.printStackTrace(); } shopDeliveryBind.setAuthToken(tokenVo.getAccessToken()); shopDeliveryBind.setRefreshToken(tokenVo.getRefreshToken()); shopDeliveryBind.setExpiresIn(tokenVo.getExpireIn()); iShopDeliveryBindService.saveOrUpdate(shopDeliveryBind); } @Override @Transactional(rollbackFor = Exception.class) public void bindAipt(AiptAuthDto aiptAuthDto) { Merchant merchant = iMerchantService.getOne(new QueryWrapper().eq("code", aiptAuthDto.getUnique_id())); if (merchant == null) { throw new CustomAppException("商户不存在!"); } DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.AIPT.getType()); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper().eq("merchant_id", merchant.getId()). eq("delivery_id", delivery.getId()).eq("deleted",0).isNull("shop_id")); if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setAccount(aiptAuthDto.getPhone()); shopDeliveryBind.setThirdShopId(aiptAuthDto.getUnique_id()); shopDeliveryBind.setMerchantId(merchant.getId()); if ("bind".equals(aiptAuthDto.getState())){ shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); } else { shopDeliveryBind.setBindStatus(BindStatusEnum.UNBIND.status); } shopDeliveryBind.setDeliveryId(delivery.getId()); // 获取余额 try { QueryAmount queryAmount = new QueryAmount(); queryAmount.setPhone(aiptAuthDto.getPhone()); queryAmount.setLbClient(DeliveryTypeEnums.AIPT.getName()); ResObject res = client.queryAmount(queryAmount); if (res.getCode() == 0) { AmountResultVo vo = res.getData(); shopDeliveryBind.setBalance(vo.getBalanceAmount()); } } catch (Exception e) { e.printStackTrace(); } iShopDeliveryBindService.save(shopDeliveryBind); } } @Override @Transactional(rollbackFor = Exception.class) public void bindKf(KfAuthDto kfAuthDto) { Merchant merchant = iMerchantService.getOne(new QueryWrapper().eq("code", kfAuthDto.getMerchantCode())); if (merchant == null) { throw new CustomAppException("商家不存在!"); } DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.KUAI_FU.getType()); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper().eq("merchant_id", merchant.getId()). eq("delivery_id", delivery.getId()).eq("deleted",0).isNull("shop_id")); if (shopDeliveryBind == null) { shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setThirdShopId(kfAuthDto.getOpenid()); shopDeliveryBind.setMerchantId(merchant.getId()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); shopDeliveryBind.setDeliveryId(delivery.getId()); shopDeliveryBind.setAuthToken(kfAuthDto.getAccess_token()); // 表示该商家是首次绑定,不是yes则是下过订单后的账户 shopDeliveryBind.setAccount("yes"); iShopDeliveryBindService.save(shopDeliveryBind); } } @Override public boolean getUuptVerifyCode(String mobile, String ip) { ResObject resObject = client.bindUserApply(mobile, ip); return resObject.getCode() == 0; } @Override @Transactional(rollbackFor = Exception.class) public String bindUupt(Member member, String mobile, String validateCode, String cityName) { ResObject resObject = client.bindUserSubmit(mobile, validateCode, cityName); if (resObject.getCode() != 0) { return resObject.getMsg(); } BindUserSubmitResponse response = resObject.getData(); DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.UUPT.getType()); ShopDeliveryBind shopDeliveryBind = new ShopDeliveryBind(); shopDeliveryBind.setAccount(mobile); ResObject res = client.getBalance(response.getOpenid()); if(res.getCode() == 0) { shopDeliveryBind.setBalance(new BigDecimal(res.getData().getAccountMoney())); } shopDeliveryBind.setThirdShopId(response.getOpenid()); shopDeliveryBind.setMerchantId(member.getMerchantId()); shopDeliveryBind.setBindStatus(BindStatusEnum.BIND.status); shopDeliveryBind.setDeliveryId(delivery.getId()); iShopDeliveryBindService.save(shopDeliveryBind); return "ok"; } @Override @Transactional(rollbackFor = Exception.class) public String unBindUupt(Member member, String mobile) { DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.UUPT.getType()); ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper().eq("merchant_id",member.getMerchantId()). eq("delivery_id", delivery.getId()).eq("deleted",0).isNull("shop_id")); log.info("uupt解除绑定, shopDeliveryBind: {}", JSONObject.toJSONString(shopDeliveryBind)); if (shopDeliveryBind == null) { throw new CustomAppException("UUPT解除绑定失败, 请联系客服人员处理!"); } if (!mobile.equals(shopDeliveryBind.getAccount())) { throw new CustomAppException("请确认解绑手机号是否正确!"); } if (StringUtils.isBlank(shopDeliveryBind.getThirdShopId())) { throw new CustomAppException("UUPT解除绑定失败, 请联系客服人员处理!"); } ResObject resObject = client.cancelBind(shopDeliveryBind.getThirdShopId()); log.info("uupt 解除绑定返回: {}", JSONObject.toJSONString(resObject)); if (resObject.getCode() == 0) { iShopDeliveryBindService.removeById(shopDeliveryBind.getId()); return "ok"; } return resObject.getMsg(); } @Override public List shopList(Member member, Long deliveryId) { List list; if (MemberTypeEnum.MERCHANT.type.equals(member.getMemberType())) { list = iShopService.findListByMerchantId(member.getMerchantId()); } else if (MemberTypeEnum.SHOP.type.equals(member.getMemberType())) { list = iShopService.findListById(member.getShopId()); } else { throw new CustomAppException("无效的用户类型!"); } List shopDeliveryBindList = iShopDeliveryBindService.list(new QueryWrapper() .eq("merchant_id", member.getMerchantId()) .eq("delivery_id", deliveryId) .eq("deleted", 0)); Map> shopBindMap = shopDeliveryBindList.stream().collect(Collectors.groupingBy(ShopDeliveryBind::getShopId)); DspDelivery dspDelivery = iDspDeliveryService.getById(deliveryId); list.forEach(shopBindDto -> { // 达达、达达优质是否已绑定开放平台模式 if (DeliveryTypeEnums.DADA.getType().equals(dspDelivery.getType()) || DeliveryTypeEnums.DADA_YZ.getType().equals(dspDelivery.getType())) { int openCount = iShopDeliveryBindService.count(new QueryWrapper() .eq("delivery_id", dspDelivery.getId()) .eq("merchant_id", member.getMerchantId()) .eq("dada_bind_type", DadaBindTypeEnum.OPEN.type) .eq("deleted", 0)); shopBindDto.setDadaBindType(openCount > 0 ? DadaBindTypeEnum.OPEN.type : null); } List shopDeliveryBinds = shopBindMap.get(shopBindDto.getShopId()); if (CollectionUtils.isNotEmpty(shopDeliveryBinds)) { ShopDeliveryBind shopDeliveryBind = shopDeliveryBinds.get(0); BeanUtils.copyProperties(shopDeliveryBind, shopBindDto); shopBindDto.setBindId(shopDeliveryBind.getId()); if (BindStatusEnum.BIND.status.equals(shopBindDto.getBindStatus())) { // 更新余额 DeliveryInfo delivery = new DeliveryInfo(); delivery.setThirdShopId(shopDeliveryBind.getThirdShopId()); delivery.setShopId(shopDeliveryBind.getThirdShopId()); delivery.setAuthToken(shopDeliveryBind.getAuthToken()); delivery.setPhone(shopDeliveryBind.getAccount()); delivery.setType(dspDelivery.getType()); QueryAmount queryAmount = QueryAmountEntityUtils.queryAmount(delivery, member.getMerchantId(),1); ResObject amountRes = client.queryAmount(queryAmount); if (amountRes.getCode() == 0) { // 更新余额 AmountResultVo vo = amountRes.getData(); this.updateAmount(dspDelivery, member.getMerchantId(), shopDeliveryBind.getShopId(), vo.getBalanceAmount()); shopBindDto.setBalance(vo.getBalanceAmount()); } } } }); return list.stream().sorted(Comparator.comparing(ShopBindDto::getShopId)).collect(Collectors.toList()); } @Override @Transactional(rollbackFor = Exception.class) public Shop sfBind(Long shopId, String accountContactName, String accountContactPhone, String accountIdCard, String businessLicense, String healthPermits) { Shop shop = iShopService.getById(shopId); if (Objects.isNull(shop)) { throw new CustomAppException("门店不存在或被删除!"); } if (shop.getMerchantId() == null) { throw new CustomAppException("请先申请成为商家!"); } Merchant merchant = iMerchantService.getById(shop.getMerchantId()); if (merchant.getStatus().equals(MerchantStatusEnum.STOP.status)) { throw new CustomAppException("该商家已禁用!"); } if (merchant.getStatus().equals(MerchantStatusEnum.TO_BE_CERTIFIED.status)) { throw new CustomAppException("请等待商家通过认证!"); } DspDelivery dspDelivery = iDspDeliveryService.findOne(DeliveryTypeEnums.SHUN_FENG.getType()); List deliveryBindList = iShopDeliveryBindService.list(new QueryWrapper().eq("deleted", 0).eq("shop_id", shopId) .eq("delivery_id", dspDelivery.getId()).eq("bind_status", BindStatusEnum.BIND.getStatus())); if (CollectionUtils.isNotEmpty(deliveryBindList)) { throw new CustomAppException("该门店已经绑定顺丰!"); } shop.setLegalPerson(accountContactName); shop.setDirectorMobile(accountContactPhone); shop.setIdcardNo(accountIdCard); shop.setBizLicenseIdcard(businessLicense); shop.setFoodLicensePic(healthPermits); iShopService.updateById(shop); ShopVo shopVo = ShopEntityUtils.createShopVo(shop, dspDelivery, merchant); log.info("门店信息:" + JsonMapper.nonEmptyMapper().toJson(shopVo)); ResObject res = client.createShop(shopVo); // 顺丰自运力绑定插入数据 if ("shunfeng".equals(shopVo.getLbClient())) { ShopDeliveryBind deliveryBind= iShopDeliveryBindService.getOne(new QueryWrapper() .eq("deleted", 0) .eq("merchant_id", merchant.getId()) .eq("shop_id", shop.getId()) .eq("delivery_id", dspDelivery.getId())); if (Objects.isNull(deliveryBind)) { deliveryBind = new ShopDeliveryBind(); } deliveryBind.setMerchantId(merchant.getId()); deliveryBind.setShopId(shop.getId()); deliveryBind.setDeliveryId(dspDelivery.getId()); if (res.getCode() == 0) { deliveryBind.setBindStatus(BindStatusEnum.CHECK.getStatus()); }else { deliveryBind.setAuthMsg(res.getMsg()); deliveryBind.setBindStatus(BindStatusEnum.REFUND.getStatus()); } iShopDeliveryBindService.saveOrUpdate(deliveryBind); } return shop; } @Override public List sfShopList(Member member, Integer deliveryType) { List list = Lists.newArrayList(); if (MemberTypeEnum.MERCHANT.type.equals(member.getMemberType())) { list = iShopService.findListByMerchantId(member.getMerchantId()); } else if (MemberTypeEnum.SHOP.type.equals(member.getMemberType())) { list = iShopService.findListById(member.getShopId()); } else { throw new CustomAppException("无效的用户类型!"); } DspDelivery delivery = iDspDeliveryService.findOne(deliveryType); List shopList = iShopService.list(new QueryWrapper() .eq("merchant_id", member.getMerchantId()) .eq("deleted", 0)); // 已经绑定的顺丰店铺 List shopDeliveryBindList = iShopDeliveryBindService.list(new QueryWrapper() .eq("merchant_id", member.getMerchantId()) .eq("delivery_id", delivery.getId()) .eq("deleted", 0) .isNotNull("third_shop_id")); List shopBinds = shopDeliveryBindList.stream().map(ShopDeliveryBind::getShopId).collect(Collectors.toList()); List newShopList = null; if (CollectionUtils.isNotEmpty(shopBinds)) { newShopList = shopList.stream().filter(shop -> !shopBinds.contains(shop.getId())).collect(Collectors.toList()); } if (CollectionUtils.isEmpty(newShopList)) { newShopList = shopList; } for (Shop shop : newShopList) { ShopBindDto dto = new ShopBindDto(); dto.setShopId(shop.getId()); dto.setShopName(shop.getName()); list.add(dto); } return list.stream().sorted(Comparator.comparing(ShopBindDto::getShopId)).collect(Collectors.toList()); } @Override public List sfList(Member member) { List bindList = iShopDeliveryBindService.sfList(member.getMerchantId()); if (CollectionUtils.isNotEmpty(bindList)) { for (ShopDeliveryBindDto dto : bindList) { Shop shop = iShopService.getById(dto.getShopId()); dto.setShopCode(shop.getCode()); if (BindStatusEnum.BIND.getStatus().equals(dto.getBindStatus())) { dto.setAccountContactPhone(shop.getDirectorMobile()); // 查询余额 DeliveryInfo delivery = new DeliveryInfo(); delivery.setThirdShopId(dto.getThirdShopId()); delivery.setShopId(dto.getThirdShopId()); delivery.setAuthToken(dto.getAuthToken()); delivery.setPhone(dto.getAccount()); delivery.setType(dto.getDeliveryType()); DspDelivery dspDelivery = iDspDeliveryService.getById(dto.getDeliveryId()); QueryAmount queryAmount = QueryAmountEntityUtils.queryAmount(delivery, member.getMerchantId(),1); ResObject amountRes = client.queryAmount(queryAmount); if (amountRes.getCode() == 0) { // 更新余额 AmountResultVo vo = amountRes.getData(); this.updateAmount(dspDelivery, member.getMerchantId(), dto.getShopId(), vo.getBalanceAmount()); dto.setBalance(vo.getBalanceAmount()); } } } } return bindList; } }