Merge branch 'platform-agent'

master
bynt 1 year ago
commit d2e62c1a81

@ -181,6 +181,11 @@ public class DefaultNumberConstants {
*/ */
public static final int FIVE_THOUSAND = 5000; public static final int FIVE_THOUSAND = 5000;
/**
* 1
*/
public static final int TEN_THOUSAND = 10000;
/** /**
* *
*/ */

@ -0,0 +1,44 @@
package com.baiye.modules.agent.controller;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import com.baiye.modules.agent.service.ChannelManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Set;
/**
* @author wjt
* @date 2023/4/12
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/channel")
@Api(tags = "渠道商管理")
public class ChannelManageController {
private final ChannelManageService channelManageService;
@ApiOperation("查询任务(分页)")
@GetMapping("/queryAll")
public ResponseEntity<Object> queryAll(ChannelQuery channelQuery, Pageable pageable) {
return new ResponseEntity<>(channelManageService.queryAll(channelQuery, pageable), HttpStatus.OK);
}
@ApiOperation("激活开通")
@PostMapping("/active")
public CommonResponse<Object> active(@RequestBody Set<Long> ids) {
return channelManageService.active(ids);
}
@ApiOperation("禁用")
@PostMapping("/forbidden")
public CommonResponse<Object> forbidden(@RequestBody Set<Long> ids) {
return channelManageService.forbidden(ids);
}
}

@ -0,0 +1,45 @@
package com.baiye.modules.agent.controller;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import com.baiye.modules.agent.entity.query.ResourceAssignInfoQuery;
import com.baiye.modules.agent.service.ChannelResourceAssignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wjt
* @date 2023/4/14
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/resource")
@Api(tags = "资源包管理")
public class ChannelResourceAssignController {
private final ChannelResourceAssignService channelResourceAssignService;
@ApiOperation("查询任务(分页)")
@GetMapping("/queryAll")
public ResponseEntity<Object> queryAll(ChannelQuery channelResourceQuery, Pageable pageable) {
return new ResponseEntity<>(channelResourceAssignService.queryAll(channelResourceQuery, pageable), HttpStatus.OK);
}
@ApiOperation("查询任务(分页)")
@GetMapping("/query/assign")
public ResponseEntity<Object> queryAssign(ResourceAssignInfoQuery resourceAssignInfoQuery, Pageable pageable) {
return new ResponseEntity<>(channelResourceAssignService.queryAssign(resourceAssignInfoQuery, pageable), HttpStatus.OK);
}
@GetMapping("/assign")
@ApiOperation("配量")
public CommonResponse<Object> assignNum(@RequestParam("channelId") Long channelId, @RequestParam("customId") Integer assignNum) {
return channelResourceAssignService.assignNum(channelId, assignNum);
}
}

@ -0,0 +1,50 @@
package com.baiye.modules.agent.controller;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.entity.ChannelCustom;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import com.baiye.modules.agent.service.CustomManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* @author wjt
* @date 2023/4/12
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/custom")
@Api(tags = "客户管理")
public class CustomManageController {
private final CustomManageService customManageService;
@PostMapping("/add")
@ApiOperation("新增账号")
public CommonResponse<Object> addCustom(@RequestBody ChannelCustom channelCustom) {
return customManageService.addCustom(channelCustom);
}
@GetMapping("/assign")
@ApiOperation("配量")
public CommonResponse<Object> assignNum(@RequestParam("customId") Long customId, @RequestParam("customId") Integer assignNum) {
return customManageService.assignNum(customId, assignNum);
}
@ApiOperation("查询任务(分页)")
@GetMapping("/queryAll")
public ResponseEntity<Object> queryAll(ChannelQuery channelCustomQuery, Pageable pageable) {
return new ResponseEntity<>(customManageService.queryAll(channelCustomQuery, pageable), HttpStatus.OK);
}
@GetMapping("/link")
@ApiOperation("生成链接")
public CommonResponse<Object> addLink(@RequestParam("customId") Long customId, @RequestParam("type") Integer type) {
return CommonResponse.createBySuccess();
}
}

@ -0,0 +1,55 @@
package com.baiye.modules.agent.dao;
import com.baiye.modules.agent.entity.ChannelCustom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Set;
/**
* @author wjt
* @date 2023/4/12
*/
@Repository
public interface ChannelCustomDao extends JpaRepository<ChannelCustom, Long>, JpaSpecificationExecutor<ChannelCustom> {
/**
*
*
* @param name
* @return
*/
ChannelCustom findByAndChannelName(String name);
/**
* id
*
* @param parentIds id
* @return
*/
@Query("select c from ChannelCustom c where c.parentId in ?1")
List<ChannelCustom> findByParentId(Set<Long> parentIds);
/**
*
*
* @param ids id
* @param status
* @return
*/
@Modifying
@Query("UPDATE ChannelCustom set status = ?2 where id in ?1")
int updateCustomStatus(Set<Long> ids, Integer status);
/**
* id
*
* @param userId
* @return
*/
ChannelCustom findByUserId(Long userId);
}

@ -0,0 +1,14 @@
package com.baiye.modules.agent.dao;
import com.baiye.modules.agent.entity.ChannelResourceAssign;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
/**
* @author wjt
* @date 2023/4/13
*/
@Repository
public interface ChannelResourceAssignDao extends JpaRepository<ChannelResourceAssign, Long>, JpaSpecificationExecutor<ChannelResourceAssign> {
}

@ -0,0 +1,101 @@
package com.baiye.modules.agent.entity;
import cn.hutool.core.date.DateUtil;
import com.baiye.valid.AddGroup;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* @author wjt
* @date 2023/4/12
*/
@Data
@Entity
@Table(name = "tb_channel_custom")
@EntityListeners(AuditingEntityListener.class)
public class ChannelCustom implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ApiModelProperty(value = "系统用户id")
@Column(name = "user_id")
private Long userId;
@ApiModelProperty(value = "手机")
@Column(name = "phone")
private String phone;
@ApiModelProperty(value = "名称")
@Column(name = "channel_name")
@NotNull(groups = AddGroup.class)
private String channelName;
@ApiModelProperty(value = "客户类型 1-渠道商 2-直客")
@Column(name = "type")
@NotNull(groups = AddGroup.class)
private Integer type;
@ApiModelProperty(value = "总分配量")
@Column(name = "total_num")
private Integer totalNum;
@ApiModelProperty(value = "分配余量")
@Column(name = "surplus_num")
private Integer surplusNum;
@ApiModelProperty(value = "用户状态 1 正常 2-待开通-3 禁用")
@Column(name = "status")
private Integer status;
@ApiModelProperty(value = "激活码")
@Column(name = "activation_code")
private String activationCode;
@ApiModelProperty(value = "创建时间")
@Column(name = "create_time")
private Date createTime;
@ApiModelProperty(value = "最近一次购包时间")
@Column(name = "purchase_time")
private Date purchaseTime;
@ApiModelProperty(value = "父id")
@Column(name = "parent_id")
@NotNull(groups = AddGroup.class)
private Long parentId;
@Transient
private List<ChannelCustom> children;
@Transient
private Integer allCustomNum;
@Transient
private Integer normalNum;
@Transient
private Integer waitNum;
public ChannelCustom addChannel(String name, String phone, Long userId) {
this.setChannelName(name);
this.setType(1);
this.setTotalNum(0);
this.setSurplusNum(0);
this.setStatus(1);
this.setActivationCode(null);
this.setCreateTime(DateUtil.date());
this.setPurchaseTime(DateUtil.date());
this.setParentId(null);
this.setUserId(userId);
this.setPhone(phone);
return this;
}
}

