AgentManageServices.php 18 KB

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