UserSpreadServices.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserSpreadDao;
  15. use app\services\order\store\BranchOrderServices;
  16. use app\services\store\StoreUserServices;
  17. use app\services\store\SystemStoreStaffServices;
  18. use app\services\user\level\SystemUserLevelServices;
  19. /**
  20. * Class UserSpreadServices
  21. * @package app\services\user
  22. * @mixin UserSpreadDao
  23. */
  24. class UserSpreadServices extends BaseServices
  25. {
  26. /**
  27. * UserSpreadServices constructor.
  28. * @param UserSpreadDao $dao
  29. */
  30. public function __construct(UserSpreadDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 记录推广关系
  36. * @param int $uid
  37. * @param int $spread_uid
  38. * @param int $spread_time
  39. * @param int $admin_id
  40. * @return bool
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function setSpread(int $uid, int $spread_uid, int $spread_time = 0, int $admin_id = 0)
  46. {
  47. if (!$uid || !$spread_uid) return false;
  48. /** @var UserServices $userServices */
  49. $userServices = app()->make(UserServices::class);
  50. if (!$userServices->userExist($uid)) {
  51. return false;
  52. }
  53. if (!$userServices->userExist($spread_uid)) {
  54. return false;
  55. }
  56. $data = ['uid' => $uid, 'spread_uid' => $spread_uid, 'spread_time' => $spread_time ?: time(), 'admin_id' => $admin_id];
  57. try {
  58. /** @var SystemStoreStaffServices $storeStaffServices */
  59. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  60. $staffInfo = $storeStaffServices->getStaffInfoByUid($spread_uid);
  61. } catch (\Throwable $e) {
  62. $staffInfo = [];
  63. }
  64. if ($staffInfo) {
  65. $data['store_id'] = $staffInfo['store_id'];
  66. $data['staff_id'] = $staffInfo['id'];
  67. }
  68. if ($this->dao->save($data)) {
  69. if ($staffInfo) {
  70. //记录门店用户
  71. /** @var StoreUserServices $storeUserServices */
  72. $storeUserServices = app()->make(StoreUserServices::class);
  73. $storeUserServices->setStoreUser($uid, (int)$staffInfo['store_id']);
  74. }
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. /**
  81. * 查询推广用户uids
  82. * @param int $uid
  83. * @param int $type 1:一级2:二级 0:所有
  84. * @param array $where
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function getSpreadUids(int $uid, int $type = 0, array $where = [])
  91. {
  92. if (!$uid) return [];
  93. /** @var UserServices $userServices */
  94. $userServices = app()->make(UserServices::class);
  95. if (!$userServices->userExist($uid)) {
  96. return [];
  97. }
  98. if ($where && isset($where['time'])) {
  99. $where['timeKey'] = 'spread_time';
  100. }
  101. $where['spread_uid'] = $uid;
  102. $spread_one = $this->dao->getSpreadUids($where);
  103. if ($type == 1) {
  104. return $spread_one;
  105. }
  106. $where['spread_uid'] = $spread_one;
  107. $spread_two = $this->dao->getSpreadUids($where);
  108. if ($type == 2) {
  109. return $spread_two;
  110. }
  111. return array_unique(array_merge($spread_one, $spread_two));
  112. }
  113. /**
  114. * 门店推广统计详情列表
  115. * @param int $store_id
  116. * @param int $staff_id
  117. * @param array $time
  118. * @return array|array[]
  119. */
  120. public function time(int $store_id, int $staff_id, array $time = [])
  121. {
  122. if (!$time) {
  123. return [[], []];
  124. }
  125. [$start, $stop, $front, $front_stop] = $time;
  126. $where = ['store_id' => $store_id];
  127. if ($staff_id) {
  128. $where['staff_id'] = $staff_id;
  129. }
  130. $frontPrice = $this->dao->count($where + ['time' => [$front, $front_stop], 'timeKey' => 'spread_time']);
  131. $nowPrice = $this->dao->count($where + ['time' => [$start, $stop], 'timeKey' => 'spread_time']);
  132. [$page, $limit] = $this->getPageValue();
  133. $list = $this->dao->getList($where + ['time' => [$start, $stop], 'timeKey' => 'spread_time'], '*', ['user'], $page, $limit);
  134. /** @var BranchOrderServices $order */
  135. $order = app()->make(BranchOrderServices::class);
  136. $order_where = ['time' => $time, 'is_del' => 0, 'is_system_del' => 0, 'paid' => 1, 'pid' => 0, 'refund_status' => [0, 3]];
  137. foreach ($list as &$item) {
  138. $item['spread_time'] = $item['spread_time'] ? date('Y-m-d H:i:s', $item['spread_time']) : '';
  139. $orderCount = $order->column($order_where + ['uid' => $item['uid']], 'count(`id`) as count,sum(`pay_price`) as price');
  140. $item['order_count'] = $orderCount[0]['count'] ?? 0;
  141. $item['order_price'] = $orderCount[0]['price'] ?? 0.00;
  142. }
  143. return [[$nowPrice, $frontPrice], $list];
  144. }
  145. /**
  146. * 获取推广列表
  147. * @param int $uid
  148. * @param UserServices $userServices
  149. * @return array
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function getSpreadList(array $where, string $field = '*', array $with = ['user'], bool $type = true)
  155. {
  156. [$page, $limit] = $this->getPageValue();
  157. $list = $this->dao->getList($where, $field, $with, $page, $limit);
  158. $count = $this->dao->count($where);
  159. /** @var BranchOrderServices $order */
  160. $order = app()->make(BranchOrderServices::class);
  161. foreach ($list as &$item) {
  162. $item['spread_time'] = $item['spread_time'] ? date('Y-m-d H:i:s', $item['spread_time']) : '';
  163. $item['type'] = $item['admin_id'] ? ('手动变更(' . ($item['real_name'] ?? '') . ')') : '自动变更';
  164. if ($type) {
  165. $orderCount = $order->column(['uid' => $item['uid'], 'is_del' => 0, 'is_system_del' => 0, 'paid' => 1, 'pid' => 0, 'refund_status' => [0, 3]], 'count(`id`) as count,sum(`pay_price`) as price');
  166. $item['order_count'] = $orderCount[0]['count'] ?? 0;
  167. $item['order_price'] = $orderCount[0]['price'] ?? 0.00;
  168. }
  169. }
  170. return compact('list', 'count');
  171. }
  172. /**
  173. * 获取好友uids 我推广的 推广我的
  174. * @param int $uid
  175. * @return array
  176. */
  177. public function getFriendUids(int $uid)
  178. {
  179. $result = [];
  180. if ($uid) {
  181. $spread = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
  182. $sup_spread = $this->dao->getColumn(['uid' => $uid], 'spread_uid');
  183. $result = array_unique(array_merge($spread, $sup_spread));
  184. }
  185. return $result;
  186. }
  187. /**
  188. * 获取好友
  189. * @param int $id
  190. * @param string $field
  191. * @return array
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. public function getFriendList(int $uid, string $field = 'uid,nickname,level,add_time')
  197. {
  198. $uids = $this->getFriendUids($uid);
  199. $list = [];
  200. $count = 0;
  201. if ($uids) {
  202. [$page, $limit] = $this->getPageValue();
  203. /** @var UserServices $userServices */
  204. $userServices = app()->make(UserServices::class);
  205. $list = $userServices->getList(['uid' => $uids], $field, $page, $limit);
  206. /** @var SystemUserLevelServices $systemLevelServices */
  207. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  208. $systemLevelList = $systemLevelServices->getWhereLevelList([], 'id,name');
  209. if ($systemLevelList) $systemLevelServices = array_combine(array_column($systemLevelList, 'id'), $systemLevelList);
  210. foreach ($list as &$item) {
  211. $item['type'] = $systemLevelServices[$item['level']]['name'] ?? '暂无';
  212. $item['add_time'] = $item['add_time'] && is_numeric($item['add_time']) ? date('Y-m-d H:i:s', $item['add_time']) : '';
  213. }
  214. $count = $this->dao->count(['spread_uid' => $uid]);
  215. }
  216. return compact('list', 'count');
  217. }
  218. }