@ -0,0 +1,53 @@
package com.baiye.modules.agent.entity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* @author wjt
* @date 2023/4/12
*/
@Data
@Entity
@Table(name = "tb_channel_resource_assign")
@EntityListeners(AuditingEntityListener.class)
public class ChannelResourceAssign implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ApiModelProperty(value = "渠道商id")
@Column(name = "channel_id")
private Long channelId;
@ApiModelProperty(value = "分配量")
@Column(name = "assign_num")
private Integer assignNum;
@ApiModelProperty(value = "分配人id")
@Column(name = "assign_by")
private Long assignBy;
@ApiModelProperty(value = "分配人名称")
@Column(name = "assign_name")
private String assignName;
@ApiModelProperty(value = "时间")
@Column(name = "create_time")
private Date createTime;
public ChannelResourceAssign addChannelResourceAssign(Long channelId, Integer assignNum, Long assignBy, String assignName) {
this.setChannelId(channelId);
this.setAssignNum(assignNum);
this.setAssignBy(assignBy);
this.setAssignName(assignName);
return this;
}
}

@ -0,0 +1,28 @@
package com.baiye.modules.agent.entity.query;
import com.baiye.annotation.Query;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author wjt
* @date 2023/4/13
*/
@Data
public class ChannelQuery {
@ApiModelProperty(value = "客户名称")
@Query(type = Query.Type.INNER_LIKE)
private String channelName;
@ApiModelProperty(value = "默认排序为0 1-总量排序 2-余量排序")
private Integer sort = 0;
@ApiModelProperty(value = "客户类型 1-渠道商 2-直客")
@Query(type = Query.Type.EQUAL)
private Integer type;
@Query(propName = "parentId", type = Query.Type.EQUAL)
private Long id;
}

