HongbaoPlan.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qisse
  5. * Date: 2017/6/27
  6. * Time: 20:02
  7. */
  8. namespace app\core\model;
  9. use think\Cache;
  10. use think\Log;
  11. use think\Model;
  12. use tool\Common;
  13. class HongbaoPlan extends Model
  14. {
  15. protected $pk = 'hongbaoPlanID';
  16. protected $resultSetType = 'collection';
  17. protected $updateTime = 'updateTime';
  18. protected $createTime = 'addTime';
  19. protected $autoWriteTimestamp = true;
  20. const STATUS_ONLINE = 1;
  21. const STATUS_OFFLINE = 2;
  22. const STATUS_OVER = 3;
  23. const STATUSS = [
  24. self::STATUS_ONLINE=>'已启用',
  25. self::STATUS_OFFLINE=>'未启用',
  26. self::STATUS_OVER=>'已发放',
  27. ];
  28. const TYPE_USER_NEW = 1;
  29. const TYPE_TIMEING = 2;
  30. const TYPESS = [
  31. [
  32. 'typeID'=>self::TYPE_USER_NEW,
  33. 'name'=>'新手注册'
  34. ],
  35. [
  36. 'typeID'=>self::TYPE_TIMEING,
  37. 'name'=>'定时发送'
  38. ]
  39. ];
  40. public function hongbao() {
  41. return $this->belongsTo('Hongbao', 'hongbaoID');
  42. }
  43. public function getTypeList() {
  44. return self::TYPESS;
  45. }
  46. public function getSendTimeAttr($value) {
  47. return Common::timetodate($value);
  48. }
  49. public function getStatusTextAttr($value, $data) {
  50. return self::STATUSS[$data['status']];
  51. }
  52. public function getTypeNameAttr($value, $data) {
  53. foreach (self::TYPESS as $item) {
  54. if($item['typeID'] == $data['typeID']) {
  55. return $item['name'];
  56. }
  57. }
  58. return '未知';
  59. }
  60. //新手注册领红包
  61. public function sendUserOnRegister($user) {
  62. //当前新手注册只能为一个有效
  63. $hongbaoPlan = self::where([
  64. 'typeID'=>1
  65. ])->find();
  66. $hongbaoList = Hongbao::where([
  67. 'hongbaoID'=>['in', $hongbaoPlan['hongbaoIDS']]
  68. ])->select();
  69. $data = [];
  70. if($hongbaoList->isEmpty()) {
  71. return false;
  72. }
  73. foreach ($hongbaoList->toArray() as $k=>$item) {
  74. //如果当前红包不可用
  75. if($item['status'] != Hongbao::STATUS_ONLINE) {
  76. continue;
  77. }
  78. $userHongbao = [];
  79. $userHongbao['userID'] = $user['userID'];
  80. $userHongbao['hongbaoID'] = $item['hongbaoID'];
  81. if($item['effectTimeType'] == 1) {
  82. $userHongbao['beginTime'] = Common::datetotime(Common::timetodate(THINK_START_TIME, 0));
  83. $userHongbao['endTime'] = Common::datetotime(Common::timetodate(THINK_START_TIME, 0)) + $item['term'] * 86400;
  84. }
  85. else if($item['effectTimeType'] == 2) {
  86. $userHongbao['beginTime'] = $item['beginTime'];
  87. $userHongbao['endTime'] = $item['endTime'];
  88. }
  89. $userHongbao['getType'] = 1;
  90. $userHongbao['status'] = UserHongbao::STATUS_UNUSED;
  91. $userHongbao['addTime'] = THINK_START_TIME;
  92. $userHongbao['updateTime'] = THINK_START_TIME;
  93. $data[] = $userHongbao;
  94. }
  95. (new UserHongbao())->insertAll($data);
  96. return true;
  97. }
  98. //自动派发红包
  99. public function sendUserOnTiming() {
  100. $userSpan = 99;
  101. $hongbaoPlanList = self::with(['hongbao'])->where([
  102. 'typeID'=>2,
  103. 'sendTime'=>['lt', (int)THINK_START_TIME],
  104. 'status'=>HongbaoPlan::STATUS_ONLINE
  105. ])->select();
  106. foreach ($hongbaoPlanList->toArray() as $k=>$item) {
  107. //对于某一个红包,
  108. $userBeginID = Cache::get('HongbaoPlan'.$item['hongbaoPlanID'].'UserBeginID');
  109. $userBeginID = $userBeginID ? $userBeginID : 1;
  110. $userMaxID = User::max('userID');
  111. if($userBeginID > $userMaxID) {
  112. //已经全部发放完成
  113. self::update([
  114. 'status'=>HongbaoPlan::STATUS_OVER
  115. ], [
  116. 'hongbaoPlanID'=>$item['hongbaoPlanID']
  117. ]);
  118. Cache::rm('HongbaoPlan'.$item['hongbaoPlanID'].'UserBeginID');
  119. continue;
  120. }
  121. $userEndID = $userBeginID + $userSpan;
  122. $userList = User::where([
  123. 'isForged'=>0,
  124. 'userID'=>['between', [$userBeginID, $userEndID]]
  125. ])->select();
  126. $data = [];
  127. foreach ($userList as $user) {
  128. //给用户发红包啦
  129. if ($item['hongbao']['status'] != Hongbao::STATUS_ONLINE) {
  130. continue;
  131. }
  132. //判断用户是否重复领取
  133. $userHongbao = [];
  134. $userHongbao['userID'] = $user['userID'];
  135. $userHongbao['hongbaoID'] = $item['hongbaoID'];
  136. if ($item['hongbao']['effectTimeType'] == 1) {
  137. $userHongbao['beginTime'] = Common::datetotime(Common::timetodate(THINK_START_TIME, 0));
  138. $userHongbao['endTime'] = Common::datetotime(Common::timetodate(THINK_START_TIME, 0)) + $item['hongbao']['term'] * 86400;
  139. } else if ($item['hongbao']['effectTimeType'] == 2) {
  140. $userHongbao['beginTime'] = $item['hongbao']['beginTime'];
  141. $userHongbao['endTime'] = $item['hongbao']['endTime'];
  142. }
  143. $userHongbao['getType'] = 2;
  144. $userHongbao['status'] = UserHongbao::STATUS_UNUSED;
  145. $userHongbao['addTime'] = THINK_START_TIME;
  146. $userHongbao['updateTime'] = THINK_START_TIME;
  147. $userHongbao['hongbaoPlanID'] = $item['hongbaoPlanID'];
  148. $data[] = $userHongbao;
  149. }
  150. (new UserHongbao())->insertAll($data);
  151. Cache::set('HongbaoPlan'.$item['hongbaoPlanID'].'UserBeginID', $userEndID + 1);
  152. }
  153. }
  154. //给某个用户发红包
  155. public function sendUser($user, $hongbaoIDS, $operator, $note, $mobile) {
  156. $hongbaoList = Hongbao::where([
  157. 'hongbaoID'=>['in', $hongbaoIDS]
  158. ])->select();
  159. $data = [];
  160. if($hongbaoList->isEmpty()) {
  161. return false;
  162. }
  163. foreach ($hongbaoList->toArray() as $k=>$item) {
  164. //如果当前红包不可用
  165. if($item['status'] != Hongbao::STATUS_ONLINE) {
  166. continue;
  167. }
  168. if($user) {
  169. $userHongbao = [];
  170. $userHongbao['userID'] = $user['userID'];
  171. $userHongbao['hongbaoID'] = $item['hongbaoID'];
  172. if($item['effectTimeType'] == 1) {
  173. $userHongbao['beginTime'] = Common::datetotime(Common::timetodate(THINK_START_TIME, 0));
  174. $userHongbao['endTime'] = Common::datetotime(Common::timetodate(THINK_START_TIME, 0)) + $item['term'] * 86400;
  175. }
  176. else if($item['effectTimeType'] == 2) {
  177. $userHongbao['beginTime'] = $item['beginTime'];
  178. $userHongbao['endTime'] = $item['endTime'];
  179. }
  180. $userHongbao['getType'] = 1;
  181. $userHongbao['status'] = UserHongbao::STATUS_UNUSED;
  182. $userHongbao['addTime'] = THINK_START_TIME;
  183. $userHongbao['updateTime'] = THINK_START_TIME;
  184. $userHongbao['operator'] = $operator;
  185. $userHongbao['note'] = $note;
  186. $data[] = $userHongbao;
  187. }
  188. //发送一个短信
  189. Sms::create([
  190. 'mobile'=>$mobile,
  191. 'message'=>\app\core\service\SMS::message_setJiaxiYuebiao($user ? $user['trueName'] : '佳乾客户', '一张新年加息券'.$item['year'], 'http://t.cn/RQC0dnv'),
  192. 'note'=>'狗年约标',
  193. 'sendTime'=>THINK_START_TIME
  194. ]);
  195. }
  196. (new UserHongbao())->insertAll($data);
  197. return true;
  198. }
  199. }