GuaGuaAttendService.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace bizHd\promote\services;
  3. use bizHd\base\services\BaseService;
  4. use common\components\error;
  5. use Yii;
  6. class GuaGuaAttendService extends BaseService
  7. {
  8. public static $baseFile = '\bizHd\promote\classes\GuaGuaAttendClass';
  9. //确认还有没抽奖机会,如果还有则用户抽奖次数+1 ssh 2019.8.10
  10. public static function confirmToAddTime($card, $userId, $sjId)
  11. {
  12. $id = $card['id'];
  13. $attend = GuaGuaAttendService::getByCondition(['cardId' => $id, 'userId' => $userId], true);
  14. if (empty($card['drawCount']) == false && isset($attend->num) && $card['drawCount'] <= $attend->num) {
  15. error::instance()->setError('你的抽奖次数已经用完了');
  16. return false;
  17. }
  18. if (empty($attend)) {
  19. //增加抽奖记录
  20. $addTime = time();
  21. $date = date("Y-m-d H:i:s");
  22. $data = ['sjId' => $sjId, 'userId' => $userId, 'cardId' => $id, 'num' => 1, 'addTime' => $addTime, 'createTime' => $date];
  23. self::add($data);
  24. //参与抽奖人数+1
  25. $drawUser = $card['drawUser'] + 1;
  26. GuaGuaService::updateById($id, ['drawUser' => $drawUser]);
  27. return true;
  28. }
  29. $attend->num += 1;
  30. $attend->save();
  31. return true;
  32. }
  33. //获取用户剩余刮卡次数 ssh 2019.8.10
  34. public static function getRemainNum($card, $userId)
  35. {
  36. //-1表示一直可以刮
  37. if (empty($card['drawCount'])) {
  38. return -1;
  39. }
  40. $id = $card['id'];
  41. $attend = self::getByCondition(['cardId' => $id, 'userId' => $userId]);
  42. $usedNum = isset($attend['num']) ? $attend['num'] : 0;
  43. return ($card['drawCount'] - $usedNum);
  44. }
  45. }