@ -0,0 +1,21 @@
package com.baiye.modules.agent.entity.query;
import com.baiye.annotation.Query;
import lombok.Data;
import java.sql.Timestamp;
import java.util.List;
/**
* @author wjt
* @date 2023/4/14
*/
@Data
public class ResourceAssignInfoQuery {
@Query
private Long id;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
}

@ -0,0 +1,48 @@
package com.baiye.modules.agent.service;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import org.springframework.data.domain.Pageable;
import java.util.Set;
/**
* @author wjt
* @date 2023/4/12
*/
public interface ChannelManageService {
/**
*
*
* @param channelName
* @param userId id
* @param phone
* @return
*/
CommonResponse<Object> addChannel(String channelName, String phone, Long userId);
/**
* admin
*
* @param channelQuery
* @param pageable
* @return
*/
Object queryAll(ChannelQuery channelQuery, Pageable pageable);
/**
*
*
* @param ids ids
* @return
*/
CommonResponse<Object> active(Set<Long> ids);
/**
*
*
* @param ids ids
* @return
*/
CommonResponse<Object> forbidden(Set<Long> ids);
}

@ -0,0 +1,40 @@
package com.baiye.modules.agent.service;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import com.baiye.modules.agent.entity.query.ResourceAssignInfoQuery;
import org.springframework.data.domain.Pageable;
/**
* @author wjt
* @date 2023/4/14
*/
public interface ChannelResourceAssignService {
/**
* admin
*
* @param channelQuery
* @param pageable
* @return
*/
Object queryAll(ChannelQuery channelQuery, Pageable pageable);
/**
*
*
* @param resourceAssignInfoQuery
* @param pageable
* @return
*/
Object queryAssign(ResourceAssignInfoQuery resourceAssignInfoQuery, Pageable pageable);
/**
*
*
* @param customId id
* @param assignNum
* @return
*/
CommonResponse<Object> assignNum(Long customId, Integer assignNum);
}

