puhanshu
2022-08-16 43f0578e80af82ecae6c61b51bd0539c6b960603
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsMerchantServiceImpl.java
@@ -3,23 +3,14 @@
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import javax.annotation.Resource;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.LoginWithPhoneDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PutUserInfoDTO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.IndexTopStatisticsVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsGameVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsLoginUserInfoVO;
import com.panzhihua.service_community.util.NearbyUtil;
import com.spatial4j.core.shape.Rectangle;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@@ -33,18 +24,26 @@
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.dtos.community.convenient.DisableOrEnableConvenientMerchantDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.DisableOrEnableMcsMerchantDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.LoginWithPhoneDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.McsMerchantDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PageMcsMerchantDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PutUserInfoDTO;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.microCommercialStreet.IndexTopStatisticsVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsGameVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsLoginUserInfoVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsMerchantVO;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.service_community.dao.McsConfigDAO;
import com.panzhihua.service_community.dao.McsGameDAO;
import com.panzhihua.service_community.dao.McsInformationDAO;
import com.panzhihua.service_community.dao.McsMerchantDAO;
import com.panzhihua.service_community.dao.McsOrderDao;
import com.panzhihua.service_community.entity.McsConfig;
import com.panzhihua.service_community.entity.McsMerchant;
import com.panzhihua.service_community.entity.McsOrder;
import com.panzhihua.service_community.service.McsMerchantService;
/**
@@ -59,6 +58,7 @@
    private static final String MERCHANT_PUBLISH_LIMIT_KEY = "MERCHANT_PUBLISH_LIMIT";
    private static final String DEFAULT_IMAGE_URL = "https://www.psciio.com//idcard/91313573d071436ab1f934231b31c6e5.jpg";
    private static final int DEFAULT_PUBLISH_LIMIT = 2;
    private static final String CUSTOMER_SERVICE_CONTACT_NUM="CUSTOMER_SERVICE_CONTACT_NUM";
    @Resource
    private UserService userService;
@@ -67,7 +67,11 @@
    @Resource
    private McsGameDAO mcsGameDAO;
    @Resource
    private McsInformationDAO mcsInformationDAO;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private McsOrderDao mcsOrderDao;
    /**
     * 新增数字商业街商家
@@ -77,6 +81,14 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addMcsMerchant(McsMerchantDTO mcsMerchantDTO) {
        McsMerchant existMerchant = this.baseMapper.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getPhone, mcsMerchantDTO.getPhone()));
        if (nonNull(existMerchant)) {
            if (existMerchant.getIsDel()) {
                this.baseMapper.deleteById(existMerchant.getId());
            } else {
                return R.fail("手机号已存在");
            }
        }
        McsMerchant mcsMerchant = new McsMerchant();
        BeanUtils.copyProperties(mcsMerchantDTO, mcsMerchant);
        Integer litDays = mcsMerchantDTO.getLitDays();
@@ -161,7 +173,10 @@
            return R.fail("商家不存在");
        }
        mcsMerchant.setIsDel(true);
        this.baseMapper.updateById(mcsMerchant);
        int num = this.baseMapper.updateById(mcsMerchant);
        if (num > 0) {
            this.baseMapper.enableUserStatus(mcsMerchant.getUserId());
        }
        return R.ok();
    }
@@ -219,19 +234,28 @@
            return R.fail("账号不存在");
        }
        retrieveMerchantSurplusLitDays(merchantVO);
        McsConfig mcsConfig=mcsConfigDAO.selectOne(new QueryWrapper<McsConfig>().lambda().eq(McsConfig::getKey,CUSTOMER_SERVICE_CONTACT_NUM));
        if(mcsConfig!=null){
            merchantVO.setCustomPhone(mcsConfig.getValue());
        }
        return R.ok(merchantVO);
    }
    /**
     * 完成订单更新/新增商家信息
     *
     * @param merchantName
     * @param orderNo
     * @param configId
     * @param userId
     * @return
     */
    @Override
    public R updateOrAddMcsMerchantAfterOrder(String merchantName, Long configId, Long userId) {
    public R updateOrAddMcsMerchantAfterOrder(String orderNo, Long configId, Long userId) {
        McsOrder mcsOrder = mcsOrderDao.selectOne(new QueryWrapper<McsOrder>().lambda().eq(McsOrder::getOrderNo, orderNo));
        if (isNull(mcsOrder)) {
            return R.fail("订单不存在");
        }
        McsMerchant mcsMerchant = this.baseMapper.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        McsConfig mcsConfig = mcsConfigDAO.selectById(configId);
        if (isNull(mcsConfig) || isBlank(mcsConfig.getValue())) {
@@ -242,6 +266,7 @@
            return R.fail("配置有误");
        }
        Integer litDays = (Integer)packageObject.get("day");
        String merchantName = mcsOrder.getMerchantName();
        Date nowDate = new Date();
        int num;
        if (isNull(mcsMerchant)) {
@@ -250,7 +275,7 @@
            if (R.isOk(userInfoVOR) && nonNull(userInfoVOR.getData())) {
                LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(userInfoVOR.getData()), LoginUserInfoVO.class);
                mcsMerchant = new McsMerchant();
                mcsMerchant.setName(isBlank(merchantName) ? "默认名称" : merchantName);
                mcsMerchant.setName(isBlank(merchantName) ? loginUserInfoVO.getName() : merchantName);
                mcsMerchant.setPhone(loginUserInfoVO.getPhone());
                mcsMerchant.setAccount(loginUserInfoVO.getAccount());
                mcsMerchant.setLevel(1);
@@ -261,7 +286,13 @@
                mcsMerchant.setUserId(userId);
                mcsMerchant.setCreatedBy(userId);
                mcsMerchant.setUpdatedBy(userId);
                mcsMerchant.setAddress(mcsOrder.getAddress());
                mcsMerchant.setLat(mcsOrder.getLat());
                mcsMerchant.setLon(mcsOrder.getLon());
                num = this.baseMapper.insert(mcsMerchant);
                McsMerchantDTO mcsMerchantDTO = new McsMerchantDTO();
                mcsMerchantDTO.setName(mcsMerchant.getName());
                userService.putMcsMerchantUser(mcsMerchantDTO);
            } else {
                return R.fail("获取用户信息失败");
            }
@@ -274,11 +305,9 @@
            } else {
                nowExpireAt = DateUtils.addDay(previousExpireAt, litDays);
            }
            McsMerchant mcsMerchant1 = new McsMerchant();
            mcsMerchant1.setId(mcsMerchant.getId());
            mcsMerchant1.setExpireAt(nowExpireAt);
            mcsMerchant1.setUpdatedBy(userId);
            num = this.baseMapper.updateById(mcsMerchant1);
            mcsMerchant.setExpireAt(nowExpireAt);
            mcsMerchant.setUpdatedBy(userId);
            num = this.baseMapper.updateById(mcsMerchant);
        }
        if (num > 0) {
            return R.ok(mcsMerchant.getId());
@@ -353,17 +382,7 @@
        Page page = new Page<>();
        page.setSize(pageMcsMerchantDTO.getPageSize());
        page.setCurrent(pageMcsMerchantDTO.getPageNum());
        String lat = pageMcsMerchantDTO.getLat();
        String lon = pageMcsMerchantDTO.getLon();
        Integer distance = pageMcsMerchantDTO.getDistance();
        IPage<McsMerchantVO> mcsMerchants;
        if (isNotEmpty(lat) && isNotEmpty(lon) && nonNull(distance)) {
            Rectangle rectangle = new NearbyUtil().getRectangle(distance, Double.parseDouble(lon), Double.parseDouble(lat));
            mcsMerchants = this.baseMapper.pageH5McsMerchant(page, rectangle.getMinX(), rectangle.getMaxX(),
                    rectangle.getMinY(), rectangle.getMaxY(), pageMcsMerchantDTO);
        } else {
            mcsMerchants = this.baseMapper.pageH5McsMerchant(page, null, null, null, null, pageMcsMerchantDTO);
        }
        IPage<McsMerchantVO> mcsMerchants = this.baseMapper.pageH5McsMerchant(page, pageMcsMerchantDTO);
        return R.ok(mcsMerchants);
    }
