BuyService.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.bofeng.service;
  2. import com.baomidou.mybatisplus.toolkit.IdWorker;
  3. import com.bofeng.dao.BuyMapper;
  4. import com.bofeng.dao.JmTuangouDao;
  5. import com.bofeng.entity.Buy;
  6. import com.bofeng.entity.JmTuangou;
  7. import org.joda.time.DateTime;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Propagation;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. @Service
  15. @Transactional(readOnly = true)
  16. public class BuyService {
  17. @Autowired
  18. private BuyMapper buyMapper;
  19. @Autowired
  20. private JmTuangouDao jmTuangouDao;
  21. @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  22. public int buyGroup(Buy buy) {
  23. Buy buy1 = buyMapper.selectBuyOne(buy.getUserId(), buy.getJmId());
  24. JmTuangou roleType = jmTuangouDao.getSelectOneRoleId(buy.getJmId());
  25. if (buy1 != null) {
  26. JmTuangou jmTuangou = jmTuangouDao.selectJmTuangouByJmId(buy1.getJmId());
  27. Buy buy2 = buyMapper.selectById(buy1.getBuyId());
  28. if (roleType.getRoleType() != 1) {
  29. if (jmTuangou.getTgMaxNum().intValue() >= jmTuangou.getCurrentNum().intValue() - buy2.getBuyCount().intValue() + buy.getBuyCount().intValue()) {
  30. //个人限额(指挥部的限额 和 管理员的限额)
  31. if (roleType.getTgItemNum().intValue() != 0 && roleType.getTgItemNum().intValue() < buy.getBuyCount().intValue()) {
  32. return 4;
  33. }
  34. buy.setBuyId(buy1.getBuyId());
  35. return buyMapper.updateById(buy);
  36. } else {
  37. return 3;//不允许更改团购数量
  38. }
  39. } else {
  40. //指挥部的更改
  41. //个人限额(指挥部的限额 和 管理员的限额)
  42. if (roleType.getTgItemNum().intValue() != 0 && roleType.getTgItemNum().intValue() < buy.getBuyCount().intValue()) {
  43. return 4;
  44. }
  45. buy.setBuyId(buy1.getBuyId());
  46. return buyMapper.updateById(buy);
  47. }
  48. }
  49. //1 是指挥部
  50. if (roleType.getRoleType() != 1) {
  51. //第一次购买团购数量大于最大上限数量不允许
  52. JmTuangou jmTuangou = jmTuangouDao.selectJmTuangouByJmId(buy.getJmId());
  53. if (jmTuangou.getTgMaxNum().intValue() < jmTuangou.getCurrentNum().intValue() + buy.getBuyCount().intValue()) {
  54. return 3;
  55. }
  56. }
  57. //个人限额(指挥部的限额 和 管理员的限额)
  58. if (roleType.getTgItemNum().intValue() != 0 && roleType.getTgItemNum().intValue() < buy.getBuyCount().intValue()) {
  59. return 4;
  60. }
  61. buy.setBuyId(IdWorker.getId());
  62. buy.setUserCreate(buy.getUserId());
  63. buy.setTimeCreate(new DateTime());
  64. buy.setBuyStatus(1);//在团
  65. return buyMapper.insert(buy);
  66. }
  67. @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  68. public List<JmTuangou> group(Long userId) {
  69. updateStatus();
  70. return buyMapper.group(userId);
  71. }
  72. @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  73. public List<Buy> MyGroup(Long userId) {
  74. updateStatus();
  75. return buyMapper.MyGroup(userId);
  76. }
  77. public Buy queryGroup(Long buyId) {
  78. return buyMapper.selectById(buyId);
  79. }
  80. public JmTuangou selectGroup(Long userId, Long jmId) {
  81. return buyMapper.selectGroup(userId, jmId);
  82. }
  83. @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  84. public int buyBack(Buy buy) {
  85. //在该套餐截止日期之前 并且是在团状态的才能撤单
  86. JmTuangou jmTuangou = buyMapper.selectGroup(buy.getUserId(), buy.getJmId());
  87. if (jmTuangou.getTgEndTime().getTime() > new DateTime().getMillis() && jmTuangou.getTgStatus() == 1) {
  88. Buy buy1 = buyMapper.selectBuyOne(buy.getUserId(), buy.getJmId());
  89. buy.setBuyId(buy1.getBuyId());
  90. buy.setBuyStatus(2);//撤单
  91. return buyMapper.updateById(buy);
  92. }
  93. //不允许更改
  94. return 2;
  95. }
  96. public Buy status(Buy buy) {
  97. return buyMapper.status(buy.getJmId(), buy.getUserId());
  98. }
  99. //截止日期状态的更改
  100. @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  101. public int updateStatus() {
  102. List<JmTuangou> jmTuangous = jmTuangouDao.selectAll();
  103. if (jmTuangous != null && jmTuangous.size() > 0) {
  104. for (JmTuangou list : jmTuangous) {
  105. //判断是指挥部还是管理员
  106. if (list.getRoleType() == 1) {
  107. if (list.getTgEndTime().getTime() < new DateTime().getMillis()) {
  108. jmTuangouDao.updateStatusByJmId(list.getJmId(), 2L);//发货
  109. }
  110. } else {
  111. // 团购数量 最小起订量
  112. Buy buy = buyMapper.selectCountNum(list.getJmId());
  113. //已经到截止时间
  114. if (list.getTgEndTime().getTime() < new DateTime().getMillis()) {
  115. if (buy != null) { //最小起订量
  116. if (list.getTgMinNum().longValue() > buy.getCount().longValue()) {
  117. jmTuangouDao.updateStatusByJmId(list.getJmId(), 4L);//取消
  118. } else {
  119. jmTuangouDao.updateStatusByJmId(list.getJmId(), 2L);//发货
  120. }
  121. } else {
  122. jmTuangouDao.updateStatusByJmId(list.getJmId(), 4L);//取消
  123. }
  124. } /*else {
  125. //团购数量已经过了最大起订量的 就直接发货
  126. if (buy != null) {
  127. if (list.getTgMaxNum().intValue() == buy.getCount().intValue()) {
  128. jmTuangouDao.updateStatusByJmId(list.getJmId(), 2L);//发货
  129. }
  130. }
  131. }*/
  132. }
  133. }
  134. }
  135. return 1;
  136. }
  137. }