@ -0,0 +1,40 @@
package com.baiye.modules.agent.service;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.entity.ChannelCustom;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import org.springframework.data.domain.Pageable;
/**
* @author wjt
* @date 2023/4/12
*/
public interface CustomManageService {
/**
*
*
* @param channelCustom
* @return
*/
CommonResponse<Object> addCustom(ChannelCustom channelCustom);
/**
*
*
* @param customId id
* @param assignNum
* @return
*/
CommonResponse<Object> assignNum(Long customId, Integer assignNum);
/**
*
*
* @param channelCustomQuery
* @param pageable
* @return
*/
Object queryAll(ChannelQuery channelCustomQuery, Pageable pageable);
}

@ -0,0 +1,124 @@
package com.baiye.modules.agent.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.dao.ChannelCustomDao;
import com.baiye.modules.agent.entity.ChannelCustom;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import com.baiye.modules.agent.service.ChannelManageService;
import com.baiye.modules.platform.service.dto.CreateUserDTO;
import com.baiye.modules.system.service.UserService;
import com.baiye.util.PageUtil;
import com.baiye.util.QueryHelp;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author wjt
* @date 2023/4/12
*/
@Service
@RequiredArgsConstructor
public class ChannelManageServiceImpl implements ChannelManageService {
private final ChannelCustomDao channelCustomDao;
private final UserService userService;
@Override
public CommonResponse<Object> addChannel(String channelName, String phone, Long userId) {
channelCustomDao.save(new ChannelCustom().addChannel(channelName, phone, userId));
return CommonResponse.createBySuccess();
}
@Override
public Object queryAll(ChannelQuery channelQuery, Pageable pageable) {
Sort sort;
if (channelQuery.getSort() != null && channelQuery.getSort() == 1) {
sort = Sort.by(Sort.Direction.DESC, "totalNum");
} else if (channelQuery.getSort() != null && channelQuery.getSort() == 2) {
sort = Sort.by(Sort.Direction.DESC, "surplusNum");
} else {
sort = Sort.by(Sort.Direction.DESC, "createTime");
}
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
Page<ChannelCustom> channelCustoms = channelCustomDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, channelQuery, criteriaBuilder), pageRequest);
List<ChannelCustom> content = new ArrayList<>(channelCustoms.getContent());
if (CollUtil.isEmpty(content)) {
return channelCustoms;
}
//父级
Map<Long, ChannelCustom> parentMap = new HashMap<>(8);
for (ChannelCustom channelCustom : content) {
if (channelCustom.getParentId() == null) {
parentMap.put(channelCustom.getId(), channelCustom);
}
}
//查询出所有子集
Set<Long> set = parentMap.keySet();
List<ChannelCustom> byParentId = channelCustomDao.findByParentId(set);
content.addAll(byParentId);
//子集
Map<Long, List<ChannelCustom>> userMap = content.stream().filter(channelCustom -> channelCustom.getParentId() != null)
.collect(Collectors.groupingBy(ChannelCustom::getParentId));
int allCustomNum = 0;
int normalNum = 0;
int waitNum = 0;
for (Long key : userMap.keySet()) {
ChannelCustom channelCustom;
if (parentMap.containsKey(key)) {
channelCustom = parentMap.get(key);
} else {
channelCustom = channelCustomDao.findById(key).orElse(new ChannelCustom());
}
List<ChannelCustom> children = userMap.get(key);
if (CollUtil.isNotEmpty(children)) {
List<ChannelCustom> normalNums = children.stream().filter(c -> c.getStatus() == 1).collect(Collectors.toList());
List<ChannelCustom> waitNums = children.stream().filter(c -> c.getStatus() == 2).collect(Collectors.toList());
normalNum = normalNums.size();
waitNum = waitNums.size();
}
channelCustom.setAllCustomNum(allCustomNum);
channelCustom.setNormalNum(normalNum);
channelCustom.setWaitNum(waitNum);
channelCustom.setChildren(children);
parentMap.put(key, channelCustom);
}
content = new ArrayList<>(parentMap.values());
return PageUtil.toPage(content, content.size());
}
@Override
@Transactional(rollbackFor = Exception.class)
public CommonResponse<Object> active(Set<Long> ids) {
List<ChannelCustom> channelCustoms = channelCustomDao.findAllById(ids);
for (ChannelCustom channelCustom : channelCustoms) {
String channelName = channelCustom.getChannelName();
String phone = channelCustom.getPhone();
//同步到系统用户
CreateUserDTO createUserDTO = new CreateUserDTO().addCreateUserDTO(channelName, phone);
List<Long> userOrFile = userService.createUserOrFile(createUserDTO);
Long userId = userOrFile.get(0);
channelCustom.setUserId(userId);
channelCustom.setStatus(DefaultNumberConstants.ONE_NUMBER);
}
channelCustomDao.saveAll(channelCustoms);
return CommonResponse.createBySuccess();
}
@Override
@Transactional(rollbackFor = Exception.class)
public CommonResponse<Object> forbidden(Set<Long> ids) {
channelCustomDao.updateCustomStatus(ids, 3);
userService.updateUserStatusByCompanyId(false, new ArrayList<>(ids));
return CommonResponse.createBySuccess();
}
}