@@ -390,13 +409,46 @@
        return R.ok();
    }
    /**
     * 根据userId获取商家信息
     * @param userId
     * @return
     */
    @Override
    public R retrieveMcsMerchantInfoByUserId(Long userId) {
        McsMerchant mcsMerchant = this.baseMapper.selectOne(new QueryWrapper<McsMerchant>().lambda()
                .eq(McsMerchant::getUserId, userId).eq(McsMerchant::getIsDel, false));
        McsMerchantVO mcsMerchantVO = null;
        if (nonNull(mcsMerchant)) {
            mcsMerchantVO = new McsMerchantVO();
            BeanUtils.copyProperties(mcsMerchant, mcsMerchantVO);
        }
        return R.ok(mcsMerchantVO);
    }
    /**
     * 定时任务-每隔5分钟执行一次,将已到期的微商业街商家旗下所有的游戏/资讯全部下架
     * @return
     */
    @Override
    public R offResourceForMcsMerchant() {
        int num = 0;
        List<Long> needDealIds = this.baseMapper.selectNeedDealExpiredMerchant();
        if (nonNull(needDealIds) && !needDealIds.isEmpty()) {
            mcsGameDAO.setOffByMerchantIds(needDealIds);
            mcsInformationDAO.setOffByMerchantIds(needDealIds);
            num = this.baseMapper.updateIsPauseStatus(needDealIds);
        }
        return R.ok("执行任务数:" + num);
    }
    private void retrieveMerchantSurplusLitDays(McsMerchantVO merchantVO) {
        Integer publishLimit = merchantVO.getPublishLimit();
        Integer publishCount = mcsGameDAO.selectPublishCount(merchantVO.getId());
        Integer idleTotal = publishLimit - publishCount;
        merchantVO.setIdleTotal(idleTotal > 0 ? idleTotal : 0);
        if (nonNull(merchantVO.getExpireAt())) {
            int surplusLitDays = DateUtils.differentDays(new Date(), merchantVO.getExpireAt());
            int surplusLitDays = DateUtils.retrieveRemainingDays(merchantVO.getExpireAt());
            merchantVO.setSurplusLitDays(surplusLitDays > 0 ? surplusLitDays : 0);
        }
    }