|
|
|
@ -2,24 +2,32 @@ package com.baiye.modules.agent.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import cn.hutool.core.text.CharSequenceUtil;
|
|
|
|
|
import cn.hutool.core.text.StrPool;
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
import com.baiye.constant.DefaultNumberConstants;
|
|
|
|
|
import com.baiye.http.CommonResponse;
|
|
|
|
|
import com.baiye.modules.agent.dao.ChannelCustomDao;
|
|
|
|
|
import com.baiye.modules.agent.dao.ChannelResourceAssignDao;
|
|
|
|
|
import com.baiye.model.enums.UrlLinkEnum;
|
|
|
|
|
import com.baiye.modules.agent.entity.ChannelCustom;
|
|
|
|
|
import com.baiye.modules.agent.entity.ChannelCustomTag;
|
|
|
|
|
import com.baiye.modules.agent.entity.ChannelResourceAssign;
|
|
|
|
|
import com.baiye.modules.agent.entity.query.ChannelQuery;
|
|
|
|
|
import com.baiye.modules.agent.repository.ChannelCustomRepository;
|
|
|
|
|
import com.baiye.modules.agent.repository.ChannelCustomTagRepository;
|
|
|
|
|
import com.baiye.modules.agent.repository.ChannelResourceAssignRepository;
|
|
|
|
|
import com.baiye.modules.agent.service.CustomManageService;
|
|
|
|
|
import com.baiye.modules.platform.service.dto.CreateUserDTO;
|
|
|
|
|
import com.baiye.modules.system.domain.Role;
|
|
|
|
|
import com.baiye.modules.system.domain.User;
|
|
|
|
|
import com.baiye.modules.system.service.impl.UserServiceImpl;
|
|
|
|
|
import com.baiye.util.FirstLetter;
|
|
|
|
|
import com.baiye.util.PageUtil;
|
|
|
|
|
import com.baiye.util.QueryHelp;
|
|
|
|
|
import com.baiye.util.SecurityUtils;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
@ -29,7 +37,10 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author wjt
|
|
|
|
@ -38,15 +49,23 @@ import java.util.Set;
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class CustomManageServiceImpl implements CustomManageService {
|
|
|
|
|
private final ChannelCustomDao channelCustomDao;
|
|
|
|
|
private final ChannelResourceAssignDao channelResourceAssignDao;
|
|
|
|
|
private final ChannelCustomRepository channelCustomRepository;
|
|
|
|
|
|
|
|
|
|
private final ChannelCustomTagRepository channelCustomTagRepository;
|
|
|
|
|
|
|
|
|
|
private final ChannelResourceAssignRepository channelResourceAssignRepository;
|
|
|
|
|
|
|
|
|
|
private final UserServiceImpl userServiceImpl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${generate.url}")
|
|
|
|
|
private String configurationUrl;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public CommonResponse<Object> addCustom(ChannelCustom channelCustom) {
|
|
|
|
|
//验证重复
|
|
|
|
|
ChannelCustom byAndChannelName = channelCustomDao.findByAndChannelName(channelCustom.getChannelName());
|
|
|
|
|
ChannelCustom byAndChannelName = channelCustomRepository.findByAndChannelName(channelCustom.getChannelName());
|
|
|
|
|
if (ObjectUtil.isNotEmpty(byAndChannelName)) {
|
|
|
|
|
return CommonResponse.createByErrorMessage("名称重复");
|
|
|
|
|
}
|
|
|
|
@ -67,7 +86,7 @@ public class CustomManageServiceImpl implements CustomManageService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//获取父账号的总量
|
|
|
|
|
ChannelCustom parent = channelCustomDao.findById(channelCustom.getParentId()).orElse(new ChannelCustom());
|
|
|
|
|
ChannelCustom parent = channelCustomRepository.findById(channelCustom.getParentId()).orElse(new ChannelCustom());
|
|
|
|
|
if (parent.getSurplusNum() == null || parent.getSurplusNum() < channelCustom.getTotalNum()) {
|
|
|
|
|
return CommonResponse.createByErrorMessage("可分配的余量不足");
|
|
|
|
|
}
|
|
|
|
@ -78,8 +97,8 @@ public class CustomManageServiceImpl implements CustomManageService {
|
|
|
|
|
String activeCode = "by" + getFourNum(channelCustom.getParentId().intValue()) + DateUtil.format(DateUtil.date(), "MMdd") + RandomUtil.randomString(4);
|
|
|
|
|
channelCustom.setActivationCode(activeCode);
|
|
|
|
|
|
|
|
|
|
channelCustomDao.save(channelCustom);
|
|
|
|
|
channelCustomDao.save(parent);
|
|
|
|
|
channelCustomRepository.save(channelCustom);
|
|
|
|
|
channelCustomRepository.save(parent);
|
|
|
|
|
//这里如果是代理商 直接同步
|
|
|
|
|
CreateUserDTO createUserDTO = new CreateUserDTO().addCreateUserDTO(channelCustom.getChannelName(), channelCustom.getPhone());
|
|
|
|
|
User user = new User();
|
|
|
|
@ -95,15 +114,15 @@ public class CustomManageServiceImpl implements CustomManageService {
|
|
|
|
|
userServiceImpl.create(user);
|
|
|
|
|
//分配记录
|
|
|
|
|
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign().addChannelResourceAssign(parent.getParentId(), channelCustom.getTotalNum(), channelCustom.getId(), channelCustom.getChannelName());
|
|
|
|
|
channelResourceAssignDao.save(channelResourceAssign);
|
|
|
|
|
channelResourceAssignRepository.save(channelResourceAssign);
|
|
|
|
|
return CommonResponse.createBySuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public CommonResponse<Object> assignNum(Long customId, Integer assignNum) {
|
|
|
|
|
ChannelCustom channelCustom = channelCustomDao.findById(customId).orElse(new ChannelCustom());
|
|
|
|
|
ChannelCustom parent = channelCustomDao.findById(channelCustom.getParentId()).orElse(new ChannelCustom());
|
|
|
|
|
ChannelCustom channelCustom = channelCustomRepository.findById(customId).orElse(new ChannelCustom());
|
|
|
|
|
ChannelCustom parent = channelCustomRepository.findById(channelCustom.getParentId()).orElse(new ChannelCustom());
|
|
|
|
|
int parentTotalNum = parent.getTotalNum();
|
|
|
|
|
|
|
|
|
|
if (channelCustom.getType() == DefaultNumberConstants.ONE_NUMBER) {
|
|
|
|
@ -123,11 +142,11 @@ public class CustomManageServiceImpl implements CustomManageService {
|
|
|
|
|
//增加客户的总量和余量
|
|
|
|
|
channelCustom.setTotalNum(channelCustom.getTotalNum() + assignNum);
|
|
|
|
|
channelCustom.setSurplusNum(channelCustom.getSurplusNum() + assignNum);
|
|
|
|
|
channelCustomDao.save(parent);
|
|
|
|
|
channelCustomDao.save(channelCustom);
|
|
|
|
|
channelCustomRepository.save(parent);
|
|
|
|
|
channelCustomRepository.save(channelCustom);
|
|
|
|
|
//添加分配记录
|
|
|
|
|
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign().addChannelResourceAssign(parent.getParentId(), assignNum, channelCustom.getId(), channelCustom.getChannelName());
|
|
|
|
|
channelResourceAssignDao.save(channelResourceAssign);
|
|
|
|
|
channelResourceAssignRepository.save(channelResourceAssign);
|
|
|
|
|
return CommonResponse.createBySuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -143,14 +162,63 @@ public class CustomManageServiceImpl implements CustomManageService {
|
|
|
|
|
}
|
|
|
|
|
//这里的id 是userId;
|
|
|
|
|
Long id = SecurityUtils.getCurrentUserId();
|
|
|
|
|
ChannelCustom byUserId = channelCustomDao.findByUserId(id);
|
|
|
|
|
ChannelCustom byUserId = channelCustomRepository.findByUserId(id);
|
|
|
|
|
channelCustomQuery.setId(byUserId.getId());
|
|
|
|
|
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
|
|
|
|
|
Page<ChannelCustom> channelCustoms = channelCustomDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, channelCustomQuery, criteriaBuilder), pageRequest);
|
|
|
|
|
Page<ChannelCustom> channelCustoms = channelCustomRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, channelCustomQuery, criteriaBuilder), pageRequest);
|
|
|
|
|
return PageUtil.toPage(channelCustoms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String createLinkUrl(Long customId, Integer type) {
|
|
|
|
|
ChannelCustomTag customTag = channelCustomTagRepository.findByCustomIdAndType(customId, type);
|
|
|
|
|
// 已存在直接返回
|
|
|
|
|
if (ObjectUtil.isNotNull(customTag)) {
|
|
|
|
|
UrlLinkEnum linkEnum = UrlLinkEnum.find(type);
|
|
|
|
|
if (ObjectUtil.isNotNull(linkEnum) && linkEnum.getUrl() != null) {
|
|
|
|
|
return configurationUrl.concat(linkEnum.getUrl()).concat(customTag.getTagStr());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ChannelCustom channelCustom = channelCustomRepository.findById(customId).orElseGet(ChannelCustom::new);
|
|
|
|
|
if (StringUtils.isNotBlank(channelCustom.getChannelName())) {
|
|
|
|
|
// 公司名字转小写 拼接平台
|
|
|
|
|
String firstPinYin = FirstLetter.getFirstPinYin(channelCustom.getChannelName());
|
|
|
|
|
UrlLinkEnum linkEnum = UrlLinkEnum.find(type);
|
|
|
|
|
if (ObjectUtil.isNotNull(linkEnum) && linkEnum.getUrl() != null) {
|
|
|
|
|
String tagUrl = linkEnum.name().toLowerCase().concat(StrPool.DASHED)
|
|
|
|
|
.concat(firstPinYin.toLowerCase())
|
|
|
|
|
.concat(StrPool.DASHED).concat(DateUtil.format(DateUtil.date(), "MMdd"))
|
|
|
|
|
.concat(StrPool.DASHED).concat(RandomUtil.randomString
|
|
|
|
|
(DefaultNumberConstants.FOUR_NUMBER));
|
|
|
|
|
String fullLink = configurationUrl.concat(linkEnum.getUrl()).concat(tagUrl);
|
|
|
|
|
ChannelCustomTag channelCustomTag = new ChannelCustomTag();
|
|
|
|
|
channelCustomTag.setTagType(type);
|
|
|
|
|
channelCustomTag.setTagStr(tagUrl);
|
|
|
|
|
channelCustomTag.setChannelId(customId);
|
|
|
|
|
channelCustomTagRepository.save(channelCustomTag);
|
|
|
|
|
return fullLink;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return CharSequenceUtil.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Map<Integer, String> findUrlByCustomId(Long customId) {
|
|
|
|
|
List<ChannelCustomTag> byChannelId = channelCustomTagRepository.findByChannelId(customId);
|
|
|
|
|
return byChannelId.stream().collect(Collectors.toMap(ChannelCustomTag::getTagType, tag -> {
|
|
|
|
|
String tagStr = tag.getTagStr();
|
|
|
|
|
Integer tagType = tag.getTagType();
|
|
|
|
|
UrlLinkEnum linkEnum = UrlLinkEnum.find(tagType);
|
|
|
|
|
return configurationUrl.concat(linkEnum.getUrl()).concat(tagStr);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ChannelCustom findCustomByUserIdAndStatus(Long userId, Integer status) {
|
|
|
|
|
return channelCustomRepository.findByUserIdAndStatus(userId, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getFourNum(Integer num) {
|
|
|
|
|
DecimalFormat decimalFormat = new DecimalFormat("0000");
|
|
|
|
|
return decimalFormat.format(num);
|
|
|
|
|