AgentManageServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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\services\BaseServices;
  13. use app\services\order\StoreOrderServices;
  14. use app\services\order\StoreOrderStatusServices;
  15. use app\services\other\QrcodeServices;
  16. use app\services\system\attachment\SystemAttachmentServices;
  17. use app\services\user\UserBrokerageFrozenServices;
  18. use app\services\user\UserBrokerageServices;
  19. use app\services\user\UserExtractServices;
  20. use app\services\user\UserServices;
  21. use crmeb\exceptions\AdminException;
  22. use crmeb\services\app\MiniProgramService;
  23. use app\services\other\UploadService;
  24. /**
  25. *
  26. * Class AgentManageServices
  27. * @package app\services\agent
  28. */
  29. class AgentManageServices extends BaseServices
  30. {
  31. /**
  32. * @param array $where
  33. * @param bool $is_page
  34. * @return array
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function agentSystemPage(array $where, $is_page = true)
  40. {
  41. /** @var UserServices $userServices */
  42. $userServices = app()->make(UserServices::class);
  43. $data = $userServices->getAgentUserList($where, '*', $is_page);
  44. /** @var UserBrokerageServices $frozenPrices */
  45. $frozenPrices = app()->make(UserBrokerageServices::class);
  46. /** @var StoreOrderServices $orderServices */
  47. $orderServices = app()->make(StoreOrderServices::class);
  48. foreach ($data['list'] as &$item) {
  49. $item['headimgurl'] = $item['avatar'];
  50. $item['extract_count_price'] = $item['extract'][0]['extract_count_price'] ?? 0;
  51. $item['extract_count_num'] = $item['extract'][0]['extract_count_num'] ?? 0;
  52. $item['spread_name'] = $item['spreadUser']['nickname'] ?? '';
  53. if ($item['spread_name']) {
  54. $item['spread_name'] .= '/' . $item['spread_uid'];
  55. } else {
  56. $item['spread_name'] = '--';
  57. }
  58. $item['spread_count'] = $item['spreadCount'][0]['spread_count'] ?? 0;
  59. $item['order_price'] = $item['order'][0]['order_price'] ?? 0;
  60. $item['order_count'] = $item['order'][0]['order_count'] ?? 0;
  61. $item['broken_commission'] = $frozenPrices->getUserFrozenPrice($item['uid']);
  62. if ($item['broken_commission'] < 0)
  63. $item['broken_commission'] = 0;
  64. $item['new_money'] = $item['bill'][0]['brokerage_money'] ?? 0;
  65. if ($item['brokerage_price'] > $item['broken_commission'])
  66. $item['new_money'] = bcsub((string)$item['brokerage_price'], (string)$item['broken_commission'], 2);
  67. else
  68. $item['new_money'] = 0;
  69. $item['brokerage_money'] = bcadd((string)$item['brokerage_price'], (string)$item['extract_count_price'], 2);
  70. unset($item['extract'], $item['order'], $item['bill'], $item['spreadUser'], $item['spreadCount']);
  71. if (strpos($item['headimgurl'], '/statics/system_images/') !== false) {
  72. $item['headimgurl'] = set_file_url($item['headimgurl']);
  73. }
  74. $item['spread_order'] = $orderServices->get(
  75. [['spread_uid', '=', $item['uid']], ['paid', '=', 1], ['refund_status', '=', 0], ['pid', '>=', 0]],
  76. ['sum(pay_price) as order_price', 'count(id) as order_count']
  77. );
  78. }
  79. return $data;
  80. }
  81. /**
  82. * 分销头部信息
  83. * @param $where
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getSpreadBadge($where)
  90. {
  91. /** @var UserServices $userServices */
  92. $userServices = app()->make(UserServices::class);
  93. $uids = $userServices->getAgentUserIds($where);
  94. //分销员人数
  95. $data['uids'] = $uids;
  96. $data['sum_count'] = count($uids);
  97. //发展会员人数以及用户的可提现金额
  98. $data['spread_sum'] = 0;
  99. $data['extract_price'] = 0;
  100. if ($data['sum_count']) {
  101. //发展会员人数
  102. $data['spread_sum'] = $userServices->getCount([['spread_uid', 'in', $uids]]);
  103. //获取某个用户可提现金额
  104. /** @var UserBrokerageFrozenServices $frozenPrices */
  105. $frozenPrices = app()->make(UserBrokerageFrozenServices::class);
  106. $data['extract_price'] = bcsub((string)$userServices->getSumBrokerage(['uid' => $uids]), $frozenPrices->getSumFrozenBrokerage($uids), 2);
  107. }
  108. //订单总数,订单金额,提现次数
  109. $data['order_count'] = 0;
  110. $data['pay_price'] = 0;
  111. $data['extract_count'] = 0;
  112. if ($data['sum_count']) {
  113. /** @var StoreOrderServices $storeOrder */
  114. $storeOrder = app()->make(StoreOrderServices::class);
  115. //订单总数
  116. $data['order_count'] = $storeOrder->getCount([['uid', 'in', $uids], ['paid', '=', 1], ['refund_status', '=', 0], ['pid', '<=', 0]]);
  117. //订单金额
  118. $data['pay_price'] = $storeOrder->sum([['uid', 'in', $uids], ['paid', '=', 1], ['refund_status', '=', 0], ['pid', '<=', 0]], 'pay_price');
  119. //提现次数
  120. $data['extract_count'] = app()->make(UserExtractServices::class)->getCount([['uid', 'in', $uids], ['status', '=', 1]]);
  121. }
  122. return [
  123. [
  124. 'name' => '分销员人数(人)',
  125. 'count' => $data['sum_count'],
  126. 'className' => 'iconfaqirenshu',
  127. 'col' => 4,
  128. ],
  129. [
  130. 'name' => '推广用户数量(人)',
  131. 'count' => $data['spread_sum'],
  132. 'className' => 'icontuiguangrenshu',
  133. 'col' => 4,
  134. ],
  135. [
  136. 'name' => '订单数(单)',
  137. 'count' => $data['order_count'],
  138. 'className' => 'icondingdanliang',
  139. 'col' => 4,
  140. ],
  141. [
  142. 'name' => '订单金额(元)',
  143. 'count' => $data['pay_price'],
  144. 'className' => 'icondingdanjine',
  145. 'col' => 4,
  146. ],
  147. [
  148. 'name' => '提现次数(次)',
  149. 'count' => $data['extract_count'],
  150. 'className' => 'iconzhichujine',
  151. 'col' => 4,
  152. ],
  153. [
  154. 'name' => '未提现金额(元)',
  155. 'count' => $data['extract_price'],
  156. 'className' => 'iconjiaoyijine',
  157. 'col' => 4,
  158. ],
  159. ];
  160. }
  161. /**
  162. * 推广人列表
  163. * @param array $where
  164. * @return mixed
  165. */
  166. public function getStairList(array $where)
  167. {
  168. /** @var UserServices $userServices */
  169. $userServices = app()->make(UserServices::class);
  170. $data = $userServices->getSairList($where);
  171. foreach ($data['list'] as &$item) {
  172. $item['spread_count'] = $item['spreadCount'][0]['spread_count'] ?? 0;
  173. $item['order_count'] = $item['order'][0]['order_count'] ?? 0;
  174. $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
  175. $item['add_time'] = $item['spread_time'] ? date("Y-m-d H:i:s", $item['spread_time']) : '';
  176. }
  177. return $data;
  178. }
  179. //TODO 废弃
  180. /**
  181. * 推广人头部信息
  182. * @param array $where
  183. * @return array[]
  184. */
  185. public function getSairBadge(array $where)
  186. {
  187. /** @var UserServices $userServices */
  188. $userServices = app()->make(UserServices::class);
  189. $data['number'] = $userServices->getSairCount($where);
  190. $where['type'] = 1;
  191. $data['one_number'] = $userServices->getSairCount($where);
  192. $where['type'] = 2;
  193. $data['two_number'] = $userServices->getSairCount($where);
  194. $col = $data['two_number'] > 0 ? 4 : 6;
  195. return [
  196. [
  197. 'name' => '总人数(人)',
  198. 'count' => $data['number'],
  199. 'col' => $col,
  200. ],
  201. [
  202. 'name' => '一级人数(人)',
  203. 'count' => $data['one_number'],
  204. 'col' => $col,
  205. ],
  206. [
  207. 'name' => '二级人数(人)',
  208. 'count' => $data['two_number'],
  209. 'col' => $col,
  210. ],
  211. ];
  212. }
  213. /**
  214. * 推广订单
  215. * @param int $uid
  216. * @param array $where
  217. * @return array
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. public function getStairOrderList(int $uid, array $where)
  223. {
  224. /** @var UserServices $userServices */
  225. $userServices = app()->make(UserServices::class);
  226. $userInfo = $userServices->getUserInfo($uid);
  227. if (!$userInfo) {
  228. return ['count' => 0, 'list' => []];
  229. }
  230. /** @var StoreOrderServices $storeOrder */
  231. $storeOrder = app()->make(StoreOrderServices::class);
  232. $data = $storeOrder->getUserStairOrderList($uid, $where);
  233. if ($data['list']) {
  234. $uids = array_unique(array_column($data['list'], 'uid'));
  235. $userList = [];
  236. if ($uids) {
  237. $userList = $userServices->getColumn([['uid', 'IN', $uids]], 'nickname,phone,avatar,real_name', 'uid');
  238. }
  239. $orderIds = array_column($data['list'], 'id');
  240. $orderChangTimes = [];
  241. if ($orderIds) {
  242. /** @var StoreOrderStatusServices $storeOrderStatus */
  243. $storeOrderStatus = app()->make(StoreOrderStatusServices::class);
  244. $orderChangTimes = $storeOrderStatus->getColumn([['oid', 'IN', $orderIds], ['change_type', '=', 'user_take_delivery']], 'change_time', 'oid');
  245. }
  246. foreach ($data['list'] as &$item) {
  247. $user = $userList[$item['uid']] ?? [];
  248. $item['user_info'] = '';
  249. $item['avatar'] = '';
  250. if (count($user)) {
  251. $item['user_info'] = $user['nickname'] . '|' . ($user['phone'] ? $user['phone'] . '|' : '') . $user['real_name'];
  252. $item['avatar'] = $user['avatar'];
  253. }
  254. $item['brokerage_price'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
  255. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  256. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  257. $item['take_time'] = ($change_time = $orderChangTimes[$item['id']] ?? '') ? date('Y-m-d H:i:s', $change_time) : '暂无';
  258. }
  259. }
  260. return $data;
  261. }
  262. /**
  263. * 获取永久二维码
  264. * @param $type
  265. * @param $id
  266. * @return array|false|\PDOStatement|string|\think\Model
  267. */
  268. public function wechatCode(int $uid)
  269. {
  270. /** @var QrcodeServices $qrcode */
  271. $qrcode = app()->make(QrcodeServices::class);
  272. $code = $qrcode->getForeverQrcode('spread', $uid);
  273. if (!$code['ticket']) throw new AdminException('永久二维码获取错误');
  274. return $code;
  275. }
  276. /**
  277. * TODO 查看小程序推广二维码
  278. * @param string $uid
  279. */
  280. public function lookXcxCode(int $uid)
  281. {
  282. if (!sys_config('routine_appId') || !sys_config('routine_appsecret')) {
  283. throw new AdminException('请先配置小程序appid、appSecret等参数');
  284. }
  285. $userInfo = app()->make(UserServices::class)->getUserInfo($uid);
  286. if (!$userInfo) {
  287. throw new AdminException('数据不存在');
  288. }
  289. $name = $userInfo['uid'] . '_' . $userInfo['is_promoter'] . '_user.jpg';
  290. /** @var SystemAttachmentServices $systemAttachmentModel */
  291. $systemAttachmentModel = app()->make(SystemAttachmentServices::class);
  292. $imageInfo = $systemAttachmentModel->getInfo(['name' => $name]);
  293. if (!$imageInfo) {
  294. /** @var QrcodeServices $qrcode */
  295. $qrcode = app()->make(QrcodeServices::class);
  296. $resForever = $qrcode->qrCodeForever($uid, 'spread_routine');
  297. if ($resForever) {
  298. $resCode = MiniProgramService::appCodeUnlimitService($resForever->id, '', 280);
  299. $res = ['res' => $resCode, 'id' => $resForever->id];
  300. } else {
  301. $res = false;
  302. }
  303. if (!$res) throw new AdminException('二维码生成失败');
  304. $upload = UploadService::init();
  305. if ($upload->to('routine/spread/code')->setAuthThumb(false)->stream((string)$res['res'], $name) === false) {
  306. return $upload->getError();
  307. }
  308. $imageInfo = $upload->getUploadInfo();
  309. $imageInfo['image_type'] = sys_config('upload_type', 1);
  310. $systemAttachmentModel->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  311. $qrcode->update($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  312. $urlCode = $imageInfo['dir'];
  313. } else $urlCode = $imageInfo['att_dir'];
  314. return ['code_src' => $urlCode];
  315. }
  316. /**
  317. * 查看H5推广二维码
  318. * @param string $uid
  319. * @return mixed|string
  320. */
  321. public function lookH5Code(int $uid)
  322. {
  323. $userInfo = app()->make(UserServices::class)->getUserInfo($uid);
  324. if (!$userInfo) {
  325. throw new AdminException('数据不存在');
  326. }
  327. $name = $userInfo['uid'] . '_h5_' . $userInfo['is_promoter'] . '_user.jpg';
  328. /** @var SystemAttachmentServices $systemAttachmentModel */
  329. $systemAttachmentModel = app()->make(SystemAttachmentServices::class);
  330. $imageInfo = $systemAttachmentModel->getInfo(['name' => $name]);
  331. if (!$imageInfo) {
  332. /** @var QrcodeServices $qrcodeService */
  333. $qrcodeService = app()->make(QrcodeServices::class);
  334. $urlCode = $qrcodeService->getWechatQrcodePathAgent($uid . '_h5_' . $userInfo['is_promoter'] . '_user.jpg', '?spread=' . $uid);
  335. } else $urlCode = $imageInfo['att_dir'];
  336. return ['code_src' => $urlCode];
  337. }
  338. /**
  339. * 清除推广关系
  340. * @param int $uid
  341. * @return mixed
  342. */
  343. public function delSpread(int $uid)
  344. {
  345. $userServices = app()->make(UserServices::class);
  346. $userInfo = $userServices->getUserInfo($uid);
  347. if (!$userInfo) {
  348. throw new AdminException('数据不存在');
  349. }
  350. $spreadInfo = $userServices->get($userInfo['spread_uid']);
  351. $spreadInfo->spread_count = $spreadInfo->spread_count - 1;
  352. $spreadInfo->save();
  353. if ($userServices->update($uid, ['spread_uid' => 0, 'spread_time' => 0]) !== false) {
  354. return true;
  355. } else {
  356. throw new AdminException('解除失败');
  357. }
  358. }
  359. /**
  360. * 取消推广资格
  361. * @param int $uid
  362. * @return mixed
  363. */
  364. public function delSystemSpread(int $uid)
  365. {
  366. /** @var UserServices $userServices */
  367. $userServices = app()->make(UserServices::class);
  368. if (!$userServices->getUserInfo($uid, 'uid')) {
  369. throw new AdminException('数据不存在');
  370. }
  371. if ($userServices->update($uid, ['spread_open' => 0]) !== false)
  372. return true;
  373. else
  374. throw new AdminException('取消失败');
  375. }
  376. /**
  377. * 取消绑定上级
  378. * @return bool
  379. */
  380. public function removeSpread()
  381. {
  382. //商城分销功能是否开启 0关闭1开启
  383. if (!sys_config('brokerage_func_status')) return true;
  384. //绑定类型
  385. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  386. if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
  387. return true;
  388. } else {
  389. //分销绑定类型为时间段且没过期
  390. $store_brokerage_binding_time = (int)sys_config('store_brokerage_binding_time', 30) * 24 * 3600;
  391. $spread_time = bcsub((string)time(), (string)$store_brokerage_binding_time, 0);
  392. /** @var UserServices $userServices */
  393. $userServices = app()->make(UserServices::class);
  394. $list = $userServices->getList(['not_spread_uid' => 0, 'status' => 1, 'spread_time' => ['<', $spread_time]], 'uid,spread_uid,spread_time');
  395. foreach ($list as $userInfo) {
  396. $userServices->update($userInfo['uid'], ['spread_uid' => 0, 'spread_time' => 0], 'uid');
  397. }
  398. }
  399. return true;
  400. }
  401. /**
  402. * 配置绑定类型切换重置绑定时间
  403. * @return bool
  404. */
  405. public function resetSpreadTime()
  406. {
  407. //商城分销功能是否开启 0关闭1开启
  408. if (!sys_config('brokerage_func_status')) return true;
  409. /** @var UserServices $userServices */
  410. $userServices = app()->make(UserServices::class);
  411. $list = $userServices->getList(['not_spread_uid' => 0, 'status' => 1], 'uid');
  412. if ($list) {
  413. $uids = array_column($list, 'uid');
  414. $userServices->update([['uid', 'IN', $uids]], ['spread_time' => time()]);
  415. }
  416. return true;
  417. }
  418. }