@ -0,0 +1,63 @@
package com.baiye.modules.agent.service.impl;
import com.baiye.http.CommonResponse;
import com.baiye.modules.agent.dao.ChannelCustomDao;
import com.baiye.modules.agent.dao.ChannelResourceAssignDao;
import com.baiye.modules.agent.entity.ChannelCustom;
import com.baiye.modules.agent.entity.ChannelResourceAssign;
import com.baiye.modules.agent.entity.query.ChannelQuery;
import com.baiye.modules.agent.entity.query.ResourceAssignInfoQuery;
import com.baiye.modules.agent.service.ChannelResourceAssignService;
import com.baiye.util.PageUtil;
import com.baiye.util.QueryHelp;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author wjt
* @date 2023/4/14
*/
@Service
@RequiredArgsConstructor
public class ChannelResourceAssignServiceImpl implements ChannelResourceAssignService {
private final ChannelResourceAssignDao channelResourceAssignDao;
private final ChannelCustomDao channelCustomDao;
@Override
public Object queryAll(ChannelQuery channelQuery, Pageable pageable) {
Sort sort;
if (channelQuery.getSort() != null && channelQuery.getSort() == 1) {
sort = Sort.by(Sort.Direction.DESC, "totalNum");
} else if (channelQuery.getSort() != null && channelQuery.getSort() == 2) {
sort = Sort.by(Sort.Direction.DESC, "surplusNum");
} else {
sort = Sort.by(Sort.Direction.DESC, "createTime");
}
channelQuery.setId(null);
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
Page<ChannelResourceAssign> channelCustoms = channelResourceAssignDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, channelQuery, criteriaBuilder), pageRequest);
return PageUtil.toPage(channelCustoms);
}
@Override
public Object queryAssign(ResourceAssignInfoQuery resourceAssignInfoQuery, Pageable pageable) {
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(Sort.Direction.DESC, "create_time"));
return channelResourceAssignDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, resourceAssignInfoQuery, criteriaBuilder), pageRequest);
}
@Override
@Transactional(rollbackFor = Exception.class)
public CommonResponse<Object> assignNum(Long customId, Integer assignNum) {
ChannelCustom channelCustom = channelCustomDao.findById(customId).orElse(new ChannelCustom());
//增加客户的总量和余量
channelCustom.setTotalNum(channelCustom.getTotalNum() + assignNum);
channelCustom.setSurplusNum(channelCustom.getSurplusNum() + assignNum);
channelCustomDao.save(channelCustom);
return CommonResponse.createBySuccess();
}
}

