|
|
@@ -12,37 +12,40 @@ import com.td.boss.game.comconfigland.pojo.ComConfigLand;
|
|
|
import com.td.boss.game.comconfigland.service.ComConfigLandService;
|
|
|
import com.td.boss.game.comexplainland.service.ComExplainLandService;
|
|
|
import com.td.boss.game.comexplainland.vo.ComExplainLandVo;
|
|
|
+import com.td.boss.game.commallseed.service.ComMallSeedService;
|
|
|
+import com.td.boss.game.commallseed.vo.ComMallSeedVo;
|
|
|
import com.td.boss.game.complayercultivate.pojo.ComPlayerCultivate;
|
|
|
import com.td.boss.game.complayercultivate.repository.ComPlayerCultivateRepository;
|
|
|
import com.td.boss.game.complayercultivate.vo.ComPlayerCultivateSeedVo;
|
|
|
import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
|
|
|
import com.td.boss.game.complayerland.pojo.ComPlayerLand;
|
|
|
import com.td.boss.game.complayerland.service.ComPlayerLandService;
|
|
|
+import com.td.boss.game.complayerland.vo.ComPlayerLandAndPlantVo;
|
|
|
+import com.td.boss.game.complayerland.vo.ComPlayerLandSimpleVo;
|
|
|
+import com.td.boss.game.complayerland.vo.ComPlayerLandVo;
|
|
|
import com.td.boss.game.comsnbtran.vo.ComSnbTranVo;
|
|
|
import com.td.boss.game.comusers.service.ComUsersService;
|
|
|
import com.td.boss.game.comusers.vo.ComUsersVo;
|
|
|
-import com.td.boss.util.DappUtil;
|
|
|
-import com.td.boss.util.DoubleUtil;
|
|
|
-import com.td.boss.util.RedisData;
|
|
|
-import com.td.boss.util.RedisLock;
|
|
|
+import com.td.boss.util.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.persistence.criteria.Root;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@Transactional
|
|
|
public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ComMallSeedService comMallSeedService;
|
|
|
@Autowired
|
|
|
private ComPlayerLandService comPlayerLandService;
|
|
|
@Autowired
|
|
|
@@ -71,54 +74,102 @@ public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce
|
|
|
//根据土地config id查找,对应ConfigLandId ,
|
|
|
ComConfigLand comConfigLand = comConfigLandService.findById(configLandId);
|
|
|
//如果土地等于不能初始化,或者已经种植,不能进行购买
|
|
|
- if (comConfigLand == null || comConfigLand.getIsInit().equals(0) || comConfigLand.getIsPlant().equals(1)) {
|
|
|
+ if (comConfigLand == null || comConfigLand.getIsInit().equals(0)) {
|
|
|
return Result.of(null, false, ResultEnum.LAND_DATA_ERROR.getMessage(), ResultEnum.LAND_DATA_ERROR.getCode());
|
|
|
}
|
|
|
- ComPlayerLand comPlayerLand = comPlayerLandService.findByUserIdAndLandId(userId, configLandId);
|
|
|
if (ArrayUtil.contains(CultivateKeys.mallType, comConfigLand.getMallType()) == false) {
|
|
|
return Result.of(null, false, ResultEnum.LAND_DATA_ERROR.getMessage(), ResultEnum.LAND_DATA_ERROR.getCode());
|
|
|
}
|
|
|
String _redisKey = "LOCK:COM_PLAYER_CULTIVATE_LAND:" + userId;
|
|
|
long landTime = System.currentTimeMillis() + RedisData.getLandTimeout();
|
|
|
+ try {
|
|
|
+ if (!redisLock.lock(_redisKey, String.valueOf(landTime))) {
|
|
|
+ log.info("锁住key:" + _redisKey);
|
|
|
+ return Result.of(null, false, ResultEnum.REDIS_IS_LOCK.getMessage(), ResultEnum.REDIS_IS_LOCK.getCode());
|
|
|
+ }
|
|
|
+ ComPlayerLand comPlayerLand = comPlayerLandService.findByUserIdAndLandId(userId, configLandId);
|
|
|
+ if (comPlayerLand != null) {
|
|
|
+ throw new Exception(ResultEnum.LAND_IS_LEASE.getMessage());
|
|
|
+ }
|
|
|
+ Integer _getTimes = getTimes(userId);
|
|
|
+ if (_getTimes <= 0) {
|
|
|
+ throw new Exception(ResultEnum.LAND_NO_TIMES_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ //创建新土地
|
|
|
+ Date nowDate = new Date();
|
|
|
+ comPlayerLand = new ComPlayerLand();
|
|
|
+ comPlayerLand.setConfigLandId(comConfigLand.getId());
|
|
|
+ comPlayerLand.setUserId(userId);
|
|
|
+ comPlayerLand.setName(comConfigLand.getName());
|
|
|
+ //初始化一下未种植参数
|
|
|
+ comPlayerLand.setIsPlant(0);
|
|
|
+ comPlayerLand.setCreateTime(nowDate);
|
|
|
+ comPlayerLand.setPlantSteal(1);
|
|
|
+ comPlayerLand.setMallType(comConfigLand.getMallType());
|
|
|
+
|
|
|
+ comPlayerLand.setLandDescribe("养殖场租赁,每次租赁日期为1个月.");
|
|
|
+ comPlayerLand.setLeaseMultiple(CultivateKeys.amountLimit[comConfigLand.getMallType() - 1]); // 养殖场倍数,暂时设置最大值
|
|
|
+ comPlayerLand.setLeaseDate(4); // 租赁类型改成 4
|
|
|
+ //设置租赁日期,定义一个30天日期
|
|
|
+ comPlayerLand.setLeaseTime(com.td.boss.util.DateUtil.getOldDateAddDay(nowDate,30));
|
|
|
+ //租赁土地
|
|
|
+ comPlayerLand.setIsLease(1);
|
|
|
+ comPlayerLand.setUpdateTime(nowDate);
|
|
|
+ //记录一下租赁费用
|
|
|
+ comPlayerLand.setRentalExpenses(1.0d);
|
|
|
+ comPlayerLandService.AddComPlayerLand(comPlayerLand);
|
|
|
+
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put("LandInfo", getComPlayerLandAndPlantVo(comPlayerLand));
|
|
|
+ map.put("times", _getTimes - 1);
|
|
|
+ redisLock.unlock(_redisKey, String.valueOf(landTime));
|
|
|
+ return Result.of(map, true, "租赁成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ redisLock.unlock(_redisKey, String.valueOf(landTime));
|
|
|
+ return ErrorResult(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改某个土地 【牧场养殖渔场】
|
|
|
+ *
|
|
|
+ * @param userId
|
|
|
+ * @param configLandId
|
|
|
+ * @param multiple
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public Result<?> multipleLand(String userId, Integer configLandId, Integer multiple) {
|
|
|
+
|
|
|
+ //倍数不能为零
|
|
|
+ if (multiple <= 0) {
|
|
|
+ return Result.of(null, false, ResultEnum.LAND_MULTIPLE_ERROR.getMessage(), ResultEnum.LAND_MULTIPLE_ERROR.getCode());
|
|
|
+ }
|
|
|
+ ComPlayerLand comPlayerLand = comPlayerLandService.findByUserIdAndLandId(userId, configLandId);
|
|
|
+ if (comPlayerLand == null && ArrayUtil.contains(CultivateKeys.mallType, comPlayerLand.getMallType()) == false) {
|
|
|
+ return Result.of(null, false, ResultEnum.LAND_DATA_ERROR.getMessage(), ResultEnum.LAND_DATA_ERROR.getCode());
|
|
|
+ }
|
|
|
+ //不能超过最大值,倍数
|
|
|
+ if (CultivateKeys.amountLimit[comPlayerLand.getMallType() - 1] < multiple) {
|
|
|
+ return Result.of(null, false, "最大养殖量:" + CultivateKeys.amountLimit[comPlayerLand.getMallType() - 1], ResultEnum.LAND_MULTIPLE_CONFIG_ERROR.getCode());
|
|
|
+ }
|
|
|
+ String _redisKey = "LOCK:COM_PLAYER_CULTIVATE_LAND:" + userId;
|
|
|
+ long landTime = System.currentTimeMillis() + RedisData.getLandTimeout();
|
|
|
if (!redisLock.lock(_redisKey, String.valueOf(landTime))) {
|
|
|
log.info("锁住key:" + _redisKey);
|
|
|
return ErrorResult("操作频繁!");
|
|
|
}
|
|
|
try {
|
|
|
- if (getTimes(userId) <= 0) {
|
|
|
- return Result.of(null, false, ResultEnum.LAND_NO_TIMES_ERROR.getMessage(), ResultEnum.LAND_NO_TIMES_ERROR.getCode());
|
|
|
- }
|
|
|
- if (comPlayerLand == null) {
|
|
|
- //创建新土地
|
|
|
- comPlayerLand = new ComPlayerLand();
|
|
|
- comPlayerLand.setConfigLandId(comConfigLand.getId());
|
|
|
- comPlayerLand.setUserId(userId);
|
|
|
- comPlayerLand.setName(comConfigLand.getName());
|
|
|
- //todo 续租问题,如果用户续租,保留之前的状态
|
|
|
- //初始化一下未种植参数
|
|
|
- comPlayerLand.setIsPlant(0);
|
|
|
- comPlayerLand.setCreateTime(new Date());
|
|
|
- comPlayerLand.setPlantSteal(1);
|
|
|
- comPlayerLand.setMallType(comConfigLand.getMallType());
|
|
|
-
|
|
|
- comPlayerLand.setLandDescribe("每次租赁1个月");
|
|
|
- comPlayerLand.setLeaseMultiple(1);
|
|
|
- comPlayerLand.setLeaseDate(1);
|
|
|
- //设置租赁日期
|
|
|
- comPlayerLand.setLeaseTime(DateUtil.offset(new Date(), DateField.MONTH, 1));
|
|
|
- //租赁土地
|
|
|
- comPlayerLand.setIsLease(1);
|
|
|
- comPlayerLand.setUpdateTime(new Date());
|
|
|
- //记录一下租赁费用
|
|
|
- comPlayerLand.setRentalExpenses(1.0d);
|
|
|
- comPlayerLandService.AddComPlayerLand(comPlayerLand);
|
|
|
- }
|
|
|
- return Result.of(comPlayerLand, true, "租赁成功");
|
|
|
+ //修改
|
|
|
+ comPlayerLand.setLeaseMultiple(multiple);
|
|
|
+ comPlayerLandService.AddComPlayerLand(comPlayerLand);
|
|
|
+ redisLock.unlock(_redisKey, String.valueOf(landTime));
|
|
|
+ return Result.of(CopyUtil.copy(comPlayerLand, ComPlayerLandVo.class), true, "修改养殖场成功");
|
|
|
} catch (Exception e) {
|
|
|
- } finally {
|
|
|
redisLock.unlock(_redisKey, String.valueOf(landTime));
|
|
|
}
|
|
|
- return ErrorResult("租赁失败!");
|
|
|
+ return ErrorResult("修改养殖场失败!");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -131,11 +182,11 @@ public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce
|
|
|
public Integer getTimes(String userId) {
|
|
|
Integer totalcount = DappUtil.getChildrenBuyLandCount(userId);
|
|
|
long nowcount = comPlayerLandService.findAllByUserId(userId).stream()
|
|
|
- .filter(a -> a.getMallType() != null && ArrayUtil.contains(CultivateKeys.mallType,a.getMallType())).count();
|
|
|
+ .filter(a -> a.getMallType() != null && ArrayUtil.contains(CultivateKeys.mallType, a.getMallType())).count();
|
|
|
ComExplainLandVo comExplainLandVo = new ComExplainLandVo();
|
|
|
comExplainLandVo.setUserId(userId);
|
|
|
Result<List<ComExplainLandVo>> list = comExplainLandService.list(comExplainLandVo);
|
|
|
- long historycount = list.getData().stream().filter(a -> a.getMallType() != null && ArrayUtil.contains(CultivateKeys.mallType,a.getMallType())).count();
|
|
|
+ long historycount = list.getData().stream().filter(a -> a.getMallType() != null && ArrayUtil.contains(CultivateKeys.mallType, a.getMallType())).count();
|
|
|
return totalcount - Convert.toInt(nowcount) - Convert.toInt(historycount);
|
|
|
}
|
|
|
|
|
|
@@ -218,7 +269,9 @@ public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce
|
|
|
comPlayerCultivate.setActivtyTime(new Date());
|
|
|
comPlayerCultivate.setSnb(_needPrice);
|
|
|
cultivateRepository.save(comPlayerCultivate);
|
|
|
- return Result.of(null, true, "成功");
|
|
|
+
|
|
|
+
|
|
|
+ return Result.of(getComPlayerLandAndPlantVo(comPlayerLand), true, "成功");
|
|
|
} catch (Exception e) {
|
|
|
} finally {
|
|
|
redisLock.unlock(_redisKey, String.valueOf(time));
|
|
|
@@ -250,13 +303,14 @@ public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce
|
|
|
long l = (days - times) * 2;
|
|
|
//延期后的日期=成熟期+追加天数
|
|
|
//当天也算一天。过了0点就是新的一天
|
|
|
- return Convert.toInt(l, 0) + comPlayerLand.getPlantMature() - 1;
|
|
|
+ return comPlayerLand.getPlantMature();// Convert.toInt(l, 0) + comPlayerLand.getPlantMature() - 1;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 是否已经喂养
|
|
|
- * @param userId 用户编号
|
|
|
- * @param landType 土地类型 具体是养殖、渔场、放养
|
|
|
+ *
|
|
|
+ * @param userId 用户编号
|
|
|
+ * @param landType 土地类型 具体是养殖、渔场、放养
|
|
|
* @param configLandId
|
|
|
* @param begin
|
|
|
* @param end
|
|
|
@@ -281,4 +335,59 @@ public class ComPlayerCultivateSerivceImpl implements ComPlayerCultivateSerivce
|
|
|
private Result<?> ErrorResult(String msg) {
|
|
|
return Result.of(null, false, msg);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取一个养殖的对象
|
|
|
+ *
|
|
|
+ * @param comPlayerLand
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ComPlayerLandAndPlantVo getComPlayerLandAndPlantVo(ComPlayerLand comPlayerLand) {
|
|
|
+
|
|
|
+ ComPlayerLandAndPlantVo comPlayerLandAndPlantVo = CopyUtil.copy(comPlayerLand, ComPlayerLandAndPlantVo.class);
|
|
|
+ Long leaseDaysMill = comPlayerLandAndPlantVo.getLeaseTime().getTime() - com.td.boss.util.DateUtil.getNowDate().getTime();
|
|
|
+ Long leaseAllMill = comPlayerLandAndPlantVo.getLeaseTime().getTime() - comPlayerLandAndPlantVo.getCreateTime().getTime();
|
|
|
+ Integer allDays = com.td.boss.util.DateUtil.getDays(leaseAllMill).intValue() <= 0 ? 0 : com.td.boss.util.DateUtil.getDays(leaseAllMill).intValue();
|
|
|
+ comPlayerLandAndPlantVo.setLeaseDays(allDays);
|
|
|
+ Integer leaseDays = com.td.boss.util.DateUtil.getDays(leaseDaysMill).intValue() <= 0 ? 0 : com.td.boss.util.DateUtil.getDays(leaseDaysMill).intValue();
|
|
|
+ comPlayerLandAndPlantVo.setLeaseDaysRemaining(leaseDays);
|
|
|
+ comPlayerLandAndPlantVo.setLeaseDaysMill(leaseDaysMill);
|
|
|
+ if (comPlayerLandAndPlantVo.getIsPlant().equals(1)) {
|
|
|
+ ComMallSeedVo comMallSeedVo = comMallSeedService.findById(comPlayerLandAndPlantVo.getPlantId());
|
|
|
+ //todo [牧场养殖渔场需求] 如果是养殖类的,计算延期后的成熟期
|
|
|
+ if (ArrayUtil.contains(CultivateKeys.mallType, comPlayerLandAndPlantVo.getMallType())) {
|
|
|
+ Integer days = getHarvestDays(comPlayerLandAndPlantVo.getUserId(), comPlayerLandAndPlantVo.getConfigLandId());
|
|
|
+ comMallSeedVo.setMaturity(days);
|
|
|
+
|
|
|
+ //获取并设置今日喂养状态
|
|
|
+ Date begin = cn.hutool.core.date.DateUtil.beginOfDay(new Date());
|
|
|
+ Date end = cn.hutool.core.date.DateUtil.endOfDay(new Date());
|
|
|
+ boolean animalEat = isAnimalEat(comPlayerLandAndPlantVo.getUserId(), comPlayerLandAndPlantVo.getMallType(), comPlayerLandAndPlantVo.getConfigLandId(), begin, end);
|
|
|
+ comPlayerLandAndPlantVo.setAnimalEat(animalEat);
|
|
|
+ }
|
|
|
+ comPlayerLandAndPlantVo.setSeedInfo(comMallSeedVo);
|
|
|
+ Date _harvestTime = com.td.boss.util.DateUtil.getNowDateMinusDay(comMallSeedVo.getMaturity());
|
|
|
+ long diff = comPlayerLandAndPlantVo.getPlantStart().getTime() - _harvestTime.getTime();
|
|
|
+ comPlayerLandAndPlantVo.setPlantDaysMill(diff);
|
|
|
+ if (diff <= 0) {
|
|
|
+ //说明可以收获了
|
|
|
+ comPlayerLandAndPlantVo.setPlantDaysRemaining(0);
|
|
|
+ comPlayerLandAndPlantVo.setPlantHoursRemaining(0);
|
|
|
+ //todo [枯萎]
|
|
|
+ if (diff * -1 / CultivateKeys.dayLong > CultivateKeys.witheredDay) {
|
|
|
+ comPlayerLandAndPlantVo.setWithered(true);
|
|
|
+ }
|
|
|
+ //这里不触发灾害
|
|
|
+ } else {
|
|
|
+ comPlayerLandAndPlantVo.setPlantDaysRemaining(com.td.boss.util.DateUtil.getDays(diff).intValue());
|
|
|
+ comPlayerLandAndPlantVo.setPlantHoursRemaining(com.td.boss.util.DateUtil.getHours(diff).intValue());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //未种植时候,设置为0
|
|
|
+ comPlayerLandAndPlantVo.setPlantDaysRemaining(0);
|
|
|
+ comPlayerLandAndPlantVo.setPlantHoursRemaining(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ return comPlayerLandAndPlantVo;
|
|
|
+ }
|
|
|
}
|