|
@@ -0,0 +1,107 @@
|
|
|
|
|
+package com.izouma.jiashanxia.service;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.izouma.jiashanxia.domain.*;
|
|
|
|
|
+import com.izouma.jiashanxia.dto.GoodsDTO;
|
|
|
|
|
+import com.izouma.jiashanxia.dto.GoodsVO;
|
|
|
|
|
+import com.izouma.jiashanxia.dto.PageQuery;
|
|
|
|
|
+import com.izouma.jiashanxia.dto.WriteOffRecordDTO;
|
|
|
|
|
+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.repo.WriteOffRecordRepo;
|
|
|
|
|
+import com.izouma.jiashanxia.utils.JpaUtils;
|
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@AllArgsConstructor
|
|
|
|
|
+public class WriteOffRecordService {
|
|
|
|
|
+
|
|
|
|
|
+ private WriteOffRecordRepo writeOffRecordRepo;
|
|
|
|
|
+ private UserSetFlowRepo userSetFlowRepo;
|
|
|
|
|
+ private UserSetRepo userSetRepo;
|
|
|
|
|
+ private GoodsInfoRepo goodsInfoRepo;
|
|
|
|
|
+ private UserSetFlowService userSetFlowService;
|
|
|
|
|
+
|
|
|
|
|
+ public Page<WriteOffRecord> all(PageQuery pageQuery) {
|
|
|
|
|
+// return writeOffRecordRepo.findAll(JpaUtils.toSpecification(pageQuery, WriteOffRecord.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
|
|
+ return writeOffRecordRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
|
|
|
|
|
+ List<Predicate> and = JpaUtils.toPredicates(pageQuery, WriteOffRecord.class, root, criteriaQuery, criteriaBuilder);
|
|
|
|
|
+ if (StrUtil.isNotEmpty(pageQuery.getSearch())) {
|
|
|
|
|
+ List<Predicate> or = new ArrayList<>();
|
|
|
|
|
+ or.add(criteriaBuilder.like(root.get("user").get("nickname"), "%" + pageQuery.getSearch() + "%"));
|
|
|
|
|
+ or.add(criteriaBuilder.like(root.get("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 WriteOffRecord save(WriteOffRecord writeOffRecord) {
|
|
|
|
|
+ if (writeOffRecord.getUserId().equals(writeOffRecord.getWriteOffUserId())) {
|
|
|
|
|
+ throw new BusinessException("不可自己核销自己");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 用户已有套餐
|
|
|
|
|
+ Map<Long, UserSet> userSetMap = userSetRepo.findAllByUserId(writeOffRecord.getUserId())
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .collect(Collectors.toMap(UserSet::getGoodsInfoId, userSet -> userSet));
|
|
|
|
|
+
|
|
|
|
|
+ List<GoodsDTO> goodsDTOS = JSONObject.parseArray(writeOffRecord.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("核销数量大于套餐数量");
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 保存套餐流水
|
|
|
|
|
+ userSetFlowRepo.save(
|
|
|
|
|
+ UserSetFlow.builder()
|
|
|
|
|
+ .userId(writeOffRecord.getUserId())
|
|
|
|
|
+ .content(writeOffRecord.getContent())
|
|
|
|
|
+ .type(FlowType.WRITE_OFF)
|
|
|
|
|
+ .build());
|
|
|
|
|
+
|
|
|
|
|
+ return writeOffRecordRepo.save(writeOffRecord);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public WriteOffRecordDTO getDTO(Long id) {
|
|
|
|
|
+ WriteOffRecord writeOffRecord = writeOffRecordRepo.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 = userSetFlowService.toGoodsVO(writeOffRecord.getContent(), goodsMap);
|
|
|
|
|
+
|
|
|
|
|
+ return WriteOffRecordDTO.builder()
|
|
|
|
|
+ .id(writeOffRecord.getId())
|
|
|
|
|
+ .createdAt(writeOffRecord.getCreatedAt())
|
|
|
|
|
+ .nickname(writeOffRecord.getUser().getNickname())
|
|
|
|
|
+ .writeOffNickname(writeOffRecord.getWriteOffUser().getNickname())
|
|
|
|
|
+ .goods(goodsVOS)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|