AgentManageServices.php 18 KB

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