@ -0,0 +1,158 @@
package com.baiye.modules.agent.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
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.modules.agent.entity.ChannelCustom;
import com.baiye.modules.agent.entity.ChannelResourceAssign;
import com.baiye.modules.agent.entity.query.ChannelQuery;
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.PageUtil;
import com.baiye.util.QueryHelp;
import com.baiye.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;
/**
* @author wjt
* @date 2023/4/12
*/
@Service
@RequiredArgsConstructor
public class CustomManageServiceImpl implements CustomManageService {
private final ChannelCustomDao channelCustomDao;
private final ChannelResourceAssignDao channelResourceAssignDao;
private final UserServiceImpl userServiceImpl;
@Override
@Transactional(rollbackFor = Exception.class)
public CommonResponse<Object> addCustom(ChannelCustom channelCustom) {
//验证重复
ChannelCustom byAndChannelName = channelCustomDao.findByAndChannelName(channelCustom.getChannelName());
if (ObjectUtil.isNotEmpty(byAndChannelName)) {
return CommonResponse.createByErrorMessage("名称重复");
}
//新建账号状态为2 待开通
channelCustom.setStatus(DefaultNumberConstants.TWO_NUMBER);
//设置分配
if (channelCustom.getType() == DefaultNumberConstants.ONE_NUMBER) {
if (channelCustom.getTotalNum() != null) {
if (channelCustom.getTotalNum() < DefaultNumberConstants.TEN_THOUSAND) {
return CommonResponse.createByErrorMessage("渠道商账号最低分配1万");
}
}
} else {
if (channelCustom.getTotalNum() != null) {
if (channelCustom.getTotalNum() < DefaultNumberConstants.ONE_THOUSAND) {
return CommonResponse.createByErrorMessage("直客账号最低分配1000");
}
}
}
//获取父账号的总量
ChannelCustom parent = channelCustomDao.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);
channelCustom.setActivationCode(activeCode);
channelCustomDao.save(channelCustom);
channelCustomDao.save(parent);
//这里如果是代理商 直接同步
CreateUserDTO createUserDTO = new CreateUserDTO().addCreateUserDTO(channelCustom.getChannelName(), channelCustom.getPhone());
User user = new User();
BeanUtil.copyProperties(createUserDTO, user);
user.setUsername(user.getUsername().trim());
user.setWhichUserId(parent.getUserId());
user.setIsReview(Boolean.FALSE);
Set<Role> roles = new HashSet<>();
Role role = new Role();
role.setId(14L);
roles.add(role);
user.setRoles(roles);
userServiceImpl.create(user);
//分配记录
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign().addChannelResourceAssign(parent.getParentId(), channelCustom.getTotalNum(), channelCustom.getId(), channelCustom.getChannelName());
channelResourceAssignDao.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());
int parentTotalNum = parent.getTotalNum();
if (channelCustom.getType() == DefaultNumberConstants.ONE_NUMBER) {
if (assignNum < DefaultNumberConstants.TEN_THOUSAND) {
return CommonResponse.createByErrorMessage("渠道商账号最低分配1万");
}
} else {
if (assignNum < DefaultNumberConstants.ONE_THOUSAND) {
return CommonResponse.createByErrorMessage("直客账号最低分配1000");
}
}
if (parentTotalNum < assignNum) {
return CommonResponse.createByErrorMessage("可分配的余量不足");
}
//扣减父级渠道商的余量
parent.setSurplusNum(parent.getSurplusNum() - assignNum);
//增加客户的总量和余量
channelCustom.setTotalNum(channelCustom.getTotalNum() + assignNum);
channelCustom.setSurplusNum(channelCustom.getSurplusNum() + assignNum);
channelCustomDao.save(parent);
channelCustomDao.save(channelCustom);
//添加分配记录
ChannelResourceAssign channelResourceAssign = new ChannelResourceAssign().addChannelResourceAssign(parent.getParentId(), assignNum, channelCustom.getId(), channelCustom.getChannelName());
channelResourceAssignDao.save(channelResourceAssign);
return CommonResponse.createBySuccess();
}
@Override
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;
Long id = SecurityUtils.getCurrentUserId();
ChannelCustom byUserId = channelCustomDao.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);
return PageUtil.toPage(channelCustoms);
}
private String getFourNum(Integer num) {
DecimalFormat decimalFormat = new DecimalFormat("0000");
return decimalFormat.format(num);
}
}

