优化查询问题

master
bynt 1 year ago
parent 65f52b2c1b
commit 91730b4f7d

@ -0,0 +1,58 @@
package com.baiye.modules.platform.domain.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import java.util.Date;
/**
* @author Enzo
* @date : 2022/11/9
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CallDeductDataVO {
private Long id;
@ApiModelProperty(value = "扣减时长")
private Integer deductDuration;
@ApiModelProperty(value = "扣减金额")
private Double deductAmount;
@ApiModelProperty(value = "线索id")
private Long clueId;
@ApiModelProperty(value = "人员id")
private Long memberId;
@ApiModelProperty(value = "所属公司")
private Long companyId;
@ApiModelProperty(value = "状态")
private Boolean status;
@ApiModelProperty(value = "扣减时的版本号")
private Integer version;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "消费类型1 话费消耗 2 套餐充值 3 余额充值 4 资源兑换")
private Integer type;
@ApiModelProperty(value = "套餐类型")
private String comboType;
@ApiModelProperty(value = "用户名")
private String username;
}

@ -17,6 +17,7 @@ package com.baiye.modules.platform.repository;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import com.baiye.modules.platform.domain.CallDeduct; import com.baiye.modules.platform.domain.CallDeduct;
import com.baiye.modules.platform.domain.vo.CallDeductDataVO;
import com.baiye.modules.platform.domain.vo.CallDeductVO; import com.baiye.modules.platform.domain.vo.CallDeductVO;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
@ -52,8 +53,9 @@ public interface CallDeductRepository extends JpaRepository<CallDeduct, Long>, J
* @param endDate * @param endDate
* @return * @return
*/ */
@Query(value = "from CallDeduct where companyId = ?1 and createTime >= ?2 and createTime <= ?3") @Query(value = "SELECT new com.baiye.modules.platform.domain.vo.CallDeductDataVO(c.id,c.deductDuration,c.deductAmount," +
List<CallDeduct> queryByCompanyIdAndTime(Long companyId, DateTime parse, DateTime endDate); " c.clueId, c.memberId,c.companyId,c.status,c.version,c.createTime,c.type,c.comboType,u.username) FROM CallDeduct c, User u WHERE u.id = c.memberId and c.companyId = ?1 and c.createTime between ?2 and ?3")
List<CallDeductDataVO> queryByCompanyIdAndTime(Long companyId, DateTime parse, DateTime endDate);
/** /**
* id * id

@ -8,6 +8,8 @@ import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baiye.constant.DefaultNumberConstants; import com.baiye.constant.DefaultNumberConstants;
import com.baiye.modules.platform.domain.CallDeduct; import com.baiye.modules.platform.domain.CallDeduct;
import com.baiye.modules.platform.domain.Company;
import com.baiye.modules.platform.domain.vo.CallDeductDataVO;
import com.baiye.modules.system.domain.User; import com.baiye.modules.system.domain.User;
import com.baiye.modules.platform.domain.vo.CallDeductResponseVO; import com.baiye.modules.platform.domain.vo.CallDeductResponseVO;
import com.baiye.modules.platform.domain.vo.CallDeductVO; import com.baiye.modules.platform.domain.vo.CallDeductVO;
@ -29,10 +31,7 @@ import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** /**
* @author Enzo * @author Enzo
@ -91,24 +90,18 @@ public class CallDeductServiceImpl implements CallDeductService {
Map<String, Object> map = Maps.newHashMap(); Map<String, Object> map = Maps.newHashMap();
String queryTime = criteria.getQueryTime(); String queryTime = criteria.getQueryTime();
DateTime parse = DateUtil.parse(queryTime, DatePattern.NORM_MONTH_PATTERN); DateTime parse = DateUtil.parse(queryTime, DatePattern.NORM_MONTH_PATTERN);
DateTime endDate = DateUtil.offsetMonth(parse, DefaultNumberConstants.ONE_NUMBER); DateTime endDate = DateUtil.offsetMonth(parse, DefaultNumberConstants.FIVE_NUMBER);
List<CallDeduct> deductList = List<CallDeductDataVO> deductList =
callDeductRepository.queryByCompanyIdAndTime callDeductRepository.queryByCompanyIdAndTime
(companyId, parse, endDate); (companyId, parse, endDate);
deductList.forEach(deduct -> {
User byId = userRepository.findById(deduct.getMemberId()).orElseGet(User::new);
if (ObjectUtil.isNotNull(byId)) {
deduct.setUsername(byId.getUsername());
}
});
List<CallDeduct> list = PageUtil.toPage(pageable.getPageNumber(), List<CallDeductDataVO> list = PageUtil.toPage(pageable.getPageNumber(),
pageable.getPageSize(), deductList); pageable.getPageSize(), deductList);
CompanyDto companyById = companyService.findCompanyById(companyId); Company byId = companyService.findById(companyId);
map.put("content", list); map.put("content", list);
map.put("chargeBill", queryTime); map.put("chargeBill", queryTime);
map.put("totalElements", deductList.size()); map.put("totalElements", deductList.size());
map.put("companyName", companyById.getCompanyName()); map.put("companyName", byId.getCompanyName());
map.put("billTarikh", DateUtil.format(endDate, DatePattern.NORM_DATE_PATTERN)); map.put("billTarikh", DateUtil.format(endDate, DatePattern.NORM_DATE_PATTERN));
return map; return map;
} }
@ -130,18 +123,12 @@ public class CallDeductServiceImpl implements CallDeductService {
String queryTime = criteria.getQueryTime(); String queryTime = criteria.getQueryTime();
DateTime parse = DateUtil.parse(queryTime, "yyyy-MM"); DateTime parse = DateUtil.parse(queryTime, "yyyy-MM");
DateTime endDate = DateUtil.offsetMonth(parse, DefaultNumberConstants.ONE_NUMBER); DateTime endDate = DateUtil.offsetMonth(parse, DefaultNumberConstants.ONE_NUMBER);
List<CallDeduct> deductList = List<CallDeductDataVO> deductList =
callDeductRepository.queryByCompanyIdAndTime callDeductRepository.queryByCompanyIdAndTime
(companyId, parse, endDate); (companyId, parse, endDate);
deductList.forEach(deduct -> {
User byId = userRepository.findById(deduct.getMemberId()).orElseGet(User::new);
if (ObjectUtil.isNotNull(byId)) {
deduct.setUsername(byId.getUsername());
}
});
List<Map<String, Object>> list = new ArrayList<>(); List<Map<String, Object>> list = new ArrayList<>();
for (CallDeduct deduct : deductList) { for (CallDeductDataVO deduct : deductList) {
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
map.put("账号名称", deduct.getUsername()); map.put("账号名称", deduct.getUsername());
map.put("费用类型", deduct.getType() == DefaultNumberConstants.ONE_NUMBER ? "日结话费" : "套餐充值"); map.put("费用类型", deduct.getType() == DefaultNumberConstants.ONE_NUMBER ? "日结话费" : "套餐充值");

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.baiye.modules.platform.domain.CallDeduct; import com.baiye.modules.platform.domain.CallDeduct;
import com.baiye.modules.platform.domain.vo.CallDeductDataVO;
import com.baiye.modules.platform.repository.CallDeductRepository; import com.baiye.modules.platform.repository.CallDeductRepository;
import com.baiye.modules.platform.repository.CompanyRepository; import com.baiye.modules.platform.repository.CompanyRepository;
import com.baiye.modules.report.dao.ReportDeductRepository; import com.baiye.modules.report.dao.ReportDeductRepository;
@ -56,10 +57,10 @@ public class ReportDeductsSync {
} }
List<ReportDeduct> list = new ArrayList<>(); List<ReportDeduct> list = new ArrayList<>();
List<CallDeduct> callDeducts;
if (companyId != null) { if (companyId != null) {
callDeducts = callDeductRepository.queryByCompanyIdAndTime(companyId, beginOfDay, endOfDay); List<CallDeductDataVO> callDeducts = callDeductRepository.queryByCompanyIdAndTime(companyId, beginOfDay, endOfDay);
double sum = callDeducts.stream().mapToDouble(CallDeduct::getDeductAmount).sum(); double sum = callDeducts.stream().mapToDouble(CallDeductDataVO::getDeductAmount).sum();
ReportDeduct reportDeduct = new ReportDeduct(); ReportDeduct reportDeduct = new ReportDeduct();
reportDeduct.setDeductAmount(sum); reportDeduct.setDeductAmount(sum);
reportDeduct.setBalance(map.get(companyId) == null ? 0.0 : map.get(companyId)); reportDeduct.setBalance(map.get(companyId) == null ? 0.0 : map.get(companyId));
@ -67,7 +68,7 @@ public class ReportDeductsSync {
reportDeduct.setCompanyId(companyId); reportDeduct.setCompanyId(companyId);
list.add(reportDeduct); list.add(reportDeduct);
} else { } else {
callDeducts = callDeductRepository.queryByCompanyIdAndTime(beginOfDay, endOfDay); List<CallDeduct> callDeducts = callDeductRepository.queryByCompanyIdAndTime(beginOfDay, endOfDay);
Map<Long, List<CallDeduct>> collect = callDeducts.stream().collect(Collectors.groupingBy(CallDeduct::getCompanyId, Collectors.toList())); Map<Long, List<CallDeduct>> collect = callDeducts.stream().collect(Collectors.groupingBy(CallDeduct::getCompanyId, Collectors.toList()));
for (Long key : map.keySet()) { for (Long key : map.keySet()) {
ReportDeduct reportDeduct = new ReportDeduct(); ReportDeduct reportDeduct = new ReportDeduct();

Loading…
Cancel
Save