puzhibing
2023-11-06 029d0af8d3e0d32dbfff8dc47fb5e997332bc01d
ruoyi-modules/ruoyi-shop/src/main/java/com/ruoyi/shop/service/impl/shop/ShopServiceImpl.java
@@ -1,17 +1,21 @@
package com.ruoyi.shop.service.impl.shop;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.binarywang.wxpay.bean.ecommerce.ApplymentsResult;
import com.github.binarywang.wxpay.bean.ecommerce.ApplymentsStatusResult;
import com.github.binarywang.wxpay.bean.ecommerce.ProfitSharingReceiverRequest;
import com.github.binarywang.wxpay.bean.ecommerce.ProfitSharingReceiverResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
@@ -30,8 +34,13 @@
import com.ruoyi.shop.service.task.MemberTaskService;
import com.ruoyi.shop.service.task.ShopFileService;
import com.ruoyi.shop.service.task.ShopTaskService;
import com.ruoyi.shop.util.HuiFuTianXiaUtil;
import com.ruoyi.shop.util.WechatPayUtils;
import com.ruoyi.shop.util.map.MapUtil;
import com.ruoyi.shop.util.map.vo.TencentDistanceMatrix;
import com.ruoyi.shop.util.map.vo.TencentDistanceMatrixElements;
import com.ruoyi.system.api.constant.AppErrorConstant;
import com.ruoyi.system.api.constant.SecurityConstant;
import com.ruoyi.system.api.domain.dto.*;
import com.ruoyi.system.api.domain.poji.config.SysTag;
import com.ruoyi.system.api.domain.poji.member.Member;
@@ -39,13 +48,17 @@
import com.ruoyi.system.api.domain.poji.sys.SysUser;
import com.ruoyi.system.api.domain.vo.*;
import com.ruoyi.system.api.service.*;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.rmi.ServerException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -59,7 +72,7 @@
 * @since 2023-04-25
 */
@Service
@Slf4j
@Log4j2
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Resource
@@ -99,6 +112,9 @@
    private ShopAuthenticationService shopAuthenticationService;
    @Resource
    private ShopAuthenticationHftxService shopAuthenticationHftxService;
    @Resource
    private ShopProportionService shopProportionService;
    @Resource
@@ -133,6 +149,9 @@
    @Resource
    private RemoteUserService remoteUserService;
    @Value("${callback_path}")
    private String callback_path;
@@ -225,7 +244,6 @@
            shopRelTagService.deleteByShopId(shop.getShopId());
            shopRelUserService.deleteByShopId(shop.getShopId());
            //清空归属
            shopRelUserService.deleteByShopId(shop.getShopId());
            shopStaffService.clearShopStaffRelation(shop.getShopId());
            if(!shop.getShopName().equals(mgtEditShopDto.getShopName())){
                MgtMemberShopNameDto mgtMemberShopNameDto = new MgtMemberShopNameDto();
@@ -245,6 +263,7 @@
            shop.setFrozenFlag(0);
            shop.setCooperativeFlag(1);
            shop.setAuthFlag(0);
            shop.setAccountFlag(0);
            newShop = true;
        }
        BeanUtils.copyProperties(mgtEditShopDto,shop);
@@ -374,7 +393,12 @@
        shopProportion.setDelFlag(0);
        shopProportion.setShopId(shop.getShopId());
        shopProportion.setShopType(shop.getShopType());
        shopProportion.setProportionPercent(new BigDecimal("100.00"));
        String proportionPercentStr = redisService.getCacheObject(SecurityConstant.SHOP_COMMON_PROPORTION);
        BigDecimal proportionPercent = new BigDecimal(proportionPercentStr);
        if(proportionPercent==null){
            proportionPercent = new BigDecimal("30");
        }
        shopProportion.setProportionPercent(proportionPercent);
        shopProportion.setUpdateTime(new Date());
        shopProportion.setUpdateUserId(userId);
        shopProportionService.save(shopProportion);
