Merge remote-tracking branch 'origin/master'
commit
7711c37dc0
@ -0,0 +1,128 @@
|
||||
package com.baiye.modules.platform.httpRequest;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baiye.exception.BadRequestException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ActivationCodeRequestApi {
|
||||
|
||||
@Value("${activation.url}")
|
||||
private String URL;
|
||||
|
||||
public void register(String name, String pwd) {
|
||||
try {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", name);
|
||||
map.put("pwd", pwd);
|
||||
String post = HttpUtil.post(URL + "/api/auth/registerByBaiye", JSONUtil.toJsonStr(map));
|
||||
JSONObject jsonObject = JSONUtil.parseObj(post);
|
||||
String status = jsonObject.get("status").toString();
|
||||
if (!status.equals("200")) throw new BadRequestException("注册失败");
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("注册失败");
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getActivationCode(int codeNum, int type, String account) {
|
||||
try {
|
||||
String url = URL + "/api/activate-code/genActivateCodeByBaiye";
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("codeNum", codeNum);
|
||||
map.put("type", type);
|
||||
map.put("account", account);
|
||||
String response = HttpUtil.get(url, map);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(response);
|
||||
String status = jsonObject.get("status").toString();
|
||||
if (status.equals("200")) {
|
||||
return JSONUtil.parseArray(jsonObject.get("data"));
|
||||
} else {
|
||||
throw new BadRequestException("激活失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("激活失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,8 @@
|
||||
package com.baiye.modules.platform.repository;
|
||||
|
||||
import com.baiye.modules.platform.domain.ActivationRecord;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface ActivationCodeRepository extends JpaRepository<ActivationRecord, Long>, JpaSpecificationExecutor<ActivationRecord> {
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baiye.modules.platform.rest;
|
||||
|
||||
import com.baiye.http.CommonResponse;
|
||||
import com.baiye.model.dto.ActivationCodeQueryCriteria;
|
||||
import com.baiye.model.dto.TaskQueryCriteria;
|
||||
import com.baiye.modules.platform.service.ActivationCodeService;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/activationRecord")
|
||||
@Api(tags = "激活码管理")
|
||||
public class ActivationCodeController {
|
||||
|
||||
private final ActivationCodeService activationCodeService;
|
||||
|
||||
@ApiOperation("生成激活码")
|
||||
@GetMapping("/generate")
|
||||
public CommonResponse<Object> generateActivationCode(@RequestParam("codeNum") int codeNum, @RequestParam("codeType") int codeType){
|
||||
activationCodeService.generateActivationCode(codeNum, codeType);
|
||||
return CommonResponse.createBySuccess();
|
||||
}
|
||||
|
||||
@ApiOperation("查询(分页)")
|
||||
@GetMapping("/queryAll")
|
||||
public ResponseEntity<Object> queryAll(ActivationCodeQueryCriteria activationCodeQueryCriteria, Pageable pageable) {
|
||||
return new ResponseEntity<>(activationCodeService.queryAll(activationCodeQueryCriteria, pageable), HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package com.baiye.modules.platform.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import com.baiye.exception.BadRequestException;
|
||||
import com.baiye.model.dto.ActivationCodeQueryCriteria;
|
||||
import com.baiye.modules.platform.domain.ActivationRecord;
|
||||
import com.baiye.modules.platform.domain.Company;
|
||||
import com.baiye.modules.platform.httpRequest.ActivationCodeRequestApi;
|
||||
import com.baiye.modules.platform.repository.ActivationCodeRepository;
|
||||
import com.baiye.modules.platform.repository.CompanyRepository;
|
||||
import com.baiye.modules.platform.service.ActivationCodeService;
|
||||
import com.baiye.modules.platform.service.CompanyService;
|
||||
import com.baiye.modules.platform.service.WechatSendMessageService;
|
||||
import com.baiye.modules.platform.service.mapstruct.ActivationCodeMapper;
|
||||
import com.baiye.util.PageUtil;
|
||||
import com.baiye.util.QueryHelp;
|
||||
import com.baiye.util.SecurityUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ActivationCodeServiceImpl implements ActivationCodeService {
|
||||
|
||||
private final ActivationCodeRepository activationCodeRepository;
|
||||
|
||||
private final CompanyRepository companyRepository;
|
||||
|
||||
private final ActivationCodeRequestApi activationCodeRequestApi;
|
||||
|
||||
private final ActivationCodeMapper activationCodeMapper;
|
||||
|
||||
private final CompanyService companyService;
|
||||
|
||||
private final WechatSendMessageService wechatSendMessageService;
|
||||
|
||||
@Value("${activation.namePrefix}")
|
||||
private String namePrefix;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void generateActivationCode(int codeNum, int codeType) {
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String name = namePrefix + currentUserId;
|
||||
Company company = companyRepository.findByUserId(currentUserId);
|
||||
Integer isActivation = company.getIsActivation();
|
||||
// 未激活过,先进行注册
|
||||
if (isActivation == 0) wechatSendMessageService.register(namePrefix, name, company.getId());
|
||||
// 计费,默认季卡
|
||||
double price = 300.00;
|
||||
if (codeType == 2) price = 1080.00;
|
||||
double totalPrice = company.getUserBalance() - (codeNum * price);
|
||||
if (totalPrice < 0) throw new BadRequestException("余额不足");
|
||||
// 生成激活码
|
||||
JSONArray array = activationCodeRequestApi.getActivationCode(codeNum, codeType, name);
|
||||
if (CollUtil.isNotEmpty(array)) {
|
||||
// 扣费
|
||||
companyService.updateUserBalanceByCompanyId(totalPrice, company.getId());
|
||||
// 插入激活码记录
|
||||
List<ActivationRecord> list = new ArrayList<>();
|
||||
for (Object obj : array) {
|
||||
ActivationRecord activationRecord = new ActivationRecord((String) obj, currentUserId, codeType);
|
||||
list.add(activationRecord);
|
||||
}
|
||||
activationCodeRepository.saveAll(list);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAll(ActivationCodeQueryCriteria criteria, Pageable pageable) {
|
||||
PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(Sort.Direction.DESC, "createTime"));
|
||||
criteria.setUserId(SecurityUtils.getCurrentUserId());
|
||||
Page<ActivationRecord> page = activationCodeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageRequest);
|
||||
return PageUtil.toPage(page.map(activationCodeMapper::toDto));
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.baiye.modules.platform.service.mapstruct;
|
||||
|
||||
import com.baiye.model.base.BaseMapper;
|
||||
import com.baiye.modules.platform.domain.ActivationRecord;
|
||||
import com.baiye.modules.platform.domain.Label;
|
||||
import com.baiye.modules.platform.service.dto.ActivationCodeDto;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface ActivationCodeMapper extends BaseMapper<ActivationCodeDto, ActivationRecord> {
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baiye.modules.report.dao;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import com.baiye.modules.platform.domain.CallDeduct;
|
||||
import com.baiye.modules.report.entity.ReportDeduct;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
@Repository
|
||||
public interface ReportDeductRepository extends JpaRepository<ReportDeduct, Long>, JpaSpecificationExecutor<ReportDeduct> {
|
||||
|
||||
/**
|
||||
* 公司id查找记录
|
||||
*
|
||||
* @param companyId
|
||||
* @param parse
|
||||
* @param endDate
|
||||
* @return
|
||||
*/
|
||||
@Query(value = "from ReportDeduct where companyId = ?1 and createTime >= ?2 and createTime <= ?3")
|
||||
List<ReportDeduct> queryByCompanyIdAndTime(Long companyId, DateTime parse, DateTime endDate);
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package com.baiye.timed;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baiye.modules.platform.domain.CallDeduct;
|
||||
import com.baiye.modules.platform.repository.CallDeductRepository;
|
||||
import com.baiye.modules.platform.repository.CompanyRepository;
|
||||
import com.baiye.modules.report.dao.ReportDeductRepository;
|
||||
import com.baiye.modules.report.entity.ReportDeduct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigInteger;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ReportDeductsSync {
|
||||
@Resource
|
||||
private CompanyRepository companyRepository;
|
||||
@Resource
|
||||
private ReportDeductRepository repository;
|
||||
@Resource
|
||||
private CallDeductRepository callDeductRepository;
|
||||
|
||||
// @Scheduled(cron = "0 0/5 * * * ? ")
|
||||
@Scheduled(cron = "0 0 22 * * ? ")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void countReport() {
|
||||
log.info("++++++++++++++++++++++countReport deduct start time {} ++++++++++++++++", DateUtil.now());
|
||||
String beginOfDay = DateUtil.formatDateTime(DateUtil.yesterday());
|
||||
String endOfDay = DateUtil.formatDateTime(DateUtil.date());
|
||||
//统计每个公司的消费额
|
||||
List<ReportDeduct> reportDeducts = reportDeducts(DateUtil.parseDateTime(beginOfDay), DateUtil.parseDateTime(endOfDay), null);
|
||||
repository.saveAll(reportDeducts);
|
||||
log.info("++++++++++++++++++++++countReport deduct end time {} ++++++++++++++++", DateUtil.now());
|
||||
}
|
||||
|
||||
public List<ReportDeduct> reportDeducts(DateTime beginOfDay, DateTime endOfDay, Long companyId) {
|
||||
|
||||
List<Map<String, Object>> userBalances = companyRepository.findUserBalanceByUserId(companyId);
|
||||
HashMap<Long, Double> map = new HashMap<>();
|
||||
for (Map<String, Object> map1 : userBalances) {
|
||||
BigInteger key = (BigInteger) map1.get("id");
|
||||
Long id = key.longValue();
|
||||
Double value = (Double) map1.get("userBalance");
|
||||
map.put(id, value);
|
||||
}
|
||||
|
||||
List<ReportDeduct> list = new ArrayList<>();
|
||||
List<CallDeduct> callDeducts;
|
||||
if (companyId != null) {
|
||||
callDeducts = callDeductRepository.queryByCompanyIdAndTime(companyId, beginOfDay, endOfDay);
|
||||
double sum = callDeducts.stream().mapToDouble(CallDeduct::getDeductAmount).sum();
|
||||
ReportDeduct reportDeduct = new ReportDeduct();
|
||||
reportDeduct.setDeductAmount(sum);
|
||||
reportDeduct.setBalance(map.get(companyId) == null ? 0.0 : map.get(companyId));
|
||||
reportDeduct.setCreateTime(new Date());
|
||||
reportDeduct.setCompanyId(companyId);
|
||||
list.add(reportDeduct);
|
||||
} else {
|
||||
callDeducts = callDeductRepository.queryByCompanyIdAndTime(beginOfDay, endOfDay);
|
||||
Map<Long, List<CallDeduct>> collect = callDeducts.stream().collect(Collectors.groupingBy(CallDeduct::getCompanyId, Collectors.toList()));
|
||||
for (Long key : map.keySet()) {
|
||||
ReportDeduct reportDeduct = new ReportDeduct();
|
||||
reportDeduct.setBalance(map.get(key) == null ? 0.0 : map.get(key));
|
||||
reportDeduct.setCreateTime(new Date());
|
||||
reportDeduct.setCompanyId(key);
|
||||
List<CallDeduct> callDeducts1 = collect.get(key);
|
||||
|
||||
if (CollUtil.isEmpty(callDeducts1)) {
|
||||
reportDeduct.setDeductAmount(0.0);
|
||||
} else {
|
||||
Double sum = callDeducts1.stream().mapToDouble(CallDeduct::getDeductAmount).sum();
|
||||
reportDeduct.setDeductAmount(sum);
|
||||
}
|
||||
list.add(reportDeduct);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baiye.model.dto;
|
||||
|
||||
import com.baiye.annotation.Query;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ActivationCodeQueryCriteria {
|
||||
|
||||
@Query
|
||||
private String activationCode;
|
||||
|
||||
@Query
|
||||
private Long userId;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baiye.module.dao;
|
||||
|
||||
import com.baiye.module.entity.ReportToker;
|
||||
import com.baiye.module.entity.ReportTokerCall;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
@Repository
|
||||
public interface ReportTokerCallRepository extends JpaRepository<ReportTokerCall, Long>, JpaSpecificationExecutor<ReportTokerCall> {
|
||||
|
||||
/**
|
||||
* 统计投客任务
|
||||
*
|
||||
* @param beginTime
|
||||
* @param endTime
|
||||
* @param managerId
|
||||
* @return
|
||||
*/
|
||||
@Query(value = "select * from tb_report_toker_call where (create_time between ?1 and ?2) and manager_id = ?3", nativeQuery = true)
|
||||
List<ReportTokerCall> queryAllByTimeAndManagerId(String beginTime, String endTime,Long managerId);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baiye.module.service.dto;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
@Data
|
||||
public class ClueTalkDTO {
|
||||
private Long managerId;
|
||||
|
||||
private Date newestCallTime;
|
||||
|
||||
private Integer clueCallStatus;
|
||||
}
|
Loading…
Reference in New Issue