AgentManageServices.php 17 KB

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