@ -1,13 +1,16 @@
package com.baiye.modules.agent.service.impl ;
package com.baiye.modules.agent.service.impl ;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.text.CharSequenceUtil ;
import cn.hutool.core.text.CharSequenceUtil ;
import cn.hutool.core.text.StrPool ;
import cn.hutool.core.text.StrPool ;
import cn.hutool.core.util.ObjectUtil ;
import cn.hutool.core.util.ObjectUtil ;
import cn.hutool.core.util.RandomUtil ;
import cn.hutool.core.util.RandomUtil ;
import com.baiye.constant.DefaultNumberConstants ;
import com.baiye.constant.DefaultNumberConstants ;
import com.baiye.exception.BadRequestException ;
import com.baiye.http.CommonResponse ;
import com.baiye.http.CommonResponse ;
import com.baiye.model.enums.ResponseCode ;
import com.baiye.model.enums.UrlLinkEnum ;
import com.baiye.model.enums.UrlLinkEnum ;
import com.baiye.modules.agent.entity.ChannelCustom ;
import com.baiye.modules.agent.entity.ChannelCustom ;
import com.baiye.modules.agent.entity.ChannelCustomTag ;
import com.baiye.modules.agent.entity.ChannelCustomTag ;
@ -21,11 +24,13 @@ import com.baiye.modules.platform.service.dto.CreateUserDTO;
import com.baiye.modules.system.domain.Role ;
import com.baiye.modules.system.domain.Role ;
import com.baiye.modules.system.domain.User ;
import com.baiye.modules.system.domain.User ;
import com.baiye.modules.system.service.impl.UserServiceImpl ;
import com.baiye.modules.system.service.impl.UserServiceImpl ;
import com.baiye.util.MobileUtil ;
import com.baiye.util.FirstLetter ;
import com.baiye.util.FirstLetter ;
import com.baiye.util.PageUtil ;
import com.baiye.util.PageUtil ;
import com.baiye.util.QueryHelp ;
import com.baiye.util.QueryHelp ;
import com.baiye.util.SecurityUtils ;
import com.baiye.util.SecurityUtils ;
import lombok.RequiredArgsConstructor ;
import lombok.RequiredArgsConstructor ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.data.domain.Page ;
import org.springframework.data.domain.Page ;
@ -36,10 +41,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import java.text.DecimalFormat ;
import java.text.DecimalFormat ;
import java.util.HashSet ;
import java.util.* ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
/ * *
/ * *
@ -48,6 +50,7 @@ import java.util.stream.Collectors;
* /
* /
@Service
@Service
@RequiredArgsConstructor
@RequiredArgsConstructor
@Slf4j
public class CustomManageServiceImpl implements CustomManageService {
public class CustomManageServiceImpl implements CustomManageService {
private final ChannelCustomRepository channelCustomRepository ;
private final ChannelCustomRepository channelCustomRepository ;
@ -69,52 +72,61 @@ public class CustomManageServiceImpl implements CustomManageService {
if ( ObjectUtil . isNotEmpty ( byAndChannelName ) ) {
if ( ObjectUtil . isNotEmpty ( byAndChannelName ) ) {
return CommonResponse . createByErrorMessage ( "名称重复" ) ;
return CommonResponse . createByErrorMessage ( "名称重复" ) ;
}
}
if ( ! MobileUtil . checkPhone ( channelCustom . getPhone ( ) ) ) {
return CommonResponse . createByErrorMessage ( ResponseCode . PHONE_NUMBER_IS_INCORRECT . getDesc ( ) ) ;
}
//新建账号状态为2 待开通
//新建账号状态为2 待开通
channelCustom . setStatus ( DefaultNumberConstants . TWO_NUMBER ) ;
channelCustom . setStatus ( DefaultNumberConstants . TWO_NUMBER ) ;
//获取父账号的总量
ChannelCustom parent = getChannelCustomByUserId ( ) ;
//设置分配
//设置分配
if ( channelCustom . getType ( ) = = DefaultNumberConstants . ONE_NUMBER ) {
if ( channelCustom . getT otalNum( ) ! = null & & channelCustom . getTotalNum ( ) > 0 ) {
if ( channelCustom . getT otalNum( ) ! = null ) {
if ( channelCustom . getT ype( ) = = DefaultNumberConstants . ONE_NUMBER ) {
if ( channelCustom . getTotalNum ( ) < DefaultNumberConstants . TEN_THOUSAND ) {
if ( channelCustom . getTotalNum ( ) < DefaultNumberConstants . TEN_THOUSAND ) {
return CommonResponse . createByErrorMessage ( "渠道商账号最低分配1万" ) ;
return CommonResponse . createByErrorMessage ( "渠道商账号最低分配1万" ) ;
}
}
}
} else {
} else {
if ( channelCustom . getTotalNum ( ) ! = null ) {
if ( channelCustom . getTotalNum ( ) < DefaultNumberConstants . ONE_THOUSAND ) {
if ( channelCustom . getTotalNum ( ) < DefaultNumberConstants . ONE_THOUSAND ) {
return CommonResponse . createByErrorMessage ( "直客账号最低分配1000" ) ;
return CommonResponse . createByErrorMessage ( "直客账号最低分配1000" ) ;
}
}
}
}
if ( parent . getSurplusNum ( ) = = null | | parent . getSurplusNum ( ) < channelCustom . getTotalNum ( ) ) {
return CommonResponse . createByErrorMessage ( "可分配的余量不足" ) ;
}
channelCustom . setSurplusNum ( channelCustom . getTotalNum ( ) ) ;
//减掉渠道商余量
parent . setSurplusNum ( parent . getTotalNum ( ) - channelCustom . getTotalNum ( ) ) ;
parent . setCreateTime ( DateUtil . date ( ) ) ;
channelCustomRepository . save ( parent ) ;
//分配记录
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign ( ) . addChannelResourceAssign ( parent . getId ( ) , channelCustom . getTotalNum ( ) , channelCustom . getId ( ) , channelCustom . getChannelName ( ) ) ;
channelResourceAssignRepository . save ( channelResourceAssign ) ;
}
}
//获取父账号的总量
ChannelCustom parent = channelCustomRepository . findById ( channelCustom . getParentId ( ) ) . orElse ( new ChannelCustom ( ) ) ;
if ( parent . getSurplusNum ( ) = = null | | parent . getSurplusNum ( ) < channelCustom . getTotalNum ( ) ) {
return CommonResponse . createByErrorMessage ( "可分配的余量不足" ) ;
}
channelCustom . setSurplusNum ( channelCustom . getTotalNum ( ) ) ;
//减掉渠道商余量
parent . setSurplusNum ( parent . getTotalNum ( ) - channelCustom . getTotalNum ( ) ) ;
//激活码
//激活码
String activeCode = "by" + getFourNum ( channelCustom . getParentId ( ) . intValue ( ) ) + DateUtil . format ( DateUtil . date ( ) , "MMdd" ) + RandomUtil . randomString ( 4 ) ;
String activeCode = "by" + getFourNum ( parent . getId ( ) . intValue ( ) ) + DateUtil . format ( DateUtil . date ( ) , "MMdd" ) + RandomUtil . randomString ( 4 ) ;
channelCustom . setActivationCode ( activeCode ) ;
channelCustom . setActivationCode ( activeCode ) ;
channelCustom . setParentId ( parent . getId ( ) ) ;
channelCustomRepository . save ( channelCustom ) ;
channelCustomRepository . save ( parent ) ;
//这里如果是代理商 直接同步
//这里如果是代理商 直接同步
CreateUserDTO createUserDTO = new CreateUserDTO ( ) . addCreateUserDTO ( channelCustom . getChannelName ( ) , channelCustom . getPhone ( ) ) ;
if ( channelCustom . getType ( ) = = DefaultNumberConstants . ONE_NUMBER ) {
User user = new User ( ) ;
CreateUserDTO createUserDTO = new CreateUserDTO ( ) . addCreateUserDTO ( channelCustom . getChannelName ( ) , channelCustom . getPhone ( ) ) ;
BeanUtil . copyProperties ( createUserDTO , user ) ;
User user = new User ( ) ;
user . setUsername ( user . getUsername ( ) . trim ( ) ) ;
BeanUtil . copyProperties ( createUserDTO , user ) ;
user . setWhichUserId ( parent . getUserId ( ) ) ;
user . setUsername ( user . getUsername ( ) . trim ( ) ) ;
user . setIsReview ( Boolean . FALSE ) ;
user . setWhichUserId ( parent . getUserId ( ) ) ;
Set < Role > roles = new HashSet < > ( ) ;
user . setIsReview ( Boolean . FALSE ) ;
Role role = new Role ( ) ;
Set < Role > roles = new HashSet < > ( ) ;
role . setId ( 14L ) ;
Role role = new Role ( ) ;
roles . add ( role ) ;
role . setId ( 14L ) ;
user . setRoles ( roles ) ;
roles . add ( role ) ;
userServiceImpl . create ( user ) ;
user . setRoles ( roles ) ;
//分配记录
Long userId = userServiceImpl . create ( user ) . getId ( ) ;
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign ( ) . addChannelResourceAssign ( parent . getParentId ( ) , channelCustom . getTotalNum ( ) , channelCustom . getId ( ) , channelCustom . getChannelName ( ) ) ;
channelCustom . setUserId ( userId ) ;
channelResourceAssignRepository . save ( channelResourceAssign ) ;
channelCustom . setStatus ( 1 ) ;
}
channelCustomRepository . save ( channelCustom ) ;
return CommonResponse . createBySuccess ( ) ;
return CommonResponse . createBySuccess ( ) ;
}
}
@ -123,8 +135,7 @@ public class CustomManageServiceImpl implements CustomManageService {
public CommonResponse < Object > assignNum ( Long customId , Integer assignNum ) {
public CommonResponse < Object > assignNum ( Long customId , Integer assignNum ) {
ChannelCustom channelCustom = channelCustomRepository . findById ( customId ) . orElse ( new ChannelCustom ( ) ) ;
ChannelCustom channelCustom = channelCustomRepository . findById ( customId ) . orElse ( new ChannelCustom ( ) ) ;
ChannelCustom parent = channelCustomRepository . findById ( channelCustom . getParentId ( ) ) . orElse ( new ChannelCustom ( ) ) ;
ChannelCustom parent = channelCustomRepository . findById ( channelCustom . getParentId ( ) ) . orElse ( new ChannelCustom ( ) ) ;
int parentTotalNum = parent . getTotalNum ( ) ;
int parentSurplusNum = parent . getSurplusNum ( ) ;
if ( channelCustom . getType ( ) = = DefaultNumberConstants . ONE_NUMBER ) {
if ( channelCustom . getType ( ) = = DefaultNumberConstants . ONE_NUMBER ) {
if ( assignNum < DefaultNumberConstants . TEN_THOUSAND ) {
if ( assignNum < DefaultNumberConstants . TEN_THOUSAND ) {
return CommonResponse . createByErrorMessage ( "渠道商账号最低分配1万" ) ;
return CommonResponse . createByErrorMessage ( "渠道商账号最低分配1万" ) ;
@ -134,42 +145,40 @@ public class CustomManageServiceImpl implements CustomManageService {
return CommonResponse . createByErrorMessage ( "直客账号最低分配1000" ) ;
return CommonResponse . createByErrorMessage ( "直客账号最低分配1000" ) ;
}
}
}
}
if ( parent Total Num < assignNum ) {
if ( parent Surplus Num < assignNum ) {
return CommonResponse . createByErrorMessage ( "可分配的余量不足" ) ;
return CommonResponse . createByErrorMessage ( "可分配的余量不足" ) ;
}
}
//扣减父级渠道商的余量
//扣减父级渠道商的余量
parent . setSurplusNum ( parent . get SurplusNum( ) - assignNum ) ;
parent . setSurplusNum ( parent SurplusNum - assignNum ) ;
//增加客户的总量和余量
//增加客户的总量和余量
channelCustom . setTotalNum ( channelCustom . getTotalNum ( ) + assignNum ) ;
channelCustom . setTotalNum ( channelCustom . getTotalNum ( ) + assignNum ) ;
channelCustom . setSurplusNum ( channelCustom . getSurplusNum ( ) + assignNum ) ;
channelCustom . setSurplusNum ( channelCustom . getSurplusNum ( ) + assignNum ) ;
channelCustomRepository . save ( parent ) ;
channelCustom . setPurchaseTime ( DateUtil . date ( ) ) ;
channelCustomRepository . save ( channelCustom ) ;
//添加分配记录
//添加分配记录
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign ( ) . addChannelResourceAssign ( parent . getParentId ( ) , assignNum , c hannelC ustom. get Id( ) , channelCustom . getChannelName ( ) ) ;
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign ( ) . addChannelResourceAssign ( parent . getParentId ( ) , assignNum , c ustomId, channelCustom . getChannelName ( ) ) ;
channelResourceAssignRepository . save ( channelResourceAssign ) ;
channelResourceAssignRepository . save ( channelResourceAssign ) ;
channelCustomRepository . save ( parent ) ;
channelCustomRepository . save ( channelCustom ) ;
return CommonResponse . createBySuccess ( ) ;
return CommonResponse . createBySuccess ( ) ;
}
}
@Override
@Override
public Object queryAll ( ChannelQuery channelCustomQuery , Pageable pageable ) {
public Object queryAll ( ChannelQuery channelCustomQuery , Pageable pageable ) {
Sort sort ;
if ( channelCustomQuery . getSort ( ) ! = null & & channelCustomQuery . getSort ( ) = = 1 ) {
sort = Sort . by ( Sort . Direction . DESC , "totalNum" ) ;
} else if ( channelCustomQuery . getSort ( ) ! = null & & channelCustomQuery . getSort ( ) = = 2 ) {
sort = Sort . by ( Sort . Direction . DESC , "surplusNum" ) ;
} else {
sort = Sort . by ( Sort . Direction . DESC , "createTime" ) ;
}
//这里的id 是userId;
//这里的id 是userId;
Long id = SecurityUtils . getCurrentUserId ( ) ;
Long customId = getChannelCustomByUserId ( ) . getId ( ) ;
ChannelCustom byUserId = channelCustomRepository . findByUserId ( id ) ;
channelCustomQuery . setId ( customId ) ;
channelCustomQuery . setId ( byUserId . getId ( ) ) ;
PageRequest pageRequest = PageRequest . of ( pageable . getPageNumber ( ) , pageable . getPageSize ( ) , Sort . by ( Sort . Direction . DESC , "createTime" ) ) ;
PageRequest pageRequest = PageRequest . of ( pageable . getPageNumber ( ) , pageable . getPageSize ( ) , sort ) ;
Page < ChannelCustom > channelCustoms = channelCustomRepository . 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 ) ;
return PageUtil . toPage ( channelCustoms ) ;
}
}
@Override
@Transactional ( rollbackFor = Exception . class )
public CommonResponse < Object > updateDmpLimitNum ( Long customId , Integer dmpLimitNum ) {
channelCustomRepository . updateDmpLimitNum ( customId , dmpLimitNum ) ;
return CommonResponse . createBySuccess ( ) ;
}
@Override
@Override
public String createLinkUrl ( Long customId , Integer type ) {
public String createLinkUrl ( Long customId , Integer type ) {
ChannelCustomTag customTag = channelCustomTagRepository . findByCustomIdAndType ( customId , type ) ;
ChannelCustomTag customTag = channelCustomTagRepository . findByCustomIdAndType ( customId , type ) ;
@ -188,9 +197,9 @@ public class CustomManageServiceImpl implements CustomManageService {
if ( ObjectUtil . isNotNull ( linkEnum ) & & linkEnum . getUrl ( ) ! = null ) {
if ( ObjectUtil . isNotNull ( linkEnum ) & & linkEnum . getUrl ( ) ! = null ) {
String tagUrl = linkEnum . name ( ) . toLowerCase ( ) . concat ( StrPool . DASHED )
String tagUrl = linkEnum . name ( ) . toLowerCase ( ) . concat ( StrPool . DASHED )
. concat ( firstPinYin . toLowerCase ( ) )
. concat ( firstPinYin . toLowerCase ( ) )
. concat ( StrPool . DASHED ) . concat ( DateUtil . format ( DateUtil . date ( ) , "MMdd" ) )
. concat ( StrPool . DASHED ) . concat ( DateUtil . format ( DateUtil . date ( ) , "MMdd" ) )
. concat ( StrPool . DASHED ) . concat ( RandomUtil . randomString
. concat ( StrPool . DASHED ) . concat ( RandomUtil . randomString
( DefaultNumberConstants . FOUR_NUMBER ) ) ;
( DefaultNumberConstants . FOUR_NUMBER ) ) ;
String fullLink = configurationUrl . concat ( linkEnum . getUrl ( ) ) . concat ( tagUrl ) ;
String fullLink = configurationUrl . concat ( linkEnum . getUrl ( ) ) . concat ( tagUrl ) ;
ChannelCustomTag channelCustomTag = new ChannelCustomTag ( ) ;
ChannelCustomTag channelCustomTag = new ChannelCustomTag ( ) ;
channelCustomTag . setTagType ( type ) ;
channelCustomTag . setTagType ( type ) ;
@ -204,14 +213,22 @@ public class CustomManageServiceImpl implements CustomManageService {
}
}
@Override
@Override
public Map< Integer , String > findUrlByCustomId ( Long customId ) {
public List< Map < String , Object > > findUrlByCustomId ( Long customId ) {
List < ChannelCustomTag > byChannelId = channelCustomTagRepository . findByChannelId ( customId ) ;
List < ChannelCustomTag > byChannelId = channelCustomTagRepository . findByChannelId ( customId ) ;
return byChannelId . stream ( ) . collect ( Collectors . toMap ( ChannelCustomTag : : getTagType , tag - > {
List < Map < String , Object > > list = new ArrayList < > ( ) ;
String tagStr = tag . getTagStr ( ) ;
if ( CollUtil . isNotEmpty ( byChannelId ) ) {
Integer tagType = tag . getTagType ( ) ;
for ( ChannelCustomTag tag : byChannelId ) {
UrlLinkEnum linkEnum = UrlLinkEnum . find ( tagType ) ;
Integer tagType = tag . getTagType ( ) ;
return configurationUrl . concat ( linkEnum . getUrl ( ) ) . concat ( tagStr ) ;
String tagStr = tag . getTagStr ( ) ;
} ) ) ;
UrlLinkEnum linkEnum = UrlLinkEnum . find ( tagType ) ;
String concat = configurationUrl . concat ( linkEnum . getUrl ( ) ) . concat ( tagStr ) ;
Map < String , Object > map = new HashMap < > ( 2 ) ;
map . put ( "type" , tagType ) ;
map . put ( "link" , concat ) ;
list . add ( map ) ;
}
}
return list ;
}
}
@Override
@Override
@ -223,4 +240,14 @@ public class CustomManageServiceImpl implements CustomManageService {
DecimalFormat decimalFormat = new DecimalFormat ( "0000" ) ;
DecimalFormat decimalFormat = new DecimalFormat ( "0000" ) ;
return decimalFormat . format ( num ) ;
return decimalFormat . format ( num ) ;
}
}
private ChannelCustom getChannelCustomByUserId ( ) {
Long id = SecurityUtils . getCurrentUserId ( ) ;
ChannelCustom byUserId = channelCustomRepository . findByUserId ( id ) ;
if ( ObjectUtil . isNull ( byUserId ) ) {
log . info ( "系统账号id={}" , id ) ;
throw new BadRequestException ( "账号异常,请联系管理员" ) ;
}
return byUserId ;
}
}
}