AgentManageServices.php 17 KB

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