@@ -437,7 +461,7 @@
        }else{
            shop.setCooperativeFlag(0);
        }
        shop.setShopStatus(handelShopStatus(shop.getFrozenFlag(),shop.getCooperativeFlag(),shop.getAuthFlag()));
        shop.setShopStatus(handelShopStatus(shop.getFrozenFlag(),shop.getCooperativeFlag(),shop.getAuthFlag(),shop.getAccountFlag()));
        shop.setUpdateTime(new Date());
        shop.setUpdateUserId(mgtTerminateCooperationDto.getUserId());
        this.saveOrUpdate(shop);
@@ -458,7 +482,7 @@
        }else{
            shop.setFrozenFlag(0);
        }
        shop.setShopStatus(handelShopStatus(shop.getFrozenFlag(),shop.getCooperativeFlag(),shop.getAuthFlag()));
        shop.setShopStatus(handelShopStatus(shop.getFrozenFlag(),shop.getCooperativeFlag(),shop.getAuthFlag(),shop.getAccountFlag()));
        this.saveOrUpdate(shop);
    }
@@ -469,13 +493,13 @@
     * @param
     * @return  Integer
     */
    private Integer handelShopStatus(Integer frozenFlag, Integer cooperativeFlag, Integer authFlag){
    private Integer handelShopStatus(Integer frozenFlag, Integer cooperativeFlag, Integer authFlag,Integer accountFlag){
        Integer shopStatus = 1;
        if(frozenFlag == 1){
            shopStatus = 0;
            return shopStatus;
        }
        if(authFlag == 0){
        if(authFlag == 0 || accountFlag==0){
            shopStatus = 3;
            return shopStatus;
        }
@@ -710,7 +734,6 @@
            shopTransferRecord.setAfterUserId(mgtTransferShopDto.getTransferUserId());
            shopTransferRecord.setTransferRemark(mgtTransferShopDto.getTransferRemark());
            shopTransferRecordService.save(shopTransferRecord);
            shopRelUserService.deleteByUserId(shop.getBelongUserId());
        }
        shop.setBelongUserId(mgtTransferShopDto.getTransferUserId());
        this.saveOrUpdate(shop);
@@ -739,8 +762,31 @@
            //获取绑定商户
            shop = this.getById(member.getRelationShopId());
        }else if(StringUtils.isNotBlank(appNearbyShopDto.getLatitude())&&StringUtils.isNotBlank(appNearbyShopDto.getLongitude())){
            AppNearShopVo appNearShopVo = shopMapper.getNearbyShop(appNearbyShopDto);
            shop = this.getById(appNearShopVo.getShopId());
            List<AppNearShopVo> appNearShopVos = shopMapper.getNearbyShops(appNearbyShopDto);
            if(appNearShopVos.size() > 0){
                StringBuffer to = new StringBuffer();
                for (AppNearShopVo appNearShopVo : appNearShopVos) {
                    to.append(appNearShopVo.getLatitude() + "," + appNearShopVo.getLongitude() + ";");
                }
                String form = appNearbyShopDto.getLatitude() + "," + appNearbyShopDto.getLongitude();
                R<List<TencentDistanceMatrix>> r = MapUtil.tencentDistanceMatrix(form, to.substring(0, to.length() - 1), "walking");
                if(r.getCode() != 200){
                    throw new ServiceException(r.getMsg());
                }
                List<TencentDistanceMatrix> data = r.getData();
                //存储最短距离
                BigDecimal dis = new BigDecimal(0);
                AppNearShopVo appNearShopVo = null;
                List<TencentDistanceMatrixElements> elements = data.get(0).getElements();
                for (int i = 0; i < elements.size(); i++) {
                    TencentDistanceMatrixElements matrix = elements.get(i);
                    if(dis.compareTo(BigDecimal.ZERO) == 0 || dis.compareTo(new BigDecimal(matrix.getDistance())) > 0){
                        dis = new BigDecimal(matrix.getDistance());
                        appNearShopVo = appNearShopVos.get(i);
                    }
                }
                shop = this.getById(appNearShopVo.getShopId());
            }
        }else{
            return appNearbyShopVo;
        }
@@ -1010,6 +1056,17 @@
    }
    /**
     * 商户进件列表数据(汇付天下)
     * @param page
     * @param mgtShopAuthPageDto
     * @return
     */
    @Override
    public List<MgtShopHFTXAuthPageVo> pageMgtShopHFTXAuth(Page page, MgtShopHFTXAuthPageDto mgtShopAuthPageDto) {
        return shopMapper.pageMgtShopHFTXAuth(page, mgtShopAuthPageDto);
    }
    /**
     * @description boardShopTotal
     * @param
     * @return MgtBulletinBoardVo
@@ -1030,7 +1087,7 @@
     * @return  void
     */
    @Override
    public void mgtShopAuth(MgtShopAuthDto mgtShopAuthDto) throws WxPayException {
    public void mgtShopAuth(MgtShopAuthDto mgtShopAuthDto) {
        ShopAuthentication shopAuthentication = shopAuthenticationService.getById(mgtShopAuthDto.getAuthId());
        Shop shop = this.getByShopId(shopAuthentication.getShopId());
@@ -1056,6 +1113,83 @@
        shopAuthentication.setAuditStatus(3);
        shopAuthentication.setApplymentId(applymentsResult.getApplymentId());
        shopAuthenticationService.saveOrUpdate(shopAuthentication);
    }
    /**
     * 商户进件(汇付天下)
     * @param mgtShopHFTXAuthDto
     */
    @Override
    public void mgtShopHFTXAuth(MgtShopHFTXAuthDto mgtShopHFTXAuthDto) {
        ShopAuthenticationHftx shopAuthenticationHftx = shopAuthenticationHftxService.getOne(new QueryWrapper<ShopAuthenticationHftx>().eq("shop_id", mgtShopHFTXAuthDto.getShopId()));
        if(null != shopAuthenticationHftx && "Y".equals(shopAuthenticationHftx.getAuditStatus())){
            throw new ServiceException("不能重复进件");
        }
        if(null == shopAuthenticationHftx){
            shopAuthenticationHftx = new ShopAuthenticationHftx();
            shopAuthenticationHftx.setIsDelete(0);
            shopAuthenticationHftx.setInsertTime(new Date());
        }
        BeanUtils.copyProperties(mgtShopHFTXAuthDto, shopAuthenticationHftx);
        shopAuthenticationHftx.setReqSeqId(IdUtils.simpleUUID());
        shopAuthenticationHftx.setAuditStatus("");
        shopAuthenticationHftx.setAuditDesc("");
        /**
         * 开始调起进件接口
         */
        R<MerchantBasicdataVo> r = null;
        //用户
        if(mgtShopHFTXAuthDto.getType() == 1){
            r = HuiFuTianXiaUtil.merchantBasicdataIndv(mgtShopHFTXAuthDto, shopAuthenticationHftx, callback_path + "/shop/mgt/shop/merchantBasicdataNotify");
        }
        //企业进件
        if(mgtShopHFTXAuthDto.getType() == 2){
            r = HuiFuTianXiaUtil.merchantBasicdataEnt(mgtShopHFTXAuthDto, shopAuthenticationHftx, callback_path + "/shop/mgt/shop/merchantBasicdataNotify");
        }
        if(null == r){
            throw new ServiceException("参数异常");
        }
        if(r.getCode() == 200){
            MerchantBasicdataVo data = r.getData();
            shopAuthenticationHftx.setAuditStatus("U");
            shopAuthenticationHftx.setHuifuId(data.getHuifuId());
            shopAuthenticationHftx.setTokenNo(data.getTokenNo());
            shopAuthenticationHftx.setApplyNo(data.getApplyNo());
            shopAuthenticationHftxService.saveOrUpdate(shopAuthenticationHftx);
        }else{
            throw new ServiceException(r.getMsg());
        }
    }
    /**
     * 商户进件异步通知
     * @return
     */
    @Override
    public R<String> merchantBasicdataNotify(MerchantBasicDataNotifyDto dto) {
        R<MerchantBasicdataVo> merchantBasicdataVoR = HuiFuTianXiaUtil.merchantBasicDataNotify(dto);
        if(merchantBasicdataVoR.getCode() != 200){
            return R.fail(merchantBasicdataVoR.getMsg());
        }
        MerchantBasicdataVo data = merchantBasicdataVoR.getData();
        ShopAuthenticationHftx shopAuthenticationHftx = shopAuthenticationHftxService.getOne(new QueryWrapper<ShopAuthenticationHftx>().eq("req_seq_id", data.getReqSeqId()));
        if("U".equals(shopAuthenticationHftx.getAuditStatus())){
            shopAuthenticationHftx.setAuditStatus(data.getAuditStatus());
            shopAuthenticationHftx.setAuditDesc(data.getAuditDesc());
            shopAuthenticationHftx.setHuifuId(data.getHuifuId());
            shopAuthenticationHftx.setTokenNo(data.getTokenNo());
            shopAuthenticationHftx.setApplyNo(data.getApplyNo());
            shopAuthenticationHftxService.updateById(shopAuthenticationHftx);
            if("Y".equals(data.getAuditStatus())){
                Shop shop = this.getByShopId(shopAuthenticationHftx.getShopId());
                shop.setShopStatus(1);
                this.updateById(shop);
            }
        }
        return R.ok(data.getReqSeqId());
    }
    /**
@@ -1194,6 +1328,23 @@
            }
        }
        return shopAuthGetVo;
    }
    /**
     * 获取商户审核资料(汇付天下)
     * @param shopId
     * @return
     */
    @Override
    public MgtShopHFTXAuthGetVo getMgtShopHFTXAuthInfo(Long shopId) {
        ShopAuthenticationHftx shopAuthenticationHftx = shopAuthenticationHftxService.getOne(new QueryWrapper<ShopAuthenticationHftx>().eq("shop_id", shopId));
        MgtShopHFTXAuthGetVo mgtShopHFTXAuthGetVo = new MgtShopHFTXAuthGetVo();
        if(null == shopAuthenticationHftx){
            return mgtShopHFTXAuthGetVo;
        }
        BeanUtils.copyProperties(shopAuthenticationHftx, mgtShopHFTXAuthGetVo);
        return mgtShopHFTXAuthGetVo;
    }
    /**
@@ -1454,6 +1605,23 @@
        List<ShopAuthentication> list = shopAuthenticationService.getShopAuthNeedUpdateStatus();
        log.info("定时检查微信二级商户"+list.toString());
        list.forEach(item -> queryApplyStatusByApplymentId(item.getApplymentId(), item.getShopId()));
        List<ShopAuthenticationHftx> list1 = shopAuthenticationHftxService.list(new QueryWrapper<ShopAuthenticationHftx>().eq("audit_status", "U").eq("is_delete", 0));
        for (ShopAuthenticationHftx shopAuthenticationHftx : list1) {
            R<MerchantBasicdataVo> r = HuiFuTianXiaUtil.queryMerchantBasicdata(IdUtils.simpleUUID(), shopAuthenticationHftx.getHuifuId());
            if(r.getCode() == 200){
                MerchantBasicdataVo data = r.getData();
                if(org.springframework.util.StringUtils.hasLength(data.getTokenNo())){
                    shopAuthenticationHftx.setTokenNo(data.getTokenNo());
                    shopAuthenticationHftx.setAuditStatus("Y");
                    shopAuthenticationHftx.setAuditDesc("成功");
                    Shop shop = this.getByShopId(shopAuthenticationHftx.getShopId());
                    shop.setShopStatus(1);
                    this.updateById(shop);
                    shopAuthenticationHftxService.updateById(shopAuthenticationHftx);
                }
            }
        }
    }
    /**
@@ -1483,7 +1651,7 @@
    private void applySuccessUpdateShopStatus(Long shopId){
        // 更新商户状态为正常
        Shop shop = this.getById(shopId);
        Integer shopStatus = handelShopStatus(shop.getFrozenFlag(), shop.getCooperativeFlag(), 1);
        Integer shopStatus = handelShopStatus(shop.getFrozenFlag(), shop.getCooperativeFlag(), 1,shop.getAccountFlag());
        LambdaUpdateWrapper<Shop> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Shop::getShopId, shopId)
                .set(Shop::getShopStatus, shopStatus)
@@ -1593,17 +1761,29 @@
    /**
     * @description
     * @description  添加分账方
     * @author  jqs
     * @date    2023/8/23 9:44
     * @param
     * @return  void
     */
    @Override
    public ProfitSharingReceiverResult addProfitSharingReceiver(ProfitSharingReceiverRequest request){
    public void addProfitSharingReceiver(MgtShopAuthGetDto mgtShopAuthGetDto){
        try {
            ProfitSharingReceiverResult result = wechatPayUtils.addProfitSharingReceiver(request);
            return result;
            ShopAuthentication shopAuthentication = shopAuthenticationService.getById(mgtShopAuthGetDto.getAuthId());
            Shop shop = this.getByShopId(shopAuthentication.getShopId());
            if(shopAuthentication!=null&&shopAuthentication.getAuditStatus()==6){
                ProfitSharingReceiverRequest request = new ProfitSharingReceiverRequest();
                request.setAccount(shopAuthentication.getSubMchid());
                request.setName(shopAuthentication.getBlShopName());
                ProfitSharingReceiverResult result = wechatPayUtils.addProfitSharingReceiver(request);
                if(StringUtils.isNotBlank(result.getAccount())){
                    shop.setAccountFlag(1);
                    shop.setShopStatus(handelShopStatus(shop.getFrozenFlag(),shop.getCooperativeFlag(),shop.getAuthFlag(),1));
                    this.saveOrUpdate(shop);
                }
            }
        } catch (WxPayException e) {
            throw new RuntimeException(e);
        }
@@ -1623,4 +1803,39 @@
        return shopMapper.getTaskMsgList();
    }
    /**
     * 设置商户分账比例
     * @param mgtShopShareRatioSetDto
     * @return
     */
    @Override
    public R setMgtShopShareRatio(MgtShopShareRatioSetDto mgtShopShareRatioSetDto) {
        ShopAuthenticationHftx shopAuthenticationHftx = shopAuthenticationHftxService.getOne(new QueryWrapper<ShopAuthenticationHftx>()
                .eq("shop_id", mgtShopShareRatioSetDto.getShopId()).eq("is_delete", 0));
        shopAuthenticationHftx.setShareRatio(mgtShopShareRatioSetDto.getShareRatio());
        shopAuthenticationHftxService.updateById(shopAuthenticationHftx);
        return R.ok();
    }
    /**
     * 查询结算记录
     * @param merchantBasicdataSettlementDto
     * @return
     */
    @Override
    public R<List<MerchantBasicdataSettlementVo>> getMerchantBasicDataSettlement(MerchantBasicdataSettlementDto merchantBasicdataSettlementDto) {
        ShopAuthenticationHftx shopAuthenticationHftx = shopAuthenticationHftxService.getOne(new QueryWrapper<ShopAuthenticationHftx>()
                .eq("shop_id", merchantBasicdataSettlementDto.getShopId()).eq("is_delete", 0));
        if(null == shopAuthenticationHftx){
            return R.ok(new ArrayList<>());
        }
        String huifuId = shopAuthenticationHftx.getHuifuId();
        String startTime = merchantBasicdataSettlementDto.getStartTime();
        String endTime = merchantBasicdataSettlementDto.getEndTime();
        Integer pageNum = merchantBasicdataSettlementDto.getPageNum();
        Integer pageSize = merchantBasicdataSettlementDto.getPageSize();
        return HuiFuTianXiaUtil.getMerchantBasicdataSettlement(IdUtils.simpleUUID(), huifuId, startTime, endTime, pageNum.toString(), pageSize.toString());
    }
}