UserGiftQuotaServices.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\dao\user\UserGiftQuotaDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use think\facade\Log;
  17. /**
  18. * 礼包额度服务
  19. * Class UserGiftQuotaServices
  20. * @package app\services\user
  21. */
  22. class UserGiftQuotaServices extends \app\services\BaseServices
  23. {
  24. /**
  25. * 礼包额度记录类型
  26. * @var array
  27. */
  28. protected $incomeData = [
  29. 'order_gift_quota' => [
  30. 'title' => '购买礼包赠送额度',
  31. 'type' => 'order_gift_quota',
  32. 'mark' => '购买礼包商品消费{%pay_price%}元,赠送额度{%number%}',
  33. 'status' => 1,
  34. 'pm' => 1
  35. ],
  36. 'use_direct_referral_quota' => [
  37. 'title' => '获得礼包直推奖扣除额度',
  38. 'type' => 'use_direct_referral_quota',
  39. 'mark' => '{%nickname%}购买礼包商品消费{%pay_price%}元,获得直推奖{%number%}',
  40. 'status' => 1,
  41. 'pm' => 0
  42. ],
  43. 'use_spot_quota' => [
  44. 'title' => '获得礼包见点奖扣除额度',
  45. 'type' => 'use_spot_quota',
  46. 'mark' => '{%nickname%}购买礼包商品消费{%pay_price%}元,获得见点奖{%number%}',
  47. 'status' => 1,
  48. 'pm' => 0
  49. ],
  50. 'freeze_quotas' => [
  51. 'title' => '额度不足冻结',
  52. 'type' => 'freeze_quotas',
  53. 'mark' => '{%nickname%}购买礼包商品消费{%pay_price%}元,额度不足,{%number%}元被冻结',
  54. 'status' => 1,
  55. 'pm' => 0
  56. ],
  57. 'unfreeze_and_deduct_quotas' => [
  58. 'title' => '解冻并扣除额度',
  59. 'type' => 'unfreeze_and_deduct_quotas',
  60. 'mark' => '获得新额度,解冻{%number%}元并扣除',
  61. 'status' => 1,
  62. 'pm' => 0
  63. ],
  64. ];
  65. /**
  66. * UserGiftQuotaServices constructor.
  67. * @param UserGiftQuotaDao $dao
  68. */
  69. public function __construct(UserGiftQuotaDao $dao)
  70. {
  71. $this->dao = $dao;
  72. }
  73. /**
  74. * 写入礼包额度记录
  75. * @param string $type 写入类型
  76. * @param int $uid
  77. * @param int|string|array $number
  78. * @param int|string $balance
  79. * @param $linkId
  80. * @return bool|mixed
  81. */
  82. public function income(string $type, int $uid, $number, $balance, $linkId)
  83. {
  84. $data = $this->incomeData[$type] ?? null;
  85. if (!$data) {
  86. return true;
  87. }
  88. $data['uid'] = $uid;
  89. $data['balance'] = $balance ?? 0;
  90. $data['link_id'] = $linkId;
  91. if (is_array($number)) {
  92. $key = array_keys($number);
  93. $key = array_map(function ($item) {
  94. return '{%' . $item . '%}';
  95. }, $key);
  96. $value = array_values($number);
  97. $data['number'] = $number['number'] ?? 0;
  98. $data['mark'] = str_replace($key, $value, $data['mark']);
  99. } else {
  100. $data['number'] = $number;
  101. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  102. }
  103. $data['add_time'] = time();
  104. return $this->dao->save($data);
  105. }
  106. }