AgentManageServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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['brokerage_money'] = $item['bill'][0]['brokerage_money'] ?? 0;
  57. if ($item['brokerage_price'] > $item['broken_commission'])
  58. $item['brokerage_money'] = bcsub($item['brokerage_price'], $item['broken_commission'], 2);
  59. else
  60. $item['brokerage_money'] = 0;
  61. $item['new_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. $data['extract_price'] = $userServices->getSumBrokerage(['uid' => $uids]);
  89. }
  90. //分销员人数
  91. $data['order_count'] = 0;
  92. $data['pay_price'] = 0;
  93. $data['pay_price'] = 0;
  94. $data['extract_count'] = 0;
  95. if ($data['sum_count']) {
  96. /** @var StoreOrderServices $storeOrder */
  97. $storeOrder = app()->make(StoreOrderServices::class);
  98. //订单总数
  99. $data['order_count'] = $storeOrder->getCount([['uid', 'in', $uids], ['paid', '=', 1], ['refund_status', '=', 0]]);
  100. //订单金额
  101. $data['pay_price'] = $storeOrder->sum([['uid', 'in', $uids], ['paid', '=', 1], ['refund_status', '=', 0]], 'pay_price');
  102. //提现次数
  103. $data['extract_count'] = app()->make(UserExtractServices::class)->getCount([['uid', 'in', $uids], ['status', '=', 1]]);
  104. }
  105. return [
  106. [
  107. 'name' => '分销员人数(人)',
  108. 'count' => $data['sum_count'],
  109. 'className' => 'md-contacts',
  110. 'col' => 6,
  111. ],
  112. [
  113. 'name' => '发展会员人数(人)',
  114. 'count' => $data['spread_sum'],
  115. 'className' => 'md-contact',
  116. 'col' => 6,
  117. ],
  118. [
  119. 'name' => '订单数(单)',
  120. 'count' => $data['order_count'],
  121. 'className' => 'md-cart',
  122. 'col' => 6,
  123. ],
  124. [
  125. 'name' => '订单金额(元)',
  126. 'count' => $data['pay_price'],
  127. 'className' => 'md-bug',
  128. 'col' => 6,
  129. ],
  130. [
  131. 'name' => '提现次数(次)',
  132. 'count' => $data['extract_count'],
  133. 'className' => 'md-basket',
  134. 'col' => 6,
  135. ],
  136. [
  137. 'name' => '未提现金额(元)',
  138. 'count' => $data['extract_price'],
  139. 'className' => 'ios-at-outline',
  140. 'col' => 6,
  141. ],
  142. ];
  143. }
  144. /**
  145. * 推广人列表
  146. * @param array $where
  147. * @return mixed
  148. */
  149. public function getStairList(array $where)
  150. {
  151. /** @var UserServices $userServices */
  152. $userServices = app()->make(UserServices::class);
  153. $data = $userServices->getSairList($where);
  154. foreach ($data['list'] as &$item) {
  155. $item['spread_count'] = $item['spreadCount'][0]['spread_count'] ?? 0;
  156. $item['order_count'] = $item['order'][0]['order_count'] ?? 0;
  157. $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
  158. $item['add_time'] = $item['add_time'] ? date("Y-m-d H:i:s", $item['add_time']) : '';
  159. }
  160. return $data;
  161. }
  162. /**
  163. * 推广人头部信息
  164. * @param array $where
  165. * @return array[]
  166. */
  167. public function getSairBadge(array $where)
  168. {
  169. /** @var UserServices $userServices */
  170. $userServices = app()->make(UserServices::class);
  171. $data['number'] = $userServices->getSairCount($where);
  172. $where['type'] = 1;
  173. $data['one_number'] = $userServices->getSairCount($where);
  174. $where['type'] = 2;
  175. $data['two_number'] = $userServices->getSairCount($where);
  176. $col = $data['two_number'] > 0 ? 4 : 6;
  177. return [
  178. [
  179. 'name' => '总人数(人)',
  180. 'count' => $data['number'],
  181. 'col' => $col,
  182. ],
  183. [
  184. 'name' => '一级人数(人)',
  185. 'count' => $data['one_number'],
  186. 'col' => $col,
  187. ],
  188. [
  189. 'name' => '二级人数(人)',
  190. 'count' => $data['two_number'],
  191. 'col' => $col,
  192. ],
  193. ];
  194. }
  195. /**
  196. * 推广订单
  197. * @param array $where
  198. * @return array
  199. */
  200. public function getStairOrderList(int $uid, array $where)
  201. {
  202. /** @var UserServices $userServices */
  203. $userServices = app()->make(UserServices::class);
  204. $userInfo = $userServices->getUserInfo($uid);
  205. if (!$userInfo) {
  206. return ['count' => 0, 'list' => []];
  207. }
  208. /** @var StoreOrderServices $storeOrder */
  209. $storeOrder = app()->make(StoreOrderServices::class);
  210. $data = $storeOrder->getUserStairOrderList($uid, $where);
  211. $spreadInfo = $userInfo->nickname . "|" . ($userInfo->phone ? $userInfo->phone . "|" : '') . $userInfo->uid;
  212. $uids = array_column($data['list'], 'uid');
  213. $userList = [];
  214. if ($uids) {
  215. $userList = $userServices->getColumn([['uid', 'IN', $uids]], 'nickname,phone,avatar,real_name', 'uid');
  216. }
  217. $orderIds = array_column($data['list'], 'id');
  218. $userBillList = [];
  219. $orderChangTimes = [];
  220. if ($orderIds) {
  221. /** @var UserBillServices $userBill */
  222. $userBill = app()->make(UserBillServices::class);
  223. $userBillList = $userBill->getColumn([['category', '=', 'now_money'], ['type', '=', 'brokerage'], ['link_id', 'IN', $orderIds], ['uid', '=', $uid]], 'number', 'link_id');
  224. /** @var StoreOrderStatusServices $storeOrderStatus */
  225. $storeOrderStatus = app()->make(StoreOrderStatusServices::class);
  226. $orderChangTimes = $storeOrderStatus->getColumn([['oid', 'IN', $orderIds], ['change_type', '=', 'user_take_delivery']], 'change_time', 'oid');
  227. }
  228. foreach ($data['list'] as &$item) {
  229. $user = $userList[$item['uid']] ?? [];
  230. $item['user_info'] = '';
  231. $item['avatar'] = '';
  232. if (count($user)) {
  233. $item['user_info'] = $user['nickname'] . '|' . ($user['phone'] ? $user['phone'] . '|' : '') . $user['real_name'];
  234. $item['avatar'] = $user['avatar'];
  235. }
  236. $item['spread_info'] = $spreadInfo;
  237. $item['brokerage_price'] = $userBillList[$item['id']] ?? 0;
  238. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  239. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  240. $item['take_time'] = ($change_time = $orderChangTimes[$item['id']] ?? '') ? date('Y-m-d H:i:s', $change_time) : '暂无';
  241. }
  242. return $data;
  243. }
  244. /**
  245. * TODO 获取分销员列表头部统计信息
  246. * @param $where
  247. * @return array[]
  248. */
  249. public function getStairOrderBadge(array $where)
  250. {
  251. $data = [];
  252. return [
  253. [
  254. 'name' => '总金额(元)',
  255. 'count' => $data['order_price'] ?? 0,
  256. 'col' => 6,
  257. ],
  258. [
  259. 'name' => '订单总数(单)',
  260. 'count' => $data['order_count'] ?? 0,
  261. 'col' => 6,
  262. ],
  263. [
  264. 'name' => '返佣总金额(元)',
  265. 'count' => $data['number_price'] ?? 0,
  266. 'col' => 6,
  267. ],
  268. [
  269. 'name' => '一级总金额(元)',
  270. 'count' => $data['one_price'] ?? 0,
  271. 'col' => 6,
  272. ],
  273. [
  274. 'name' => '一级订单数(单)',
  275. 'count' => $data['one_count'] ?? 0,
  276. 'col' => 6,
  277. ],
  278. [
  279. 'name' => '二级总金额(元)',
  280. 'count' => $data['two_price'] ?? 0,
  281. 'col' => 6,
  282. ],
  283. [
  284. 'name' => '二级订单数(单)',
  285. 'count' => $data['two_count'] ?? 0,
  286. 'col' => 6,
  287. ],
  288. ];
  289. }
  290. /**
  291. * 获取永久二维码
  292. * @param $type
  293. * @param $id
  294. * @return array|false|\PDOStatement|string|\think\Model
  295. */
  296. public function wechatCode(int $uid)
  297. {
  298. /** @var QrcodeServices $qrcode */
  299. $qrcode = app()->make(QrcodeServices::class);
  300. $code = $qrcode->getForeverQrcode('spread', $uid);
  301. if (!$code['ticket']) exception('永久二维码获取错误');
  302. return $code;
  303. }
  304. /**
  305. * TODO 查看小程序推广二维码
  306. * @param string $uid
  307. */
  308. public function lookXcxCode(int $uid)
  309. {
  310. if (!sys_config('routine_appId') || !sys_config('routine_appsecret')) {
  311. throw new ValidateException('请先在设置->应用设置->小程序设置中配置小程序');
  312. }
  313. $userInfo = app()->make(UserServices::class)->getUserInfo($uid);
  314. if (!$userInfo) {
  315. throw new AdminException('数据不存在');
  316. }
  317. $name = $userInfo['uid'] . '_' . $userInfo['is_promoter'] . '_user.jpg';
  318. /** @var SystemAttachmentServices $systemAttachmentModel */
  319. $systemAttachmentModel = app()->make(SystemAttachmentServices::class);
  320. $imageInfo = $systemAttachmentModel->getInfo(['name' => $name]);
  321. if (!$imageInfo) {
  322. /** @var QrcodeServices $qrcode */
  323. $qrcode = app()->make(QrcodeServices::class);
  324. $resForever = $qrcode->qrCodeForever($uid, 'spread_routine');
  325. if ($resForever) {
  326. $resCode = MiniProgramService::qrcodeService()->appCodeUnlimit($resForever->id, '', 280);
  327. $res = ['res' => $resCode, 'id' => $resForever->id];
  328. } else {
  329. $res = false;
  330. }
  331. if (!$res) throw new ValidateException('二维码生成失败');
  332. $upload = UploadService::init();
  333. if ($upload->to('routine/spread/code')->stream((string)$res['res'], $name) === false) {
  334. return $upload->getError();
  335. }
  336. $imageInfo = $upload->getUploadInfo();
  337. $imageInfo['image_type'] = sys_config('upload_type', 1);
  338. $systemAttachmentModel->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  339. $qrcode->update($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  340. $urlCode = $imageInfo['dir'];
  341. } else $urlCode = $imageInfo['att_dir'];
  342. return ['code_src' => $urlCode];
  343. }
  344. /**
  345. * 查看H5推广二维码
  346. * @param string $uid
  347. * @return mixed|string
  348. */
  349. public function lookH5Code(int $uid)
  350. {
  351. $userInfo = app()->make(UserServices::class)->getUserInfo($uid);
  352. if (!$userInfo) {
  353. throw new AdminException('数据不存在');
  354. }
  355. $name = $userInfo['uid'] . '_h5_' . $userInfo['is_promoter'] . '_user.jpg';
  356. /** @var SystemAttachmentServices $systemAttachmentModel */
  357. $systemAttachmentModel = app()->make(SystemAttachmentServices::class);
  358. $imageInfo = $systemAttachmentModel->getInfo(['name' => $name]);
  359. if (!$imageInfo) {
  360. $urlCode = QrcodeService::getWechatQrcodePath($uid . '_h5_' . $userInfo['is_promoter'] . '_user.jpg', '?spread=' . $uid);
  361. } else $urlCode = $imageInfo['att_dir'];
  362. return ['code_src' => $urlCode];
  363. }
  364. /**
  365. * 清除推广关系
  366. * @param int $uid
  367. * @return mixed
  368. */
  369. public function delSpread(int $uid)
  370. {
  371. $userServices = app()->make(UserServices::class);
  372. if (!$userServices->getUserInfo($uid)) {
  373. throw new AdminException('数据不存在');
  374. }
  375. if ($userServices->update($uid, ['spread_uid' => 0]) !== false)
  376. return true;
  377. else
  378. throw new AdminException('解除失败');
  379. }
  380. }