|
|
@@ -1,21 +1,26 @@
|
|
|
package com.izouma.jiashanxia.service;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.jiashanxia.domain.GoodsInfo;
|
|
|
+import com.izouma.jiashanxia.domain.OrderInfo;
|
|
|
+import com.izouma.jiashanxia.domain.UserSet;
|
|
|
import com.izouma.jiashanxia.domain.UserSetFlow;
|
|
|
-import com.izouma.jiashanxia.dto.GoodsDTO;
|
|
|
-import com.izouma.jiashanxia.dto.GoodsVO;
|
|
|
-import com.izouma.jiashanxia.dto.PageQuery;
|
|
|
-import com.izouma.jiashanxia.dto.UserSetFlowVO;
|
|
|
+import com.izouma.jiashanxia.dto.*;
|
|
|
+import com.izouma.jiashanxia.enums.FlowType;
|
|
|
import com.izouma.jiashanxia.exception.BusinessException;
|
|
|
import com.izouma.jiashanxia.repo.GoodsInfoRepo;
|
|
|
import com.izouma.jiashanxia.repo.UserSetFlowRepo;
|
|
|
+import com.izouma.jiashanxia.repo.UserSetRepo;
|
|
|
import com.izouma.jiashanxia.utils.JpaUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -25,11 +30,31 @@ public class UserSetFlowService {
|
|
|
|
|
|
private UserSetFlowRepo userSetFlowRepo;
|
|
|
private GoodsInfoRepo goodsInfoRepo;
|
|
|
+ private UserSetRepo userSetRepo;
|
|
|
|
|
|
public Page<UserSetFlow> all(PageQuery pageQuery) {
|
|
|
return userSetFlowRepo.findAll(JpaUtils.toSpecification(pageQuery, UserSetFlow.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ 核销列表
|
|
|
+ */
|
|
|
+ public Page<UserSetFlow> writeOffAll(PageQuery pageQuery) {
|
|
|
+ return userSetFlowRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
|
|
|
+ List<Predicate> and = JpaUtils.toPredicates(pageQuery, UserSetFlow.class, root, criteriaQuery, criteriaBuilder);
|
|
|
+ and.add(criteriaBuilder.equal(root.join("type"), FlowType.WRITE_OFF));
|
|
|
+ if (StrUtil.isNotEmpty(pageQuery.getSearch())) {
|
|
|
+ List<Predicate> or = new ArrayList<>();
|
|
|
+ or.add(and.get(and.size() - 1));
|
|
|
+ and.remove(and.get(and.size() - 1));
|
|
|
+ or.add(criteriaBuilder.like(root.join("user").get("nickname"), "%" + pageQuery.getSearch() + "%"));
|
|
|
+ or.add(criteriaBuilder.like(root.join("writeOffUser")
|
|
|
+ .get("nickname"), "%" + pageQuery.getSearch() + "%"));
|
|
|
+ and.add(criteriaBuilder.or(or.toArray(new Predicate[0])));
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
+ }), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
|
|
|
public List<UserSetFlowVO> my(Long userId) {
|
|
|
List<UserSetFlowVO> vos = new ArrayList<>();
|
|
|
@@ -46,7 +71,7 @@ public class UserSetFlowService {
|
|
|
.collect(Collectors.toMap(GoodsInfo::getId, GoodsInfo::getName));
|
|
|
// 转vo
|
|
|
flows.forEach(flow -> {
|
|
|
- List<GoodsVO> goodsVOS = this.toGoodsVO(flow.getContent(),goodsMap);
|
|
|
+ List<GoodsVO> goodsVOS = this.toGoodsVO(flow.getContent(), goodsMap);
|
|
|
UserSetFlowVO vo = new UserSetFlowVO();
|
|
|
BeanUtil.copyProperties(flow, vo);
|
|
|
vo.setContent(goodsVOS);
|
|
|
@@ -67,4 +92,58 @@ public class UserSetFlowService {
|
|
|
);
|
|
|
return goodsVOS;
|
|
|
}
|
|
|
+
|
|
|
+ /*
|
|
|
+ 核销
|
|
|
+ */
|
|
|
+ public UserSetFlow writeOff(WriteOffSaveVO writeOffSaveVO) {
|
|
|
+ if (writeOffSaveVO.getUserId().equals(writeOffSaveVO.getWriteOffUserId())) {
|
|
|
+ throw new BusinessException("不可自己核销自己");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户已有套餐
|
|
|
+ Map<Long, UserSet> userSetMap = userSetRepo.findAllByUserId(writeOffSaveVO.getUserId())
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.toMap(UserSet::getGoodsInfoId, userSet -> userSet));
|
|
|
+
|
|
|
+ List<GoodsDTO> goodsDTOS = JSONObject.parseArray(writeOffSaveVO.getContent(), GoodsDTO.class);
|
|
|
+ goodsDTOS.forEach(goodsDTO -> {
|
|
|
+ UserSet userSet = userSetMap.get(goodsDTO.getGoodsInfoId());
|
|
|
+ if (ObjectUtil.isEmpty(userSet)) {
|
|
|
+ throw new BusinessException("无此项目");
|
|
|
+ }
|
|
|
+ if (goodsDTO.getNum() > userSet.getNum()) {
|
|
|
+ throw new BusinessException("核销数量大于套餐数量");
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 保存套餐流水
|
|
|
+ return userSetFlowRepo.save(
|
|
|
+ UserSetFlow.builder()
|
|
|
+ .userId(writeOffSaveVO.getUserId())
|
|
|
+ .content(writeOffSaveVO.getContent())
|
|
|
+ .type(FlowType.WRITE_OFF)
|
|
|
+ .writeOffUserId(writeOffSaveVO.getWriteOffUserId())
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ public WriteOffRecordDTO getDTO(Long id) {
|
|
|
+ UserSetFlow writeOffRecord = userSetFlowRepo.findById(id).orElseThrow(new BusinessException("无核销记录"));
|
|
|
+ // 转vo
|
|
|
+ List<GoodsDTO> goodsDTOS = JSONObject.parseArray(writeOffRecord.getContent(), GoodsDTO.class);
|
|
|
+ Set<Long> ids = goodsDTOS.stream().map(GoodsDTO::getGoodsInfoId).collect(Collectors.toSet());
|
|
|
+ Map<Long, String> goodsMap = goodsInfoRepo.findAllById(ids)
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.toMap(GoodsInfo::getId, GoodsInfo::getName));
|
|
|
+ List<GoodsVO> goodsVOS = this.toGoodsVO(writeOffRecord.getContent(), goodsMap);
|
|
|
+
|
|
|
+ return WriteOffRecordDTO.builder()
|
|
|
+ .id(writeOffRecord.getId())
|
|
|
+ .createdAt(writeOffRecord.getCreatedAt())
|
|
|
+ .nickname(writeOffRecord.getUser().getNickname())
|
|
|
+ .writeOffNickname(writeOffRecord.getWriteOffUser().getNickname())
|
|
|
+ .goods(goodsVOS)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
}
|