SystemUserLevelTaskServices.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\system\config;
  13. use qiniu\basic\BaseServices;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\exception\ValidateException;
  18. use think\facade\Log;
  19. class SystemUserLevelTaskServices extends BaseServices
  20. {
  21. protected $tasks = [
  22. 'expend' => ['name' => 'expend', 'title' => '消费满额', 'info' => '消费满{%num1%}', 'unit1' => '元'],
  23. 'achievement' => ['name' => 'achievement', 'title' => '业绩达标', 'info' => '业绩满{%num1%}', 'unit1' => '元'],
  24. 'spread_num' => ['name' => 'spread_num', 'title' => '推荐会员', 'info' => '邀请等级{%num1%}及以上的用户{%num2%}', 'unit1' => '级', 'unit2' => '人'],
  25. ];
  26. public function taskBase()
  27. {
  28. $tasks = $this->tasks;
  29. foreach ($tasks as $v) {
  30. $units = [];
  31. for ($i = 1; ; $i++) {
  32. $unit = 'unit' . $i;
  33. if (isset($v[$unit])) {
  34. $units[] = ['label' => $v[$unit], 'replace' => "{%num" . $i . "%}", 'key' => $i, 'num_name' => 'num' . $i];
  35. } else {
  36. break;
  37. }
  38. }
  39. $tasks[$v['name']]['units'] = $units;
  40. }
  41. return $tasks;
  42. }
  43. /**
  44. * @param array $task
  45. * @return array
  46. */
  47. public function checkTask(array $task = []): array
  48. {
  49. foreach ($task as &$v) {
  50. if (!$this->tasks[$v['name'] ?? '']) throw new ValidateException('任务不存在');
  51. if (!isset($v['units'])) {
  52. throw new ValidateException('请设置任务数量');
  53. }
  54. foreach ($v['units'] as $task_value) {
  55. if (($task_value['value'] ?? 0) < 0) throw new ValidateException('任务数量不能小于0');
  56. if (!isset($task_value['replace'])) throw new ValidateException('任务参数异常');
  57. $v['info'] = str_replace($task_value['replace'], ($task_value['value'] ?? 0) . ($task_value['label'] ?? ''), ($v['info'] ?? $this->tasks[$v['name']]['info']));
  58. }
  59. $v['must'] = $v['must'] ?? 0;
  60. }
  61. return $task;
  62. }
  63. /**
  64. * @param $id
  65. * @param $uid
  66. * @param int $start_time
  67. * @return bool
  68. * @throws DataNotFoundException
  69. * @throws DbException
  70. * @throws ModelNotFoundException
  71. */
  72. public function checkLevelTask($id, $uid, int $start_time = 0)
  73. {
  74. /** @var SystemUserLevelServices $services */
  75. $services = app()->make(SystemUserLevelServices::class);
  76. $level = $services->getLevel($id);
  77. $task = $level['task'];
  78. if (!count($task)) return false;
  79. $res = false;
  80. $must = [];
  81. $chose = [];
  82. foreach ($task as &$v) {
  83. if (!method_exists($this, $v['name'])) {
  84. Log::error('等级任务' . $v['name'] . '未完成');
  85. continue;
  86. }
  87. if (!isset($v['units'])) {
  88. Log::error('任务数量未设置');
  89. continue;
  90. }
  91. $nums = [];
  92. foreach ($v['units'] as $task_value) {
  93. if (($task_value['value'] ?? 0) < 0) throw new ValidateException('任务数量不能小于0');
  94. $nums['num' . (string)$task_value['key']] = $task_value['value'] ?? 0;
  95. }
  96. if (!$this->{'task_' . $v['name']}($uid, $start_time, $nums)) {
  97. if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 0];
  98. else $chose[] = ['name' => $v['name'], 'status' => 0];
  99. } else {
  100. if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 1];
  101. else $chose[] = ['name' => $v['name'], 'status' => 1];
  102. }
  103. }
  104. if ((count($must) == array_sum(array_column($must, 'status'))) && (array_sum(array_column($chose, 'status')) >= 1)) $res = true;
  105. return $res;
  106. }
  107. //Todo 任务方法
  108. /**
  109. * 消费金额
  110. * @param $uid
  111. * @param $start_time
  112. * @param array $num
  113. * @return bool
  114. */
  115. private function task_expend($uid, $start_time, array $num): bool
  116. {
  117. return true;
  118. }
  119. /**
  120. * 业绩
  121. * @param $uid
  122. * @param $start_time
  123. * @param array $num
  124. * @return bool
  125. */
  126. private function task_achievement($uid, $start_time, array $num): bool
  127. {
  128. return true;
  129. }
  130. /**
  131. * 推广人数
  132. * @param $uid
  133. * @param $start_time
  134. * @param array $num
  135. * @return bool
  136. */
  137. private function task_spread_num($uid, $start_time, array $num): bool
  138. {
  139. return true;
  140. }
  141. }