|
@@ -1,11 +1,14 @@
|
|
|
package com.flyer.foster.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.flyer.exception.BusinessException;
|
|
|
import com.flyer.foster.dto.app.FosterRecordAddDTO;
|
|
|
import com.flyer.foster.entity.FosterRecord;
|
|
|
import com.flyer.foster.entity.Garden;
|
|
|
import com.flyer.foster.mapper.IFosterRecordMapper;
|
|
|
+import com.flyer.foster.pojo.LzTree;
|
|
|
import com.flyer.foster.pojo.StpAppUtil;
|
|
|
import com.flyer.foster.service.IFosterRecordService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -15,6 +18,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
@@ -38,20 +45,42 @@ public class FosterRecordServiceImpl extends ServiceImpl<IFosterRecordMapper, Fo
|
|
|
@Override
|
|
|
public boolean addFosterRecord(FosterRecordAddDTO addDTO) {
|
|
|
int tenantId = StpAppUtil.getTokenSession().getInt("tenantId");
|
|
|
- // 根据果园id找到配置的飞鸟看园果园id
|
|
|
- Garden garden = iGardenService.getById(addDTO.getGardenId());
|
|
|
- if (garden == null) {
|
|
|
- throw new BusinessException("无效的果园id");
|
|
|
- }
|
|
|
- // 飞鸟看园果园id
|
|
|
- Integer birdseyeGardenId = garden.getBirdseyeGardenId();
|
|
|
- // 从缓存中取出果园下的树,获取一个未曾分配的树id,
|
|
|
- Map<Object, Object> treeCache = redisUtil.hmget("birdseye:tree");
|
|
|
+ // 一个用户在同一个果园只能认领一棵树
|
|
|
+ FosterRecord fosterRecord = this.lambdaQuery()
|
|
|
+ .eq(FosterRecord::getAppUserId, addDTO.getAppUserId())
|
|
|
+ .eq(FosterRecord::getGardenId, addDTO.getGardenId())
|
|
|
+ .one();
|
|
|
+ if (fosterRecord == null) {
|
|
|
+ // 根据果园id找到配置的飞鸟看园果园id
|
|
|
+ Garden garden = iGardenService.getById(addDTO.getGardenId());
|
|
|
+ if (garden == null) {
|
|
|
+ throw new BusinessException("无效的果园id");
|
|
|
+ }
|
|
|
+ // 飞鸟看园果园id
|
|
|
+ Integer birdseyeGardenId = garden.getBirdseyeGardenId();
|
|
|
+ // 从缓存中取出果园下的树,获取一个未曾分配的树id,
|
|
|
+ Map<Object, Object> treeCache = redisUtil.hmget("birdseye:tree");
|
|
|
|
|
|
- FosterRecord fosterRecord = new FosterRecord();
|
|
|
- BeanUtil.copyProperties(addDTO, fosterRecord);
|
|
|
- fosterRecord.setTenantId(tenantId);
|
|
|
-// return this.save(fosterRecord);
|
|
|
+ Integer birdseyeTreeId = 0;
|
|
|
+ for (Map.Entry<Object, Object> map : treeCache.entrySet()) {
|
|
|
+ if (map.getKey().equals(birdseyeGardenId.toString())) {
|
|
|
+ List<LzTree> lzTreeList = JSON.parseArray(map.getValue().toString(), LzTree.class);
|
|
|
+ Collections.shuffle(lzTreeList);
|
|
|
+ // 获取一个飞鸟看园树id
|
|
|
+ birdseyeTreeId = lzTreeList.get(0).getId();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fosterRecord = new FosterRecord();
|
|
|
+ BeanUtil.copyProperties(addDTO, fosterRecord);
|
|
|
+ fosterRecord.setTenantId(tenantId);
|
|
|
+ fosterRecord.setBirdseyeTreeId(birdseyeTreeId);
|
|
|
+ fosterRecord.setCreatedBy("kelei");
|
|
|
+ fosterRecord.setCreatedTime(LocalDateTime.now());
|
|
|
+ fosterRecord.setUpdatedBy("kelei");
|
|
|
+ fosterRecord.setUpdatedTime(LocalDateTime.now());
|
|
|
+ this.save(fosterRecord);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
}
|