ShopCouponClass.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace biz\shop\classes;
  3. use biz\base\classes\BaseClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\hb\classes\HbClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use common\components\noticeUtil;
  8. use common\components\util;
  9. class ShopCouponClass extends BaseClass
  10. {
  11. public static $baseFile = '\biz\shop\models\ShopCoupon';
  12. const STATUS_UN_USE = 1;
  13. const STATUS_USED = 2;
  14. const STATUS_EXPIRE = 3;
  15. const TAKE_UN = 1;
  16. const TAKE_HAS = 2;
  17. public static $defaultCoupon = [
  18. [
  19. 'name' => '红包',
  20. 'amount' => '5',
  21. 'miniCost' => '50',
  22. 'beginTime' => "+6 days",
  23. 'endTime' => "+7 days",
  24. ],
  25. [
  26. 'name' => '红包',
  27. 'amount' => '10',
  28. 'miniCost' => '50',
  29. 'beginTime' => "+3 days",
  30. 'endTime' => "+7 days",
  31. ],
  32. [
  33. 'name' => '红包',
  34. 'amount' => '15',
  35. 'miniCost' => '50',
  36. 'beginTime' => "+0 days",
  37. 'endTime' => "+7 days",
  38. ]
  39. ];
  40. //充值发红包 ssh 20211002
  41. public static function rechargeGiveCoupon($recharge)
  42. {
  43. $sjId = $recharge->sjId ?? 0;
  44. $shopId = $recharge->shopId ?? 0;
  45. $ghsId = $recharge->ghsId ?? 0;
  46. $hbAmount = $recharge->hbAmount ?? 0;
  47. $hbNum = $recharge->hbNum ?? 0;
  48. $valid = $recharge->valid ?? 0;
  49. $miniExpend = $recharge->miniExpend ?? 0;
  50. $amount = $recharge->amount ?? 0;
  51. $rechargeId = $recharge->id ?? 0;
  52. if ($hbNum <= 0) {
  53. noticeUtil::push("充值成功但红包没有发放成功,数量:{$hbNum} 金额:{$amount} rechargeId:{$rechargeId}", '15280215347');
  54. return false;
  55. }
  56. $ghs = GhsClass::getById($ghsId, true);
  57. if (empty($ghs)) {
  58. noticeUtil::push("充值成功但红包没有发放成功,没有找到供货商,数量:{$hbNum} 金额:{$amount} rechargeId:{$rechargeId}", '15280215347');
  59. return false;
  60. }
  61. $ghsName = $ghs->name ?? '';
  62. $ghsAvatar = $ghs->avatar ?? '';
  63. $ghsSjId = $ghs->sjId ?? 0;
  64. $ghsShopId = $ghs->shopId ?? 0;
  65. $customId = $ghs->customId ?? 0;
  66. $custom = CustomClass::getById($customId, true);
  67. if (empty($custom)) {
  68. noticeUtil::push("充值成功但红包没有发放成功,没有找到客户,数量:{$hbNum} 金额:{$amount} rechargeId:{$rechargeId}", '15280215347');
  69. return false;
  70. }
  71. $customName = $custom->name ?? '';
  72. $customPy = $custom->py ?? '';
  73. $customAvatar = $custom->avatar ?? '';
  74. $customSjId = $custom->sjId ?? 0;
  75. $customShopId = $custom->shopId ?? 0;
  76. for ($i = 0; $i < $hbNum; $i++) {
  77. $start = date("Y-m-d H:i:s");
  78. $end = date("Y-m-d H:i:s", time() + $valid * 86400);
  79. $coupon = [
  80. 'sjId' => $sjId,
  81. 'shopId' => $shopId,
  82. 'beginTime' => $start,
  83. 'endTime' => $end,
  84. 'name' => '红包',
  85. 'amount' => $hbAmount,
  86. 'miniCost' => $miniExpend,
  87. 'status' => HbClass::STATUS_UN_USE,
  88. 'ghsId' => $ghsId,
  89. 'ghsName' => $ghsName,
  90. 'ghsAvatar' => $ghsAvatar,
  91. 'ghsSjId' => $ghsSjId,
  92. 'ghsShopId' => $ghsShopId,
  93. 'customId' => $customId,
  94. 'customName' => $customName,
  95. 'customPy' => $customPy,
  96. 'customAvatar' => $customAvatar,
  97. 'customSjId' => $customSjId,
  98. 'customShopId' => $customShopId,
  99. 'from' => HbClass::FROM_RECHARGE,
  100. 'targetId' => $rechargeId,
  101. 'remark' => $start . "充值{$amount}元",
  102. ];
  103. HbClass::add($coupon);
  104. }
  105. }
  106. public static function groupBaseInfo($list)
  107. {
  108. return $list;
  109. }
  110. // 赠送3张红包
  111. public static function addDefaultCoupon($data)
  112. {
  113. $couponData = [];
  114. foreach (self::$defaultCoupon as $val) {
  115. $coupon = [
  116. 'sjId' => $data['sjId'],
  117. 'shopId' => $data['shopId'],
  118. 'beginTime' => date('Y-m-d 00:00:00', strtotime($val['beginTime'])),
  119. 'endTime' => date('Y-m-d 00:00:00', strtotime($val['endTime'])),
  120. 'name' => $val['name'],
  121. 'amount' => $val['amount'],
  122. 'miniCost' => $val['miniCost'],
  123. 'status' => self::STATUS_UN_USE,
  124. ];
  125. $couponData[] = $coupon;
  126. }
  127. return self::batchAdd($couponData);
  128. }
  129. public static function expire($coupon)
  130. {
  131. $coupon->status = ShopCouponClass::STATUS_EXPIRE;
  132. return $coupon->save();
  133. }
  134. public static function ghsSendHb($shopId, $ghsId, $amount)
  135. {
  136. $current = date("Y-m-d H:i:s");
  137. $list = self::getAllByCondition(['shopId' => $shopId, 'status' => ShopCouponClass::STATUS_UN_USE, 'ghsId' => $ghsId], null, '*', null, true);
  138. if (empty($list)) {
  139. return $list;
  140. }
  141. $arr = [];
  142. foreach ($list as $item) {
  143. if ($current >= $item->beginTime && $current < $item->endTime) {
  144. if ($item->miniCost <= $amount) {
  145. $arr[] = $item;
  146. }
  147. }
  148. }
  149. return $arr;
  150. }
  151. // 商家是否有可用红包
  152. public static function hasValidCoupon($shopId, $ghsId)
  153. {
  154. }
  155. public static function valid($coupon, $shopId)
  156. {
  157. if (empty($coupon)) {
  158. util::fail('没有红包');
  159. }
  160. if (isset($coupon->shopId) && $coupon->shopId != $shopId) {
  161. util::fail('不是您的红包');
  162. }
  163. if ($coupon->status != self::STATUS_UN_USE) {
  164. util::fail('此红包无法使用');
  165. }
  166. if (isset($coupon->take) && $coupon->take == self::TAKE_HAS) {
  167. util::fail('红包被占用');
  168. }
  169. $current = date("Y-m-d H:i:s");
  170. if ($coupon->endTime < $current) {
  171. util::fail('红包已过期');
  172. }
  173. return true;
  174. }
  175. //红包回滚 ssh 2021.5.18
  176. public static function rollback($coupon)
  177. {
  178. if (isset($coupon->take) && $coupon->take == self::TAKE_HAS) {
  179. $coupon->take = self::TAKE_UN;
  180. $coupon->save();
  181. }
  182. }
  183. }