|
|
@@ -0,0 +1,156 @@
|
|
|
+package com.td.boss.game.complayercultivate.serivce;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateField;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.td.boss.common.pojo.ComConfigKeys;
|
|
|
+import com.td.boss.common.pojo.Result;
|
|
|
+import com.td.boss.common.service.ComConfigService;
|
|
|
+import com.td.boss.game.complayercultivate.pojo.ComPlayerCultivate;
|
|
|
+import com.td.boss.game.complayercultivate.pojo.ComPlayerCultivateDetail;
|
|
|
+import com.td.boss.game.complayercultivate.repository.ComPlayerCultivateDetailRepository;
|
|
|
+import com.td.boss.game.complayercultivate.repository.ComPlayerCultivateRepository;
|
|
|
+import com.td.boss.game.complayercultivate.vo.*;
|
|
|
+import com.td.boss.util.RedisData;
|
|
|
+import com.td.boss.util.RedisLock;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@Transactional
|
|
|
+public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ComConfigService comConfigService;
|
|
|
+ @Autowired
|
|
|
+ private ComPlayerCultivateRepository cultivateRepository;
|
|
|
+ @Autowired
|
|
|
+ private ComPlayerCultivateDetailRepository cultivateDetailRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisLock redisLock;
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public Result<?> zulin(String userId, String landId) {
|
|
|
+ long time = System.currentTimeMillis() + RedisData.getPlayerCultivateZuLinTimeout();
|
|
|
+ String _redisKey = "LOCK:COM_PLAYER_CULTIVATE_ZULIN:" + userId;
|
|
|
+ if (redisLock.lock(_redisKey, String.valueOf(time))) {
|
|
|
+ log.info("锁住key:" + _redisKey);
|
|
|
+ return ErrorResult("操作频繁!");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ComPlayerCultivateLandVo comPlayerCultivateLandVo = getLandById(landId);
|
|
|
+ if (comPlayerCultivateLandVo != null) {
|
|
|
+ return ErrorResult("土地不存在!");
|
|
|
+ }
|
|
|
+ if (cultivateRepository.findByUserId(userId) != null) {
|
|
|
+ return ErrorResult("该土地已被租赁!");
|
|
|
+ }
|
|
|
+ ComPlayerCultivate comPlayerCultivate = new ComPlayerCultivate();
|
|
|
+ comPlayerCultivate.setCreateTime(new Date());
|
|
|
+ comPlayerCultivate.setLandId(comPlayerCultivateLandVo.getLandId());
|
|
|
+ comPlayerCultivate.setLandType(comPlayerCultivateLandVo.getLandType());
|
|
|
+ comPlayerCultivate.setLandName(comPlayerCultivateLandVo.getLandName());
|
|
|
+ comPlayerCultivate.setLandLimit(comPlayerCultivateLandVo.getLandLimit());
|
|
|
+ comPlayerCultivate.setIsActivity(0);
|
|
|
+ comPlayerCultivate.setUserId(userId);
|
|
|
+ cultivateRepository.save(comPlayerCultivate);
|
|
|
+ return Result.of(comPlayerCultivate, true, "租赁成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ redisLock.unlock(_redisKey, String.valueOf(time));
|
|
|
+ }
|
|
|
+ return ErrorResult("租赁失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public Result<?> toufang(String userId, String landId, String seedId, Integer seedNum) {
|
|
|
+ long time = System.currentTimeMillis() + RedisData.getPlayerCultivateTouFangTimeout();
|
|
|
+ String _redisKey = "LOCK:COM_PLAYER_CULTIVATE_TOUFANG:" + userId;
|
|
|
+ if (redisLock.lock(_redisKey, String.valueOf(time))) {
|
|
|
+ return ErrorResult("操作频繁!");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ComPlayerCultivate comPlayerCultivate = cultivateRepository.findByUserId(userId);
|
|
|
+ if (comPlayerCultivate == null) {
|
|
|
+ return ErrorResult("该土地未被租赁!");
|
|
|
+ }
|
|
|
+ ComPlayerCultivateSeedVo seedVo = getSeedById(seedId);
|
|
|
+ if (seedVo == null) {
|
|
|
+ return ErrorResult("幼崽不存在!");
|
|
|
+ }
|
|
|
+ if (comPlayerCultivate != null && Integer.getInteger("1").equals(comPlayerCultivate.getIsActivity())) {
|
|
|
+ return ErrorResult("该土地正在使用中!");
|
|
|
+ }
|
|
|
+ //计算一下成熟的时间
|
|
|
+ DateTime maturityTime = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, seedVo.getMaturity());
|
|
|
+ if (maturityTime.after(comPlayerCultivate.getEndTime())) {
|
|
|
+ return ErrorResult("土地租赁时间不足!");
|
|
|
+ }
|
|
|
+ //判断一下种植最大数量
|
|
|
+ if (comPlayerCultivate.getLandLimit() > seedNum) {
|
|
|
+ return ErrorResult("最多可投放" + comPlayerCultivate.getLandLimit() + "!");
|
|
|
+ }
|
|
|
+ comPlayerCultivate.setIsActivity(1);
|
|
|
+ cultivateRepository.save(comPlayerCultivate);
|
|
|
+
|
|
|
+ ComPlayerCultivateDetail comPlayerCultivateDetailVo = new ComPlayerCultivateDetail();
|
|
|
+ comPlayerCultivateDetailVo.setCreateTime(new Date());
|
|
|
+ comPlayerCultivateDetailVo.setSeed(JSONUtil.toJsonStr(seedVo));
|
|
|
+ comPlayerCultivateDetailVo.setSeedId(seedVo.getSeedId());
|
|
|
+ comPlayerCultivateDetailVo.setSeedNum(seedNum);
|
|
|
+ comPlayerCultivateDetailVo.setIsMaturity(0);
|
|
|
+ comPlayerCultivateDetailVo.setMaturityTime(maturityTime);
|
|
|
+ comPlayerCultivateDetailVo.setIncomeNum(seedVo.getHarvest() * seedNum);
|
|
|
+ cultivateDetailRepository.save(comPlayerCultivateDetailVo);
|
|
|
+ return Result.of(null, true, "投放成功!");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ } finally {
|
|
|
+ redisLock.unlock(_redisKey, String.valueOf(time));
|
|
|
+ }
|
|
|
+ return ErrorResult("投放失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //todo 少一个增加币的机会和减少币的机会,和推荐明细
|
|
|
+ private void getTimes(){
|
|
|
+
|
|
|
+ }
|
|
|
+ //todo 少一个被偷的逻辑
|
|
|
+ //todo 少一个成熟的逻辑
|
|
|
+ @Override
|
|
|
+ public List<ComPlayerCultivateLandVo> getLand() {
|
|
|
+ return comConfigService.selectByKey(ComConfigKeys.cultivateLand, ComPlayerCultivateLandVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ComPlayerCultivateFoodVo> getFood() {
|
|
|
+ return comConfigService.selectByKey(ComConfigKeys.cultivateFood, ComPlayerCultivateFoodVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ComPlayerCultivateSeedVo> getSeed() {
|
|
|
+ return comConfigService.selectByKey(ComConfigKeys.cultivateSeed, ComPlayerCultivateSeedVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Result<?> ErrorResult(String msg) {
|
|
|
+ return Result.of(null, false, msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ComPlayerCultivateLandVo getLandById(String landId) {
|
|
|
+ return getLand().stream().filter(a -> a.getLandId().toString().equals(landId)).findFirst().orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ComPlayerCultivateSeedVo getSeedById(String seedId) {
|
|
|
+ return getSeed().stream().filter(a -> a.getSeedId().toString().equals(seedId)).findFirst().orElse(null);
|
|
|
+ }
|
|
|
+}
|