SystemUserLevelTaskServices.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $name = 'task_' . $v['name'];
  84. if (!method_exists($this, $name)) {
  85. Log::error('等级任务' . $v['name'] . '未完成');
  86. continue;
  87. }
  88. if (!isset($v['units'])) {
  89. Log::error('任务数量未设置');
  90. continue;
  91. }
  92. $nums = [];
  93. foreach ($v['units'] as $task_value) {
  94. if (($task_value['value'] ?? 0) < 0) throw new ValidateException('任务数量不能小于0');
  95. $nums['num' . (string)$task_value['key']] = $task_value['value'] ?? 0;
  96. }
  97. if (!$this->{'task_' . $v['name']}($uid, $start_time, $nums)) {
  98. if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 0];
  99. else $chose[] = ['name' => $v['name'], 'status' => 0];
  100. } else {
  101. if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 1];
  102. else $chose[] = ['name' => $v['name'], 'status' => 1];
  103. }
  104. }
  105. if ((count($must) == array_sum(array_column($must, 'status'))) && (count($chose) ? (array_sum(array_column($chose, 'status')) >= 1) : true)) $res = true;
  106. return $res;
  107. }
  108. //Todo 任务方法
  109. /**
  110. * 消费金额
  111. * @param $uid
  112. * @param $start_time
  113. * @param array $num
  114. * @return bool
  115. */
  116. private function task_expend($uid, $start_time, array $num): bool
  117. {
  118. return true;
  119. }
  120. /**
  121. * 业绩
  122. * @param $uid
  123. * @param $start_time
  124. * @param array $num
  125. * @return bool
  126. */
  127. private function task_achievement($uid, $start_time, array $num): bool
  128. {
  129. return true;
  130. }
  131. /**
  132. * 推广人数
  133. * @param $uid
  134. * @param $start_time
  135. * @param array $num
  136. * @return bool
  137. */
  138. private function task_spread_num($uid, $start_time, array $num): bool
  139. {
  140. return true;
  141. }
  142. }