MemberService.php 102 KB

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