MemberService.php 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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\MemberAccountList;
  13. use app\model\MemberCert;
  14. use app\model\MemberDevice;
  15. use app\model\MemberInfo;
  16. use app\model\MemberRole;
  17. use app\model\Order;
  18. use app\model\OrderSheet;
  19. use app\model\PayDetail;
  20. use app\model\SysCategory;
  21. use app\model\SysDept;
  22. use app\model\SysSerial;
  23. use app\model\SysUser;
  24. use support\Db;
  25. use support\exception\BusinessException;
  26. use support\Log;
  27. use support\Request;
  28. use support\Response;
  29. class MemberService
  30. {
  31. public static function list(Request $request)
  32. {
  33. $page = $request->get('page', 1);
  34. $pageSize = $request->get('pageSize', 10);
  35. $name = $request->get('name', '');
  36. $nickname = $request->get('nickname', '');
  37. $mobile = $request->get('mobile', '');
  38. $level = $request->get('level', '');
  39. $isAuth = $request->get('is_auth', '');
  40. $addtime = $request->get('member_addtimes', []);
  41. $type = $request->get('type', '');
  42. $status = $request->get('member_status', '');
  43. $memberIsPartner = $request->get('member_is_partner', '');
  44. $memberIsReferrer = $request->get('member_is_referrer', '');
  45. // 兼容老等级搜索
  46. $levelIds = [];
  47. if (!empty($level)) {
  48. $levelName = MemberRole::where('member_role_id', $level)->value('member_role_name');
  49. if (!empty($levelName)) {
  50. $levelIds = MemberRole::where('member_role_name', $levelName)->pluck('member_role_id')->toArray();
  51. }
  52. }
  53. $rows = Member::with([
  54. 'account' => function ($query) {
  55. $query->where('member_account_status', 'ACTIVED');
  56. }
  57. ])->where('member_is_owner', 'N')
  58. ->where('member_mobile', '<>', '0000')
  59. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  60. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  61. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  62. ->when($name != '', function ($query) use ($name) {
  63. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  64. })->when($mobile != '', function ($query) use ($mobile) {
  65. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  66. })->when($level != '', function ($query) use ($level, $levelIds) {
  67. if ($level == 'other') {
  68. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  69. } else if ($level == 'VIP') {
  70. $query->where('member.member_is_vip', 'Y');
  71. } else if (!empty($levelIds)) {
  72. $query->whereIn('member_role.member_role_id', $levelIds);
  73. } else {
  74. $query->where('member_role.member_role_id', $level);
  75. }
  76. })->when($isAuth != '', function ($query) use ($isAuth) {
  77. if ($isAuth == 'Y') {
  78. $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', '<>', '');
  79. } else {
  80. $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', '');
  81. }
  82. })->when(!empty($addtime), function ($query) use ($addtime) {
  83. $addtime[0] = strtotime($addtime[0]);
  84. $addtime[1] = strtotime($addtime[1]);
  85. $query->whereBetween('member.member_addtimes', $addtime);
  86. })->when(!empty($type), function ($query) use ($type) {
  87. if ($type == 'todayAdd') {
  88. $addtime[0] = strtotime(date('Y-m-d') . ' 00:00:00');
  89. $addtime[1] = strtotime(date('Y-m-d') . ' 23:59:59');
  90. } else if ($type == 'monthAdd') {
  91. $addtime[0] = strtotime(date('Y-m-01') . ' 00:00:00');
  92. $addtime[1] = strtotime(date('Y-m-t') . ' 23:59:59');
  93. }
  94. $query->whereBetween('member.member_addtimes', $addtime);
  95. })->when(!empty($nickname), function ($query) use ($nickname) {
  96. $query->where(function ($query2) use ($nickname) {
  97. $query2->where('member_info.member_info_nickname', 'like', '%' . $nickname . '%')->orWhere('member.member_mobile', 'like', '%' . $nickname . '%');
  98. });
  99. })->when(!empty($status), function ($query) use ($status) {
  100. $query->where('member_status', $status);
  101. })->when(!empty($memberIsPartner), function ($query) use ($memberIsPartner) {
  102. if ($memberIsPartner == 'Y') {
  103. $query->where('member_is_partner', 'Y');
  104. }
  105. })->when(!empty($memberIsReferrer), function ($query) use ($memberIsReferrer) {
  106. if ($memberIsReferrer == 'Y') {
  107. $query->where('member_is_referrer', 'Y');
  108. }
  109. })
  110. ->select('member_id', 'member_is_vip', 'member_is_owner', 'join_invite_member_id', 'member_is_partner','member_partner_json', 'member_is_referrer','member_referrer_json', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  111. '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',
  112. 'member_role.member_role_id', 'member_role.member_role_name',
  113. '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'
  114. // 'member_account.member_account_surplus'
  115. );
  116. // 总条数
  117. $total = $rows->count('member_id');
  118. // 已认证条数
  119. $authModel = clone $rows;
  120. $authTotal = $authModel->where('member_cert.member_cert_name','<>','')
  121. ->where('member_cert.member_cert_nbr','<>','')
  122. ->count();
  123. $rows = $rows->orderBy('member_addtimes', 'DESC')
  124. ->forPage($page, $pageSize)
  125. ->get()
  126. ->toArray();
  127. foreach ($rows as &$row) {
  128. $row['coupon_count'] = CouponDetail::where('join_coupon_detail_member_id', $row['member_id'])->count();
  129. $row['member_mobile'] = substr($row['member_mobile'], 0, 3) . '****' . substr($row['member_mobile'], 7);
  130. // 推荐人
  131. if (!empty($row['join_invite_member_id'])) {
  132. $row['referrer_name'] = MemberService::getMemberNameByMemberId($row['join_invite_member_id']);
  133. }
  134. $row['info'] = [
  135. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '会员',
  136. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : '',
  137. 'member_info_referee' => !empty($row['member_info_referee']) ? $row['member_info_referee'] : '',
  138. 'member_info_business' => !empty($row['member_info_business']) ? $row['member_info_business'] : '',
  139. 'member_info_service' => !empty($row['member_info_service']) ? $row['member_info_service'] : '',
  140. 'member_info_city' => !empty($row['member_info_city']) ? explode('-', $row['member_info_city']) : '',
  141. 'member_info_address' => !empty($row['member_info_address']) ? $row['member_info_address'] : '',
  142. 'member_info_birthday' => !empty($row['member_info_birthday']) ? $row['member_info_birthday'] : '',
  143. 'member_info_gender' => !empty($row['member_info_gender']) ? $row['member_info_gender'] : '',
  144. ];
  145. if (substr($row['info']['member_info_headimg'], 0, 2) == '..') {
  146. $row['info']['member_info_headimg'] = '';
  147. }
  148. if (!empty($row['info']['member_info_address']) && in_array($row['info']['member_info_address'], ['请选择', '省-市-区'])) {
  149. $row['info']['member_info_address'] = '';
  150. }
  151. $row['cert'] = [
  152. 'member_cert_birth' => $row['member_cert_birth'],
  153. 'member_cert_gender' => $row['member_cert_gender'],
  154. 'member_cert_name' => $row['member_cert_name'],
  155. 'member_cert_nbr' => $row['member_cert_nbr'],
  156. 'member_cert_province' => !empty($row['member_cert_province']) && is_json($row['member_cert_province']) ? json_decode($row['member_cert_province']) : '',
  157. 'member_cert_addr' => $row['member_cert_addr'],
  158. 'member_cert_face' => !empty($row['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_face'] : '',
  159. 'member_cert_photo' => !empty($row['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_photo'] : '',
  160. 'member_cert_nation' => $row['member_cert_nation'],
  161. ];
  162. $account = [];
  163. if (!empty($row['account'])) {
  164. foreach ($row['account'] as $item) {
  165. if ($item['member_account_classify'] == 'WELFARE') {
  166. $account['welfare'] = [
  167. 'member_account_name' => $item['member_account_name'],
  168. 'member_account_income' => $item['member_account_income'],
  169. 'member_account_expend' => $item['member_account_expend'],
  170. 'member_account_surplus' => $item['member_account_surplus'],
  171. 'member_account_added' => $item['member_account_added'],
  172. ];
  173. } else if ($item['member_account_classify'] == 'POINTS') {
  174. $account['points'] = [
  175. 'member_account_name' => $item['member_account_name'],
  176. 'member_account_income' => $item['member_account_income'],
  177. 'member_account_expend' => $item['member_account_expend'],
  178. 'member_account_surplus' => $item['member_account_surplus'],
  179. 'member_account_added' => $item['member_account_added'],
  180. ];
  181. } else if ($item['member_account_classify'] == 'CASH') {
  182. $account['cash'] = [
  183. 'member_account_name' => $item['member_account_name'],
  184. 'member_account_income' => $item['member_account_income'],
  185. 'member_account_expend' => $item['member_account_expend'],
  186. 'member_account_surplus' => $item['member_account_surplus'],
  187. 'member_account_added' => $item['member_account_added'],
  188. ];
  189. } else if ($item['member_account_classify'] == 'CARD') {
  190. $account['card'] = [
  191. 'member_account_name' => $item['member_account_name'],
  192. 'member_account_income' => $item['member_account_income'],
  193. 'member_account_expend' => $item['member_account_expend'],
  194. 'member_account_surplus' => $item['member_account_surplus'],
  195. 'member_account_added' => $item['member_account_added'],
  196. ];
  197. } else if ($item['member_account_classify'] == 'VIP') {
  198. $account['vip'] = [
  199. 'member_account_name' => $item['member_account_name'],
  200. 'member_account_income' => $item['member_account_income'],
  201. 'member_account_expend' => $item['member_account_expend'],
  202. 'member_account_surplus' => $item['member_account_surplus'],
  203. 'member_account_added' => $item['member_account_added'],
  204. ];
  205. }
  206. }
  207. }
  208. $row['account'] = $account;
  209. $row['is_auth'] = "未认证";
  210. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  211. $row['is_auth'] = "已认证";
  212. }
  213. $row['source'] = "APP";
  214. $row['belong'] = [];
  215. $row['referee'] = [];
  216. if (!empty($row['member_extend_json'])) {
  217. $extendJson = json_decode($row['member_extend_json'], true);
  218. if (isset($extendJson['come_from'])) {
  219. $row['source'] = $extendJson['come_from'] ?? '';
  220. }
  221. if (isset($extendJson['belong'])) {
  222. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  223. if ($user) {
  224. $row['belong']['user_id'] = $extendJson['belong']['user_id'];
  225. $row['belong']['username'] = $user->user_name;
  226. $row['belong']['user_mobile'] = $user->user_mobile;
  227. }
  228. $row['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  229. }
  230. if (isset($extendJson['referee'])) {
  231. foreach ($extendJson['referee'] as $key => $referee) {
  232. if (preg_match('/^1[3-9]\d{9}$/', $referee)) {
  233. $member = Member::with([
  234. 'cert' => function ($query) {
  235. $query->select('join_cert_member_id', 'member_cert_name');
  236. }
  237. ])->where('member_mobile', $referee)->where('member_status', 'ACTIVED')
  238. ->first();
  239. if ($member->cert && $member->cert->member_cert_name) {
  240. $referee = $member->cert->member_cert_name . '-' . $referee;
  241. }
  242. }
  243. $row['referee'][] = [
  244. 'name' => $referee,
  245. 'time' => $key
  246. ];
  247. }
  248. }
  249. }
  250. // 粉丝
  251. $fans1 = Member::where('join_invite_member_id', $row['member_id'])
  252. ->select('member_id')
  253. ->get()
  254. ->toArray();
  255. $fans1Ids = array_column($fans1, 'member_id');
  256. $fans2Count = Member::whereIn('join_invite_member_id', $fans1Ids)
  257. ->count();
  258. $row['fans_total'] = count($fans1) + $fans2Count;
  259. // 合伙人信息
  260. if ($row['member_is_partner'] == 'Y' && !empty($row['member_partner_json'])){
  261. $partnerJson = json_decode($row['member_partner_json'],true);
  262. $row['partner_add_times'] = $partnerJson['datetime'] ?? '';
  263. unset($row['member_partner_json']);
  264. }
  265. // 推荐信息
  266. if ($row['member_is_referrer'] == 'Y' && !empty($row['member_referrer_json'])){
  267. $referrerJson = json_decode($row['member_referrer_json'],true);
  268. $row['referrer_add_times'] = $referrerJson['datetime'] ?? '';
  269. unset($row['member_referrer_json']);
  270. }
  271. }
  272. return json_success('', compact('rows', 'page', 'pageSize', 'total','authTotal'));
  273. }
  274. public static function exportMember(Request $request)
  275. {
  276. $memberIds = $request->get('member_id', '');
  277. $name = $request->get('name', '');
  278. $mobile = $request->get('mobile', '');
  279. $level = $request->get('level', '');
  280. $isAuth = $request->get('is_auth', '');
  281. $addtime = $request->get('member_addtimes', []);
  282. $isPartner = $request->get('member_is_partner', '');
  283. $isReferrer = $request->get('member_is_referrer', '');
  284. $rows = Member::with('account')
  285. ->where('member_is_owner', 'N')
  286. ->where('member_mobile', '<>', '0000')
  287. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  288. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  289. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  290. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  291. // ->where('member_account.member_account_classify', 'CASH')
  292. ->when($memberIds != '', function ($query) use ($memberIds) {
  293. $query->whereIn('member.member_id', $memberIds);
  294. })->when($name != '', function ($query) use ($name) {
  295. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  296. })->when($mobile != '', function ($query) use ($mobile) {
  297. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  298. })->when($isPartner != '', function ($query) use ($isPartner) {
  299. $query->where('member.member_is_partner', $isPartner);
  300. })->when($isReferrer != '', function ($query) use ($isReferrer) {
  301. $query->where('member.member_is_referrer', $isReferrer);
  302. })->when($level != '', function ($query) use ($level) {
  303. if ($level == 'other') {
  304. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  305. } else {
  306. $query->where('member_role.member_role_id', $level);
  307. }
  308. })->when($isAuth != '', function ($query) use ($isAuth) {
  309. if ($isAuth == 'Y') {
  310. $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', '<>', '');
  311. } else {
  312. $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', '');
  313. }
  314. })->when(!empty($addtime), function ($query) use ($addtime) {
  315. $addtime[0] = strtotime($addtime[0]);
  316. $addtime[1] = strtotime($addtime[1]);
  317. $query->whereBetween('member.member_addtimes', $addtime);
  318. })->select('member_id', 'join_invite_member_id', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  319. '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',
  320. 'member_role.member_role_id', 'member_role.member_role_name',
  321. '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',
  322. // 'member_account.member_account_surplus'
  323. )->orderBy('member_addtimes', 'DESC')
  324. ->get()
  325. ->toArray();
  326. $exportData = [];
  327. foreach ($rows as $row) {// 推荐人
  328. if (!empty($row['join_invite_member_id'])) {
  329. $row['referrer_name'] = MemberService::getMemberNameByMemberId($row['join_invite_member_id']);
  330. }
  331. $row['info'] = [
  332. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '会员',
  333. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : '',
  334. 'member_info_referee' => !empty($row['member_info_referee']) ? $row['member_info_referee'] : '',
  335. 'member_info_business' => !empty($row['member_info_business']) ? $row['member_info_business'] : '',
  336. 'member_info_service' => !empty($row['member_info_service']) ? $row['member_info_service'] : ''
  337. ];
  338. $row['cert'] = [
  339. 'member_cert_name' => $row['member_cert_name'],
  340. ];
  341. $account = [];
  342. if (!empty($row['account'])) {
  343. foreach ($row['account'] as $item) {
  344. if ($item['member_account_classify'] == 'POINTS') {
  345. $account['points'] = [
  346. 'member_account_surplus' => $item['member_account_surplus'],
  347. 'member_account_added' => $item['member_account_added'],
  348. ];
  349. } else if ($item['member_account_classify'] == 'CASH') {
  350. $account['cash'] = [
  351. 'member_account_surplus' => $item['member_account_surplus'],
  352. 'member_account_added' => $item['member_account_added'],
  353. ];
  354. } else if ($item['member_account_classify'] == 'CARD') {
  355. $account['card'] = [
  356. 'member_account_surplus' => $item['member_account_surplus'],
  357. 'member_account_added' => $item['member_account_added'],
  358. ];
  359. }
  360. }
  361. }
  362. // $row['account'] = $account;
  363. $row['is_auth'] = "未认证";
  364. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  365. $row['is_auth'] = "已认证";
  366. }
  367. $row['source'] = "APP";
  368. if (!empty($row['member_extend_json'])) {
  369. $extendJson = json_decode($row['member_extend_json'], true);
  370. if (isset($extendJson['come_from'])) {
  371. $row['source'] = $extendJson['come_from'] ?? '';
  372. }
  373. }
  374. $sourceStr = ['APP' => "APP", 'CUSTOMER' => "客户管理", 'SYSTEM' => "后台"];
  375. $exportData[] = [
  376. 'member_id' => $row['member_id'],
  377. 'member_info_nickname' => $row['info']['member_info_nickname'] ?? '',
  378. 'member_mobile' => $row['member_mobile'] ?? '',
  379. 'member_cert_name' => $row['cert']['member_cert_name'] ?? '',
  380. 'source' => in_array($row['source'], $sourceStr) ? $sourceStr[$row['source']] : 'APP',
  381. 'cash' => isset($account['cash']) ? $account['cash']['member_account_surplus'] + $account['cash']['member_account_added'] : 0,
  382. 'points' => isset($account['points']) ? $account['points']['member_account_surplus'] + $account['points']['member_account_added'] : 0,
  383. 'is_auth' => $row['is_auth'],
  384. 'level' => $row['member_role_name'] ?? '普通会员',
  385. 'member_addtimes' => $row['member_addtimes'],
  386. 'referrer_name' => $row['referrer_name'] ?? '',
  387. 'member_info_business' => $row['info']['member_info_business'] ?? '',
  388. 'member_info_service' => $row['info']['member_info_service'] ?? '',
  389. ];
  390. }
  391. return json_success('', $exportData);
  392. }
  393. public static function info($memberId)
  394. {
  395. $member = Member::with([
  396. 'cert',
  397. 'info',
  398. 'role' => function ($query) {
  399. $query->select('member_role_id', 'member_role_name');
  400. },
  401. 'account'
  402. ])
  403. ->where('member_id', $memberId)
  404. ->first();
  405. if (!$member) {
  406. return json_fail('查询错误');
  407. }
  408. $member = $member->toArray();
  409. unset($member['member_password']);
  410. $member['info'] = [
  411. 'member_info_nickname' => !empty($member['info']) && !empty($member['info']['member_info_nickname']) ? $member['info']['member_info_nickname'] : substr($member['member_mobile'], -4, 4) . '会员',
  412. 'member_info_headimg' => !empty($member['info']) && !empty($member['info']['member_info_headimg']) ? $member['info']['member_info_headimg'] : '',
  413. 'member_info_referee' => !empty($member['info']) && !empty($member['info']['member_info_referee']) ? $member['info']['member_info_referee'] : '',
  414. 'member_info_business' => !empty($member['info']) && !empty($member['info']['member_info_business']) ? $member['info']['member_info_business'] : '',
  415. 'member_info_service' => !empty($member['info']) && !empty($member['info']['member_info_service']) ? $member['info']['member_info_service'] : '',
  416. 'member_info_city' => !empty($member['info']) && !empty($member['info']['member_info_city']) ? explode('-', $member['info']['member_info_city']) : '',
  417. 'member_info_address' => !empty($member['info']) && !empty($member['info']['member_info_address']) ? $member['info']['member_info_address'] : '',
  418. 'member_info_gender' => !empty($member['info']) && !empty($member['info']['member_info_gender']) ? $member['info']['member_info_gender'] : '',
  419. 'member_info_birthday' => !empty($member['info']) && !empty($member['info']['member_info_birthday']) ? $member['info']['member_info_birthday'] : '',
  420. ];
  421. if (!empty($member['info']['member_info_address']) && in_array($member['info']['member_info_address'], ['请选择', '省-市-区'])) {
  422. $member['info']['member_info_address'] = '';
  423. }
  424. $member['cert'] = [
  425. 'member_cert_birth' => $member['cert']['member_cert_birth'] ?? '',
  426. 'member_cert_gender' => $member['cert']['member_cert_gender'] ?? '',
  427. 'member_cert_name' => $member['cert']['member_cert_name'] ?? '',
  428. 'member_cert_nbr' => $member['cert']['member_cert_nbr'] ?? '',
  429. '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']) : '',
  430. 'member_cert_addr' => $member['cert']['member_cert_addr'] ?? '',
  431. 'member_cert_face' => !empty($member['cert']) && !empty($member['cert']['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $member['cert']['member_cert_face'] : '',
  432. 'member_cert_photo' => !empty($member['cert']) && !empty($member['cert']['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $member['cert']['member_cert_photo'] : '',
  433. 'member_cert_nation' => $member['cert']['member_cert_nation'] ?? '',
  434. 'member_cert_addtimes' => $member['cert']['member_cert_addtimes'] ?? '',
  435. ];
  436. if (!empty($member['cert']['member_cert_addtimes'])) {
  437. $member['cert']['member_cert_addtimes'] = date('Y-m-d', $member['cert']['member_cert_addtimes']);
  438. } else {
  439. $member['cert']['member_cert_addtimes'] = date('Y-m-d', strtotime($member['member_addtimes']));
  440. }
  441. if (!empty($member['join_invite_member_id'])) {
  442. $inviteMember = Member::with([
  443. 'info' => function ($query) {
  444. $query->select('join_info_member_id', 'member_info_nickname');
  445. },
  446. 'cert' => function ($query) {
  447. $query->select('join_cert_member_id', 'member_cert_name');
  448. }
  449. ])->where('member_id', $member['join_invite_member_id'])
  450. ->first();
  451. $inviteMemberName = '';
  452. if (!empty($inviteMember->cert) && !empty($inviteMember->cert->member_cert_name)) {
  453. $inviteMemberName = $inviteMember->cert->member_cert_name;
  454. } elseif (!empty($inviteMemberName->info) && !empty($inviteMemberName->info->member_info_nickname)) {
  455. $inviteMemberName = $inviteMemberName->info->member_info_nickname;
  456. }
  457. $member['invite_member'] = $inviteMemberName . (!empty($inviteMember->member_invite_code) ? '(' . $inviteMember->member_invite_code . ')' : '');
  458. }
  459. $member['is_auth'] = "未认证";
  460. if (!empty($member['cert']['member_cert_nbr']) && !empty($member['cert']['member_cert_name'])) {
  461. $member['is_auth'] = "已认证";
  462. }
  463. $member['source'] = "APP";
  464. $member['belong'] = [];
  465. $member['referee'] = [];
  466. if (!empty($member['member_extend_json'])) {
  467. $extendJson = json_decode($member['member_extend_json'], true);
  468. if (isset($extendJson['come_from'])) {
  469. $member['source'] = $extendJson['come_from'] ?? '';
  470. }
  471. if (isset($extendJson['belong'])) {
  472. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  473. if ($user) {
  474. $member['belong']['user_id'] = $extendJson['belong']['user_id'];
  475. $member['belong']['username'] = $user->user_name;
  476. $member['belong']['user_mobile'] = $user->user_mobile;
  477. }
  478. $member['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  479. }
  480. if (isset($extendJson['referee'])) {
  481. foreach ($extendJson['referee'] as $key => $referee) {
  482. if (preg_match('/^1[3-9]\d{9}$/', $referee)) {
  483. $refereeMember = Member::with([
  484. 'cert' => function ($query) {
  485. $query->select('join_cert_member_id', 'member_cert_name');
  486. }
  487. ])->where('member_mobile', $referee)->where('member_status', 'ACTIVED')
  488. ->first();
  489. if ($refereeMember->cert && $refereeMember->cert->member_cert_name) {
  490. $referee = $refereeMember->cert->member_cert_name . '-' . $referee;
  491. }
  492. }
  493. $member['referee'][] = [
  494. 'name' => $referee,
  495. 'time' => $key
  496. ];
  497. }
  498. }
  499. }
  500. if (!empty($member['join_invite_member_id'])) {
  501. $inviteMember = Member::with('cert', 'info')
  502. ->where('member_id', $member['join_invite_member_id'])
  503. ->select('member_id', 'member_mobile', 'member_invite_code')
  504. ->first();
  505. if (!empty($inviteMember)) {
  506. $inviteMember = $inviteMember->toArray();
  507. $mobile = '';
  508. if (!empty($inviteMember['member_mobile'])) {
  509. $mobile = $inviteMember['member_mobile'];
  510. }
  511. $nickname = '';
  512. if (!empty($inviteMember['info']) && !empty($inviteMember['info']['member_info_nickname'])) {
  513. $nickname = $inviteMember['info']['member_info_nickname'];
  514. }
  515. $certname = '';
  516. if (!empty($inviteMember['cert']) && !empty($inviteMember['cert']['member_cert_name'])) {
  517. $certname = $inviteMember['cert']['member_cert_name'];
  518. }
  519. $member['invite_name'] = MemberService::getMemberName($mobile, $certname, $nickname);
  520. $member['invite_code'] = $inviteMember['member_invite_code'] ?? '';
  521. }
  522. }
  523. if (!empty($member['member_partner_json'])) {
  524. $partnerJson = json_decode($member['member_partner_json'], true);
  525. if (isset($partnerJson['duedate'])) {
  526. $member['partner_info'] = [
  527. 'add_times'=>$partnerJson['datetime'],
  528. 'surplus' => ceil((strtotime($partnerJson['duedate'] . ' 23:59:59') - time()) / (3600 * 24)),
  529. 'dates' => date('Y/m/d 00:00:00', strtotime($partnerJson['datetime'])) . '-' . date('Y/m/d 23:59:59', strtotime($partnerJson['duedate']))
  530. ];
  531. }
  532. }
  533. if (!empty($member['member_referrer_json'])) {
  534. $referrerJson = json_decode($member['member_referrer_json'], true);
  535. if (isset($referrerJson['duedate'])) {
  536. $member['referrer_info'] = [
  537. 'add_times'=>$referrerJson['datetime'],
  538. 'surplus' => ceil((strtotime($referrerJson['duedate'] . ' 23:59:59') - time()) / (3600 * 24)),
  539. 'dates' => date('Y/m/d 00:00:00', strtotime($referrerJson['datetime'])) . '-' . date('Y/m/d 23:59:59', strtotime($referrerJson['duedate']))
  540. ];
  541. }
  542. }
  543. $account = [];
  544. if (!empty($member['account'])) {
  545. foreach ($member['account'] as $item) {
  546. if ($item['member_account_classify'] == 'WELFARE') {
  547. $account['welfare'] = [
  548. 'member_account_name' => $item['member_account_name'],
  549. 'member_account_income' => $item['member_account_income'],
  550. 'member_account_expend' => $item['member_account_expend'],
  551. 'member_account_surplus' => $item['member_account_surplus'],
  552. 'member_account_added' => $item['member_account_added'],
  553. ];
  554. } else if ($item['member_account_classify'] == 'POINTS') {
  555. $account['points'] = [
  556. 'member_account_name' => $item['member_account_name'],
  557. 'member_account_income' => $item['member_account_income'],
  558. 'member_account_expend' => $item['member_account_expend'],
  559. 'member_account_surplus' => $item['member_account_surplus'],
  560. 'member_account_added' => $item['member_account_added'],
  561. ];
  562. } else if ($item['member_account_classify'] == 'CASH') {
  563. $account['cash'] = [
  564. 'member_account_name' => $item['member_account_name'],
  565. 'member_account_income' => $item['member_account_income'],
  566. 'member_account_expend' => $item['member_account_expend'],
  567. 'member_account_surplus' => $item['member_account_surplus'],
  568. 'member_account_added' => $item['member_account_added'],
  569. ];
  570. } else if ($item['member_account_classify'] == 'CARD') {
  571. $account['card'] = [
  572. 'member_account_name' => $item['member_account_name'],
  573. 'member_account_income' => $item['member_account_income'],
  574. 'member_account_expend' => $item['member_account_expend'],
  575. 'member_account_surplus' => $item['member_account_surplus'],
  576. 'member_account_added' => $item['member_account_added'],
  577. ];
  578. } else if ($item['member_account_classify'] == 'VIP') {
  579. $account['vip'] = [
  580. 'member_account_name' => $item['member_account_name'],
  581. 'member_account_income' => $item['member_account_income'],
  582. 'member_account_expend' => $item['member_account_expend'],
  583. 'member_account_surplus' => $item['member_account_surplus'],
  584. 'member_account_added' => $item['member_account_added'],
  585. ];
  586. }
  587. }
  588. }
  589. $member['account'] = $account;
  590. return json_success('', $member);
  591. }
  592. /**
  593. * VIP账户明细
  594. */
  595. public static function vipInfo(Request $request)
  596. {
  597. $memberId = $request->get('member_id', '');
  598. $page = $request->get('page', 1);
  599. $pageSize = $request->get('pageSize', 20);
  600. $account = MemberAccount::where('join_account_member_id', $memberId)
  601. ->where('member_account_classify', 'VIP')
  602. ->first();
  603. if (!$account) {
  604. return json_fail('暂无数据');
  605. }
  606. $balance = $account->member_account_surplus + $account->member_account_added;
  607. $info = PayDetail::where(function ($query) use ($memberId) {
  608. $query->where('join_pay_member_id', $memberId)
  609. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  610. ->where('pay_category', 'VIP')
  611. ->where('pay_status', 'SUCCESS');
  612. })->orWhere(function ($query) use ($memberId) {
  613. $query->where('join_pay_member_id', $memberId)
  614. ->where('pay_prepayid', 'like', '%VIP%')
  615. ->where('pay_status', 'SUCCESS');
  616. })->orderByDesc('pay_addtimes')
  617. ->get()
  618. ->toArray();
  619. $data = [];
  620. foreach ($info as $key => $item) {
  621. $categoryIds = [];
  622. if (!empty($item['join_pay_order_id'])) {
  623. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  624. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  625. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  626. }
  627. $categoryName = [];
  628. foreach ($categoryIds as $id) {
  629. $category = SysCategory::where('category_id', $id)->first();
  630. if (!empty($category->category_super_path)) {
  631. $path = explode('#', $category->category_super_path);
  632. $ids = array_filter($path);
  633. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  634. } else {
  635. $categoryName[] = $category->category_name;
  636. }
  637. }
  638. $item['goods_category'] = implode(',', array_unique($categoryName));
  639. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  640. $item['goods_category'] = '自定义金额充值';
  641. }
  642. if (in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  643. $item['balance_type'] = 'add';
  644. } else {
  645. $item['balance_type'] = 'cut';
  646. }
  647. if ($key == 0 && in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  648. if (!empty($item['pay_extend_json'])) {
  649. $extendJson = json_decode($item['pay_extend_json'], true);
  650. if (!isset($extendJson['added_amount'])) {
  651. $extendJson['added_amount'] = 0;
  652. }
  653. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  654. $data[] = [
  655. 'join_pay_member_id' => $item['join_pay_member_id'],
  656. 'pay_addtimes' => $item['pay_addtimes'],
  657. 'pay_amount' => $extendJson['added_amount'],
  658. 'pay_category' => 'ADDED',
  659. 'balance' => number_format($balance, 2, '.', ''),
  660. 'balance_type' => 'add',
  661. ];
  662. }
  663. $balance = $balance - $extendJson['added_amount'];
  664. }
  665. $item['balance'] = number_format($balance, 2, '.', '');
  666. $balance = $balance - $item['pay_amount'];
  667. $data[] = $item;
  668. continue;
  669. }
  670. if (in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  671. if (!empty($item['pay_extend_json'])) {
  672. $extendJson = json_decode($item['pay_extend_json'], true);
  673. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  674. $data[] = [
  675. 'join_pay_member_id' => $item['join_pay_member_id'],
  676. 'pay_addtimes' => $item['pay_addtimes'],
  677. 'pay_amount' => $extendJson['added_amount'],
  678. 'pay_category' => 'ADDED',
  679. 'balance' => number_format($balance, 2, '.', ''),
  680. 'balance_type' => 'add',
  681. ];
  682. $balance = $balance - $extendJson['added_amount'];
  683. }
  684. }
  685. $item['balance'] = number_format($balance, 2, '.', '');
  686. $balance = $balance - $item['pay_amount'];
  687. } else {
  688. $item['balance'] = number_format($balance, 2, '.', '');
  689. $balance = $balance + $item['pay_amount'];
  690. }
  691. $data[] = $item;
  692. }
  693. $total = count($data);
  694. $start = ($page - 1) * $pageSize;
  695. $rows = array_slice($data, $start, $pageSize);
  696. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  697. }
  698. public static function welfareInfo(Request $request)
  699. {
  700. $memberId = $request->get('member_id', '');
  701. $page = $request->get('page', 1);
  702. $pageSize = $request->get('pageSize', 20);
  703. $account = MemberAccount::where('join_account_member_id', $memberId)
  704. ->where('member_account_classify', 'WELFARE')
  705. ->first();
  706. if (!$account) {
  707. return json_fail('暂无数据');
  708. }
  709. $balance = $account->member_account_surplus + $account->member_account_added;
  710. $info = PayDetail::where(function ($query) use ($memberId) {
  711. $query->where('join_pay_member_id', $memberId)
  712. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  713. ->where('pay_category', 'RECHARGE')
  714. ->where('pay_prepayid', 'WELFARE')
  715. ->where('pay_status', 'SUCCESS');
  716. })->orWhere(function ($query) use ($memberId) {
  717. $query->where('join_pay_member_id', $memberId)
  718. ->where('pay_prepayid', 'like', '%WELFARE%')
  719. ->where('pay_status', 'SUCCESS');
  720. })->orderByDesc('pay_addtimes')
  721. ->get()
  722. ->toArray();
  723. $data = [];
  724. foreach ($info as $key => $item) {
  725. $categoryIds = [];
  726. if (!empty($item['join_pay_order_id'])) {
  727. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  728. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  729. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  730. }
  731. $categoryName = [];
  732. foreach ($categoryIds as $id) {
  733. $category = SysCategory::where('category_id', $id)->first();
  734. if (!empty($category->category_super_path)) {
  735. $path = explode('#', $category->category_super_path);
  736. $ids = array_filter($path);
  737. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  738. } else {
  739. $categoryName[] = $category->category_name;
  740. }
  741. }
  742. $item['goods_category'] = implode(',', array_unique($categoryName));
  743. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  744. $item['goods_category'] = '自定义金额充值';
  745. }
  746. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  747. $item['balance_type'] = 'add';
  748. } else {
  749. $item['balance_type'] = 'cut';
  750. }
  751. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  752. if (!empty($item['pay_extend_json'])) {
  753. $extendJson = json_decode($item['pay_extend_json'], true);
  754. if (!isset($extendJson['added_amount'])) {
  755. $extendJson['added_amount'] = 0;
  756. }
  757. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  758. $data[] = [
  759. 'join_pay_member_id' => $item['join_pay_member_id'],
  760. 'pay_addtimes' => $item['pay_addtimes'],
  761. 'pay_amount' => $extendJson['added_amount'],
  762. 'pay_category' => 'ADDED',
  763. 'balance' => number_format($balance, 2, '.', ''),
  764. 'balance_type' => 'add',
  765. ];
  766. }
  767. $balance = $balance - $extendJson['added_amount'];
  768. }
  769. $item['balance'] = number_format($balance, 2, '.', '');
  770. $balance = $balance - $item['pay_amount'];
  771. $data[] = $item;
  772. continue;
  773. }
  774. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  775. if (!empty($item['pay_extend_json'])) {
  776. $extendJson = json_decode($item['pay_extend_json'], true);
  777. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  778. $data[] = [
  779. 'join_pay_member_id' => $item['join_pay_member_id'],
  780. 'pay_addtimes' => $item['pay_addtimes'],
  781. 'pay_amount' => $extendJson['added_amount'],
  782. 'pay_category' => 'ADDED',
  783. 'balance' => number_format($balance, 2, '.', ''),
  784. 'balance_type' => 'add',
  785. ];
  786. $balance = $balance - $extendJson['added_amount'];
  787. }
  788. }
  789. $item['balance'] = number_format($balance, 2, '.', '');
  790. $balance = $balance - $item['pay_amount'];
  791. } else {
  792. $item['balance'] = number_format($balance, 2, '.', '');
  793. $balance = $balance + $item['pay_amount'];
  794. }
  795. $data[] = $item;
  796. }
  797. $total = count($data);
  798. $start = ($page - 1) * $pageSize;
  799. $rows = array_slice($data, $start, $pageSize);
  800. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  801. }
  802. /**
  803. * @Desc 佣金记录
  804. * @Author Gorden
  805. * @Date 2024/9/30 14:52
  806. *
  807. * @param Request $request
  808. * @return Response
  809. */
  810. public static function commissionList(Request $request)
  811. {
  812. $memberId = $request->get('member_id');
  813. $page = $request->get('page', 1);
  814. $pageSize = $request->get('pageSize', 20);
  815. $rows = MemberAccountList::where('join_member_account_list_member_id', $memberId)
  816. ->where('member_account_list_attr', 'IN');
  817. $total = $rows->count();
  818. $rows = $rows->forPage($page, $pageSize)
  819. ->get()
  820. ->toArray();
  821. foreach ($rows as &$row) {
  822. if (!empty($row['member_account_list_json'])) {
  823. $memberAccountListJson = json_decode($row['member_account_list_json'], true);
  824. if ($memberAccountListJson['master_member_id']) {
  825. $row['master_member_name'] = MemberService::getMemberNameByMemberId($memberAccountListJson['master_member_id']);
  826. }
  827. }
  828. }
  829. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  830. }
  831. /**
  832. * @Desc 我的粉丝列表
  833. * @Author Gorden
  834. * @Date 2024/9/20 10:22
  835. *
  836. * @param Request $request
  837. */
  838. public static function fansList(Request $request)
  839. {
  840. $memberId = $request->get('member_id');
  841. $name = $request->get('name', '');
  842. $mobile = $request->get('mobile', '');
  843. $memberIds = [];
  844. $memberInviteIds = [];
  845. if (!empty($name)) {
  846. $memberIds = MemberCert::where('member_cert_name', 'like', '%' . $name . '%')->pluck('join_cert_member_id')->toArray();
  847. $memberInviteIds = Member::whereIn('member_id', $memberIds)->pluck('join_invite_member_id')->toArray();
  848. }
  849. if (!empty($mobile)) {
  850. $members = Member::where('member_mobile', 'like', '%' . $mobile . '%')
  851. ->select('member_id', 'join_invite_member_id')
  852. ->get()
  853. ->toArray();
  854. $memberIds2 = array_column($members, 'member_id');
  855. $memberInviteIds2 = array_column($members, 'join_invite_member_id');
  856. $memberIds = array_merge($memberIds, $memberIds2);
  857. $memberInviteIds = array_merge($memberInviteIds, $memberInviteIds2);
  858. }
  859. $fans1 = Member::with([
  860. 'info' => function ($query) {
  861. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  862. },
  863. 'cert' => function ($query) {
  864. $query->select('join_cert_member_id', 'member_cert_name');
  865. },
  866. 'role' => function ($query) {
  867. $query->select('member_role_id', 'member_role_name');
  868. }
  869. ])->where('join_invite_member_id', $memberId)
  870. ->when(!empty($memberIds), function ($query) use ($memberIds, $memberInviteIds) {
  871. $allSub = array_merge($memberIds, $memberInviteIds);
  872. $query->whereIn('member_id', $allSub);
  873. })
  874. ->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')
  875. ->get()
  876. ->toArray();
  877. $fans1Ids = array_column($fans1, 'id');
  878. $fans2 = Member::with([
  879. 'info' => function ($query) {
  880. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  881. },
  882. 'cert' => function ($query) {
  883. $query->select('join_cert_member_id', 'member_cert_name');
  884. },
  885. 'role' => function ($query) {
  886. $query->select('member_role_id', 'member_role_name');
  887. }
  888. ])->whereIn('join_invite_member_id', $fans1Ids)
  889. ->when(!empty($memberIds), function ($query) use ($memberIds) {
  890. $query->whereIn('member_id', $memberIds);
  891. })
  892. ->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')
  893. ->get()
  894. ->toArray();
  895. $fans = array_merge($fans1, $fans2);
  896. foreach ($fans as &$fan) {
  897. $memberMobile = $fan['member_mobile'] ?? '';
  898. // $memberNickname = $fan['info'] && $fan['info']['member_info_nickname'] ? $fan['info']['member_info_nickname'] : '';
  899. $memberCertName = $fan['cert'] && $fan['cert']['member_cert_name'] ? $fan['cert']['member_cert_name'] : '';
  900. $fan['member_name'] = '';
  901. if (!empty($memberCertName) && !empty($memberMobile)) {
  902. $fan['member_name'] = $memberCertName . '-' . $memberMobile;
  903. } elseif (!empty($memberCertName)) {
  904. $fan['member_name'] = $memberCertName;
  905. } elseif (!empty($memberMobile)) {
  906. $fan['member_name'] = $memberMobile;
  907. }
  908. if (!empty($fan['info']) && substr($fan['info']['member_info_headimg'], 0, 4) == 'http') {
  909. $fan['member_info_headimg'] = $fan['info']['member_info_headimg'];
  910. } else {
  911. $fan['member_info_headimg'] = 'https://img.wanyuewellness.com.cn/images/avatar_default.png';
  912. }
  913. // 等级名称
  914. $fan['role_name'] = $fan['role'] && $fan['role']['member_role_name'] ? $fan['role']['member_role_name'] : '';
  915. unset($fan['info'], $fan['cert'], $fan['role']);
  916. }
  917. $tree = new Tree($fans);
  918. return json_success('success', $tree->getTree());
  919. }
  920. /**
  921. * 格式化树
  922. * @param $items
  923. * @return Response
  924. */
  925. protected static function formatTree($items): Response
  926. {
  927. $format_items = [];
  928. foreach ($items as $item) {
  929. $format_items[] = [
  930. 'name' => $item->title ?? $item->name ?? $item->id,
  931. 'value' => (string)$item->id,
  932. 'id' => $item->id,
  933. 'pid' => $item->pid,
  934. ];
  935. }
  936. $tree = new Tree($format_items);
  937. return json_success('success', $tree->getTree());
  938. }
  939. public static function clearWelfare(Request $request)
  940. {
  941. $memberId = $request->post('member_id');
  942. $payTime = $request->post('pay_paytimes', date('Y-m-d H:i:s'));
  943. $payTime = date('Y-m-d H:i:s', strtotime($payTime));
  944. $payRemark = $request->post('pay_remark', '');
  945. $clearPwd = $request->post('clear_password');
  946. if (!$clearPwd || $clearPwd != '123456') {
  947. return json_fail('清零密码错误');
  948. }
  949. Db::beginTransaction();
  950. try {
  951. $account = MemberAccount::where('join_account_member_id', $memberId)
  952. ->where('member_account_classify', 'WELFARE')
  953. ->where('member_account_status', 'ACTIVED')
  954. ->first();
  955. if (!$account) {
  956. throw new BusinessException("账户异常");
  957. }
  958. if ($account->member_account_surplus <= 0) {
  959. throw new BusinessException("账户余额为0,无需清零");
  960. }
  961. // 创建清除记录
  962. PayDetail::insert([
  963. 'join_pay_member_id' => $memberId,
  964. 'pay_status' => 'SUCCESS',
  965. 'pay_category' => 'CLEAR',
  966. 'pay_amount' => $account->member_account_surplus,
  967. 'pay_paytimes' => $payTime,
  968. 'pay_prepayid' => $memberId . '-WELFARE',
  969. 'pay_remark' => $payRemark,
  970. 'pay_addtimes' => time()
  971. ]);
  972. // 账户归零
  973. $account->member_account_expend = $account->member_account_expend + $account->member_account_surplus;
  974. $account->member_account_surplus = 0;
  975. $account->save();
  976. Db::commit();
  977. return json_success('success');
  978. } catch (BusinessException $e) {
  979. Db::rollBack();
  980. return json_fail($e->getMessage());
  981. } catch (\Exception $e) {
  982. dump($e->getMessage());
  983. Db::rollBack();
  984. return json_fail('清零失败');
  985. }
  986. }
  987. public static function balanceInfo(Request $request)
  988. {
  989. $memberId = $request->get('member_id', '');
  990. $page = $request->get('page', 1);
  991. $pageSize = $request->get('pageSize', 20);
  992. $account = MemberAccount::where('join_account_member_id', $memberId)
  993. ->where('member_account_classify', 'CASH')
  994. ->first();
  995. if (!$account) {
  996. return json_fail('暂无数据');
  997. }
  998. $balance = $account->member_account_surplus + $account->member_account_added;
  999. $info = PayDetail::where(function ($query) use ($memberId) {
  1000. $query->where('join_pay_member_id', $memberId)
  1001. ->whereIn('pay_category', ['RECHARGE'])
  1002. ->where('pay_prepayid', '<>', $memberId . '-WELFARE')
  1003. ->where('pay_status', 'SUCCESS');
  1004. })->orWhere(function ($query) use ($memberId) {
  1005. $query->where('join_pay_member_id', $memberId)
  1006. ->where('pay_prepayid', 'like', '%CASH%')
  1007. ->where('pay_status', 'SUCCESS');
  1008. })->select('join_pay_object_json', 'join_pay_member_id', 'pay_category', 'join_pay_order_id', 'pay_extend_json', 'pay_addtimes', 'pay_amount');
  1009. // ->orderByDesc('pay_addtimes')
  1010. // ->get()
  1011. // ->toArray();
  1012. $memberAccountList = MemberAccountList::where('join_member_account_list_member_id', $memberId)
  1013. ->where('member_account_list_status', 'ACTIVED')
  1014. ->selectRaw('
  1015. member_account_list_extend_json as join_pay_object_json,
  1016. join_member_account_list_member_id as join_pay_member_id,
  1017. member_account_list_attr as pay_category,
  1018. join_list_member_account_nbr as pay_prepayid,
  1019. member_account_list_extend_json as pay_extend_json,
  1020. member_account_list_addtimes as pay_addtimes,
  1021. member_account_list_amount as pay_amount');
  1022. $info = $info->union($memberAccountList)
  1023. ->orderByDesc('pay_addtimes')
  1024. ->get()
  1025. ->toArray();
  1026. $data = [];
  1027. foreach ($info as $key => $item) {
  1028. $payObjectJson = [];
  1029. if (!empty($item['join_pay_object_json'])) {
  1030. $payObjectJson = json_decode($item['join_pay_object_json'], true);
  1031. }
  1032. // if ($item['pay_category'] == 'PARTNER') {
  1033. // $payObjectJson = json_decode($item['join_pay_object_json'], true);
  1034. // if (!Order::where('order_id', $payObjectJson['order_id'])->where('order_is_complete', 'Y')->exists()) {
  1035. // continue;
  1036. // }
  1037. // }
  1038. // if ($item['pay_category'] == 'REFERRER') {
  1039. // $payObjectJson = json_decode($item['join_pay_object_json'], true);
  1040. // if (!Order::where('order_id', $payObjectJson['order_id'])->where('order_is_complete', 'Y')->exists()) {
  1041. // continue;
  1042. // }
  1043. // }
  1044. $categoryIds = [];
  1045. if (!empty($item['join_pay_order_id'])) {
  1046. $goodsIds = [];
  1047. if (in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE', 'REFERRER'])) {
  1048. if (isset($payObjectJson['recharge_goods_id'])) {
  1049. $goodsIds = [$payObjectJson['recharge_goods_id']];
  1050. }
  1051. } else {
  1052. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  1053. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  1054. }
  1055. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  1056. }
  1057. $categoryName = [];
  1058. foreach ($categoryIds as $id) {
  1059. $category = SysCategory::where('category_id', $id)->first();
  1060. if (!empty($category->category_super_path)) {
  1061. $path = explode('#', $category->category_super_path);
  1062. $ids = array_filter($path);
  1063. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  1064. } else {
  1065. $categoryName[] = $category->category_name;
  1066. }
  1067. }
  1068. $item['goods_category'] = implode(',', array_unique($categoryName));
  1069. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  1070. $item['goods_category'] = '自定义金额充值';
  1071. } else if (mb_substr($item['goods_category'], 0, 7) == '会员合伙人产品') {
  1072. $item['goods_category'] = '会员合伙人充值';
  1073. }
  1074. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'CROWN_CONTINUE', 'PARTNER', 'IN'])) {
  1075. $item['balance_type'] = 'add';
  1076. } else {
  1077. $item['balance_type'] = 'cut';
  1078. }
  1079. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'CROWN_CONTINUE'])) {
  1080. if (!empty($item['pay_extend_json']) && !in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE'])) {
  1081. $extendJson = json_decode($item['pay_extend_json'], true);
  1082. if (!isset($extendJson['added_amount'])) {
  1083. $extendJson['added_amount'] = 0;
  1084. }
  1085. // 计划任务还没跑
  1086. // if ($item['pay_category'] == 'RECHARGE' && (!isset($extendJson['notify']) || $extendJson['notify'] != 'success')) {
  1087. // continue;
  1088. // }
  1089. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  1090. $payCategory = 'ADDED';
  1091. if ($item['pay_category'] == 'GIVE') {
  1092. $payCategory = 'GIVE';
  1093. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1094. $payCategory = 'CROWN_CONTINUE';
  1095. }
  1096. $data[] = [
  1097. 'join_pay_member_id' => $item['join_pay_member_id'],
  1098. 'pay_addtimes' => $item['pay_addtimes'],
  1099. 'pay_amount' => $extendJson['added_amount'],
  1100. 'pay_category' => $payCategory,
  1101. 'balance' => number_format($balance, 2, '.', ''),
  1102. 'balance_type' => 'add',
  1103. ];
  1104. } elseif (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
  1105. $payCategory = 'ADDED';
  1106. if ($item['pay_category'] == 'GIVE') {
  1107. $payCategory = 'GIVE';
  1108. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1109. $payCategory = 'CROWN_CONTINUE';
  1110. }
  1111. $extendJson['added_amount'] = $item['pay_amount'] * $payObjectJson['recharge_config']['nbr'];
  1112. $data[] = [
  1113. 'join_pay_member_id' => $item['join_pay_member_id'],
  1114. 'pay_addtimes' => $item['pay_addtimes'],
  1115. 'pay_amount' => $extendJson['added_amount'],
  1116. 'pay_category' => $payCategory,
  1117. 'balance' => number_format($balance, 2, '.', ''),
  1118. 'balance_type' => 'add',
  1119. ];
  1120. }
  1121. $balance = $balance - $extendJson['added_amount'];
  1122. }
  1123. if (in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE'])) {
  1124. $addedAmount = 0;
  1125. if (!isset($payObjectJson['recharge_config'])) {
  1126. $addedAmount = 0;
  1127. }
  1128. if (isset($payObjectJson['recharge_config']) && !empty($payObjectJson['recharge_config']['nbr']) && floatval($payObjectJson['recharge_config']['nbr'] > 0)) {
  1129. $addedAmount = $payObjectJson['recharge_config']['nbr'];
  1130. $data[] = [
  1131. 'join_pay_member_id' => $item['join_pay_member_id'],
  1132. 'pay_addtimes' => $item['pay_addtimes'],
  1133. 'pay_amount' => $addedAmount,
  1134. 'pay_category' => 'ADDED',
  1135. 'balance' => number_format($balance, 2, '.', ''),
  1136. 'balance_type' => 'add',
  1137. ];
  1138. }
  1139. $balance = $balance - $addedAmount;
  1140. }
  1141. $item['balance'] = number_format($balance, 2, '.', '');
  1142. $balance = $balance - $item['pay_amount'];
  1143. if (!in_array($item['pay_category'], ['GIVE', 'CROWN_CONTINUE'])) {
  1144. $data[] = $item;
  1145. }
  1146. continue;
  1147. }
  1148. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'CROWN_CONTINUE', 'PARTNER', 'COMBINE', 'IN'])) {
  1149. if (!empty($item['pay_extend_json']) && !in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE'])) {
  1150. $extendJson = json_decode($item['pay_extend_json'], true);
  1151. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  1152. $payCategory = 'ADDED';
  1153. if ($item['pay_category'] == 'GIVE') {
  1154. $payCategory = 'GIVE';
  1155. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1156. $payCategory = 'CROWN_CONTINUE';
  1157. }
  1158. $data[] = [
  1159. 'join_pay_member_id' => $item['join_pay_member_id'],
  1160. 'pay_addtimes' => $item['pay_addtimes'],
  1161. 'pay_amount' => $extendJson['added_amount'],
  1162. 'pay_category' => $payCategory,
  1163. 'balance' => number_format($balance, 2, '.', ''),
  1164. 'balance_type' => 'add',
  1165. ];
  1166. $balance = $balance - $extendJson['added_amount'];
  1167. } elseif (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
  1168. $payCategory = 'ADDED';
  1169. if ($item['pay_category'] == 'GIVE') {
  1170. $payCategory = 'GIVE';
  1171. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1172. $payCategory = 'CROWN_CONTINUE';
  1173. }
  1174. $extendJson['added_amount'] = $item['pay_amount'] * $payObjectJson['recharge_config']['nbr'];
  1175. $data[] = [
  1176. 'join_pay_member_id' => $item['join_pay_member_id'],
  1177. 'pay_addtimes' => $item['pay_addtimes'],
  1178. 'pay_amount' => $extendJson['added_amount'],
  1179. 'pay_category' => $payCategory,
  1180. 'balance' => number_format($balance, 2, '.', ''),
  1181. 'balance_type' => 'add',
  1182. ];
  1183. $balance = $balance - $extendJson['added_amount'];
  1184. }
  1185. }
  1186. if (in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE'])) {
  1187. $addedAmount = 0;
  1188. if (!isset($payObjectJson['recharge_config'])) {
  1189. $addedAmount = 0;
  1190. }
  1191. if (isset($payObjectJson['recharge_config']) && !empty($payObjectJson['recharge_config']['nbr']) && floatval($payObjectJson['recharge_config']['nbr'] > 0)) {
  1192. $addedAmount = $payObjectJson['recharge_config']['nbr'];
  1193. $data[] = [
  1194. 'join_pay_member_id' => $item['join_pay_member_id'],
  1195. 'pay_addtimes' => $item['pay_addtimes'],
  1196. 'pay_amount' => $addedAmount,
  1197. 'pay_category' => 'ADDED',
  1198. 'balance' => number_format($balance, 2, '.', ''),
  1199. 'balance_type' => 'add',
  1200. ];
  1201. }
  1202. $balance = $balance - $addedAmount;
  1203. }
  1204. $item['balance'] = number_format($balance, 2, '.', '');
  1205. $balance = $balance - $item['pay_amount'];
  1206. } else {
  1207. $item['balance'] = number_format($balance, 2, '.', '');
  1208. $balance = $balance + $item['pay_amount'];
  1209. }
  1210. if (!in_array($item['pay_category'], ['GIVE', 'CROWN_CONTINUE'])) {
  1211. $data[] = $item;
  1212. }
  1213. }
  1214. $total = count($data);
  1215. $start = ($page - 1) * $pageSize;
  1216. $rows = array_slice($data, $start, $pageSize);
  1217. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  1218. }
  1219. public static function pointInfo($memberId)
  1220. {
  1221. $account = MemberAccount::where('join_account_member_id', $memberId)
  1222. ->where('member_account_classify', 'POINTS')
  1223. ->first();
  1224. if (!$account) {
  1225. return json_fail('暂无数据');
  1226. }
  1227. $balance = $account->member_account_surplus + $account->member_account_added;
  1228. $points = ClientPoints::where('join_client_points_member_id', $memberId)->orderBy('client_points_addtimes', 'DESC')->get()->toArray();
  1229. $nowPoint = 0;
  1230. foreach ($points as $key => &$point) {
  1231. $point['goods_category'] = '其他';
  1232. if (!empty($point['join_client_points_goods_id'])) {
  1233. $categoryId = Goods::where('goods_id', $point['join_client_points_goods_id'])->pluck('join_goods_category_id');
  1234. $category = SysCategory::where('category_id', $categoryId)->first();
  1235. if (!empty($category->category_super_path)) {
  1236. $path = explode('#', $category->category_super_path);
  1237. $ids = array_filter($path);
  1238. $point['goods_category'] = SysCategory::where('category_id', current($ids))->value('category_name');
  1239. } else {
  1240. $point['goods_category'] = $category->category_name;
  1241. }
  1242. }
  1243. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  1244. $point['balance_type'] = 'add';
  1245. } else {
  1246. $point['balance_type'] = 'cut';
  1247. }
  1248. if ($key == 0) {
  1249. $point['balance'] = number_format($balance, 2, '.', '');
  1250. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  1251. $balance = $balance - $point['client_points_nbr'];
  1252. } else {
  1253. $balance = $balance + $point['client_points_nbr'];
  1254. }
  1255. continue;
  1256. }
  1257. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  1258. $point['balance'] = number_format($balance, 2, '.', '');
  1259. $balance = $balance - $point['client_points_nbr'];
  1260. // $point['balance'] = $balance = $nowPoint = number_format($balance - $point['client_points_nbr'], 2, '.', '');
  1261. } else {
  1262. $point['balance'] = number_format($balance, 2, '.', '');
  1263. $balance = $balance + $point['client_points_nbr'];
  1264. // $point['balance'] = $balance = $nowPoint = number_format($balance + $point['client_points_nbr'], 2, '.', '');
  1265. }
  1266. }
  1267. return json_success('', $points);
  1268. }
  1269. /**
  1270. * @Desc 添加会员
  1271. * @Author Gorden
  1272. * @Date 2024/3/25 13:40
  1273. *
  1274. * @param $params
  1275. * @return \support\Response
  1276. */
  1277. public static function add($params)
  1278. {
  1279. Db::beginTransaction();
  1280. try {
  1281. // 排重
  1282. if (Member::where('member_mobile', $params['mobile'])->where('member_status', 'ACTIVED')->exists()) {
  1283. throw new BusinessException("会员已存在");
  1284. }
  1285. $memberId = self::generateMemberId();
  1286. // 先写主表,要ID
  1287. $memberData = [
  1288. 'member_id' => $memberId,
  1289. 'member_is_owner' => $params['is_owner'] ?? 'N',
  1290. 'member_classify' => $params['classify'] ?? '',
  1291. 'member_status' => $params['status'] ?? 'PENDING',
  1292. 'member_mobile' => $params['mobile'] ?? '',
  1293. 'member_category' => $params['member_category'] ?? '后台添加',
  1294. 'member_invite_code' => self::generateInviteCode(),
  1295. // 'member_from' => $params['source'],
  1296. // 'member_extend_json' => json_encode(['come_from' => $params['source']]),
  1297. 'member_addtimes' => time(),
  1298. ];
  1299. $extendJson['come_from'] = $params['source'];
  1300. if (!empty($params['user_id'])) {
  1301. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  1302. if (!$deptId) {
  1303. throw new BusinessException('请先关联业务员和门店');
  1304. }
  1305. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  1306. }
  1307. $memberData['member_extend_json'] = json_encode($extendJson);
  1308. Member::insert($memberData);
  1309. // 账户表
  1310. $accountData = [
  1311. 'join_account_member_id' => $memberId,
  1312. 'member_account_classify' => 'POINTS',
  1313. 'member_account_status' => 'ACTIVED',
  1314. 'member_account_category' => 'NORMAL',
  1315. 'member_account_nbr' => $memberId . '-POINTS',
  1316. 'member_account_name' => '积分账户',
  1317. 'member_account_addtimes' => time()
  1318. ];
  1319. // 积分账户
  1320. MemberAccount::insert($accountData);
  1321. $accountData['member_account_classify'] = 'CASH';
  1322. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1323. $accountData['member_account_name'] = '余额账户';
  1324. // 现金/余额账户
  1325. MemberAccount::insert($accountData);
  1326. // 福利账户
  1327. // if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY'){
  1328. // $welfareData = [
  1329. // 'join_account_member_id'=>$memberId,
  1330. // 'member_account_classify'=>'WELFARE',
  1331. // 'member_account_status'=>'ACTIVED',
  1332. // 'member_account_category'=>'NORMAL',
  1333. // 'member_account_nbr'=>$memberId.'-WELFARE',
  1334. // 'member_account_name'=>'福利账户',
  1335. // 'member_account_addtimes'=>time()
  1336. // ];
  1337. // MemberAccount::insert($welfareData);
  1338. // }
  1339. // member cert
  1340. $certData = [
  1341. 'join_cert_member_id' => $memberId,
  1342. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  1343. 'member_cert_name' => $params['cert_name'] ?? '',
  1344. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  1345. 'member_cert_birth' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  1346. 'member_cert_gender' => $params['gender'] ?? '',
  1347. // 'member_cert_province' => !empty($params['province']) ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  1348. // 'member_cert_city' => $params['city'] ?? '',
  1349. // 'member_cert_addr' => $params['addr'] ?? '',
  1350. // 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  1351. // 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  1352. ];
  1353. MemberCert::insert($certData);
  1354. // member info
  1355. $infoData = [
  1356. 'join_info_member_id' => $memberId,
  1357. 'member_info_nickname' => $params['account_name'] ?? '',
  1358. 'member_info_headimg' => !empty($params['avatar']) ? getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']) : '',
  1359. 'member_info_referee' => $params['member_info_referee'] ?? '',
  1360. 'member_info_business' => $params['member_info_business'] ?? '',
  1361. 'member_info_service' => $params['member_info_service'] ?? '',
  1362. 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
  1363. 'member_info_address' => $params['member_info_address'] ?? '',
  1364. 'member_info_birthday' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  1365. 'member_info_gender' => $params['gender'] ?? '',
  1366. ];
  1367. MemberInfo::insert($infoData);
  1368. Db::commit();
  1369. } catch (BusinessException $e) {
  1370. Db::rollBack();
  1371. return json_fail($e->getMessage());
  1372. } catch (\Exception $e) {
  1373. Db::rollBack();
  1374. dump($e->getMessage() . ';line:' . $e->getLine());
  1375. return json_fail("数据添加失败");
  1376. }
  1377. return json_success('数据添加成功', ['member_id' => $memberId]);
  1378. }
  1379. /**
  1380. * @Desc 编辑会员/ 业主
  1381. * @Author Gorden
  1382. * @Date 2024/4/20 15:18
  1383. *
  1384. * @param $params
  1385. * @return \support\Response
  1386. */
  1387. public static function update($params)
  1388. {
  1389. Db::beginTransaction();
  1390. try {
  1391. $memberId = $params['member_id'];
  1392. $member = Member::where('member_id', $params['member_id'])->first();
  1393. if (!$member) {
  1394. throw new BusinessException('数据异常');
  1395. }
  1396. $extendJson = [];
  1397. if (!empty($member->member_extend_json)) {
  1398. $extendJson = json_decode($member->member_extend_json, true);
  1399. }
  1400. $extendJson['come_from'] = $params['source'];
  1401. // 门店、业务员
  1402. if (!empty($params['user_id'])) {
  1403. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  1404. if (!$deptId) {
  1405. throw new BusinessException('您选择的业务员没有关联门店');
  1406. }
  1407. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  1408. }
  1409. // 先写主表,要ID
  1410. $memberData = [
  1411. 'member_id' => $memberId,
  1412. 'member_is_owner' => $params['is_owner'],
  1413. 'member_classify' => $params['member_classify'],
  1414. 'member_status' => $params['status'],
  1415. // 'member_mobile' => $params['mobile'],
  1416. // 'member_from' => $params['source'],
  1417. 'member_extend_json' => json_encode($extendJson)
  1418. ];
  1419. Member::where('member_id', $params['member_id'])->update($memberData);
  1420. // member cert
  1421. $certData = [
  1422. 'join_cert_member_id' => $memberId,
  1423. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  1424. 'member_cert_name' => $params['cert_name'] ?? '',
  1425. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  1426. 'member_cert_birth' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  1427. 'member_cert_gender' => $params['gender'] ?? '',
  1428. // 'member_cert_province' => $params['province'] ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  1429. // 'member_cert_city' => $params['city'] ?? '',
  1430. // 'member_cert_addr' => $params['addr'] ?? '',
  1431. 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  1432. 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  1433. ];
  1434. if (!MemberCert::where('join_cert_member_id', $memberId)->exists()) {
  1435. MemberCert::insert($certData);
  1436. } else {
  1437. MemberCert::where('join_cert_member_id', $params['member_id'])->update($certData);
  1438. }
  1439. // member info
  1440. $avatar = '';
  1441. if ($params['avatar'] && (strpos($params['avatar'], getenv('APP_IMAGE_DOMAIN')) !== false || strpos($params['avatar'], getenv('SYSTEM_AVATAR_DOMAIN')) !== false)) {
  1442. $avatar = $params['avatar'];
  1443. } else if ($params['avatar']) {
  1444. $avatar = getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']);
  1445. }
  1446. $infoData = [
  1447. 'join_info_member_id' => $memberId,
  1448. 'member_info_nickname' => $params['account_name'],
  1449. 'member_info_headimg' => $avatar,
  1450. 'member_info_referee' => $params['member_info_referee'] ?? '',
  1451. 'member_info_business' => $params['member_info_business'] ?? '',
  1452. 'member_info_service' => $params['member_info_service'] ?? '',
  1453. 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
  1454. 'member_info_address' => $params['member_info_address'] ?? '',
  1455. 'member_info_birthday' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  1456. 'member_info_gender' => $params['gender'] ?? '',
  1457. ];
  1458. if (!MemberInfo::where('join_info_member_id', $params['member_id'])->exists()) {
  1459. MemberInfo::insert($infoData);
  1460. } else {
  1461. MemberInfo::where('join_info_member_id', $params['member_id'])->update($infoData);
  1462. }
  1463. // 福利账户
  1464. if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY' && !MemberAccount::where('member_account_nbr', $memberId . '-WELFARE')->exists()) {
  1465. $welfareData = [
  1466. 'join_account_member_id' => $memberId,
  1467. 'member_account_classify' => 'WELFARE',
  1468. 'member_account_status' => 'ACTIVED',
  1469. 'member_account_category' => 'NORMAL',
  1470. 'member_account_nbr' => $memberId . '-WELFARE',
  1471. 'member_account_name' => '福利账户',
  1472. 'member_account_addtimes' => time()
  1473. ];
  1474. MemberAccount::insert($welfareData);
  1475. }
  1476. Db::commit();
  1477. } catch (BusinessException $e) {
  1478. Db::rollBack();
  1479. dump($e->getMessage() . ';line:' . $e->getLine());
  1480. return json_fail($e->getMessage());
  1481. } catch (\Exception $e) {
  1482. Db::rollBack();
  1483. dump($e->getMessage() . ';line:' . $e->getLine());
  1484. return json_fail("数据更新失败");
  1485. }
  1486. return json_success('数据更新成功');
  1487. }
  1488. /**
  1489. * @Desc 会员关联设备列表
  1490. * @Author Gorden
  1491. * @Date 2024/4/7 9:52
  1492. *
  1493. * @param $id
  1494. * @return \support\Response
  1495. */
  1496. public static function deviceList($id)
  1497. {
  1498. $family = Family::where('join_family_creator_member_id', $id)->get()->toArray();
  1499. $familyMemberIds = '';
  1500. if ($family) {
  1501. $familyIds = array_column($family, 'family_id');
  1502. $familyMember = FamilyMember::whereIn('join_family_id', $familyIds)->get()->toArray();
  1503. $familyMemberIds = array_column($familyMember, 'join_family_member_id');
  1504. }
  1505. $rows = MemberDevice::leftJoin('device', 'device.device_id', '=', 'member_device.join_member_device_id')
  1506. ->leftJoin('member', 'member.member_id', '=', 'member_device.join_device_member_id')
  1507. ->leftJoin('family_member', 'family_member.join_family_member_id', '=', 'member_device.join_device_member_id')
  1508. ->select('device.device_category', 'device.device_name', 'device.device_type', 'member.member_mobile', 'member_device.*', 'family_member.family_member_name')
  1509. ->when(!empty($familyMemberIds), function ($query) use ($familyMemberIds) {
  1510. $query->whereIn('member_device.join_device_master_member_id', $familyMemberIds);
  1511. })
  1512. ->when(empty($familyMemberIds), function ($query) use ($id) {
  1513. $query->where('member_device.join_device_master_member_id', $id);
  1514. })->orderBy('member_device_addtimes', 'desc')
  1515. ->get()
  1516. ->toArray();
  1517. return json_success('', ['rows' => $rows]);
  1518. }
  1519. /**
  1520. * @Desc 绑定设备
  1521. * @Author Gorden
  1522. * @Date 2024/4/7 10:27
  1523. *
  1524. * @param Request $request
  1525. * @return \support\Response
  1526. */
  1527. public static function addDevice(Request $request)
  1528. {
  1529. $params = $request->post();
  1530. Db::beginTransaction();
  1531. try {
  1532. $data = [
  1533. 'join_device_member_id' => $params['join_device_member_id'] ?? '',
  1534. 'join_device_master_member_id' => $params['join_device_master_member_id'],
  1535. 'join_member_device_id' => $params['join_member_device_id'],
  1536. 'member_device_status' => $params['member_device_status'],
  1537. 'member_device_key' => $params['member_device_key'] ?? '',
  1538. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1539. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1540. 'member_device_addtimes' => time(),
  1541. ];
  1542. MemberDevice::insert($data);
  1543. $device = Device::where('device_id', $params['join_member_device_id'])->whereIn('device_status', ['PROCESSING', 'PENDING'])->first();
  1544. // 激活设备
  1545. if (!$device) {
  1546. Db::rollBack();
  1547. return json_fail('当前设备不是可激活状态');
  1548. }
  1549. $device->device_status = 'ACTIVED';
  1550. $device->save();
  1551. Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => 'ACTIVED']);
  1552. _syslog("绑定设备", "用户【" . ($params['join_device_member_id'] ?? $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1553. Db::commit();
  1554. } catch (\Exception $e) {
  1555. Db::rollBack();
  1556. dump($e->getMessage());
  1557. return json_fail('绑定失败');
  1558. }
  1559. return json_success('绑定成功');
  1560. }
  1561. public static function updateDevice(Request $request)
  1562. {
  1563. $params = $request->post();
  1564. Db::beginTransaction();
  1565. try {
  1566. $data = [
  1567. 'member_device_status' => $params['member_device_status'],
  1568. 'member_device_key' => $params['member_device_key'],
  1569. 'join_device_member_id' => $params['join_device_member_id'],
  1570. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1571. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1572. ];
  1573. MemberDevice::where('member_device_id', $request->post('member_device_id'))->update($data);
  1574. $device = Device::where('device_id', $params['join_member_device_id'])->first();
  1575. if ($device->device_status != $params['member_device_status']) {
  1576. $device->device_status = $params['member_device_status'];
  1577. $device->save();
  1578. }
  1579. // Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => $params['member_device_status']]);
  1580. Db::commit();
  1581. _syslog("更新绑定设备", "用户【" . ($params['join_device_member_id'] == "" ? $params['join_device_member_id'] : $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1582. } catch (\Exception $e) {
  1583. Db::rollBack();
  1584. dump($e->getMessage());
  1585. return json_fail('绑定更新失败');
  1586. }
  1587. return json_success('绑定更新成功');
  1588. }
  1589. public static function deleteDevice(Request $request)
  1590. {
  1591. try {
  1592. Db::beginTransaction();
  1593. $memberDevice = MemberDevice::where('member_device_id', $request->post('member_device_id'))->first();
  1594. if (!$memberDevice) {
  1595. return json_fail('数据不存在');
  1596. }
  1597. $device = Device::where('device_id', $memberDevice->join_member_device_id)->first();
  1598. $device->device_status = "DISABLED";
  1599. $device->save();
  1600. // Device::where('device_id', $memberDevice->join_member_device_id)->update(['device_status' => 'DISABLED']);
  1601. $memberDevice->delete();
  1602. Db::commit();
  1603. _syslog("解除绑定设备", "用户【" . ($memberDevice->join_device_member_id == "" ? $memberDevice->join_device_member_id : $memberDevice->join_device_master_member_id) . '】,解除绑定设备【' . $device->device_name . '】');
  1604. return json_success('解除绑定成功');
  1605. } catch (\Exception $e) {
  1606. Db::rollBack();
  1607. return json_fail('绑定失败');
  1608. }
  1609. }
  1610. /**
  1611. * 手动创建会员
  1612. */
  1613. public static function createMember($params)
  1614. {
  1615. $memberId = $params['member_id'] ?? '';
  1616. if (!$memberId) {
  1617. throw new BusinessException("参数异常");
  1618. }
  1619. // 主表
  1620. $memberData = [
  1621. 'member_id' => $memberId,
  1622. 'member_is_owner' => 'N',
  1623. 'member_classify' => 'MEMBER',
  1624. 'member_status' => 'ACTIVED',
  1625. 'member_mobile' => $params['mobile'],
  1626. 'member_invite_code' => self::generateInviteCode(),
  1627. 'member_extend_json' => json_encode(['come_from' => 'SYSTEM']),
  1628. 'member_addtimes' => time()
  1629. ];
  1630. Member::insert($memberData);
  1631. // 账户表
  1632. $accountData = [
  1633. 'join_account_member_id' => $memberId,
  1634. 'member_account_classify' => 'POINTS',
  1635. 'member_account_status' => 'ACTIVED',
  1636. 'member_account_category' => 'NORMAL',
  1637. 'member_account_nbr' => $memberId . '-POINTS',
  1638. 'member_account_name' => '积分账户',
  1639. 'member_account_addtimes' => time()
  1640. ];
  1641. // 积分账户
  1642. MemberAccount::insert($accountData);
  1643. $accountData['member_account_classify'] = 'CASH';
  1644. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1645. $accountData['member_account_name'] = '余额账户';
  1646. // 现金/余额账户
  1647. MemberAccount::insert($accountData);
  1648. // info
  1649. $infoData = [
  1650. 'join_info_member_id' => $memberId,
  1651. 'member_info_nickname' => $params['username']
  1652. ];
  1653. MemberInfo::insert($infoData);
  1654. }
  1655. private static function generateMemberId()
  1656. {
  1657. $id = SysSerial::getSerial();
  1658. return "MR" . date('ymdHi') . random_string(4, 'up');
  1659. }
  1660. public static function getMemberName($mobile, $certName, $nickname)
  1661. {
  1662. $name = '';
  1663. if (!empty($certName)) {
  1664. $name = $certName;
  1665. } elseif (!empty($nickname)) {
  1666. $name = $nickname;
  1667. }
  1668. if (!empty($name) && !empty($mobile)) {
  1669. $name .= '-' . $mobile;
  1670. } elseif (!empty($mobile)) {
  1671. $name = $mobile;
  1672. }
  1673. return $name;
  1674. }
  1675. public static function getMemberCertName($mobile, $certName, $nickname)
  1676. {
  1677. $name = '';
  1678. if (!empty($certName)) {
  1679. $name = $certName;
  1680. }
  1681. if (!empty($name) && !empty($mobile)) {
  1682. $name .= '-' . $mobile;
  1683. } elseif (!empty($mobile)) {
  1684. $name = $mobile;
  1685. }
  1686. return $name;
  1687. }
  1688. public static function getMemberNickname($mobile)
  1689. {
  1690. if (empty($mobile)) {
  1691. return '';
  1692. }
  1693. return substr($mobile, -4, 4) . '会员';
  1694. }
  1695. public static function getMemberNameByMemberId($memberId)
  1696. {
  1697. $member = Member::with([
  1698. 'cert' => function ($query) {
  1699. $query->select('join_cert_member_id', 'member_cert_name');
  1700. },
  1701. 'info' => function ($query) {
  1702. $query->select('join_info_member_id', 'member_info_nickname');
  1703. },
  1704. ]
  1705. )->where('member_id', $memberId)
  1706. ->first();
  1707. if (empty($member)) {
  1708. return;
  1709. }
  1710. $member = $member->toArray();
  1711. $mobile = $certName = $nickname = '';
  1712. if (!empty($member['member_mobile'])) {
  1713. $mobile = $member['member_mobile'];
  1714. }
  1715. if (!empty($member['cert']) && !empty($member['cert']['member_cert_name'])) {
  1716. $certName = $member['cert']['member_cert_name'];
  1717. }
  1718. if (!empty($member['info']) && !empty($member['info']['member_info_nickname'])) {
  1719. $nickname = $member['info']['member_info_nickname'];
  1720. }
  1721. return self::getMemberName($mobile, $certName, $nickname);
  1722. }
  1723. /**
  1724. * @Desc 生成推荐码
  1725. * @Author Gorden
  1726. * @Date 2024/9/6 9:05
  1727. *
  1728. * @return string
  1729. */
  1730. public static function generateInviteCode()
  1731. {
  1732. $code = random_string(8, 'number');
  1733. if (Member::where('member_invite_code', $code)->exists()) {
  1734. return self::generateInviteCode();
  1735. } else {
  1736. return $code;
  1737. }
  1738. }
  1739. public static function getAvatarUrl($url)
  1740. {
  1741. if (substr($url, 0, 1) == '.' || substr($url, 0, 1) == '/' || empty($url)) {
  1742. $url = "https://img.wanyuewellness.com.cn/images/avatar_default.png";
  1743. }
  1744. return $url;
  1745. }
  1746. /**
  1747. * @Desc 获取昵称
  1748. * @Author Gorden
  1749. * @Date 2024/10/23 11:50
  1750. *
  1751. * @param $nickname
  1752. * @param $mobile
  1753. * @return mixed|string
  1754. */
  1755. public static function getNickname($nickname, $mobile)
  1756. {
  1757. if (!empty($nickname)) {
  1758. return $nickname;
  1759. } else if (!empty($mobile)) {
  1760. return substr($mobile, -4, 4) . '会员';
  1761. } else {
  1762. return '';
  1763. }
  1764. }
  1765. public static function getRoleName($roleId)
  1766. {
  1767. if (empty($roleId)) {
  1768. return '';
  1769. }
  1770. return MemberRole::where('member_role_id', $roleId)->value('member_role_name');
  1771. }
  1772. /**
  1773. * @Desc 获取身份-合伙人,推荐官
  1774. * @Author Gorden
  1775. * @Date 2024/9/28 13:41
  1776. *
  1777. * @param $isPartner
  1778. * @param $isReferrer
  1779. * @return string
  1780. */
  1781. public static function getIdentity($isPartner, $isReferrer)
  1782. {
  1783. if ($isPartner == 'Y') {
  1784. return "PARTNER";
  1785. } elseif ($isReferrer == 'Y') {
  1786. return "REFERRER";
  1787. } else {
  1788. return 'NON';
  1789. }
  1790. }
  1791. /**
  1792. * @Desc 会员合伙人过期
  1793. * @Author Gorden
  1794. * @Date 2024/10/10 9:50
  1795. *
  1796. * @return void
  1797. */
  1798. public static function partnerExpired()
  1799. {
  1800. $members = Member::where('member_is_partner', 'Y')->get()->toArray();
  1801. foreach ($members as $member) {
  1802. $partnerJson = ['member_id' => $member['member_id']];
  1803. if (!empty($member['member_partner_json'])) {
  1804. $partnerJson = json_decode($member['member_partner_json'], true);
  1805. }
  1806. if (isset($partnerJson['duedate'])) {
  1807. $duedateUnix = strtotime($partnerJson['duedate'] . ' 23:59:59');
  1808. if ($duedateUnix > time()) {
  1809. continue;
  1810. }
  1811. }
  1812. Member::where('member_id', $member['member_id'])->update(['member_is_partner' => 'N', 'member_partner_json' => '[]']);
  1813. _syslog("会员合伙人过期", "取消身份", false, $partnerJson, 1001);
  1814. Log::info("会员【" . $member['member_id'] . '】会员合伙人身份已过期', $partnerJson);
  1815. }
  1816. }
  1817. /**
  1818. * @Desc 康养推荐官过期
  1819. * @Author Gorden
  1820. * @Date 2024/10/10 10:14
  1821. *
  1822. * @return void
  1823. */
  1824. public static function referrerExpired()
  1825. {
  1826. $members = Member::where('member_is_referrer', 'Y')->get()->toArray();
  1827. foreach ($members as $member) {
  1828. $referrerJson = ['member_id' => $member['member_id']];
  1829. if (!empty($member['member_referrer_json'])) {
  1830. $referrerJson = json_decode($member['member_referrer_json'], true);
  1831. }
  1832. if (isset($referrerJson['duedate'])) {
  1833. $duedateUnix = strtotime($referrerJson['duedate'] . ' 23:59:59');
  1834. if ($duedateUnix > time()) {
  1835. continue;
  1836. }
  1837. }
  1838. Member::where('member_id', $member['member_id'])->update(['member_is_referrer' => 'N', 'member_referrer_json' => '[]']);
  1839. _syslog("康养推荐官过期", "取消身份", false, $referrerJson, 1001);
  1840. Log::info("会员【" . $member['member_id'] . '】康养推荐官身份已过期', $referrerJson);
  1841. }
  1842. }
  1843. /**
  1844. * @Desc 会员等级变更计划任务
  1845. * @Author Gorden
  1846. * @Date 2024/10/16 14:46
  1847. *
  1848. * @return void
  1849. */
  1850. public static function roleExpired()
  1851. {
  1852. $members = Member::where('join_member_role_id', '<>', '')->get()->toArray();
  1853. foreach ($members as $member) {
  1854. if (empty($member['member_role_begintime'])) {
  1855. continue;
  1856. }
  1857. $endDateTime = date('Y-m-d 23:59:59', strtotime($member['member_role_begintime'] . ' +364 days'));
  1858. if (strtotime($endDateTime) > time()) {
  1859. continue;
  1860. }
  1861. $memberUpdate = [
  1862. 'join_member_role_id' => '',
  1863. 'member_role_begintime' => '',
  1864. 'member_role_config_json' => '[]'
  1865. ];
  1866. if (!empty($member['member_role_config_json'])) {
  1867. $roleConfigJson = json_decode($member['member_role_config_json'], true);
  1868. if (!empty($roleConfigJson['next'])) {
  1869. $memberUpdate['join_member_role_id'] = $roleConfigJson['next'][0]['member_role_id'];
  1870. $memberUpdate['member_role_begintime'] = $roleConfigJson['next'][0]['member_role_begintime'];
  1871. unset($roleConfigJson['next'][0]);
  1872. $memberUpdate['member_role_config_json'] = json_encode($roleConfigJson);
  1873. }
  1874. }
  1875. Member::where('member_id', $member['member_id'])->update($memberUpdate);
  1876. Log::info("会员过期", ['member_id' => $member['member_id'], 'role_id' => $member['join_member_role_id']]);
  1877. }
  1878. }
  1879. }