AgentLevelTaskServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. namespace app\services\agent;
  12. use app\dao\agent\AgentLevelTaskDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\user\UserServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\FormBuilder as Form;
  18. use think\exception\ValidateException;
  19. use think\facade\Route as Url;
  20. /**
  21. *
  22. * Class AgentLevelTaskServices
  23. * @package app\services\agent
  24. * @mixin AgentLevelTaskDao
  25. */
  26. class AgentLevelTaskServices extends BaseServices
  27. {
  28. /**
  29. * 任务类型
  30. * type 记录在数据库中用来区分任务
  31. * name 任务名 (任务名中的{$num}会自动替换成设置的数字 + 单位)
  32. * max_number 最大设定数值 0为不限定
  33. * min_number 最小设定数值
  34. * unit 单位
  35. * */
  36. protected $TaskType = [
  37. [
  38. 'type' => 1,
  39. 'method' => 'spread',
  40. 'name' => '邀请好友{$num}成为下级',
  41. 'real_name' => '邀请好友成为下级',
  42. 'max_number' => 0,
  43. 'min_number' => 1,
  44. 'unit' => '人',
  45. 'image' => '/uploads/system/agent_spread.png'
  46. ],
  47. [
  48. 'type' => 2,
  49. 'method' => 'consumePrice',
  50. 'name' => '自身消费满{$num}',
  51. 'real_name' => '自身消费金额',
  52. 'max_number' => 0,
  53. 'min_number' => 0,
  54. 'unit' => '元',
  55. 'image' => '/uploads/system/agent_self_order_price.png'
  56. ],
  57. [
  58. 'type' => 3,
  59. 'method' => 'consumeCount',
  60. 'name' => '自身消费满{$num}',
  61. 'real_name' => '自身消费单数',
  62. 'max_number' => 0,
  63. 'min_number' => 0,
  64. 'unit' => '单',
  65. 'image' => '/uploads/system/agent_self_order.png'
  66. ],
  67. [
  68. 'type' => 4,
  69. 'method' => 'spreadConsumePrice',
  70. 'name' => '下级消费满{$num}',
  71. 'real_name' => '下级消费金额',
  72. 'max_number' => 0,
  73. 'min_number' => 0,
  74. 'unit' => '元',
  75. 'image' => '/uploads/system/agent_spread_order_price.png'
  76. ],
  77. [
  78. 'type' => 5,
  79. 'method' => 'spreadConsumeCount',
  80. 'name' => '下级消费满{$num}',
  81. 'real_name' => '下级消费单数',
  82. 'max_number' => 0,
  83. 'min_number' => 0,
  84. 'unit' => '单',
  85. 'image' => '/uploads/system/agent_spread_order.png'
  86. ],
  87. ];
  88. /**
  89. * AgentLevelTaskServices constructor.
  90. * @param AgentLevelTaskDao $dao
  91. */
  92. public function __construct(AgentLevelTaskDao $dao)
  93. {
  94. $this->dao = $dao;
  95. }
  96. /**
  97. * 获取某一个任务信息
  98. * @param int $id
  99. * @param string $field
  100. * @param array $with
  101. * @return array|\think\Model|null
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function getLevelTaskInfo(int $id, string $field = '*', array $with = [])
  107. {
  108. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  109. }
  110. /**
  111. * 获取等级列表
  112. * @param array $where
  113. * @return array
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function getLevelTaskList(array $where)
  119. {
  120. $where['is_del'] = 0;
  121. [$page, $limit] = $this->getPageValue();
  122. $list = $this->dao->getTaskList($where, '*', [], $page, $limit);
  123. if ($list) {
  124. $allTyep = $this->getTaskTypeAll();
  125. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  126. foreach ($list as &$item) {
  127. $item['type_name'] = $allTyep[$item['type']]['real_name'] ?? '';
  128. }
  129. }
  130. $count = $this->dao->count($where);
  131. return compact('count', 'list');
  132. }
  133. /**
  134. * 添加等级任务表单
  135. * @param int $id
  136. * @return array
  137. * @throws \FormBuilder\Exception\FormBuilderException
  138. */
  139. public function createForm(int $level_id)
  140. {
  141. /** @var AgentLevelServices $levelServices */
  142. $levelServices = app()->make(AgentLevelServices::class);
  143. if (!$levelServices->getLevelInfo($level_id)) {
  144. throw new AdminException('选择的等级不存在,请返回重新选择');
  145. }
  146. $taskList = $this->getTaskTypeAll();
  147. $setOptionLabel = function () use ($taskList) {
  148. $menus = [];
  149. foreach ($taskList as $task) {
  150. $menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
  151. }
  152. return $menus;
  153. };
  154. $field[] = Form::hidden('level_id', $level_id);
  155. $field[] = Form::select('type', '任务类型')->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  156. $field[] = Form::input('name', '任务名称')->col(24);
  157. $field[] = Form::number('number', '限定数量', 0);
  158. $field[] = Form::textarea('desc', '任务描述');
  159. $field[] = Form::number('sort', '排序', 0)->min(0);
  160. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  161. return create_form('添加等级任务', $field, Url::buildUrl('/agent/level_task'), 'POST');
  162. }
  163. /**
  164. * 获取修改任务数据
  165. * @param int $id
  166. * @return array
  167. * @throws \FormBuilder\Exception\FormBuilderException
  168. */
  169. public function editForm(int $id)
  170. {
  171. $levelTaskInfo = $this->getLevelTaskInfo($id);
  172. if (!$levelTaskInfo)
  173. throw new AdminException('数据不存在');
  174. $field = [];
  175. $field[] = Form::hidden('id', $id);
  176. $taskList = $this->getTaskTypeAll();
  177. $setOptionLabel = function () use ($taskList) {
  178. $menus = [];
  179. foreach ($taskList as $task) {
  180. $menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
  181. }
  182. return $menus;
  183. };
  184. $field[] = Form::select('type', '任务类型', $levelTaskInfo['type'])->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  185. $field[] = Form::input('name', '任务名称', $levelTaskInfo['name']);
  186. $field[] = Form::number('number', '限定数量', $levelTaskInfo['number'])->min(0);
  187. $field[] = Form::textarea('desc', '任务描述', $levelTaskInfo['desc']);
  188. $field[] = Form::number('sort', '排序', $levelTaskInfo['sort'])->min(0);
  189. $field[] = Form::radio('status', '是否显示', $levelTaskInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  190. return create_form('编辑等级任务', $field, Url::buildUrl('/agent/level_task/' . $id), 'PUT');
  191. }
  192. /**
  193. * 获取任务类型
  194. * @return array[]
  195. */
  196. public function getTaskTypeAll()
  197. {
  198. return $this->TaskType;
  199. }
  200. /**
  201. * 获取某个任务
  202. * @param string $type 任务类型
  203. * @return array
  204. * */
  205. public static function getTaskType($type)
  206. {
  207. foreach (self::$TaskType as $item) {
  208. if ($item['type'] == $type) return $item;
  209. }
  210. }
  211. /**
  212. * 获取用户某一个分销等级任务情况
  213. * @param int $uid
  214. * @param int $level_id
  215. * @return array
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\DbException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. */
  220. public function getUserLevelTaskList(int $uid, int $level_id)
  221. {
  222. //商城分销是否开启
  223. if (!sys_config('brokerage_func_status')) {
  224. return [];
  225. }
  226. /** @var UserServices $userServices */
  227. $userServices = app()->make(UserServices::class);
  228. $user = $userServices->getUserInfo($uid);
  229. if (!$user) {
  230. throw new ValidateException('没有此用户');
  231. }
  232. /** @var AgentLevelServices $levelServices */
  233. $levelServices = app()->make(AgentLevelServices::class);
  234. $levelInfo = $levelServices->getLevelInfo($level_id);
  235. if (!$levelInfo) {//没有默认最低等级
  236. $list = $levelServices->getList(['is_del' => 0, 'status' => 1]);
  237. $levelInfo = $list[0] ?? [];
  238. $level_id = $levelInfo['id'] ?? 0;
  239. }
  240. $taskList = $this->dao->getTaskList(['level_id' => $level_id, 'is_del' => 0, 'status' => 1]);
  241. $speedAll = $speedCount = 0;
  242. if ($taskList) {
  243. $userLevel = [];
  244. if ($user['agent_level'] ?? 0) $userLevel = $levelServices->getLevelInfo($user['agent_level']);
  245. $allTyep = $this->getTaskTypeAll();
  246. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  247. $baseUrl = sys_config('site_url');
  248. foreach ($taskList as &$task) {
  249. $task['finish'] = 1;
  250. $task['task_type_title'] = '已完成';
  251. $task['speed'] = 100;
  252. $task['new_number'] = $task['number'];
  253. //当前等级之前的等级任务 全部为完成
  254. if (!$userLevel || $userLevel['grade'] < $levelInfo['grade']) {
  255. [$title, $num, $isComplete] = $this->checkLevelTaskFinish($uid, (int)$task['id']);
  256. if (!$isComplete) {
  257. $scale = in_array($task['type'], [2, 4]) ? 2 : 0;
  258. $task['finish'] = 0;
  259. $numdata = bcsub($task['number'], $num, $scale);
  260. $task['task_type_title'] = '还需' . str_replace('{$num}', $numdata . $allTyep[$task['type']]['unit'] ?? '', $title);
  261. $task['speed'] = bcmul((string)bcdiv((string)$num, (string)$task['number'], 2), '100', 0);
  262. $task['new_number'] = $num;
  263. $task['image'] = $baseUrl.$allTyep[$task['type']]['image'] ?? '';
  264. }
  265. }
  266. $speedCount = bcadd((string)$speedCount, $task['speed'], 0);
  267. }
  268. $speedAll = count($taskList) > 0 ? floatval(bcdiv($speedCount, (string)count($taskList), 2)) : 0;
  269. }
  270. return ['list' => $taskList, 'speedAll' => $speedAll];
  271. }
  272. /**
  273. * 检测某个任务完成情况
  274. * @param int $uid
  275. * @param int $task_id
  276. * @return array|false
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\DbException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. */
  281. public function checkLevelTaskFinish(int $uid, int $task_id, $levelTaskInfo = [])
  282. {
  283. if (!$levelTaskInfo) {
  284. $levelTaskInfo = $this->getLevelTaskInfo($task_id);
  285. }
  286. if (!$levelTaskInfo) return false;
  287. $allTyep = $this->getTaskTypeAll();
  288. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  289. $userNumber = 0;
  290. $msg = $allTyep[$levelTaskInfo['type']]['name'] ?? '';
  291. switch ($levelTaskInfo['type']) {
  292. case 1:
  293. /** @var UserServices $userServices */
  294. $userServices = app()->make(UserServices::class);
  295. $userNumber = $userServices->count(['spread_uid' => $uid]);
  296. break;
  297. case 2:
  298. /** @var StoreOrderServices $storeOrderServices */
  299. $storeOrderServices = app()->make(StoreOrderServices::class);
  300. $where = ['pid' => 0, 'uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  301. $userNumber = $storeOrderServices->sum($where, 'pay_price');
  302. break;
  303. case 3:
  304. /** @var StoreOrderServices $storeOrderServices */
  305. $storeOrderServices = app()->make(StoreOrderServices::class);
  306. $where = ['pid' => 0, 'uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  307. $userNumber = $storeOrderServices->count($where);
  308. break;
  309. case 4:
  310. /** @var UserServices $userServices */
  311. $userServices = app()->make(UserServices::class);
  312. $spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
  313. if ($spread_uids) {
  314. /** @var StoreOrderServices $storeOrderServices */
  315. $storeOrderServices = app()->make(StoreOrderServices::class);
  316. $where = ['pid' => 0, 'uid' => $spread_uids, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  317. $userNumber = $storeOrderServices->sum($where, 'pay_price');
  318. }
  319. break;
  320. case 5:
  321. /** @var UserServices $userServices */
  322. $userServices = app()->make(UserServices::class);
  323. $spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
  324. if ($spread_uids) {
  325. /** @var StoreOrderServices $storeOrderServices */
  326. $storeOrderServices = app()->make(StoreOrderServices::class);
  327. $where = ['pid' => 0, 'uid' => $spread_uids, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  328. $userNumber = $storeOrderServices->count($where);
  329. }
  330. break;
  331. default:
  332. return false;
  333. }
  334. $isComplete = false;
  335. if ($userNumber >= $levelTaskInfo['number']) {
  336. /** @var AgentLevelTaskRecordServices $agentLevelTaskRecordServices */
  337. $agentLevelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  338. $isComplete = true;
  339. if (!$agentLevelTaskRecordServices->get(['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id']])) {
  340. $data = ['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id'], 'add_time' => time()];
  341. $isComplete = $agentLevelTaskRecordServices->save($data);
  342. }
  343. }
  344. return [$msg, $userNumber, $isComplete];
  345. }
  346. /**
  347. * 检测等级任务
  348. * @param int $id
  349. * @param array $data
  350. * @return bool
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\DbException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. */
  355. public function checkTypeTask(int $id, array $data)
  356. {
  357. if (!$id && (!isset($data['level_id']) || !$data['level_id'])) {
  358. throw new ValidateException('缺少等级任务必要参数');
  359. }
  360. if ($id) {
  361. $task = $this->getLevelTaskInfo($id);
  362. if (!$task) {
  363. throw new ValidateException('编辑的任务不存在');
  364. }
  365. $data['level_id'] = $task['level_id'];
  366. }
  367. /** @var AgentLevelServices $agentLevelServices */
  368. $agentLevelServices = app()->make(AgentLevelServices::class);
  369. $levelInfo = $agentLevelServices->getLevelInfo($data['level_id']);
  370. if (!$levelInfo) {
  371. throw new ValidateException('所选择的分销等级不存在或者删除,请重新选择');
  372. }
  373. $task = $this->dao->getOne(['level_id' => $data['level_id'], 'type' => $data['type'], 'is_del' => 0]);
  374. if (($id && $task && $task['id'] != $id) || (!$id && $task)) {
  375. throw new ValidateException('该等级已存在此类型任务');
  376. }
  377. $taskList = $this->dao->getTypTaskList($data['type']);
  378. if ($taskList) {
  379. foreach ($taskList as $taskInfo) {
  380. if (is_null($taskInfo['grade'])) continue;
  381. if ($levelInfo['grade'] > $taskInfo['grade'] && $data['number'] <= $taskInfo['number']) {
  382. throw new ValidateException('不能小于低等级同类型任务限定数量');
  383. }
  384. if ($levelInfo['grade'] < $taskInfo['grade'] && $data['number'] >= $taskInfo['number']) {
  385. throw new ValidateException('不能大于高等级同类型任务限定数量');
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. }