MemberService.php 109 KB

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