123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826 |
- 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<ShopDeliveryBindDto> list(Member member) {
- List<ShopDeliveryBindDto> 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<AmountResultVo> 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<ShopDeliveryBind>()
- .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<ShopDeliveryBind>()
- .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<Long> bindDeliveryIds = bindList.stream().map(ShopDeliveryBindDto::getDeliveryId).collect(Collectors.toList());
- List<ShopDeliveryBindDto> 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<ShopDeliveryBindDto> 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<ShopDeliveryBind>()
- .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<ShopDeliveryBind>()
- .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<Merchant>().eq("code", merchantCode));
- if (merchant == null) {
- log.info("达达绑定商家为空");
- return;
- }
- DspDelivery delivery = iDspDeliveryService.findOne(type);
- ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper<ShopDeliveryBind>().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<Shop>().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<ShopDeliveryBind>().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<AmountResultVo> 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<AmountResultVo> 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<ShopDeliveryBind>()
- .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<Merchant>().eq("code", tokenVo.getShopId()));
- if (merchant == null) {
- throw new CustomAppException("商户不存在!");
- }
- DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.SHAN_SONG.getType());
- ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper<ShopDeliveryBind>().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<AmountResultVo> 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<Merchant>().eq("code", aiptAuthDto.getUnique_id()));
- if (merchant == null) {
- throw new CustomAppException("商户不存在!");
- }
- DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.AIPT.getType());
- ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper<ShopDeliveryBind>().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<AmountResultVo> 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<Merchant>().eq("code", kfAuthDto.getMerchantCode()));
- if (merchant == null) {
- throw new CustomAppException("商家不存在!");
- }
- DspDelivery delivery = iDspDeliveryService.findOne(DeliveryTypeEnums.KUAI_FU.getType());
- ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getOne(new QueryWrapper<ShopDeliveryBind>().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<BindUserApplyResponse> 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<BindUserSubmitResponse> 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<GetBalanceDetailResponse> 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<ShopDeliveryBind>().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<CancelBindResponse> 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<ShopBindDto> shopList(Member member, Long deliveryId) {
- List<ShopBindDto> 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<ShopDeliveryBind> shopDeliveryBindList = iShopDeliveryBindService.list(new QueryWrapper<ShopDeliveryBind>()
- .eq("merchant_id", member.getMerchantId())
- .eq("delivery_id", deliveryId)
- .eq("deleted", 0));
- Map<Long, List<ShopDeliveryBind>> 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<ShopDeliveryBind>()
- .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<ShopDeliveryBind> 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<AmountResultVo> 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<ShopDeliveryBind> deliveryBindList = iShopDeliveryBindService.list(new QueryWrapper<ShopDeliveryBind>().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<ShopResultVo> res = client.createShop(shopVo);
- // 顺丰自运力绑定插入数据
- if ("shunfeng".equals(shopVo.getLbClient())) {
- ShopDeliveryBind deliveryBind= iShopDeliveryBindService.getOne(new QueryWrapper<ShopDeliveryBind>()
- .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<ShopBindDto> sfShopList(Member member, Integer deliveryType) {
- List<ShopBindDto> 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<Shop> shopList = iShopService.list(new QueryWrapper<Shop>()
- .eq("merchant_id", member.getMerchantId())
- .eq("deleted", 0));
- // 已经绑定的顺丰店铺
- List<ShopDeliveryBind> shopDeliveryBindList = iShopDeliveryBindService.list(new QueryWrapper<ShopDeliveryBind>()
- .eq("merchant_id", member.getMerchantId())
- .eq("delivery_id", delivery.getId())
- .eq("deleted", 0)
- .isNotNull("third_shop_id"));
- List<Long> shopBinds = shopDeliveryBindList.stream().map(ShopDeliveryBind::getShopId).collect(Collectors.toList());
- List<Shop> 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<ShopDeliveryBindDto> sfList(Member member) {
- List<ShopDeliveryBindDto> 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<AmountResultVo> 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;
- }
- }
|