@ -10,14 +10,11 @@ import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.Email; import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.Date; import java.util.*;
import java.util.Objects;
import java.util.Set;
/** /**
* @author Enzo * @author Enzo
* @date : 2022/1/21 * @date : 2022/1/21
*
*/ */
@Data @Data
public class CreateUserDTO { public class CreateUserDTO {
@ -50,7 +47,7 @@ public class CreateUserDTO {
@ApiModelProperty(value = "用户性别") @ApiModelProperty(value = "用户性别")
private String gender; private String gender;
@ApiModelProperty(value = "头像真实名称",hidden = true) @ApiModelProperty(value = "头像真实名称", hidden = true)
private String avatarName; private String avatarName;
@ApiModelProperty(value = "头像存储的路径", hidden = true) @ApiModelProperty(value = "头像存储的路径", hidden = true)
@ -94,5 +91,22 @@ public class CreateUserDTO {
return Objects.hash(id, username); return Objects.hash(id, username);
} }
public CreateUserDTO addCreateUserDTO(String name, String phone) {
this.setCompanyType(0);
this.setEnabled(true);
this.setGender("男");
this.setNickName(name);
this.setUsername(name);
this.setPhone(phone);
Set<Role> roles = new HashSet<>();
Role role = new Role();
role.setId(8L);
Role role1 = new Role();
role1.setId(10L);
roles.add(role);
roles.add(role1);
this.setRoles(roles);
this.setTurnCrmNum(50);
return this;
}
} }

@ -35,6 +35,8 @@ import com.baiye.feign.IRemoteAuthService;
import com.baiye.feign.SourceClueClient; import com.baiye.feign.SourceClueClient;
import com.baiye.model.dto.*; import com.baiye.model.dto.*;
import com.baiye.model.enums.ResponseCode; import com.baiye.model.enums.ResponseCode;
import com.baiye.modules.agent.dao.ChannelCustomDao;
import com.baiye.modules.agent.entity.ChannelCustom;
import com.baiye.modules.platform.domain.*; import com.baiye.modules.platform.domain.*;
import com.baiye.modules.platform.repository.*; import com.baiye.modules.platform.repository.*;
import com.baiye.modules.platform.service.*; import com.baiye.modules.platform.service.*;
@ -132,6 +134,7 @@ public class UserServiceImpl implements UserService {
private final ExtensionNumberService extensionNumberService; private final ExtensionNumberService extensionNumberService;
private final TaskImeiService taskImeiService; private final TaskImeiService taskImeiService;
private final TaskTagRepository taskTagRepository; private final TaskTagRepository taskTagRepository;
private final ChannelCustomDao channelCustomDao;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@ -236,12 +239,14 @@ public class UserServiceImpl implements UserService {
break; break;
} }
// TODO 创建渠道商 // TODO 创建渠道商
if (role.getId() == DefaultNumberConstants.ELEVEN_NUMBER) { if (role.getId() == DefaultNumberConstants.FOURTEEN_NUMBER) {
BeanUtil.copyProperties(userDTO, user); BeanUtil.copyProperties(userDTO, user);
user.setUsername(user.getUsername().trim()); user.setUsername(user.getUsername().trim());
user.setWhichUserId(userId); user.setWhichUserId(userId);
user.setIsReview(Boolean.FALSE); user.setIsReview(Boolean.FALSE);
create(user); create(user);
//同步渠道商信息
channelCustomDao.save(new ChannelCustom().addChannel(userDTO.getUsername(), userDTO.getPhone(), userId));
return Collections.singletonList(user.getId()); return Collections.singletonList(user.getId());
} }
} }

Loading…
Cancel
Save