增加时间方法,增加汇总部分数据,分类更改

This commit is contained in:
liuyingfang 2024-03-11 09:07:11 +08:00
parent e0291db00d
commit bcfa3f9984
17 changed files with 267 additions and 57 deletions

View File

@ -16,10 +16,13 @@
package cn.ysk.cashier.utils;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author: liaojinlong
@ -170,6 +173,17 @@ public class DateUtil {
public static LocalDateTime parseLocalDateTimeFormatyMdHms(String localDateTime) {
return LocalDateTime.from(DFY_MD_HMS.parse(localDateTime));
}
// 获取某个日期的开始时间
public static Timestamp getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
/**
* 激活码到期时间
@ -189,4 +203,81 @@ public class DateUtil {
// 转换为时间戳
return newDateTime.toInstant().toEpochMilli();
}
// 获取本周的开始时间
public static Date getBeginDayOfWeek() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
// 获取某个日期的结束时间
public static Timestamp getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
// 获取本周的结束时间
public static Date getEndDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
// 获取今年是哪一年
public static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
// 获取本月是哪一月
public static int getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
// 获取本月的开始时间
public static Date getBeginDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
// 获取本月的结束时间
public static Date getEndDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
// 获取本年的开始时间
public static Date getBeginDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
// cal.set
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
}
}

View File

@ -43,19 +43,19 @@ public class TbShopCategoryController {
private final TbShopCategoryService tbShopCategoryService;
@Log("导出数据")
@ApiOperation("导出数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('tbShopCategory:list')")
public void exportTbShopCategory(HttpServletResponse response, TbShopCategoryQueryCriteria criteria) throws IOException {
tbShopCategoryService.download(tbShopCategoryService.queryAll(criteria), response);
}
// @Log("导出数据")
// @ApiOperation("导出数据")
// @GetMapping(value = "/download")
// @PreAuthorize("@el.check('tbShopCategory:list')")
// public void exportTbShopCategory(HttpServletResponse response, TbShopCategoryQueryCriteria criteria) throws IOException {
// tbShopCategoryService.download(tbShopCategoryService.queryAll(criteria), response);
// }
@GetMapping
@Log("查询product/category")
@ApiOperation("查询product/category")
public ResponseEntity<Object> queryTbShopCategory(TbShopCategoryQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(tbShopCategoryService.queryAll(criteria,pageable),HttpStatus.OK);
public ResponseEntity<Object> queryTbShopCategory(TbShopCategoryQueryCriteria criteria){
return new ResponseEntity<>(tbShopCategoryService.queryAll(criteria),HttpStatus.OK);
}
@PostMapping

View File

@ -24,4 +24,9 @@ public class SummaryController {
private Object shopSummary(@RequestParam Integer shopId){
return summaryService.selectSummary(shopId);
}
@GetMapping("/date")
private Object shopSummaryDate(@RequestParam Integer shopId,@RequestParam Integer day){
return summaryService.selectSummaryDate(shopId,day);
}
}

View File

@ -24,7 +24,7 @@ import cn.ysk.cashier.annotation.Query;
* @date 2024-01-08
**/
@Data
public class TbShopCategoryQueryCriteria{
public class TbShopCategoryQueryCriteria {
/** 精确 */
@Query
@ -40,5 +40,9 @@ public class TbShopCategoryQueryCriteria{
private String tree;
@Query
private Integer page;
@Query
private Integer size;
}

View File

@ -38,4 +38,9 @@ public class TestTask {
public void run2(){
log.info("run2 执行成功");
}
public void orderSum(){
}
}

View File

@ -6,6 +6,7 @@ import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.Date;
import java.util.List;
/**
@ -19,7 +20,8 @@ public interface ShopUserDutyDetailRepository extends JpaRepository<TbShopUserDu
" tb_shop_user_duty_detail AS product" +
" LEFT JOIN tb_shop_user_duty AS user ON product.duty_id = USER.id " +
"WHERE user.shop_id = :shopId" +
" AND user.login_time BETWEEN :startTime AND :endTime" +
" GROUP BY product.product_id" +
" Limit 5", nativeQuery = true)
List<Object[]> searchByDutyId(@Param("shopId") Integer shopId);
List<Object[]> searchByDutyId(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
}

View File

@ -2,11 +2,14 @@ package cn.ysk.cashier.repository;
import cn.ysk.cashier.pojo.TbShopUserDuty;
import cn.ysk.cashier.repository.mapping.SumDateMapping;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import javax.persistence.Tuple;
import java.util.Date;
import java.util.List;
/**
@ -17,5 +20,17 @@ public interface ShopUserDutyRepository extends JpaRepository<TbShopUserDuty, In
"FROM TbShopUserDuty detail " +
"WHERE detail.shopId = :shopId")
Tuple sumByShopId(@Param("shopId") Integer shopId);
@Query(value = "SELECT\n" +
"\tifnull( sum( amount ), 0 ) AS amount,\n" +
"\ttrade_day as tradeDay\n" +
"FROM\n" +
"\t`tb_shop_user_duty` \n" +
"WHERE\n" +
"\tshop_id = :shopId \n" +
"\tAND return_amount < 0 \n" +
"\tAND login_time BETWEEN :startTime AND :endTime \n" +
"GROUP BY\n" +
"\tshop_id,\n" +
"\ttrade_day",nativeQuery = true)
List<SumDateMapping> sumByDate(@Param("shopId")Integer shopId, @Param("startTime") Date startTime,@Param("endTime") Date endTime);
}

View File

@ -0,0 +1,13 @@
package cn.ysk.cashier.repository.mapping;
import java.math.BigDecimal;
/**
* @author lyf
*/
public interface CountPayTypeMapping {
//使用次数
BigDecimal count();
//日期
String payType();
}

View File

@ -0,0 +1,13 @@
package cn.ysk.cashier.repository.mapping;
import java.math.BigDecimal;
/**
* @author lyf
*/
public interface SumDateMapping {
//总销售额
BigDecimal amount();
//日期
BigDecimal tradeDay();
}

View File

@ -16,8 +16,13 @@
package cn.ysk.cashier.repository.order;
import cn.ysk.cashier.pojo.order.TbOrderInfo;
import cn.ysk.cashier.repository.mapping.CountPayTypeMapping;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* @website https://eladmin.vip
@ -25,4 +30,7 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @date 2024-03-02
**/
public interface TbOrderInfoRepository extends JpaRepository<TbOrderInfo, Integer>, JpaSpecificationExecutor<TbOrderInfo> {
@Query(value = "SELECT COUNT(1) AS count,pay_type AS payType FROM tb_order_info Where shop_id = :shopId GROUP BY pay_type" ,nativeQuery = true)
List<CountPayTypeMapping> countByShopId(@Param("shopId") String shopId);
}

View File

@ -16,13 +16,21 @@
package cn.ysk.cashier.repository.shop;
import cn.ysk.cashier.pojo.shop.TbShopUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import javax.persistence.Tuple;
/**
* @website https://eladmin.vip
* @author lyf
* @date 2024-03-01
**/
public interface TbShopUserRepository extends JpaRepository<TbShopUser, Integer>, JpaSpecificationExecutor<TbShopUser> {
public interface TbShopUserRepository extends JpaRepository<TbShopUser, Integer>,JpaSpecificationExecutor<TbShopUser> {
@Query("SELECT count(0) from TbShopUser user where user.shopId = :shopId")
Tuple searchByCount(@Param("shopId") String shopId);
}

View File

@ -1,10 +1,16 @@
package cn.ysk.cashier.service;
import cn.ysk.cashier.repository.mapping.SumDateMapping;
import cn.ysk.cashier.vo.SummaryVO;
import java.util.List;
import java.util.Map;
/**
* @author lyf
*/
public interface SummaryService {
SummaryVO selectSummary(Integer shop);
Map<String,Object> selectSummaryDate(Integer shopId, Integer day);
}

View File

@ -1,10 +1,13 @@
package cn.ysk.cashier.service.impl;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.ysk.cashier.repository.ShopUserDutyDetailRepository;
import cn.ysk.cashier.repository.ShopUserDutyRepository;
import cn.ysk.cashier.repository.mapping.CountPayTypeMapping;
import cn.ysk.cashier.repository.mapping.SumDateMapping;
import cn.ysk.cashier.repository.order.TbOrderInfoRepository;
import cn.ysk.cashier.repository.shop.TbShopUserRepository;
import cn.ysk.cashier.service.SummaryService;
import cn.ysk.cashier.utils.DateUtil;
import cn.ysk.cashier.vo.SummaryVO;
import cn.ysk.cashier.vo.ProductVO;
import lombok.RequiredArgsConstructor;
@ -14,8 +17,7 @@ import javax.annotation.Resource;
import javax.persistence.Tuple;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -25,15 +27,57 @@ public class SummaryServiceImpl implements SummaryService {
private ShopUserDutyRepository shopUserDutyRepository;
@Resource
private ShopUserDutyDetailRepository shopUserDutyDetailRepository;
@Resource
private TbShopUserRepository tbShopUserRepository;
@Resource
private TbOrderInfoRepository tbOrderInfoRepository;
@Override
public SummaryVO selectSummary(Integer shopId) {
SummaryVO summaryVO = new SummaryVO();
//支付笔数
Tuple result = shopUserDutyRepository.sumByShopId(shopId);
summaryVO.setPaymentsNumber(result.get(0, Long.class));
summaryVO.setTotalSales(result.get(1, BigDecimal.class));
List<Object[]> objects = shopUserDutyDetailRepository.searchByDutyId(shopId);
summaryVO.setPaymentsNumber(result.get(0, Long.class) == null?0L : result.get(0, Long.class));
summaryVO.setTotalSales(result.get(1, BigDecimal.class) == null? new BigDecimal("0") : result.get(1, BigDecimal.class));
// List<Object[]> objects = shopUserDutyDetailRepository.searchByDutyId(shopId);
//商品销量排行前五
// List<ProductVO> list = new ArrayList<>();
// for (Object[] o :objects) {
// ProductVO productVO = new ProductVO();
// productVO.setProductId((Integer) o[0]);
// productVO.setProductName((String) o[1]);
// productVO.setProductNum((BigDecimal)o[2]);
// list.add(productVO);
// }
// summaryVO.setProductInfoList(list);
if (summaryVO.getPaymentsNumber() == 0){
summaryVO.setAverageSales(new BigDecimal("0"));
}else {
summaryVO.setAverageSales(summaryVO.getTotalSales().divide(new BigDecimal(summaryVO.getPaymentsNumber()), 2, RoundingMode.DOWN));
}
//用户数
Tuple count = tbShopUserRepository.searchByCount(shopId.toString());
summaryVO.setTotalUser(count.get(0, Long.class));
return summaryVO;
}
@Override
public Map<String,Object> selectSummaryDate(Integer shopId, Integer day) {
Date startTime = new Date();
Date endTime = new Date();
if (day == 7){
startTime = DateUtil.getBeginDayOfWeek();
endTime = DateUtil.getEndDayOfWeek();
}else if (day == 30){
startTime = DateUtil.getBeginDayOfMonth();
endTime = DateUtil.getEndDayOfMonth();
}
HashMap<String, Object> map = new HashMap<>();
//根据时间的营业额
List<SumDateMapping> sumDateMappings = shopUserDutyRepository.sumByDate(shopId,startTime,endTime);
//根据时间商品排行
List<Object[]> objects = shopUserDutyDetailRepository.searchByDutyId(shopId,startTime,endTime);
//商品销量排行前五
List<ProductVO> list = new ArrayList<>();
for (Object[] o :objects) {
ProductVO productVO = new ProductVO();
@ -42,8 +86,15 @@ public class SummaryServiceImpl implements SummaryService {
productVO.setProductNum((BigDecimal)o[2]);
list.add(productVO);
}
summaryVO.setProductInfoList(list);
summaryVO.setAverageSales(summaryVO.getTotalSales().divide(new BigDecimal(summaryVO.getPaymentsNumber()),2, RoundingMode.DOWN));
return summaryVO;
list = list.stream()
.sorted(Comparator.comparing(ProductVO::getProductNum).reversed()) // 降序排序
.collect(Collectors.toList());
map.put("total",sumDateMappings);
map.put("totalProduct",list);
//支付类型占比
List<CountPayTypeMapping> countPayTypeMappings = tbOrderInfoRepository.countByShopId(shopId.toString());
map.put("countPayType",countPayTypeMappings);
return map;
}
}

View File

@ -25,6 +25,7 @@ import cn.ysk.cashier.service.product.TbShopCategoryService;
import cn.ysk.cashier.dto.product.TbShopCategoryDto;
import cn.ysk.cashier.dto.product.TbShopCategoryQueryCriteria;
import cn.ysk.cashier.mapper.product.TbShopCategoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
@ -36,12 +37,9 @@ import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
/**
@ -58,9 +56,9 @@ public class TbShopCategoryServiceImpl implements TbShopCategoryService {
private final TbShopCategoryMapper tbShopCategoryMapper;
@Override
public Map<String,Object> queryAll(TbShopCategoryQueryCriteria criteria, Pageable pageable){
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("sort"));
Page<TbShopCategory> page = tbShopCategoryRepository.findAllBy(criteria.getShopId(), pageable);
public Map<String,Object> queryAll(TbShopCategoryQueryCriteria criteria){
PageRequest sort = PageRequest.of(criteria.getPage(), criteria.getSize(), Sort.by("sort"));
Page<TbShopCategory> page = tbShopCategoryRepository.findAllBy(criteria.getShopId(), sort);
List<Integer> treeId = new ArrayList<>();
for (TbShopCategory category : page.getContent()) {
@ -81,6 +79,7 @@ public class TbShopCategoryServiceImpl implements TbShopCategoryService {
tbShopCategoryChildren.add(tbShopCategory);
}
}
TbShopCategoryDto tbShopCategoryDto = new TbShopCategoryDto();
tbShopCategoryDto.setChildrenList(tbShopCategoryChildren);
BeanUtils.copyProperties(category, tbShopCategoryDto);
@ -93,10 +92,10 @@ public class TbShopCategoryServiceImpl implements TbShopCategoryService {
return PageUtil.toPage(result, page.getTotalElements()-children.size());
}
@Override
public List<TbShopCategoryDto> queryAll(TbShopCategoryQueryCriteria criteria){
return tbShopCategoryMapper.toDto(tbShopCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
}
// @Override
// public List<TbShopCategoryDto> queryAll(TbShopCategoryQueryCriteria criteria){
// return tbShopCategoryMapper.toDto(tbShopCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
// }
@Override
@Transactional
@ -115,9 +114,7 @@ public class TbShopCategoryServiceImpl implements TbShopCategoryService {
if (byName != null){
throw new BadRequestException("分类名称重复");
}
if (resources.getPid() == null || "".equals(resources.getPid())){
resources.setTree(resources.getId());
}else {
if (StringUtils.isNotBlank(resources.getPid())){
resources.setTree(Integer.valueOf(resources.getPid().trim()));
}
return tbShopCategoryRepository.save(resources);
@ -130,8 +127,6 @@ public class TbShopCategoryServiceImpl implements TbShopCategoryService {
if (resources.getName() == null || resources.getShopId() == null){
throw new BadRequestException("必要参数为空");
}
TbShopCategory byName = tbShopCategoryRepository.findByName(resources.getName(), resources.getShopId());
resources.setUpdatedAt(Instant.now().toEpochMilli());
ValidationUtil.isNull( tbShopCategory.getId(),"TbShopCategory","id",resources.getId());
tbShopCategory.copy(resources);

View File

@ -124,8 +124,9 @@ public class TbShopInfoServiceImpl implements TbShopInfoService {
}
}
TbShopInfo byAccount = tbShopInfoRepository.findByAccount(resources.getAccount());
if (byAccount != null){
throw new BadRequestException("登录名重复");
User byUsername = userRepository.findByUsername(resources.getAccount());
if (byAccount != null || byUsername.getId() != null){
throw new BadRequestException("登录名已注册");
}
TbShopInfo tbShopInfo = new TbShopInfo();
@ -155,8 +156,6 @@ public class TbShopInfoServiceImpl implements TbShopInfoService {
tbMerchantRegister.setShopId(String.valueOf(save.getId()));
tbMerchantRegister.setName(save.getShopName());
merchantRegisterRepository.save(tbMerchantRegister);
}
//增加商户登录账号
TbMerchantAccount tbMerchantAccount = new TbMerchantAccount();

View File

@ -35,17 +35,16 @@ public interface TbShopCategoryService {
/**
* 查询数据分页
* @param criteria 条件
* @param pageable 分页参数
* @return Map<String,Object>
*/
Map<String,Object> queryAll(TbShopCategoryQueryCriteria criteria, Pageable pageable);
Map<String,Object> queryAll(TbShopCategoryQueryCriteria criteria);
/**
* 查询所有数据不分页
* @param criteria 条件参数
* @return List<TbShopCategoryDto>
*/
List<TbShopCategoryDto> queryAll(TbShopCategoryQueryCriteria criteria);
// List<TbShopCategoryDto> queryAll(TbShopCategoryQueryCriteria criteria);
/**
* 根据ID查询

View File

@ -13,26 +13,22 @@ public class SummaryVO {
/**
* 总销售额
*/
private BigDecimal totalSales;
/**
* 今日销售额
*/
private BigDecimal todaySales;
private BigDecimal totalSales = new BigDecimal("0");
/**
* 平均每单
*/
private BigDecimal averageSales;
private BigDecimal averageSales = new BigDecimal("0");
/**
* 支付笔数
*/
private Long paymentsNumber;
/**
* 今天支付笔数
*/
private Integer todayPaymentsNumber;
private Long paymentsNumber =0L;
/**
* 商品统计数据
*/
private List<ProductVO> productInfoList;
/**
*用户数
*/
private Long totalUser =0L;
}