SystemUserLevelTaskServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ['name' => 'expend', 'title' => '消费满额', 'info' => '消费满{%num1%}', 'unit1' => '元'],
  23. ['name' => 'achievement', 'title' => '业绩达标', 'info' => '业绩满{%num1%}', 'unit1' => '元'],
  24. ['name' => 'spread_num', 'title' => '推荐会员', 'info' => '邀请等级{%num1%}及以上的用户{%num2%}', 'unit1' => '级', 'unit2' => '人'],
  25. ];
  26. public function taskBase()
  27. {
  28. return $this->tasks;
  29. }
  30. /**
  31. * @param array $task
  32. * @return array
  33. */
  34. public function checkTask(array $task = []): array
  35. {
  36. foreach ($task as &$v) {
  37. if (!$this->tasks[$v['name'] ?? '']) throw new ValidateException('任务不存在');
  38. if (($v['num1'] ?? 0) < 0) throw new ValidateException('任务数量不能小于0');
  39. $v['info'] = str_replace('{%num1%}', ($v['num1'] ?? 0) . $this->tasks[$v['name']]['unit1'], $this->tasks[$v['name']]['info']);
  40. for ($i = 2; ; $i++) {
  41. $unit = 'unit' . $i;
  42. $num = 'num' . $i;
  43. if (isset($this->tasks[$v['name']][$unit])) {
  44. if (($v[$num] ?? 0) < 0) throw new ValidateException('任务数量' . $i . '不能小于0');
  45. $v['info'] = str_replace('{%num' . $i . '%}', ($v[$num] ?? 0) . $this->tasks[$v['name']][$unit], $v['info']);
  46. } else {
  47. break;
  48. }
  49. }
  50. $v['must'] = $v['must'] ?? 0;
  51. }
  52. return $task;
  53. }
  54. /**
  55. * @param $id
  56. * @param $uid
  57. * @param int $start_time
  58. * @return bool
  59. * @throws DataNotFoundException
  60. * @throws DbException
  61. * @throws ModelNotFoundException
  62. */
  63. public function checkLevelTask($id, $uid, int $start_time = 0)
  64. {
  65. /** @var SystemUserLevelServices $services */
  66. $services = app()->make(SystemUserLevelServices::class);
  67. $level = $services->getLevel($id);
  68. $task = $level['task'];
  69. if (!count($task)) return false;
  70. $res = false;
  71. $must = [];
  72. $chose = [];
  73. foreach ($task as &$v) {
  74. if (!method_exists($this, $v['name'])) {
  75. Log::error('等级任务' . $v['name'] . '未完成');
  76. continue;
  77. }
  78. $nums = ['num1' => $v['num1']];
  79. for ($i = 2; ; $i++) {
  80. $num = 'num' . $i;
  81. if (isset($v[$num])) {
  82. $nums[$num] = $v[$num];
  83. } else {
  84. break;
  85. }
  86. }
  87. if (!$this->{'task_' . $v['name']}($uid, $start_time, $nums)) {
  88. if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 0];
  89. else $chose[] = ['name' => $v['name'], 'status' => 0];
  90. } else {
  91. if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 1];
  92. else $chose[] = ['name' => $v['name'], 'status' => 1];
  93. }
  94. }
  95. if ((count($must) == array_sum(array_column($must, 'status'))) && (array_sum(array_column($chose, 'status')) >= 1)) $res = true;
  96. return $res;
  97. }
  98. //Todo 任务方法
  99. /**
  100. * 消费金额
  101. * @param $uid
  102. * @param $start_time
  103. * @param array $num
  104. * @return bool
  105. */
  106. private function task_expend($uid, $start_time, array $num): bool
  107. {
  108. return true;
  109. }
  110. /**
  111. * 业绩
  112. * @param $uid
  113. * @param $start_time
  114. * @param array $num
  115. * @return bool
  116. */
  117. private function task_achievement($uid, $start_time, array $num): bool
  118. {
  119. return true;
  120. }
  121. /**
  122. * 推广人数
  123. * @param $uid
  124. * @param $start_time
  125. * @param array $num
  126. * @return bool
  127. */
  128. private function task_spread_num($uid, $start_time, array $num): bool
  129. {
  130. return true;
  131. }
  132. }