MemberService.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. <?php
  2. namespace app\admin\service\member;
  3. use app\common\Tree;
  4. use app\model\ClientPoints;
  5. use app\model\CouponDetail;
  6. use app\model\Device;
  7. use app\model\Family;
  8. use app\model\FamilyMember;
  9. use app\model\Goods;
  10. use app\model\Member;
  11. use app\model\MemberAccount;
  12. use app\model\MemberCert;
  13. use app\model\MemberDevice;
  14. use app\model\MemberInfo;
  15. use app\model\Order;
  16. use app\model\OrderSheet;
  17. use app\model\PayDetail;
  18. use app\model\SysCategory;
  19. use app\model\SysDept;
  20. use app\model\SysSerial;
  21. use app\model\SysUser;
  22. use support\Db;
  23. use support\exception\BusinessException;
  24. use support\Request;
  25. use support\Response;
  26. class MemberService
  27. {
  28. public static function list(Request $request)
  29. {
  30. $page = $request->get('page', 1);
  31. $pageSize = $request->get('pageSize', 10);
  32. $name = $request->get('name', '');
  33. $nickname = $request->get('nickname', '');
  34. $mobile = $request->get('mobile', '');
  35. $level = $request->get('level', '');
  36. $isAuth = $request->get('is_auth', '');
  37. $addtime = $request->get('member_addtimes', []);
  38. $type = $request->get('type', '');
  39. $status = $request->get('member_status', '');
  40. $memberIsPartner = $request->get('member_is_partner', '');
  41. $memberIsReferrer = $request->get('member_is_referrer', '');
  42. $rows = Member::with([
  43. 'account' => function ($query) {
  44. $query->where('member_account_status', 'ACTIVED');
  45. }
  46. ])
  47. ->where('member_is_owner', 'N')
  48. ->where('member_mobile', '<>', '0000')
  49. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  50. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  51. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  52. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  53. // ->where('member_account.member_account_classify', 'CASH')
  54. ->when($name != '', function ($query) use ($name) {
  55. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  56. })->when($mobile != '', function ($query) use ($mobile) {
  57. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  58. })->when($level != '', function ($query) use ($level) {
  59. if ($level == 'other') {
  60. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  61. } else if ($level == 'VIP') {
  62. $query->where('member.member_is_vip', 'Y');
  63. } else {
  64. $query->where('member_role.member_role_id', $level);
  65. }
  66. })->when($isAuth != '', function ($query) use ($isAuth) {
  67. if ($isAuth == 'Y') {
  68. $query->where('member_cert.member_cert_name', '<>', NULL)->where('member_cert.member_cert_nbr', '<>', NULL)->where('member_cert.member_cert_name', '<>', '')->where('member_cert.member_cert_nbr', '<>', '');
  69. } else {
  70. $query->where('member_cert.member_cert_name', NULL)->orWhere('member_cert.member_cert_nbr', NULL)->orWhere('member_cert.member_cert_name', '')->orWhere('member_cert.member_cert_nbr', '');
  71. }
  72. })->when(!empty($addtime), function ($query) use ($addtime) {
  73. $addtime[0] = strtotime($addtime[0]);
  74. $addtime[1] = strtotime($addtime[1]);
  75. $query->whereBetween('member.member_addtimes', $addtime);
  76. })->when(!empty($type), function ($query) use ($type) {
  77. if ($type == 'todayAdd') {
  78. $addtime[0] = strtotime(date('Y-m-d') . ' 00:00:00');
  79. $addtime[1] = strtotime(date('Y-m-d') . ' 23:59:59');
  80. } else if ($type == 'monthAdd') {
  81. $addtime[0] = strtotime(date('Y-m-01') . ' 00:00:00');
  82. $addtime[1] = strtotime(date('Y-m-t') . ' 23:59:59');
  83. }
  84. $query->whereBetween('member.member_addtimes', $addtime);
  85. })->when(!empty($nickname), function ($query) use ($nickname) {
  86. $query->where(function ($query2) use ($nickname) {
  87. $query2->where('member_info.member_info_nickname', 'like', '%' . $nickname . '%')->orWhere('member.member_mobile', 'like', '%' . $nickname . '%');
  88. });
  89. })->when(!empty($status), function ($query) use ($status) {
  90. $query->where('member_status', $status);
  91. })->when(!empty($memberIsPartner), function ($query) use ($memberIsPartner) {
  92. if ($memberIsPartner == 'Y') {
  93. $query->where('member_is_partner', 'Y');
  94. }
  95. })->when(!empty($memberIsReferrer), function ($query) use ($memberIsReferrer) {
  96. if ($memberIsReferrer == 'Y') {
  97. $query->where('member_is_referrer', 'Y');
  98. }
  99. })
  100. ->select('member_id', 'member_is_vip', 'member_is_owner', 'member_is_partner', 'member_is_referrer', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  101. 'member_info.member_info_nickname', 'member_info.member_info_headimg', 'member_info.member_info_referee', 'member_info.member_info_business', 'member_info.member_info_service', 'member_info.member_info_city', 'member_info.member_info_address', 'member_info.member_info_gender', 'member_info.member_info_birthday',
  102. 'member_role.member_role_id', 'member_role.member_role_name',
  103. 'member_cert.member_cert_birth', 'member_cert.member_cert_gender', 'member_cert.member_cert_name', 'member_cert.member_cert_nbr', 'member_cert.member_cert_province', 'member_cert.member_cert_addr', 'member_cert.member_cert_face', 'member_cert.member_cert_photo', 'member_cert.member_cert_nation'
  104. // 'member_account.member_account_surplus'
  105. );
  106. // 总条数
  107. $total = $rows->count('member_id');
  108. $rows = $rows->orderBy('member_addtimes', 'DESC')
  109. ->forPage($page, $pageSize)
  110. ->get()
  111. ->toArray();
  112. foreach ($rows as &$row) {
  113. $row['coupon_count'] = CouponDetail::where('join_coupon_detail_member_id', $row['member_id'])->count();
  114. $row['member_mobile'] = substr($row['member_mobile'], 0, 3) . '****' . substr($row['member_mobile'], 7);
  115. $row['info'] = [
  116. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '会员',
  117. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : '',
  118. 'member_info_referee' => !empty($row['member_info_referee']) ? $row['member_info_referee'] : '',
  119. 'member_info_business' => !empty($row['member_info_business']) ? $row['member_info_business'] : '',
  120. 'member_info_service' => !empty($row['member_info_service']) ? $row['member_info_service'] : '',
  121. 'member_info_city' => !empty($row['member_info_city']) ? explode('-', $row['member_info_city']) : '',
  122. 'member_info_address' => !empty($row['member_info_address']) ? $row['member_info_address'] : '',
  123. 'member_info_birthday' => !empty($row['member_info_birthday']) ? $row['member_info_birthday'] : '',
  124. 'member_info_gender' => !empty($row['member_info_gender']) ? $row['member_info_gender'] : '',
  125. ];
  126. if (substr($row['info']['member_info_headimg'], 0, 2) == '..') {
  127. $row['info']['member_info_headimg'] = '';
  128. }
  129. if (!empty($row['info']['member_info_address']) && in_array($row['info']['member_info_address'], ['请选择', '省-市-区'])) {
  130. $row['info']['member_info_address'] = '';
  131. }
  132. $row['cert'] = [
  133. 'member_cert_birth' => $row['member_cert_birth'],
  134. 'member_cert_gender' => $row['member_cert_gender'],
  135. 'member_cert_name' => $row['member_cert_name'],
  136. 'member_cert_nbr' => $row['member_cert_nbr'],
  137. 'member_cert_province' => !empty($row['member_cert_province']) && is_json($row['member_cert_province']) ? json_decode($row['member_cert_province']) : '',
  138. 'member_cert_addr' => $row['member_cert_addr'],
  139. 'member_cert_face' => !empty($row['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_face'] : '',
  140. 'member_cert_photo' => !empty($row['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_photo'] : '',
  141. 'member_cert_nation' => $row['member_cert_nation'],
  142. ];
  143. $account = [];
  144. if (!empty($row['account'])) {
  145. foreach ($row['account'] as $item) {
  146. if ($item['member_account_classify'] == 'WELFARE') {
  147. $account['welfare'] = [
  148. 'member_account_name' => $item['member_account_name'],
  149. 'member_account_income' => $item['member_account_income'],
  150. 'member_account_expend' => $item['member_account_expend'],
  151. 'member_account_surplus' => $item['member_account_surplus'],
  152. 'member_account_added' => $item['member_account_added'],
  153. ];
  154. } else if ($item['member_account_classify'] == 'POINTS') {
  155. $account['points'] = [
  156. 'member_account_name' => $item['member_account_name'],
  157. 'member_account_income' => $item['member_account_income'],
  158. 'member_account_expend' => $item['member_account_expend'],
  159. 'member_account_surplus' => $item['member_account_surplus'],
  160. 'member_account_added' => $item['member_account_added'],
  161. ];
  162. } else if ($item['member_account_classify'] == 'CASH') {
  163. $account['cash'] = [
  164. 'member_account_name' => $item['member_account_name'],
  165. 'member_account_income' => $item['member_account_income'],
  166. 'member_account_expend' => $item['member_account_expend'],
  167. 'member_account_surplus' => $item['member_account_surplus'],
  168. 'member_account_added' => $item['member_account_added'],
  169. ];
  170. } else if ($item['member_account_classify'] == 'CARD') {
  171. $account['card'] = [
  172. 'member_account_name' => $item['member_account_name'],
  173. 'member_account_income' => $item['member_account_income'],
  174. 'member_account_expend' => $item['member_account_expend'],
  175. 'member_account_surplus' => $item['member_account_surplus'],
  176. 'member_account_added' => $item['member_account_added'],
  177. ];
  178. } else if ($item['member_account_classify'] == 'VIP') {
  179. $account['vip'] = [
  180. 'member_account_name' => $item['member_account_name'],
  181. 'member_account_income' => $item['member_account_income'],
  182. 'member_account_expend' => $item['member_account_expend'],
  183. 'member_account_surplus' => $item['member_account_surplus'],
  184. 'member_account_added' => $item['member_account_added'],
  185. ];
  186. }
  187. }
  188. }
  189. $row['account'] = $account;
  190. $row['is_auth'] = "未认证";
  191. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  192. $row['is_auth'] = "已认证";
  193. }
  194. $row['source'] = "APP";
  195. $row['belong'] = [];
  196. $row['referee'] = [];
  197. if (!empty($row['member_extend_json'])) {
  198. $extendJson = json_decode($row['member_extend_json'], true);
  199. if (isset($extendJson['come_from'])) {
  200. $row['source'] = $extendJson['come_from'] ?? '';
  201. }
  202. if (isset($extendJson['belong'])) {
  203. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  204. if ($user) {
  205. $row['belong']['user_id'] = $extendJson['belong']['user_id'];
  206. $row['belong']['username'] = $user->user_name;
  207. $row['belong']['user_mobile'] = $user->user_mobile;
  208. }
  209. $row['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  210. }
  211. if (isset($extendJson['referee'])) {
  212. foreach ($extendJson['referee'] as $key => $referee) {
  213. $row['referee'][] = [
  214. 'name' => $referee,
  215. 'time' => $key
  216. ];
  217. }
  218. }
  219. }
  220. // 粉丝
  221. $fans1 = Member::where('join_invite_member_id', $row['member_id'])
  222. ->select('member_id')
  223. ->get()
  224. ->toArray();
  225. $fans1Ids = array_column($fans1, 'member_id');
  226. $fans2Count = Member::whereIn('join_invite_member_id', $fans1Ids)
  227. ->count();
  228. $row['fans_total'] = count($fans1) + $fans2Count;
  229. }
  230. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  231. }
  232. public static function exportMember(Request $request)
  233. {
  234. $memberIds = $request->get('member_id', '');
  235. $name = $request->get('name', '');
  236. $mobile = $request->get('mobile', '');
  237. $level = $request->get('level', '');
  238. $isAuth = $request->get('is_auth', '');
  239. $addtime = $request->get('member_addtimes', []);
  240. $rows = Member::with('account')
  241. ->where('member_is_owner', 'N')
  242. ->where('member_mobile', '<>', '0000')
  243. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  244. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  245. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  246. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  247. // ->where('member_account.member_account_classify', 'CASH')
  248. ->when($memberIds != '', function ($query) use ($memberIds) {
  249. $query->whereIn('member.member_id', $memberIds);
  250. })->when($name != '', function ($query) use ($name) {
  251. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  252. })->when($mobile != '', function ($query) use ($mobile) {
  253. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  254. })->when($level != '', function ($query) use ($level) {
  255. if ($level == 'other') {
  256. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  257. } else {
  258. $query->where('member_role.member_role_id', $level);
  259. }
  260. })->when($isAuth != '', function ($query) use ($isAuth) {
  261. if ($isAuth == 'Y') {
  262. $query->where('member_cert.member_cert_name', '<>', NULL)->where('member_cert.member_cert_nbr', '<>', NULL)->where('member_cert.member_cert_name', '<>', '')->where('member_cert.member_cert_nbr', '<>', '');
  263. } else {
  264. $query->where('member_cert.member_cert_name', NULL)->where('member_cert.member_cert_nbr', NULL)->where('member_cert.member_cert_name', '')->where('member_cert.member_cert_nbr', '');
  265. }
  266. })->when(!empty($addtime), function ($query) use ($addtime) {
  267. $addtime[0] = strtotime($addtime[0]);
  268. $addtime[1] = strtotime($addtime[1]);
  269. $query->whereBetween('member.member_addtimes', $addtime);
  270. })->select('member_id', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  271. 'member_info.member_info_nickname', 'member_info.member_info_headimg', 'member_info.member_info_referee', 'member_info.member_info_business', 'member_info.member_info_service',
  272. 'member_role.member_role_id', 'member_role.member_role_name',
  273. 'member_cert.member_cert_birth', 'member_cert.member_cert_gender', 'member_cert.member_cert_name', 'member_cert.member_cert_nbr', 'member_cert.member_cert_province', 'member_cert.member_cert_addr', 'member_cert.member_cert_face', 'member_cert.member_cert_photo', 'member_cert.member_cert_nation',
  274. // 'member_account.member_account_surplus'
  275. )->orderBy('member_addtimes', 'DESC')
  276. ->get()
  277. ->toArray();
  278. $exportData = [];
  279. foreach ($rows as $row) {
  280. $row['info'] = [
  281. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '会员',
  282. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : '',
  283. 'member_info_referee' => !empty($row['member_info_referee']) ? $row['member_info_referee'] : '',
  284. 'member_info_business' => !empty($row['member_info_business']) ? $row['member_info_business'] : '',
  285. 'member_info_service' => !empty($row['member_info_service']) ? $row['member_info_service'] : ''
  286. ];
  287. $row['cert'] = [
  288. 'member_cert_name' => $row['member_cert_name'],
  289. ];
  290. $account = [];
  291. if (!empty($row['account'])) {
  292. foreach ($row['account'] as $item) {
  293. if ($item['member_account_classify'] == 'POINTS') {
  294. $account['points'] = [
  295. 'member_account_surplus' => $item['member_account_surplus'],
  296. 'member_account_added' => $item['member_account_added'],
  297. ];
  298. } else if ($item['member_account_classify'] == 'CASH') {
  299. $account['cash'] = [
  300. 'member_account_surplus' => $item['member_account_surplus'],
  301. 'member_account_added' => $item['member_account_added'],
  302. ];
  303. } else if ($item['member_account_classify'] == 'CARD') {
  304. $account['card'] = [
  305. 'member_account_surplus' => $item['member_account_surplus'],
  306. 'member_account_added' => $item['member_account_added'],
  307. ];
  308. }
  309. }
  310. }
  311. // $row['account'] = $account;
  312. $row['is_auth'] = "未认证";
  313. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  314. $row['is_auth'] = "已认证";
  315. }
  316. $row['source'] = "APP";
  317. if (!empty($row['member_extend_json'])) {
  318. $extendJson = json_decode($row['member_extend_json'], true);
  319. if (isset($extendJson['come_from'])) {
  320. $row['source'] = $extendJson['come_from'] ?? '';
  321. }
  322. }
  323. $sourceStr = ['APP' => "APP", 'CUSTOMER' => "客户管理", 'SYSTEM' => "后台"];
  324. $exportData[] = [
  325. 'member_id' => $row['member_id'],
  326. 'member_info_nickname' => $row['info']['member_info_nickname'] ?? '',
  327. 'member_mobile' => $row['member_mobile'] ?? '',
  328. 'member_cert_name' => $row['cert']['member_cert_name'] ?? '',
  329. 'source' => in_array($row['source'], $sourceStr) ? $sourceStr[$row['source']] : 'APP',
  330. 'cash' => isset($account['cash']) ? $account['cash']['member_account_surplus'] + $account['cash']['member_account_added'] : 0,
  331. 'points' => isset($account['points']) ? $account['points']['member_account_surplus'] + $account['points']['member_account_added'] : 0,
  332. 'is_auth' => $row['is_auth'],
  333. 'level' => $row['member_role_name'] ?? '普通会员',
  334. 'member_addtimes' => $row['member_addtimes'],
  335. 'member_info_referee' => $row['info']['member_info_referee'] ?? '',
  336. 'member_info_business' => $row['info']['member_info_business'] ?? '',
  337. 'member_info_service' => $row['info']['member_info_service'] ?? '',
  338. ];
  339. }
  340. return json_success('', $exportData);
  341. }
  342. public static function info($memberId)
  343. {
  344. $member = Member::with([
  345. 'cert',
  346. 'info',
  347. 'role' => function ($query) {
  348. $query->select('member_role_id', 'member_role_name');
  349. }
  350. ])
  351. ->where('member_id', $memberId)
  352. ->first();
  353. if (!$member) {
  354. return json_fail('查询错误');
  355. }
  356. $member = $member->toArray();
  357. $member['info'] = [
  358. 'member_info_nickname' => !empty($member['info']) && !empty($member['info']['member_info_nickname']) ? $member['info']['member_info_nickname'] : substr($member['member_mobile'], -4, 4) . '会员',
  359. 'member_info_headimg' => !empty($member['info']) && !empty($member['info']['member_info_headimg']) ? $member['info']['member_info_headimg'] : '',
  360. 'member_info_referee' => !empty($member['info']) && !empty($member['info']['member_info_referee']) ? $member['info']['member_info_referee'] : '',
  361. 'member_info_business' => !empty($member['info']) && !empty($member['info']['member_info_business']) ? $member['info']['member_info_business'] : '',
  362. 'member_info_service' => !empty($member['info']) && !empty($member['info']['member_info_service']) ? $member['info']['member_info_service'] : '',
  363. 'member_info_city' => !empty($member['info']) && !empty($member['info']['member_info_city']) ? explode('-', $member['info']['member_info_city']) : '',
  364. 'member_info_address' => !empty($member['info']) && !empty($member['info']['member_info_address']) ? $member['info']['member_info_address'] : '',
  365. 'member_info_gender' => !empty($member['info']) && !empty($member['info']['member_info_gender']) ? $member['info']['member_info_gender'] : '',
  366. 'member_info_birthday' => !empty($member['info']) && !empty($member['info']['member_info_birthday']) ? $member['info']['member_info_birthday'] : '',
  367. ];
  368. if (!empty($member['info']['member_info_address']) && in_array($member['info']['member_info_address'], ['请选择', '省-市-区'])) {
  369. $member['info']['member_info_address'] = '';
  370. }
  371. $member['cert'] = [
  372. 'member_cert_birth' => $member['cert']['member_cert_birth'] ?? '',
  373. 'member_cert_gender' => $member['cert']['member_cert_gender'] ?? '',
  374. 'member_cert_name' => $member['cert']['member_cert_name'] ?? '',
  375. 'member_cert_nbr' => $member['cert']['member_cert_nbr'] ?? '',
  376. 'member_cert_province' => !empty($member['cert']) && !empty($member['cert']['member_cert_province']) && is_json($member['cert']['member_cert_province']) ? json_decode($member['cert']['member_cert_province']) : '',
  377. 'member_cert_addr' => $member['cert']['member_cert_addr'] ?? '',
  378. 'member_cert_face' => !empty($member['cert']) && !empty($member['cert']['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $member['cert']['member_cert_face'] : '',
  379. 'member_cert_photo' => !empty($member['cert']) && !empty($member['cert']['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $member['cert']['member_cert_photo'] : '',
  380. 'member_cert_nation' => $member['cert']['member_cert_nation'] ?? '',
  381. 'member_cert_addtimes' => $member['cert']['member_cert_addtimes'] ?? '',
  382. ];
  383. if (!empty($member['cert']['member_cert_addtimes'])) {
  384. $member['cert']['member_cert_addtimes'] = date('Y-m-d', $member['cert']['member_cert_addtimes']);
  385. } else {
  386. $member['cert']['member_cert_addtimes'] = date('Y-m-d', strtotime($member['member_addtimes']));
  387. }
  388. if (!empty($member['join_invite_member_id'])) {
  389. $inviteMember = Member::with([
  390. 'info' => function ($query) {
  391. $query->select('join_info_member_id', 'member_info_nickname');
  392. },
  393. 'cert' => function ($query) {
  394. $query->select('join_cert_member_id', 'member_cert_name');
  395. }
  396. ])->where('member_id', $member['join_invite_member_id'])
  397. ->first();
  398. $inviteMemberName = '';
  399. if (!empty($inviteMember->cert) && !empty($inviteMember->cert->member_cert_name)) {
  400. $inviteMemberName = $inviteMember->cert->member_cert_name;
  401. } elseif (!empty($inviteMemberName->info) && !empty($inviteMemberName->info->member_info_nickname)) {
  402. $inviteMemberName = $inviteMemberName->info->member_info_nickname;
  403. }
  404. $member['invite_member'] = $inviteMemberName . (!empty($inviteMember->member_invite_code) ? '(' . $inviteMember->member_invite_code . ')' : '');
  405. }
  406. $member['is_auth'] = "未认证";
  407. if (!empty($member['cert']['member_cert_nbr']) && !empty($member['cert']['member_cert_name'])) {
  408. $member['is_auth'] = "已认证";
  409. }
  410. $member['source'] = "APP";
  411. $member['belong'] = [];
  412. $member['referee'] = [];
  413. if (!empty($member['member_extend_json'])) {
  414. $extendJson = json_decode($member['member_extend_json'], true);
  415. if (isset($extendJson['come_from'])) {
  416. $member['source'] = $extendJson['come_from'] ?? '';
  417. }
  418. if (isset($extendJson['belong'])) {
  419. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  420. if ($user) {
  421. $member['belong']['user_id'] = $extendJson['belong']['user_id'];
  422. $member['belong']['username'] = $user->user_name;
  423. $member['belong']['user_mobile'] = $user->user_mobile;
  424. }
  425. $member['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  426. }
  427. if (isset($extendJson['referee'])) {
  428. foreach ($extendJson['referee'] as $key => $referee) {
  429. $member['referee'][] = [
  430. 'name' => $referee,
  431. 'time' => $key
  432. ];
  433. }
  434. }
  435. }
  436. if (!empty($member['join_invite_member_id'])) {
  437. $inviteMember = Member::with('cert', 'info')
  438. ->where('member_id', $member['join_invite_member_id'])
  439. ->select('member_id', 'member_mobile', 'member_invite_code')
  440. ->first();
  441. if (!empty($inviteMember)) {
  442. $inviteMember = $inviteMember->toArray();
  443. $mobile = '';
  444. if (!empty($inviteMember['member_mobile'])) {
  445. $mobile = $inviteMember['member_mobile'];
  446. }
  447. $nickname = '';
  448. if (!empty($inviteMember['info']) && !empty($inviteMember['info']['member_info_nickname'])) {
  449. $nickname = $inviteMember['info']['member_info_nickname'];
  450. }
  451. $certname = '';
  452. if (!empty($inviteMember['cert']) && !empty($inviteMember['cert']['member_info_nickname'])) {
  453. $nickname = $inviteMember['cert']['member_cert_name'];
  454. }
  455. $member['invite_name'] = MemberService::getMemberName($mobile, $certname, $nickname);
  456. $member['invite_code'] = $inviteMember['member_invite_code'] ?? '';
  457. }
  458. }
  459. return json_success('', $member);
  460. }
  461. /**
  462. * VIP账户明细
  463. */
  464. public static function vipInfo(Request $request)
  465. {
  466. $memberId = $request->get('member_id', '');
  467. $page = $request->get('page', 1);
  468. $pageSize = $request->get('pageSize', 20);
  469. $account = MemberAccount::where('join_account_member_id', $memberId)
  470. ->where('member_account_classify', 'VIP')
  471. ->first();
  472. if (!$account) {
  473. return json_fail('暂无数据');
  474. }
  475. $balance = $account->member_account_surplus + $account->member_account_added;
  476. $info = PayDetail::where(function ($query) use ($memberId) {
  477. $query->where('join_pay_member_id', $memberId)
  478. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  479. ->where('pay_category', 'VIP')
  480. ->where('pay_status', 'SUCCESS');
  481. })->orWhere(function ($query) use ($memberId) {
  482. $query->where('join_pay_member_id', $memberId)
  483. ->where('pay_prepayid', 'like', '%VIP%')
  484. ->where('pay_status', 'SUCCESS');
  485. })->orderByDesc('pay_addtimes')
  486. ->get()
  487. ->toArray();
  488. $data = [];
  489. foreach ($info as $key => $item) {
  490. $categoryIds = [];
  491. if (!empty($item['join_pay_order_id'])) {
  492. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  493. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  494. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  495. }
  496. $categoryName = [];
  497. foreach ($categoryIds as $id) {
  498. $category = SysCategory::where('category_id', $id)->first();
  499. if (!empty($category->category_super_path)) {
  500. $path = explode('#', $category->category_super_path);
  501. $ids = array_filter($path);
  502. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  503. } else {
  504. $categoryName[] = $category->category_name;
  505. }
  506. }
  507. $item['goods_category'] = implode(',', array_unique($categoryName));
  508. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  509. $item['goods_category'] = '自定义金额充值';
  510. }
  511. if (in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  512. $item['balance_type'] = 'add';
  513. } else {
  514. $item['balance_type'] = 'cut';
  515. }
  516. if ($key == 0 && in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  517. if (!empty($item['pay_extend_json'])) {
  518. $extendJson = json_decode($item['pay_extend_json'], true);
  519. if (!isset($extendJson['added_amount'])) {
  520. $extendJson['added_amount'] = 0;
  521. }
  522. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  523. $data[] = [
  524. 'join_pay_member_id' => $item['join_pay_member_id'],
  525. 'pay_addtimes' => $item['pay_addtimes'],
  526. 'pay_amount' => $extendJson['added_amount'],
  527. 'pay_category' => 'ADDED',
  528. 'balance' => number_format($balance, 2, '.', ''),
  529. 'balance_type' => 'add',
  530. ];
  531. }
  532. $balance = $balance - $extendJson['added_amount'];
  533. }
  534. $item['balance'] = number_format($balance, 2, '.', '');
  535. $balance = $balance - $item['pay_amount'];
  536. $data[] = $item;
  537. continue;
  538. }
  539. if (in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  540. if (!empty($item['pay_extend_json'])) {
  541. $extendJson = json_decode($item['pay_extend_json'], true);
  542. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  543. $data[] = [
  544. 'join_pay_member_id' => $item['join_pay_member_id'],
  545. 'pay_addtimes' => $item['pay_addtimes'],
  546. 'pay_amount' => $extendJson['added_amount'],
  547. 'pay_category' => 'ADDED',
  548. 'balance' => number_format($balance, 2, '.', ''),
  549. 'balance_type' => 'add',
  550. ];
  551. $balance = $balance - $extendJson['added_amount'];
  552. }
  553. }
  554. $item['balance'] = number_format($balance, 2, '.', '');
  555. $balance = $balance - $item['pay_amount'];
  556. } else {
  557. $item['balance'] = number_format($balance, 2, '.', '');
  558. $balance = $balance + $item['pay_amount'];
  559. }
  560. $data[] = $item;
  561. }
  562. $total = count($data);
  563. $start = ($page - 1) * $pageSize;
  564. $rows = array_slice($data, $start, $pageSize);
  565. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  566. }
  567. public static function welfareInfo(Request $request)
  568. {
  569. $memberId = $request->get('member_id', '');
  570. $page = $request->get('page', 1);
  571. $pageSize = $request->get('pageSize', 20);
  572. $account = MemberAccount::where('join_account_member_id', $memberId)
  573. ->where('member_account_classify', 'WELFARE')
  574. ->first();
  575. if (!$account) {
  576. return json_fail('暂无数据');
  577. }
  578. $balance = $account->member_account_surplus + $account->member_account_added;
  579. $info = PayDetail::where(function ($query) use ($memberId) {
  580. $query->where('join_pay_member_id', $memberId)
  581. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  582. ->where('pay_category', 'RECHARGE')
  583. ->where('pay_prepayid', 'WELFARE')
  584. ->where('pay_status', 'SUCCESS');
  585. })->orWhere(function ($query) use ($memberId) {
  586. $query->where('join_pay_member_id', $memberId)
  587. ->where('pay_prepayid', 'like', '%WELFARE%')
  588. ->where('pay_status', 'SUCCESS');
  589. })->orderByDesc('pay_addtimes')
  590. ->get()
  591. ->toArray();
  592. $data = [];
  593. foreach ($info as $key => $item) {
  594. $categoryIds = [];
  595. if (!empty($item['join_pay_order_id'])) {
  596. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  597. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  598. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  599. }
  600. $categoryName = [];
  601. foreach ($categoryIds as $id) {
  602. $category = SysCategory::where('category_id', $id)->first();
  603. if (!empty($category->category_super_path)) {
  604. $path = explode('#', $category->category_super_path);
  605. $ids = array_filter($path);
  606. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  607. } else {
  608. $categoryName[] = $category->category_name;
  609. }
  610. }
  611. $item['goods_category'] = implode(',', array_unique($categoryName));
  612. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  613. $item['goods_category'] = '自定义金额充值';
  614. }
  615. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  616. $item['balance_type'] = 'add';
  617. } else {
  618. $item['balance_type'] = 'cut';
  619. }
  620. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  621. if (!empty($item['pay_extend_json'])) {
  622. $extendJson = json_decode($item['pay_extend_json'], true);
  623. if (!isset($extendJson['added_amount'])) {
  624. $extendJson['added_amount'] = 0;
  625. }
  626. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  627. $data[] = [
  628. 'join_pay_member_id' => $item['join_pay_member_id'],
  629. 'pay_addtimes' => $item['pay_addtimes'],
  630. 'pay_amount' => $extendJson['added_amount'],
  631. 'pay_category' => 'ADDED',
  632. 'balance' => number_format($balance, 2, '.', ''),
  633. 'balance_type' => 'add',
  634. ];
  635. }
  636. $balance = $balance - $extendJson['added_amount'];
  637. }
  638. $item['balance'] = number_format($balance, 2, '.', '');
  639. $balance = $balance - $item['pay_amount'];
  640. $data[] = $item;
  641. continue;
  642. }
  643. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  644. if (!empty($item['pay_extend_json'])) {
  645. $extendJson = json_decode($item['pay_extend_json'], true);
  646. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  647. $data[] = [
  648. 'join_pay_member_id' => $item['join_pay_member_id'],
  649. 'pay_addtimes' => $item['pay_addtimes'],
  650. 'pay_amount' => $extendJson['added_amount'],
  651. 'pay_category' => 'ADDED',
  652. 'balance' => number_format($balance, 2, '.', ''),
  653. 'balance_type' => 'add',
  654. ];
  655. $balance = $balance - $extendJson['added_amount'];
  656. }
  657. }
  658. $item['balance'] = number_format($balance, 2, '.', '');
  659. $balance = $balance - $item['pay_amount'];
  660. } else {
  661. $item['balance'] = number_format($balance, 2, '.', '');
  662. $balance = $balance + $item['pay_amount'];
  663. }
  664. $data[] = $item;
  665. }
  666. $total = count($data);
  667. $start = ($page - 1) * $pageSize;
  668. $rows = array_slice($data, $start, $pageSize);
  669. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  670. }
  671. /**
  672. * @Desc 我的粉丝列表
  673. * @Author Gorden
  674. * @Date 2024/9/20 10:22
  675. *
  676. * @param Request $request
  677. */
  678. public static function fansList(Request $request)
  679. {
  680. $memberId = $request->get('member_id');
  681. $name = $request->get('name', '');
  682. $mobile = $request->get('mobile', '');
  683. $memberIds = [];
  684. $memberInviteIds = [];
  685. if (!empty($name)) {
  686. $memberIds = MemberCert::where('member_cert_name', 'like', '%' . $name . '%')->pluck('join_cert_member_id')->toArray();
  687. $memberInviteIds = Member::whereIn('member_id', $memberIds)->pluck('join_invite_member_id')->toArray();
  688. }
  689. if (!empty($mobile)) {
  690. $members = Member::where('member_mobile', 'like', '%' . $mobile . '%')
  691. ->select('member_id', 'join_invite_member_id')
  692. ->get()
  693. ->toArray();
  694. $memberIds2 = array_column($members, 'member_id');
  695. $memberInviteIds2 = array_column($members, 'join_invite_member_id');
  696. $memberIds = array_merge($memberIds, $memberIds2);
  697. $memberInviteIds = array_merge($memberInviteIds, $memberInviteIds2);
  698. }
  699. $fans1 = Member::with([
  700. 'info' => function ($query) {
  701. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  702. },
  703. 'cert' => function ($query) {
  704. $query->select('join_cert_member_id', 'member_cert_name');
  705. },
  706. 'role' => function ($query) {
  707. $query->select('member_role_id', 'member_role_name');
  708. }
  709. ])->where('join_invite_member_id', $memberId)
  710. ->when(!empty($memberIds), function ($query) use ($memberIds, $memberInviteIds) {
  711. $allSub = array_merge($memberIds, $memberInviteIds);
  712. $query->whereIn('member_id', $allSub);
  713. })
  714. ->select('member_id', 'join_member_role_id', 'member_id as id', 'join_invite_member_id as pid', 'member_partner_json', 'member_mobile', 'member_addtimes', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer')
  715. ->get()
  716. ->toArray();
  717. $fans1Ids = array_column($fans1, 'id');
  718. $fans2 = Member::with([
  719. 'info' => function ($query) {
  720. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  721. },
  722. 'cert' => function ($query) {
  723. $query->select('join_cert_member_id', 'member_cert_name');
  724. },
  725. 'role' => function ($query) {
  726. $query->select('member_role_id', 'member_role_name');
  727. }
  728. ])->whereIn('join_invite_member_id', $fans1Ids)
  729. ->when(!empty($memberIds), function ($query) use ($memberIds) {
  730. $query->whereIn('member_id', $memberIds);
  731. })
  732. ->select('member_id', 'join_member_role_id', 'member_id as id', 'join_invite_member_id as pid', 'member_partner_json', 'member_mobile', 'member_addtimes', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer')
  733. ->get()
  734. ->toArray();
  735. $fans = array_merge($fans1, $fans2);
  736. foreach ($fans as &$fan) {
  737. $memberMobile = $fan['member_mobile'] ?? '';
  738. // $memberNickname = $fan['info'] && $fan['info']['member_info_nickname'] ? $fan['info']['member_info_nickname'] : '';
  739. $memberCertName = $fan['cert'] && $fan['cert']['member_cert_name'] ? $fan['cert']['member_cert_name'] : '';
  740. $fan['member_name'] = '';
  741. if (!empty($memberCertName) && !empty($memberMobile)) {
  742. $fan['member_name'] = $memberCertName . '-' . $memberMobile;
  743. } elseif (!empty($memberCertName)) {
  744. $fan['member_name'] = $memberCertName;
  745. } elseif (!empty($memberMobile)) {
  746. $fan['member_name'] = $memberMobile;
  747. }
  748. if (!empty($fan['info']) && substr($fan['info']['member_info_headimg'], 0, 4) == 'http') {
  749. $fan['member_info_headimg'] = $fan['info']['member_info_headimg'];
  750. } else {
  751. $fan['member_info_headimg'] = 'https://img.wanyuewellness.com.cn/images/avatar_default.png';
  752. }
  753. // 等级名称
  754. $fan['role_name'] = $fan['role'] && $fan['role']['member_role_name'] ? $fan['role']['member_role_name'] : '';
  755. unset($fan['info'], $fan['cert'], $fan['role']);
  756. }
  757. $tree = new Tree($fans);
  758. return json_success('success', $tree->getTree());
  759. }
  760. /**
  761. * 格式化树
  762. * @param $items
  763. * @return Response
  764. */
  765. protected static function formatTree($items): Response
  766. {
  767. $format_items = [];
  768. foreach ($items as $item) {
  769. $format_items[] = [
  770. 'name' => $item->title ?? $item->name ?? $item->id,
  771. 'value' => (string)$item->id,
  772. 'id' => $item->id,
  773. 'pid' => $item->pid,
  774. ];
  775. }
  776. $tree = new Tree($format_items);
  777. return json_success('success', $tree->getTree());
  778. }
  779. public static function clearWelfare(Request $request)
  780. {
  781. $memberId = $request->post('member_id');
  782. $payTime = $request->post('pay_paytimes', date('Y-m-d H:i:s'));
  783. $payTime = date('Y-m-d H:i:s', strtotime($payTime));
  784. $payRemark = $request->post('pay_remark', '');
  785. $clearPwd = $request->post('clear_password');
  786. if (!$clearPwd || $clearPwd != '123456') {
  787. return json_fail('清零密码错误');
  788. }
  789. Db::beginTransaction();
  790. try {
  791. $account = MemberAccount::where('join_account_member_id', $memberId)
  792. ->where('member_account_classify', 'WELFARE')
  793. ->where('member_account_status', 'ACTIVED')
  794. ->first();
  795. if (!$account) {
  796. throw new BusinessException("账户异常");
  797. }
  798. if ($account->member_account_surplus <= 0) {
  799. throw new BusinessException("账户余额为0,无需清零");
  800. }
  801. // 创建清除记录
  802. PayDetail::insert([
  803. 'join_pay_member_id' => $memberId,
  804. 'pay_status' => 'SUCCESS',
  805. 'pay_category' => 'CLEAR',
  806. 'pay_amount' => $account->member_account_surplus,
  807. 'pay_paytimes' => $payTime,
  808. 'pay_prepayid' => $memberId . '-WELFARE',
  809. 'pay_remark' => $payRemark,
  810. 'pay_addtimes' => time()
  811. ]);
  812. // 账户归零
  813. $account->member_account_expend = $account->member_account_expend + $account->member_account_surplus;
  814. $account->member_account_surplus = 0;
  815. $account->save();
  816. Db::commit();
  817. return json_success('success');
  818. } catch (BusinessException $e) {
  819. Db::rollBack();
  820. return json_fail($e->getMessage());
  821. } catch (\Exception $e) {
  822. dump($e->getMessage());
  823. Db::rollBack();
  824. return json_fail('清零失败');
  825. }
  826. }
  827. public static function balanceInfo(Request $request)
  828. {
  829. $memberId = $request->get('member_id', '');
  830. $page = $request->get('page', 1);
  831. $pageSize = $request->get('pageSize', 20);
  832. $account = MemberAccount::where('join_account_member_id', $memberId)
  833. ->where('member_account_classify', 'CASH')
  834. ->first();
  835. if (!$account) {
  836. return json_fail('暂无数据');
  837. }
  838. $balance = $account->member_account_surplus + $account->member_account_added;
  839. $info = PayDetail::where(function ($query) use ($memberId) {
  840. $query->where('join_pay_member_id', $memberId)
  841. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  842. ->whereIn('pay_category', ['RECHARGE','PARTNER'])
  843. ->where('pay_prepayid', '<>', $memberId . '-WELFARE')
  844. ->where('pay_status', 'SUCCESS');
  845. })->orWhere(function ($query) use ($memberId) {
  846. $query->where('join_pay_member_id', $memberId)
  847. ->where('pay_prepayid', 'like', '%CASH%')
  848. ->where('pay_status', 'SUCCESS');
  849. })->orderByDesc('pay_addtimes')
  850. ->get()
  851. ->toArray();
  852. $data = [];
  853. foreach ($info as $key => $item) {
  854. if ($item['pay_category'] == 'PARTNER'){
  855. $payObjectJson = json_decode($item['join_pay_object_json'],true);
  856. if (!Order::where('order_id',$payObjectJson['order_id'])->where('order_is_complete','Y')->exists()){
  857. continue;
  858. }
  859. }
  860. $categoryIds = [];
  861. if (!empty($item['join_pay_order_id'])) {
  862. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  863. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  864. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  865. }
  866. $categoryName = [];
  867. foreach ($categoryIds as $id) {
  868. $category = SysCategory::where('category_id', $id)->first();
  869. if (!empty($category->category_super_path)) {
  870. $path = explode('#', $category->category_super_path);
  871. $ids = array_filter($path);
  872. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  873. } else {
  874. $categoryName[] = $category->category_name;
  875. }
  876. }
  877. $item['goods_category'] = implode(',', array_unique($categoryName));
  878. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  879. $item['goods_category'] = '自定义金额充值';
  880. }
  881. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'PARTNER'])) {
  882. $item['balance_type'] = 'add';
  883. } else {
  884. $item['balance_type'] = 'cut';
  885. }
  886. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'PARTNER'])) {
  887. if (!empty($item['pay_extend_json'])) {
  888. $extendJson = json_decode($item['pay_extend_json'], true);
  889. if (!isset($extendJson['added_amount'])) {
  890. $extendJson['added_amount'] = 0;
  891. }
  892. // 计划任务还没跑
  893. if ($item['pay_category'] == 'RECHARGE' && (!isset($extendJson['notify']) || $extendJson['notify'] != 'success')) {
  894. continue;
  895. }
  896. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  897. $data[] = [
  898. 'join_pay_member_id' => $item['join_pay_member_id'],
  899. 'pay_addtimes' => $item['pay_addtimes'],
  900. 'pay_amount' => $extendJson['added_amount'],
  901. 'pay_category' => $item['pay_category'] == 'GIVE' ? 'GIVE' : 'ADDED',
  902. 'balance' => number_format($balance, 2, '.', ''),
  903. 'balance_type' => 'add',
  904. ];
  905. }
  906. $balance = $balance - $extendJson['added_amount'];
  907. }
  908. $item['balance'] = number_format($balance, 2, '.', '');
  909. $balance = $balance - $item['pay_amount'];
  910. if ($item['pay_category'] != 'GIVE') {
  911. $data[] = $item;
  912. }
  913. continue;
  914. }
  915. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'PARTNER'])) {
  916. if (!empty($item['pay_extend_json'])) {
  917. $extendJson = json_decode($item['pay_extend_json'], true);
  918. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  919. $data[] = [
  920. 'join_pay_member_id' => $item['join_pay_member_id'],
  921. 'pay_addtimes' => $item['pay_addtimes'],
  922. 'pay_amount' => $extendJson['added_amount'],
  923. 'pay_category' => $item['pay_category'] == 'GIVE' ? 'GIVE' : 'ADDED',
  924. 'balance' => number_format($balance, 2, '.', ''),
  925. 'balance_type' => 'add',
  926. ];
  927. $balance = $balance - $extendJson['added_amount'];
  928. }
  929. }
  930. $item['balance'] = number_format($balance, 2, '.', '');
  931. $balance = $balance - $item['pay_amount'];
  932. } else {
  933. $item['balance'] = number_format($balance, 2, '.', '');
  934. $balance = $balance + $item['pay_amount'];
  935. }
  936. if ($item['pay_category'] != 'GIVE') {
  937. $data[] = $item;
  938. }
  939. }
  940. $total = count($data);
  941. $start = ($page - 1) * $pageSize;
  942. $rows = array_slice($data, $start, $pageSize);
  943. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  944. }
  945. public static function pointInfo($memberId)
  946. {
  947. $account = MemberAccount::where('join_account_member_id', $memberId)
  948. ->where('member_account_classify', 'POINTS')
  949. ->first();
  950. if (!$account) {
  951. return json_fail('暂无数据');
  952. }
  953. $balance = $account->member_account_surplus + $account->member_account_added;
  954. $points = ClientPoints::where('join_client_points_member_id', $memberId)->get()->toArray();
  955. $nowPoint = 0;
  956. foreach ($points as $key => &$point) {
  957. $point['goods_category'] = '其他';
  958. if (!empty($point['join_client_points_goods_id'])) {
  959. $categoryId = Goods::where('goods_id', $point['join_client_points_goods_id'])->pluck('join_goods_category_id');
  960. $category = SysCategory::where('category_id', $categoryId)->first();
  961. if (!empty($category->category_super_path)) {
  962. $path = explode('#', $category->category_super_path);
  963. $ids = array_filter($path);
  964. $point['goods_category'] = SysCategory::where('category_id', current($ids))->value('category_name');
  965. } else {
  966. $point['goods_category'] = $category->category_name;
  967. }
  968. }
  969. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  970. $point['balance_type'] = 'add';
  971. } else {
  972. $point['balance_type'] = 'cut';
  973. }
  974. if ($key == 0) {
  975. $point['balance'] = number_format($balance, 2, '.', '');
  976. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  977. $balance = $balance - $point['client_points_nbr'];
  978. } else {
  979. $balance = $balance + $point['client_points_nbr'];
  980. }
  981. continue;
  982. }
  983. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  984. $point['balance'] = number_format($balance, 2, '.', '');
  985. $balance = $balance - $point['client_points_nbr'];
  986. // $point['balance'] = $balance = $nowPoint = number_format($balance - $point['client_points_nbr'], 2, '.', '');
  987. } else {
  988. $point['balance'] = number_format($balance, 2, '.', '');
  989. $balance = $balance + $point['client_points_nbr'];
  990. // $point['balance'] = $balance = $nowPoint = number_format($balance + $point['client_points_nbr'], 2, '.', '');
  991. }
  992. }
  993. return json_success('', $points);
  994. }
  995. /**
  996. * @Desc 添加会员
  997. * @Author Gorden
  998. * @Date 2024/3/25 13:40
  999. *
  1000. * @param $params
  1001. * @return \support\Response
  1002. */
  1003. public static function add($params)
  1004. {
  1005. Db::beginTransaction();
  1006. try {
  1007. $memberId = self::generateMemberId();
  1008. // 先写主表,要ID
  1009. $memberData = [
  1010. 'member_id' => $memberId,
  1011. 'member_is_owner' => $params['is_owner'] ?? 'N',
  1012. 'member_classify' => $params['classify'] ?? '',
  1013. 'member_status' => $params['status'] ?? 'PENDING',
  1014. 'member_mobile' => $params['mobile'] ?? '',
  1015. 'member_category' => $params['member_category'] ?? '后台添加',
  1016. 'member_invite_code' => self::generateInviteCode(),
  1017. // 'member_from' => $params['source'],
  1018. // 'member_extend_json' => json_encode(['come_from' => $params['source']]),
  1019. 'member_addtimes' => time(),
  1020. ];
  1021. $extendJson['come_from'] = $params['source'];
  1022. if (!empty($params['user_id'])) {
  1023. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  1024. if (!$deptId) {
  1025. throw new BusinessException('请先关联业务员和门店');
  1026. }
  1027. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  1028. }
  1029. $memberData['member_extend_json'] = json_encode($extendJson);
  1030. Member::insert($memberData);
  1031. // 账户表
  1032. $accountData = [
  1033. 'join_account_member_id' => $memberId,
  1034. 'member_account_classify' => 'POINTS',
  1035. 'member_account_status' => 'ACTIVED',
  1036. 'member_account_category' => 'NORMAL',
  1037. 'member_account_nbr' => $memberId . '-POINTS',
  1038. 'member_account_name' => '积分账户',
  1039. 'member_account_addtimes' => time()
  1040. ];
  1041. // 积分账户
  1042. MemberAccount::insert($accountData);
  1043. $accountData['member_account_classify'] = 'CASH';
  1044. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1045. $accountData['member_account_name'] = '余额账户';
  1046. // 现金/余额账户
  1047. MemberAccount::insert($accountData);
  1048. // 福利账户
  1049. // if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY'){
  1050. // $welfareData = [
  1051. // 'join_account_member_id'=>$memberId,
  1052. // 'member_account_classify'=>'WELFARE',
  1053. // 'member_account_status'=>'ACTIVED',
  1054. // 'member_account_category'=>'NORMAL',
  1055. // 'member_account_nbr'=>$memberId.'-WELFARE',
  1056. // 'member_account_name'=>'福利账户',
  1057. // 'member_account_addtimes'=>time()
  1058. // ];
  1059. // MemberAccount::insert($welfareData);
  1060. // }
  1061. // member cert
  1062. $certData = [
  1063. 'join_cert_member_id' => $memberId,
  1064. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  1065. 'member_cert_name' => $params['cert_name'] ?? '',
  1066. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  1067. 'member_cert_birth' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  1068. 'member_cert_gender' => $params['gender'] ?? '',
  1069. // 'member_cert_province' => !empty($params['province']) ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  1070. // 'member_cert_city' => $params['city'] ?? '',
  1071. // 'member_cert_addr' => $params['addr'] ?? '',
  1072. // 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  1073. // 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  1074. ];
  1075. MemberCert::insert($certData);
  1076. // member info
  1077. $infoData = [
  1078. 'join_info_member_id' => $memberId,
  1079. 'member_info_nickname' => $params['account_name'] ?? '',
  1080. 'member_info_headimg' => !empty($params['avatar']) ? getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']) : '',
  1081. 'member_info_referee' => $params['member_info_referee'] ?? '',
  1082. 'member_info_business' => $params['member_info_business'] ?? '',
  1083. 'member_info_service' => $params['member_info_service'] ?? '',
  1084. 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
  1085. 'member_info_address' => $params['member_info_address'] ?? '',
  1086. 'member_info_birthday' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  1087. 'member_info_gender' => $params['gender'] ?? '',
  1088. ];
  1089. MemberInfo::insert($infoData);
  1090. Db::commit();
  1091. } catch (BusinessException $e) {
  1092. Db::rollBack();
  1093. return json_fail($e->getMessage());
  1094. } catch (\Exception $e) {
  1095. Db::rollBack();
  1096. dump($e->getMessage() . ';line:' . $e->getLine());
  1097. return json_fail("数据添加失败");
  1098. }
  1099. return json_success('数据添加成功', ['member_id' => $memberId]);
  1100. }
  1101. /**
  1102. * @Desc 编辑会员/ 业主
  1103. * @Author Gorden
  1104. * @Date 2024/4/20 15:18
  1105. *
  1106. * @param $params
  1107. * @return \support\Response
  1108. */
  1109. public static function update($params)
  1110. {
  1111. Db::beginTransaction();
  1112. try {
  1113. $memberId = $params['member_id'];
  1114. $member = Member::where('member_id', $params['member_id'])->first();
  1115. if (!$member) {
  1116. throw new BusinessException('数据异常');
  1117. }
  1118. $extendJson = [];
  1119. if (!empty($member->member_extend_json)) {
  1120. $extendJson = json_decode($member->member_extend_json, true);
  1121. }
  1122. $extendJson['come_from'] = $params['source'];
  1123. // 门店、业务员
  1124. if (!empty($params['user_id'])) {
  1125. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  1126. if (!$deptId) {
  1127. throw new BusinessException('您选择的业务员没有关联门店');
  1128. }
  1129. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  1130. }
  1131. // 先写主表,要ID
  1132. $memberData = [
  1133. 'member_id' => $memberId,
  1134. 'member_is_owner' => $params['is_owner'],
  1135. 'member_classify' => $params['member_classify'],
  1136. 'member_status' => $params['status'],
  1137. // 'member_mobile' => $params['mobile'],
  1138. // 'member_from' => $params['source'],
  1139. 'member_extend_json' => json_encode($extendJson)
  1140. ];
  1141. Member::where('member_id', $params['member_id'])->update($memberData);
  1142. // member cert
  1143. $certData = [
  1144. 'join_cert_member_id' => $memberId,
  1145. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  1146. 'member_cert_name' => $params['cert_name'] ?? '',
  1147. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  1148. 'member_cert_birth' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  1149. 'member_cert_gender' => $params['gender'] ?? '',
  1150. // 'member_cert_province' => $params['province'] ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  1151. // 'member_cert_city' => $params['city'] ?? '',
  1152. // 'member_cert_addr' => $params['addr'] ?? '',
  1153. 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  1154. 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  1155. ];
  1156. if (!MemberCert::where('join_cert_member_id', $memberId)->exists()) {
  1157. MemberCert::insert($certData);
  1158. } else {
  1159. MemberCert::where('join_cert_member_id', $params['member_id'])->update($certData);
  1160. }
  1161. // member info
  1162. $avatar = '';
  1163. if ($params['avatar'] && (strpos($params['avatar'], getenv('APP_IMAGE_DOMAIN')) !== false || strpos($params['avatar'], getenv('SYSTEM_AVATAR_DOMAIN')) !== false)) {
  1164. $avatar = $params['avatar'];
  1165. } else if ($params['avatar']) {
  1166. $avatar = getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']);
  1167. }
  1168. $infoData = [
  1169. 'join_info_member_id' => $memberId,
  1170. 'member_info_nickname' => $params['account_name'],
  1171. 'member_info_headimg' => $avatar,
  1172. 'member_info_referee' => $params['member_info_referee'] ?? '',
  1173. 'member_info_business' => $params['member_info_business'] ?? '',
  1174. 'member_info_service' => $params['member_info_service'] ?? '',
  1175. 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
  1176. 'member_info_address' => $params['member_info_address'] ?? '',
  1177. 'member_info_birthday' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  1178. 'member_info_gender' => $params['gender'] ?? '',
  1179. ];
  1180. if (!MemberInfo::where('join_info_member_id', $params['member_id'])->exists()) {
  1181. MemberInfo::insert($infoData);
  1182. } else {
  1183. MemberInfo::where('join_info_member_id', $params['member_id'])->update($infoData);
  1184. }
  1185. // 福利账户
  1186. if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY' && !MemberAccount::where('member_account_nbr', $memberId . '-WELFARE')->exists()) {
  1187. $welfareData = [
  1188. 'join_account_member_id' => $memberId,
  1189. 'member_account_classify' => 'WELFARE',
  1190. 'member_account_status' => 'ACTIVED',
  1191. 'member_account_category' => 'NORMAL',
  1192. 'member_account_nbr' => $memberId . '-WELFARE',
  1193. 'member_account_name' => '福利账户',
  1194. 'member_account_addtimes' => time()
  1195. ];
  1196. MemberAccount::insert($welfareData);
  1197. }
  1198. Db::commit();
  1199. } catch (BusinessException $e) {
  1200. Db::rollBack();
  1201. dump($e->getMessage() . ';line:' . $e->getLine());
  1202. return json_fail($e->getMessage());
  1203. } catch (\Exception $e) {
  1204. Db::rollBack();
  1205. dump($e->getMessage() . ';line:' . $e->getLine());
  1206. return json_fail("数据更新失败");
  1207. }
  1208. return json_success('数据更新成功');
  1209. }
  1210. /**
  1211. * @Desc 会员关联设备列表
  1212. * @Author Gorden
  1213. * @Date 2024/4/7 9:52
  1214. *
  1215. * @param $id
  1216. * @return \support\Response
  1217. */
  1218. public static function deviceList($id)
  1219. {
  1220. $family = Family::where('join_family_creator_member_id', $id)->get()->toArray();
  1221. $familyMemberIds = '';
  1222. if ($family) {
  1223. $familyIds = array_column($family, 'family_id');
  1224. $familyMember = FamilyMember::whereIn('join_family_id', $familyIds)->get()->toArray();
  1225. $familyMemberIds = array_column($familyMember, 'join_family_member_id');
  1226. }
  1227. $rows = MemberDevice::leftJoin('device', 'device.device_id', '=', 'member_device.join_member_device_id')
  1228. ->leftJoin('member', 'member.member_id', '=', 'member_device.join_device_member_id')
  1229. ->leftJoin('family_member', 'family_member.join_family_member_id', '=', 'member_device.join_device_member_id')
  1230. ->select('device.device_category', 'device.device_name', 'device.device_type', 'member.member_mobile', 'member_device.*', 'family_member.family_member_name')
  1231. ->when(!empty($familyMemberIds), function ($query) use ($familyMemberIds) {
  1232. $query->whereIn('member_device.join_device_master_member_id', $familyMemberIds);
  1233. })
  1234. ->when(empty($familyMemberIds), function ($query) use ($id) {
  1235. $query->where('member_device.join_device_master_member_id', $id);
  1236. })->orderBy('member_device_addtimes', 'desc')
  1237. ->get()
  1238. ->toArray();
  1239. return json_success('', ['rows' => $rows]);
  1240. }
  1241. /**
  1242. * @Desc 绑定设备
  1243. * @Author Gorden
  1244. * @Date 2024/4/7 10:27
  1245. *
  1246. * @param Request $request
  1247. * @return \support\Response
  1248. */
  1249. public static function addDevice(Request $request)
  1250. {
  1251. $params = $request->post();
  1252. Db::beginTransaction();
  1253. try {
  1254. $data = [
  1255. 'join_device_member_id' => $params['join_device_member_id'] ?? '',
  1256. 'join_device_master_member_id' => $params['join_device_master_member_id'],
  1257. 'join_member_device_id' => $params['join_member_device_id'],
  1258. 'member_device_status' => $params['member_device_status'],
  1259. 'member_device_key' => $params['member_device_key'] ?? '',
  1260. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1261. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1262. 'member_device_addtimes' => time(),
  1263. ];
  1264. MemberDevice::insert($data);
  1265. $device = Device::where('device_id', $params['join_member_device_id'])->whereIn('device_status', ['PROCESSING', 'PENDING'])->first();
  1266. // 激活设备
  1267. if (!$device) {
  1268. Db::rollBack();
  1269. return json_fail('当前设备不是可激活状态');
  1270. }
  1271. $device->device_status = 'ACTIVED';
  1272. $device->save();
  1273. Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => 'ACTIVED']);
  1274. _syslog("绑定设备", "用户【" . ($params['join_device_member_id'] ?? $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1275. Db::commit();
  1276. } catch (\Exception $e) {
  1277. Db::rollBack();
  1278. dump($e->getMessage());
  1279. return json_fail('绑定失败');
  1280. }
  1281. return json_success('绑定成功');
  1282. }
  1283. public static function updateDevice(Request $request)
  1284. {
  1285. $params = $request->post();
  1286. Db::beginTransaction();
  1287. try {
  1288. $data = [
  1289. 'member_device_status' => $params['member_device_status'],
  1290. 'member_device_key' => $params['member_device_key'],
  1291. 'join_device_member_id' => $params['join_device_member_id'],
  1292. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1293. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1294. ];
  1295. MemberDevice::where('member_device_id', $request->post('member_device_id'))->update($data);
  1296. $device = Device::where('device_id', $params['join_member_device_id'])->first();
  1297. if ($device->device_status != $params['member_device_status']) {
  1298. $device->device_status = $params['member_device_status'];
  1299. $device->save();
  1300. }
  1301. // Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => $params['member_device_status']]);
  1302. Db::commit();
  1303. _syslog("更新绑定设备", "用户【" . ($params['join_device_member_id'] == "" ? $params['join_device_member_id'] : $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1304. } catch (\Exception $e) {
  1305. Db::rollBack();
  1306. dump($e->getMessage());
  1307. return json_fail('绑定更新失败');
  1308. }
  1309. return json_success('绑定更新成功');
  1310. }
  1311. public static function deleteDevice(Request $request)
  1312. {
  1313. try {
  1314. Db::beginTransaction();
  1315. $memberDevice = MemberDevice::where('member_device_id', $request->post('member_device_id'))->first();
  1316. if (!$memberDevice) {
  1317. return json_fail('数据不存在');
  1318. }
  1319. $device = Device::where('device_id', $memberDevice->join_member_device_id)->first();
  1320. $device->device_status = "DISABLED";
  1321. $device->save();
  1322. // Device::where('device_id', $memberDevice->join_member_device_id)->update(['device_status' => 'DISABLED']);
  1323. $memberDevice->delete();
  1324. Db::commit();
  1325. _syslog("解除绑定设备", "用户【" . ($memberDevice->join_device_member_id == "" ? $memberDevice->join_device_member_id : $memberDevice->join_device_master_member_id) . '】,解除绑定设备【' . $device->device_name . '】');
  1326. return json_success('解除绑定成功');
  1327. } catch (\Exception $e) {
  1328. Db::rollBack();
  1329. return json_fail('绑定失败');
  1330. }
  1331. }
  1332. /**
  1333. * 手动创建会员
  1334. */
  1335. public static function createMember($params)
  1336. {
  1337. $memberId = $params['member_id'] ?? '';
  1338. if (!$memberId) {
  1339. throw new BusinessException("参数异常");
  1340. }
  1341. // 主表
  1342. $memberData = [
  1343. 'member_id' => $memberId,
  1344. 'member_is_owner' => 'N',
  1345. 'member_classify' => 'MEMBER',
  1346. 'member_status' => 'ACTIVED',
  1347. 'member_mobile' => $params['mobile'],
  1348. 'member_invite_code' => self::generateInviteCode(),
  1349. 'member_extend_json' => json_encode(['come_from' => 'SYSTEM']),
  1350. 'member_addtimes' => time()
  1351. ];
  1352. Member::insert($memberData);
  1353. // 账户表
  1354. $accountData = [
  1355. 'join_account_member_id' => $memberId,
  1356. 'member_account_classify' => 'POINTS',
  1357. 'member_account_status' => 'ACTIVED',
  1358. 'member_account_category' => 'NORMAL',
  1359. 'member_account_nbr' => $memberId . '-POINTS',
  1360. 'member_account_name' => '积分账户',
  1361. 'member_account_addtimes' => time()
  1362. ];
  1363. // 积分账户
  1364. MemberAccount::insert($accountData);
  1365. $accountData['member_account_classify'] = 'CASH';
  1366. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1367. $accountData['member_account_name'] = '余额账户';
  1368. // 现金/余额账户
  1369. MemberAccount::insert($accountData);
  1370. // info
  1371. $infoData = [
  1372. 'join_info_member_id' => $memberId,
  1373. 'member_info_nickname' => $params['username']
  1374. ];
  1375. MemberInfo::insert($infoData);
  1376. }
  1377. private static function generateMemberId()
  1378. {
  1379. $id = SysSerial::getSerial();
  1380. return "MR" . date('ymdHi') . random_string(4, 'up');
  1381. }
  1382. public static function getMemberName($mobile, $certName, $nickname)
  1383. {
  1384. $name = '';
  1385. if (!empty($certName)) {
  1386. $name = $certName;
  1387. } elseif (!empty($nickname)) {
  1388. $name = $nickname;
  1389. }
  1390. if (!empty($name) && !empty($mobile)) {
  1391. $name .= '-' . $mobile;
  1392. } elseif (!empty($mobile)) {
  1393. $name = $mobile;
  1394. }
  1395. return $name;
  1396. }
  1397. public static function getMemberNameByMemberId($memberId)
  1398. {
  1399. $member = Member::with([
  1400. 'cert' => function ($query) {
  1401. $query->select('join_cert_member_id', 'member_cert_name');
  1402. },
  1403. 'info' => function ($query) {
  1404. $query->select('join_info_member_id', 'member_info_nickname');
  1405. },
  1406. ]
  1407. )->where('member_id', $memberId)
  1408. ->first();
  1409. if (empty($member)) {
  1410. return;
  1411. }
  1412. $member = $member->toArray();
  1413. $mobile = $certName = $nickname = '';
  1414. if (!empty($member['member_mobile'])) {
  1415. $mobile = $member['member_mobile'];
  1416. }
  1417. if (!empty($member['cert']) && !empty($member['cert']['member_cert_name'])) {
  1418. $certName = $member['cert']['member_cert_name'];
  1419. }
  1420. if (!empty($member['info']) && !empty($member['info']['member_info_nickname'])) {
  1421. $nickname = $member['info']['member_info_nickname'];
  1422. }
  1423. return self::getMemberName($mobile, $certName, $nickname);
  1424. }
  1425. /**
  1426. * @Desc 生成推荐码
  1427. * @Author Gorden
  1428. * @Date 2024/9/6 9:05
  1429. *
  1430. * @return string
  1431. */
  1432. public static function generateInviteCode()
  1433. {
  1434. $code = random_string(8, 'number');
  1435. if (Member::where('member_invite_code', $code)->exists()) {
  1436. return self::generateInviteCode();
  1437. } else {
  1438. return $code;
  1439. }
  1440. }
  1441. }