MemberService.php 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. /**
  709. * @Desc 储值卡账户明细
  710. * @Author Gorden
  711. * @Date 2024/11/19 11:48
  712. *
  713. * @param Request $request
  714. * @return Response
  715. */
  716. public static function cardInfo(Request $request)
  717. {
  718. $memberId = $request->get('member_id', '');
  719. $accountNbr = $request->get('account_nbr', '');
  720. $page = $request->get('page', 1);
  721. $pageSize = $request->get('pageSize', 20);
  722. $account = MemberAccount::where('join_account_member_id', $memberId)
  723. ->where('member_account_nbr', $accountNbr)
  724. ->where('member_account_classify', 'CARD')
  725. ->first();
  726. if (!$account) {
  727. return json_fail('暂无数据');
  728. }
  729. $balance = $account->member_account_surplus + $account->member_account_added;
  730. $info = PayDetail::where('pay_prepayid', $accountNbr)
  731. ->where('pay_status', 'SUCCESS')
  732. ->orderByDesc('pay_addtimes')
  733. ->get()
  734. ->toArray();
  735. $data = [];
  736. foreach ($info as $key => $item) {
  737. unset($item['pay_json_request']);
  738. $categoryIds = [];
  739. if (!empty($item['join_pay_order_id'])) {
  740. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  741. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  742. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  743. }
  744. $categoryName = [];
  745. foreach ($categoryIds as $id) {
  746. $category = SysCategory::where('category_id', $id)->first();
  747. if (!empty($category->category_super_path)) {
  748. $path = explode('#', $category->category_super_path);
  749. $ids = array_filter($path);
  750. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  751. } else {
  752. $categoryName[] = $category->category_name;
  753. }
  754. }
  755. $item['goods_category'] = implode(',', array_unique($categoryName));
  756. if (in_array($item['pay_category'], ['RECHARGE','REFUND'])) {
  757. $item['balance_type'] = 'add';
  758. } else {
  759. $item['balance_type'] = 'cut';
  760. }
  761. $item['balance'] = number_format($balance, 2, '.', '');
  762. $balance = $balance + $item['pay_amount'];
  763. $data[] = $item;
  764. }
  765. // 绑定记录
  766. $memberAccountLists = MemberAccountList::where('join_list_member_account_nbr',$accountNbr)
  767. ->where('member_account_list_attr','IN')
  768. ->select('member_account_list_amount','member_account_list_datetime','member_account_list_attr','join_list_member_account_nbr','member_account_list_addtimes')
  769. ->get()
  770. ->toArray();
  771. foreach ($memberAccountLists as $item){
  772. $data[] = [
  773. 'goods_category' => '',
  774. 'balance' => $item['member_account_list_amount'],
  775. 'pay_amount' => $item['member_account_list_amount'],
  776. 'pay_category' => 'BIND',
  777. 'balance_type' => 'add',
  778. 'pay_addtimes' => $item['member_account_list_addtimes'],
  779. ];
  780. }
  781. $total = count($data);
  782. $start = ($page - 1) * $pageSize;
  783. $rows = array_slice($data, $start, $pageSize);
  784. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  785. }
  786. public static function welfareInfo(Request $request)
  787. {
  788. $memberId = $request->get('member_id', '');
  789. $page = $request->get('page', 1);
  790. $pageSize = $request->get('pageSize', 20);
  791. $account = MemberAccount::where('join_account_member_id', $memberId)
  792. ->where('member_account_classify', 'WELFARE')
  793. ->first();
  794. if (!$account) {
  795. return json_fail('暂无数据');
  796. }
  797. $balance = $account->member_account_surplus + $account->member_account_added;
  798. $info = PayDetail::where(function ($query) use ($memberId) {
  799. $query->where('join_pay_member_id', $memberId)
  800. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  801. ->where('pay_category', 'RECHARGE')
  802. ->where('pay_prepayid', 'WELFARE')
  803. ->where('pay_status', 'SUCCESS');
  804. })->orWhere(function ($query) use ($memberId) {
  805. $query->where('join_pay_member_id', $memberId)
  806. ->where('pay_prepayid', 'like', '%WELFARE%')
  807. ->where('pay_status', 'SUCCESS');
  808. })->orderByDesc('pay_addtimes')
  809. ->get()
  810. ->toArray();
  811. $data = [];
  812. foreach ($info as $key => $item) {
  813. $categoryIds = [];
  814. if (!empty($item['join_pay_order_id'])) {
  815. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  816. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  817. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  818. }
  819. $categoryName = [];
  820. foreach ($categoryIds as $id) {
  821. $category = SysCategory::where('category_id', $id)->first();
  822. if (!empty($category->category_super_path)) {
  823. $path = explode('#', $category->category_super_path);
  824. $ids = array_filter($path);
  825. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  826. } else {
  827. $categoryName[] = $category->category_name;
  828. }
  829. }
  830. $item['goods_category'] = implode(',', array_unique($categoryName));
  831. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  832. $item['goods_category'] = '自定义金额充值';
  833. }
  834. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  835. $item['balance_type'] = 'add';
  836. } else {
  837. $item['balance_type'] = 'cut';
  838. }
  839. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  840. if (!empty($item['pay_extend_json'])) {
  841. $extendJson = json_decode($item['pay_extend_json'], true);
  842. if (!isset($extendJson['added_amount'])) {
  843. $extendJson['added_amount'] = 0;
  844. }
  845. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  846. $data[] = [
  847. 'join_pay_member_id' => $item['join_pay_member_id'],
  848. 'pay_addtimes' => $item['pay_addtimes'],
  849. 'pay_amount' => $extendJson['added_amount'],
  850. 'pay_category' => 'ADDED',
  851. 'balance' => number_format($balance, 2, '.', ''),
  852. 'balance_type' => 'add',
  853. ];
  854. }
  855. $balance = $balance - $extendJson['added_amount'];
  856. }
  857. $item['balance'] = number_format($balance, 2, '.', '');
  858. $balance = $balance - $item['pay_amount'];
  859. $data[] = $item;
  860. continue;
  861. }
  862. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  863. if (!empty($item['pay_extend_json'])) {
  864. $extendJson = json_decode($item['pay_extend_json'], true);
  865. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  866. $data[] = [
  867. 'join_pay_member_id' => $item['join_pay_member_id'],
  868. 'pay_addtimes' => $item['pay_addtimes'],
  869. 'pay_amount' => $extendJson['added_amount'],
  870. 'pay_category' => 'ADDED',
  871. 'balance' => number_format($balance, 2, '.', ''),
  872. 'balance_type' => 'add',
  873. ];
  874. $balance = $balance - $extendJson['added_amount'];
  875. }
  876. }
  877. $item['balance'] = number_format($balance, 2, '.', '');
  878. $balance = $balance - $item['pay_amount'];
  879. } else {
  880. $item['balance'] = number_format($balance, 2, '.', '');
  881. $balance = $balance + $item['pay_amount'];
  882. }
  883. $data[] = $item;
  884. }
  885. $total = count($data);
  886. $start = ($page - 1) * $pageSize;
  887. $rows = array_slice($data, $start, $pageSize);
  888. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  889. }
  890. /**
  891. * @Desc 佣金记录
  892. * @Author Gorden
  893. * @Date 2024/9/30 14:52
  894. *
  895. * @param Request $request
  896. * @return Response
  897. */
  898. public static function commissionList(Request $request)
  899. {
  900. $memberId = $request->get('member_id');
  901. $page = $request->get('page', 1);
  902. $pageSize = $request->get('pageSize', 20);
  903. $rows = MemberAccountList::where('join_member_account_list_member_id', $memberId)
  904. ->where('member_account_list_attr', 'IN');
  905. $total = $rows->count();
  906. $rows = $rows->forPage($page, $pageSize)
  907. ->get()
  908. ->toArray();
  909. foreach ($rows as &$row) {
  910. if (!empty($row['member_account_list_json'])) {
  911. $memberAccountListJson = json_decode($row['member_account_list_json'], true);
  912. if ($memberAccountListJson['master_member_id']) {
  913. $row['master_member_name'] = MemberService::getMemberNameByMemberId($memberAccountListJson['master_member_id']);
  914. }
  915. }
  916. }
  917. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  918. }
  919. /**
  920. * @Desc 我的粉丝列表
  921. * @Author Gorden
  922. * @Date 2024/9/20 10:22
  923. *
  924. * @param Request $request
  925. */
  926. public static function fansList(Request $request)
  927. {
  928. $memberId = $request->get('member_id');
  929. $name = $request->get('name', '');
  930. $mobile = $request->get('mobile', '');
  931. $memberIds = [];
  932. $memberInviteIds = [];
  933. if (!empty($name)) {
  934. $memberIds = MemberCert::where('member_cert_name', 'like', '%' . $name . '%')->pluck('join_cert_member_id')->toArray();
  935. $memberInviteIds = Member::whereIn('member_id', $memberIds)->pluck('join_invite_member_id')->toArray();
  936. }
  937. if (!empty($mobile)) {
  938. $members = Member::where('member_mobile', 'like', '%' . $mobile . '%')
  939. ->select('member_id', 'join_invite_member_id')
  940. ->get()
  941. ->toArray();
  942. $memberIds2 = array_column($members, 'member_id');
  943. $memberInviteIds2 = array_column($members, 'join_invite_member_id');
  944. $memberIds = array_merge($memberIds, $memberIds2);
  945. $memberInviteIds = array_merge($memberInviteIds, $memberInviteIds2);
  946. }
  947. $fans1 = Member::with([
  948. 'info' => function ($query) {
  949. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  950. },
  951. 'cert' => function ($query) {
  952. $query->select('join_cert_member_id', 'member_cert_name');
  953. },
  954. 'role' => function ($query) {
  955. $query->select('member_role_id', 'member_role_name');
  956. }
  957. ])->where('join_invite_member_id', $memberId)
  958. ->when(!empty($memberIds), function ($query) use ($memberIds, $memberInviteIds) {
  959. $allSub = array_merge($memberIds, $memberInviteIds);
  960. $query->whereIn('member_id', $allSub);
  961. })
  962. ->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')
  963. ->get()
  964. ->toArray();
  965. $fans1Ids = array_column($fans1, 'id');
  966. $fans2 = Member::with([
  967. 'info' => function ($query) {
  968. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  969. },
  970. 'cert' => function ($query) {
  971. $query->select('join_cert_member_id', 'member_cert_name');
  972. },
  973. 'role' => function ($query) {
  974. $query->select('member_role_id', 'member_role_name');
  975. }
  976. ])->whereIn('join_invite_member_id', $fans1Ids)
  977. ->when(!empty($memberIds), function ($query) use ($memberIds) {
  978. $query->whereIn('member_id', $memberIds);
  979. })
  980. ->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')
  981. ->get()
  982. ->toArray();
  983. $fans = array_merge($fans1, $fans2);
  984. foreach ($fans as &$fan) {
  985. $memberMobile = $fan['member_mobile'] ?? '';
  986. // $memberNickname = $fan['info'] && $fan['info']['member_info_nickname'] ? $fan['info']['member_info_nickname'] : '';
  987. $memberCertName = $fan['cert'] && $fan['cert']['member_cert_name'] ? $fan['cert']['member_cert_name'] : '';
  988. $fan['member_name'] = '';
  989. if (!empty($memberCertName) && !empty($memberMobile)) {
  990. $fan['member_name'] = $memberCertName . '-' . $memberMobile;
  991. } elseif (!empty($memberCertName)) {
  992. $fan['member_name'] = $memberCertName;
  993. } elseif (!empty($memberMobile)) {
  994. $fan['member_name'] = $memberMobile;
  995. }
  996. if (!empty($fan['info']) && substr($fan['info']['member_info_headimg'], 0, 4) == 'http') {
  997. $fan['member_info_headimg'] = $fan['info']['member_info_headimg'];
  998. } else {
  999. $fan['member_info_headimg'] = 'https://img.wanyuewellness.com.cn/images/avatar_default.png';
  1000. }
  1001. // 等级名称
  1002. $fan['role_name'] = $fan['role'] && $fan['role']['member_role_name'] ? $fan['role']['member_role_name'] : '';
  1003. unset($fan['info'], $fan['cert'], $fan['role']);
  1004. }
  1005. $tree = new Tree($fans);
  1006. return json_success('success', $tree->getTree());
  1007. }
  1008. /**
  1009. * 格式化树
  1010. * @param $items
  1011. * @return Response
  1012. */
  1013. protected static function formatTree($items): Response
  1014. {
  1015. $format_items = [];
  1016. foreach ($items as $item) {
  1017. $format_items[] = [
  1018. 'name' => $item->title ?? $item->name ?? $item->id,
  1019. 'value' => (string)$item->id,
  1020. 'id' => $item->id,
  1021. 'pid' => $item->pid,
  1022. ];
  1023. }
  1024. $tree = new Tree($format_items);
  1025. return json_success('success', $tree->getTree());
  1026. }
  1027. public static function clearWelfare(Request $request)
  1028. {
  1029. $memberId = $request->post('member_id');
  1030. $payTime = $request->post('pay_paytimes', date('Y-m-d H:i:s'));
  1031. $payTime = date('Y-m-d H:i:s', strtotime($payTime));
  1032. $payRemark = $request->post('pay_remark', '');
  1033. $clearPwd = $request->post('clear_password');
  1034. if (!$clearPwd || $clearPwd != '123456') {
  1035. return json_fail('清零密码错误');
  1036. }
  1037. Db::beginTransaction();
  1038. try {
  1039. $account = MemberAccount::where('join_account_member_id', $memberId)
  1040. ->where('member_account_classify', 'WELFARE')
  1041. ->where('member_account_status', 'ACTIVED')
  1042. ->first();
  1043. if (!$account) {
  1044. throw new BusinessException("账户异常");
  1045. }
  1046. if ($account->member_account_surplus <= 0) {
  1047. throw new BusinessException("账户余额为0,无需清零");
  1048. }
  1049. // 创建清除记录
  1050. PayDetail::insert([
  1051. 'join_pay_member_id' => $memberId,
  1052. 'pay_status' => 'SUCCESS',
  1053. 'pay_category' => 'CLEAR',
  1054. 'pay_amount' => $account->member_account_surplus,
  1055. 'pay_paytimes' => $payTime,
  1056. 'pay_prepayid' => $memberId . '-WELFARE',
  1057. 'pay_remark' => $payRemark,
  1058. 'pay_addtimes' => time()
  1059. ]);
  1060. // 账户归零
  1061. $account->member_account_expend = $account->member_account_expend + $account->member_account_surplus;
  1062. $account->member_account_surplus = 0;
  1063. $account->save();
  1064. Db::commit();
  1065. return json_success('success');
  1066. } catch (BusinessException $e) {
  1067. Db::rollBack();
  1068. return json_fail($e->getMessage());
  1069. } catch (\Exception $e) {
  1070. dump($e->getMessage());
  1071. Db::rollBack();
  1072. return json_fail('清零失败');
  1073. }
  1074. }
  1075. public static function balanceInfo(Request $request)
  1076. {
  1077. $memberId = $request->get('member_id', '');
  1078. $page = $request->get('page', 1);
  1079. $pageSize = $request->get('pageSize', 20);
  1080. $account = MemberAccount::where('join_account_member_id', $memberId)
  1081. ->where('member_account_classify', 'CASH')
  1082. ->first();
  1083. if (!$account) {
  1084. return json_fail('暂无数据');
  1085. }
  1086. $balance = $account->member_account_surplus + $account->member_account_added;
  1087. $info = PayDetail::where(function ($query) use ($memberId) {
  1088. $query->where('join_pay_member_id', $memberId)
  1089. ->whereIn('pay_category', ['RECHARGE'])
  1090. ->where('pay_prepayid', '<>', $memberId . '-WELFARE')
  1091. ->where('pay_status', 'SUCCESS');
  1092. })->orWhere(function ($query) use ($memberId) {
  1093. $query->where('join_pay_member_id', $memberId)
  1094. ->where('pay_prepayid', 'like', '%CASH%')
  1095. ->where('pay_status', 'SUCCESS');
  1096. })->select('join_pay_object_json', 'join_pay_member_id', 'pay_category', 'join_pay_order_id', 'pay_extend_json', 'pay_addtimes', 'pay_amount');
  1097. // ->orderByDesc('pay_addtimes')
  1098. // ->get()
  1099. // ->toArray();
  1100. $memberAccountList = MemberAccountList::where('join_member_account_list_member_id', $memberId)
  1101. ->where('member_account_list_status', 'ACTIVED')
  1102. ->selectRaw('
  1103. member_account_list_extend_json as join_pay_object_json,
  1104. join_member_account_list_member_id as join_pay_member_id,
  1105. member_account_list_attr as pay_category,
  1106. join_list_member_account_nbr as pay_prepayid,
  1107. member_account_list_extend_json as pay_extend_json,
  1108. member_account_list_addtimes as pay_addtimes,
  1109. member_account_list_amount as pay_amount');
  1110. $info = $info->union($memberAccountList)
  1111. ->orderByDesc('pay_addtimes')
  1112. ->get()
  1113. ->toArray();
  1114. $data = [];
  1115. foreach ($info as $key => $item) {
  1116. $payObjectJson = [];
  1117. if (!empty($item['join_pay_object_json'])) {
  1118. $payObjectJson = json_decode($item['join_pay_object_json'], true);
  1119. }
  1120. $categoryIds = [];
  1121. if (!empty($item['join_pay_order_id'])) {
  1122. $goodsIds = [];
  1123. if (in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE', 'REFERRER', 'FRANCHISEE'])) {
  1124. if (isset($payObjectJson['recharge_goods_id'])) {
  1125. $goodsIds = [$payObjectJson['recharge_goods_id']];
  1126. }
  1127. } else {
  1128. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  1129. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  1130. }
  1131. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  1132. }
  1133. $categoryName = [];
  1134. foreach ($categoryIds as $id) {
  1135. $category = SysCategory::where('category_id', $id)->first();
  1136. if (!empty($category->category_super_path)) {
  1137. $path = explode('#', $category->category_super_path);
  1138. $ids = array_filter($path);
  1139. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  1140. } else {
  1141. $categoryName[] = $category->category_name;
  1142. }
  1143. }
  1144. $item['goods_category'] = implode(',', array_unique($categoryName));
  1145. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  1146. $item['goods_category'] = '自定义金额充值';
  1147. } else if (mb_substr($item['goods_category'], 0, 7) == '会员合伙人产品') {
  1148. $item['goods_category'] = '会员合伙人充值';
  1149. }
  1150. $goodsCategory = '';
  1151. if ($item['join_pay_order_id'] == 'FRANCHISEE') {
  1152. $goodsCategory = '加盟合伙人充值';
  1153. }
  1154. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'CROWN_CONTINUE', 'PARTNER', 'FRANCHISEE', 'IN'])) {
  1155. $item['balance_type'] = 'add';
  1156. } else {
  1157. $item['balance_type'] = 'cut';
  1158. }
  1159. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'FRANCHISEE', 'CROWN_CONTINUE'])) {
  1160. if (!empty($item['pay_extend_json']) && !in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE', 'FRANCHISEE'])) {
  1161. $extendJson = json_decode($item['pay_extend_json'], true);
  1162. if (!isset($extendJson['added_amount'])) {
  1163. $extendJson['added_amount'] = 0;
  1164. }
  1165. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  1166. $payCategory = 'ADDED';
  1167. if ($item['pay_category'] == 'GIVE') {
  1168. $payCategory = 'GIVE';
  1169. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1170. $payCategory = 'CROWN_CONTINUE';
  1171. }
  1172. $data[] = [
  1173. 'join_pay_member_id' => $item['join_pay_member_id'],
  1174. 'pay_addtimes' => $item['pay_addtimes'],
  1175. 'pay_amount' => $extendJson['added_amount'],
  1176. 'pay_category' => $payCategory,
  1177. 'balance' => number_format($balance, 2, '.', ''),
  1178. 'balance_type' => 'add',
  1179. 'goods_category' => $goodsCategory
  1180. ];
  1181. } elseif (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
  1182. $payCategory = 'ADDED';
  1183. if ($item['pay_category'] == 'GIVE') {
  1184. $payCategory = 'GIVE';
  1185. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1186. $payCategory = 'CROWN_CONTINUE';
  1187. }
  1188. $extendJson['added_amount'] = $item['pay_amount'] * $payObjectJson['recharge_config']['nbr'];
  1189. $data[] = [
  1190. 'join_pay_member_id' => $item['join_pay_member_id'],
  1191. 'pay_addtimes' => $item['pay_addtimes'],
  1192. 'pay_amount' => $extendJson['added_amount'],
  1193. 'pay_category' => $payCategory,
  1194. 'balance' => number_format($balance, 2, '.', ''),
  1195. 'balance_type' => 'add',
  1196. ];
  1197. }
  1198. $balance = $balance - $extendJson['added_amount'];
  1199. }
  1200. if (in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE', 'FRANCHISEE'])) {
  1201. $addedAmount = 0;
  1202. if (!isset($payObjectJson['recharge_config'])) {
  1203. $addedAmount = 0;
  1204. }
  1205. if (isset($payObjectJson['recharge_config']) && !empty($payObjectJson['recharge_config']['nbr']) && floatval($payObjectJson['recharge_config']['nbr'] > 0)) {
  1206. $addedAmount = $payObjectJson['recharge_config']['nbr'];
  1207. $data[] = [
  1208. 'join_pay_member_id' => $item['join_pay_member_id'],
  1209. 'pay_addtimes' => $item['pay_addtimes'],
  1210. 'pay_amount' => $addedAmount,
  1211. 'pay_category' => 'ADDED',
  1212. 'balance' => number_format($balance, 2, '.', ''),
  1213. 'balance_type' => 'add',
  1214. 'goods_category' => $goodsCategory
  1215. ];
  1216. }
  1217. $balance = $balance - $addedAmount;
  1218. }
  1219. $item['balance'] = number_format($balance, 2, '.', '');
  1220. $balance = $balance - $item['pay_amount'];
  1221. if (!in_array($item['pay_category'], ['GIVE', 'CROWN_CONTINUE']) && !in_array($item['join_pay_order_id'], ['FRANCHISEE'])) {
  1222. $data[] = $item;
  1223. }
  1224. continue;
  1225. }
  1226. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'CROWN_CONTINUE', 'PARTNER', 'COMBINE', 'FRANCHISEE', 'IN'])) {
  1227. if (!empty($item['pay_extend_json']) && !in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE', 'FRANCHISEE'])) {
  1228. $extendJson = json_decode($item['pay_extend_json'], true);
  1229. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  1230. $payCategory = 'ADDED';
  1231. if ($item['pay_category'] == 'GIVE') {
  1232. $payCategory = 'GIVE';
  1233. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1234. $payCategory = 'CROWN_CONTINUE';
  1235. }
  1236. $data[] = [
  1237. 'join_pay_member_id' => $item['join_pay_member_id'],
  1238. 'pay_addtimes' => $item['pay_addtimes'],
  1239. 'pay_amount' => $extendJson['added_amount'],
  1240. 'pay_category' => $payCategory,
  1241. 'balance' => number_format($balance, 2, '.', ''),
  1242. 'balance_type' => 'add',
  1243. 'goods_category' => $goodsCategory
  1244. ];
  1245. $balance = $balance - $extendJson['added_amount'];
  1246. } elseif (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
  1247. $payCategory = 'ADDED';
  1248. if ($item['pay_category'] == 'GIVE') {
  1249. $payCategory = 'GIVE';
  1250. } elseif ($item['pay_category'] == 'CROWN_CONTINUE') {
  1251. $payCategory = 'CROWN_CONTINUE';
  1252. }
  1253. $extendJson['added_amount'] = $item['pay_amount'] * $payObjectJson['recharge_config']['nbr'];
  1254. $data[] = [
  1255. 'join_pay_member_id' => $item['join_pay_member_id'],
  1256. 'pay_addtimes' => $item['pay_addtimes'],
  1257. 'pay_amount' => $extendJson['added_amount'],
  1258. 'pay_category' => $payCategory,
  1259. 'balance' => number_format($balance, 2, '.', ''),
  1260. 'balance_type' => 'add',
  1261. 'goods_category' => $goodsCategory
  1262. ];
  1263. $balance = $balance - $extendJson['added_amount'];
  1264. }
  1265. }
  1266. if (in_array($item['join_pay_order_id'], ['PARTNER', 'COMBINE', 'FRANCHISEE'])) {
  1267. $addedAmount = 0;
  1268. if (!isset($payObjectJson['recharge_config'])) {
  1269. $addedAmount = 0;
  1270. }
  1271. if (isset($payObjectJson['recharge_config']) && !empty($payObjectJson['recharge_config']['nbr']) && floatval($payObjectJson['recharge_config']['nbr'] > 0)) {
  1272. $addedAmount = $payObjectJson['recharge_config']['nbr'];
  1273. $data[] = [
  1274. 'join_pay_member_id' => $item['join_pay_member_id'],
  1275. 'pay_addtimes' => $item['pay_addtimes'],
  1276. 'pay_amount' => $addedAmount,
  1277. 'pay_category' => 'ADDED',
  1278. 'balance' => number_format($balance, 2, '.', ''),
  1279. 'balance_type' => 'add',
  1280. 'goods_category' => $goodsCategory
  1281. ];
  1282. }
  1283. $balance = $balance - $addedAmount;
  1284. }
  1285. $item['balance'] = number_format($balance, 2, '.', '');
  1286. $balance = $balance - $item['pay_amount'];
  1287. } else {
  1288. $item['balance'] = number_format($balance, 2, '.', '');
  1289. $balance = $balance + $item['pay_amount'];
  1290. }
  1291. if (!in_array($item['pay_category'], ['GIVE', 'CROWN_CONTINUE']) && !in_array($item['join_pay_order_id'], ['FRANCHISEE'])) {
  1292. $data[] = $item;
  1293. }
  1294. }
  1295. $total = count($data);
  1296. $start = ($page - 1) * $pageSize;
  1297. $rows = array_slice($data, $start, $pageSize);
  1298. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  1299. }
  1300. public static function pointInfo($memberId)
  1301. {
  1302. $account = MemberAccount::where('join_account_member_id', $memberId)
  1303. ->where('member_account_classify', 'POINTS')
  1304. ->first();
  1305. if (!$account) {
  1306. return json_fail('暂无数据');
  1307. }
  1308. $balance = $account->member_account_surplus + $account->member_account_added;
  1309. $points = ClientPoints::where('join_client_points_member_id', $memberId)->orderBy('client_points_addtimes', 'DESC')->get()->toArray();
  1310. $nowPoint = 0;
  1311. foreach ($points as $key => &$point) {
  1312. $point['goods_category'] = '其他';
  1313. if (!empty($point['join_client_points_goods_id'])) {
  1314. $categoryId = Goods::where('goods_id', $point['join_client_points_goods_id'])->pluck('join_goods_category_id');
  1315. $category = SysCategory::where('category_id', $categoryId)->first();
  1316. if (!empty($category->category_super_path)) {
  1317. $path = explode('#', $category->category_super_path);
  1318. $ids = array_filter($path);
  1319. $point['goods_category'] = SysCategory::where('category_id', current($ids))->value('category_name');
  1320. } else {
  1321. $point['goods_category'] = $category->category_name;
  1322. }
  1323. }
  1324. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  1325. $point['balance_type'] = 'add';
  1326. } else {
  1327. $point['balance_type'] = 'cut';
  1328. }
  1329. if ($key == 0) {
  1330. $point['balance'] = number_format($balance, 2, '.', '');
  1331. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  1332. $balance = $balance - $point['client_points_nbr'];
  1333. } else {
  1334. $balance = $balance + $point['client_points_nbr'];
  1335. }
  1336. continue;
  1337. }
  1338. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送", '登录活动积分'])) {
  1339. $point['balance'] = number_format($balance, 2, '.', '');
  1340. $balance = $balance - $point['client_points_nbr'];
  1341. // $point['balance'] = $balance = $nowPoint = number_format($balance - $point['client_points_nbr'], 2, '.', '');
  1342. } else {
  1343. $point['balance'] = number_format($balance, 2, '.', '');
  1344. $balance = $balance + $point['client_points_nbr'];
  1345. // $point['balance'] = $balance = $nowPoint = number_format($balance + $point['client_points_nbr'], 2, '.', '');
  1346. }
  1347. }
  1348. return json_success('', $points);
  1349. }
  1350. /**
  1351. * @Desc 添加会员
  1352. * @Author Gorden
  1353. * @Date 2024/3/25 13:40
  1354. *
  1355. * @param $params
  1356. * @return \support\Response
  1357. */
  1358. public static function add($params)
  1359. {
  1360. Db::beginTransaction();
  1361. try {
  1362. // 排重
  1363. if (Member::where('member_mobile', $params['mobile'])->where('member_status', 'ACTIVED')->exists()) {
  1364. throw new BusinessException("会员已存在");
  1365. }
  1366. $memberId = self::generateMemberId();
  1367. // 先写主表,要ID
  1368. $memberData = [
  1369. 'member_id' => $memberId,
  1370. 'member_is_owner' => $params['is_owner'] ?? 'N',
  1371. 'member_classify' => $params['classify'] ?? '',
  1372. 'member_status' => $params['status'] ?? 'PENDING',
  1373. 'member_mobile' => $params['mobile'] ?? '',
  1374. 'member_category' => $params['member_category'] ?? '后台添加',
  1375. 'member_invite_code' => self::generateInviteCode(),
  1376. // 'member_from' => $params['source'],
  1377. // 'member_extend_json' => json_encode(['come_from' => $params['source']]),
  1378. 'member_addtimes' => time(),
  1379. ];
  1380. $extendJson['come_from'] = $params['source'];
  1381. if (!empty($params['user_id'])) {
  1382. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  1383. if (!$deptId) {
  1384. throw new BusinessException('请先关联业务员和门店');
  1385. }
  1386. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  1387. }
  1388. $memberData['member_extend_json'] = json_encode($extendJson);
  1389. Member::insert($memberData);
  1390. // 账户表
  1391. $accountData = [
  1392. 'join_account_member_id' => $memberId,
  1393. 'member_account_classify' => 'POINTS',
  1394. 'member_account_status' => 'ACTIVED',
  1395. 'member_account_category' => 'NORMAL',
  1396. 'member_account_nbr' => $memberId . '-POINTS',
  1397. 'member_account_name' => '积分账户',
  1398. 'member_account_addtimes' => time()
  1399. ];
  1400. // 积分账户
  1401. MemberAccount::insert($accountData);
  1402. $accountData['member_account_classify'] = 'CASH';
  1403. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1404. $accountData['member_account_name'] = '余额账户';
  1405. // 现金/余额账户
  1406. MemberAccount::insert($accountData);
  1407. // 福利账户
  1408. // if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY'){
  1409. // $welfareData = [
  1410. // 'join_account_member_id'=>$memberId,
  1411. // 'member_account_classify'=>'WELFARE',
  1412. // 'member_account_status'=>'ACTIVED',
  1413. // 'member_account_category'=>'NORMAL',
  1414. // 'member_account_nbr'=>$memberId.'-WELFARE',
  1415. // 'member_account_name'=>'福利账户',
  1416. // 'member_account_addtimes'=>time()
  1417. // ];
  1418. // MemberAccount::insert($welfareData);
  1419. // }
  1420. // member cert
  1421. $certData = [
  1422. 'join_cert_member_id' => $memberId,
  1423. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  1424. 'member_cert_name' => $params['cert_name'] ?? '',
  1425. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  1426. 'member_cert_birth' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  1427. 'member_cert_gender' => $params['gender'] ?? '',
  1428. // 'member_cert_province' => !empty($params['province']) ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  1429. // 'member_cert_city' => $params['city'] ?? '',
  1430. // 'member_cert_addr' => $params['addr'] ?? '',
  1431. // 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  1432. // 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  1433. ];
  1434. MemberCert::insert($certData);
  1435. // member info
  1436. $infoData = [
  1437. 'join_info_member_id' => $memberId,
  1438. 'member_info_nickname' => $params['account_name'] ?? '',
  1439. 'member_info_headimg' => !empty($params['avatar']) ? getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']) : '',
  1440. 'member_info_referee' => $params['member_info_referee'] ?? '',
  1441. 'member_info_business' => $params['member_info_business'] ?? '',
  1442. 'member_info_service' => $params['member_info_service'] ?? '',
  1443. 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
  1444. 'member_info_address' => $params['member_info_address'] ?? '',
  1445. 'member_info_birthday' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  1446. 'member_info_gender' => $params['gender'] ?? '',
  1447. ];
  1448. MemberInfo::insert($infoData);
  1449. Db::commit();
  1450. } catch (BusinessException $e) {
  1451. Db::rollBack();
  1452. return json_fail($e->getMessage());
  1453. } catch (\Exception $e) {
  1454. Db::rollBack();
  1455. dump($e->getMessage() . ';line:' . $e->getLine());
  1456. return json_fail("数据添加失败");
  1457. }
  1458. return json_success('数据添加成功', ['member_id' => $memberId]);
  1459. }
  1460. /**
  1461. * @Desc 编辑会员/ 业主
  1462. * @Author Gorden
  1463. * @Date 2024/4/20 15:18
  1464. *
  1465. * @param $params
  1466. * @return \support\Response
  1467. */
  1468. public static function update($params)
  1469. {
  1470. Db::beginTransaction();
  1471. try {
  1472. $memberId = $params['member_id'];
  1473. $member = Member::where('member_id', $params['member_id'])->first();
  1474. if (!$member) {
  1475. throw new BusinessException('数据异常');
  1476. }
  1477. $extendJson = [];
  1478. if (!empty($member->member_extend_json)) {
  1479. $extendJson = json_decode($member->member_extend_json, true);
  1480. }
  1481. $extendJson['come_from'] = $params['source'];
  1482. // 门店、业务员
  1483. if (!empty($params['user_id'])) {
  1484. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  1485. if (!$deptId) {
  1486. throw new BusinessException('您选择的业务员没有关联门店');
  1487. }
  1488. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  1489. }
  1490. // 先写主表,要ID
  1491. $memberData = [
  1492. 'member_id' => $memberId,
  1493. 'member_is_owner' => $params['is_owner'],
  1494. 'member_classify' => $params['member_classify'],
  1495. 'member_status' => $params['status'],
  1496. // 'member_mobile' => $params['mobile'],
  1497. // 'member_from' => $params['source'],
  1498. 'member_extend_json' => json_encode($extendJson)
  1499. ];
  1500. Member::where('member_id', $params['member_id'])->update($memberData);
  1501. // member cert
  1502. $certData = [
  1503. 'join_cert_member_id' => $memberId,
  1504. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  1505. 'member_cert_name' => $params['cert_name'] ?? '',
  1506. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  1507. 'member_cert_birth' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  1508. 'member_cert_gender' => $params['gender'] ?? '',
  1509. // 'member_cert_province' => $params['province'] ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  1510. // 'member_cert_city' => $params['city'] ?? '',
  1511. // 'member_cert_addr' => $params['addr'] ?? '',
  1512. 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  1513. 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  1514. ];
  1515. if (!MemberCert::where('join_cert_member_id', $memberId)->exists()) {
  1516. MemberCert::insert($certData);
  1517. } else {
  1518. MemberCert::where('join_cert_member_id', $params['member_id'])->update($certData);
  1519. }
  1520. // member info
  1521. $avatar = '';
  1522. if ($params['avatar'] && (strpos($params['avatar'], getenv('APP_IMAGE_DOMAIN')) !== false || strpos($params['avatar'], getenv('SYSTEM_AVATAR_DOMAIN')) !== false)) {
  1523. $avatar = $params['avatar'];
  1524. } else if ($params['avatar']) {
  1525. $avatar = getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']);
  1526. }
  1527. $infoData = [
  1528. 'join_info_member_id' => $memberId,
  1529. 'member_info_nickname' => $params['account_name'],
  1530. 'member_info_headimg' => $avatar,
  1531. 'member_info_referee' => $params['member_info_referee'] ?? '',
  1532. 'member_info_business' => $params['member_info_business'] ?? '',
  1533. 'member_info_service' => $params['member_info_service'] ?? '',
  1534. 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
  1535. 'member_info_address' => $params['member_info_address'] ?? '',
  1536. 'member_info_birthday' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  1537. 'member_info_gender' => $params['gender'] ?? '',
  1538. ];
  1539. if (!MemberInfo::where('join_info_member_id', $params['member_id'])->exists()) {
  1540. MemberInfo::insert($infoData);
  1541. } else {
  1542. MemberInfo::where('join_info_member_id', $params['member_id'])->update($infoData);
  1543. }
  1544. // 福利账户
  1545. if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY' && !MemberAccount::where('member_account_nbr', $memberId . '-WELFARE')->exists()) {
  1546. $welfareData = [
  1547. 'join_account_member_id' => $memberId,
  1548. 'member_account_classify' => 'WELFARE',
  1549. 'member_account_status' => 'ACTIVED',
  1550. 'member_account_category' => 'NORMAL',
  1551. 'member_account_nbr' => $memberId . '-WELFARE',
  1552. 'member_account_name' => '福利账户',
  1553. 'member_account_addtimes' => time()
  1554. ];
  1555. MemberAccount::insert($welfareData);
  1556. }
  1557. Db::commit();
  1558. } catch (BusinessException $e) {
  1559. Db::rollBack();
  1560. dump($e->getMessage() . ';line:' . $e->getLine());
  1561. return json_fail($e->getMessage());
  1562. } catch (\Exception $e) {
  1563. Db::rollBack();
  1564. dump($e->getMessage() . ';line:' . $e->getLine());
  1565. return json_fail("数据更新失败");
  1566. }
  1567. return json_success('数据更新成功');
  1568. }
  1569. /**
  1570. * @Desc 会员关联设备列表
  1571. * @Author Gorden
  1572. * @Date 2024/4/7 9:52
  1573. *
  1574. * @param $id
  1575. * @return \support\Response
  1576. */
  1577. public static function deviceList($id)
  1578. {
  1579. $family = Family::where('join_family_creator_member_id', $id)->get()->toArray();
  1580. $familyMemberIds = '';
  1581. if ($family) {
  1582. $familyIds = array_column($family, 'family_id');
  1583. $familyMember = FamilyMember::whereIn('join_family_id', $familyIds)->get()->toArray();
  1584. $familyMemberIds = array_column($familyMember, 'join_family_member_id');
  1585. }
  1586. $rows = MemberDevice::leftJoin('device', 'device.device_id', '=', 'member_device.join_member_device_id')
  1587. ->leftJoin('member', 'member.member_id', '=', 'member_device.join_device_member_id')
  1588. ->leftJoin('family_member', 'family_member.join_family_member_id', '=', 'member_device.join_device_member_id')
  1589. ->select('device.device_category', 'device.device_name', 'device.device_type', 'member.member_mobile', 'member_device.*', 'family_member.family_member_name')
  1590. ->when(!empty($familyMemberIds), function ($query) use ($familyMemberIds) {
  1591. $query->whereIn('member_device.join_device_master_member_id', $familyMemberIds);
  1592. })
  1593. ->when(empty($familyMemberIds), function ($query) use ($id) {
  1594. $query->where('member_device.join_device_master_member_id', $id);
  1595. })->orderBy('member_device_addtimes', 'desc')
  1596. ->get()
  1597. ->toArray();
  1598. return json_success('', ['rows' => $rows]);
  1599. }
  1600. /**
  1601. * @Desc 绑定设备
  1602. * @Author Gorden
  1603. * @Date 2024/4/7 10:27
  1604. *
  1605. * @param Request $request
  1606. * @return \support\Response
  1607. */
  1608. public static function addDevice(Request $request)
  1609. {
  1610. $params = $request->post();
  1611. Db::beginTransaction();
  1612. try {
  1613. $data = [
  1614. 'join_device_member_id' => $params['join_device_member_id'] ?? '',
  1615. 'join_device_master_member_id' => $params['join_device_master_member_id'],
  1616. 'join_member_device_id' => $params['join_member_device_id'],
  1617. 'member_device_status' => $params['member_device_status'],
  1618. 'member_device_key' => $params['member_device_key'] ?? '',
  1619. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1620. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1621. 'member_device_addtimes' => time(),
  1622. ];
  1623. MemberDevice::insert($data);
  1624. $device = Device::where('device_id', $params['join_member_device_id'])->whereIn('device_status', ['PROCESSING', 'PENDING'])->first();
  1625. // 激活设备
  1626. if (!$device) {
  1627. Db::rollBack();
  1628. return json_fail('当前设备不是可激活状态');
  1629. }
  1630. $device->device_status = 'ACTIVED';
  1631. $device->save();
  1632. Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => 'ACTIVED']);
  1633. _syslog("绑定设备", "用户【" . ($params['join_device_member_id'] ?? $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1634. Db::commit();
  1635. } catch (\Exception $e) {
  1636. Db::rollBack();
  1637. dump($e->getMessage());
  1638. return json_fail('绑定失败');
  1639. }
  1640. return json_success('绑定成功');
  1641. }
  1642. public static function updateDevice(Request $request)
  1643. {
  1644. $params = $request->post();
  1645. Db::beginTransaction();
  1646. try {
  1647. $data = [
  1648. 'member_device_status' => $params['member_device_status'],
  1649. 'member_device_key' => $params['member_device_key'],
  1650. 'join_device_member_id' => $params['join_device_member_id'],
  1651. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1652. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1653. ];
  1654. MemberDevice::where('member_device_id', $request->post('member_device_id'))->update($data);
  1655. $device = Device::where('device_id', $params['join_member_device_id'])->first();
  1656. if ($device->device_status != $params['member_device_status']) {
  1657. $device->device_status = $params['member_device_status'];
  1658. $device->save();
  1659. }
  1660. // Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => $params['member_device_status']]);
  1661. Db::commit();
  1662. _syslog("更新绑定设备", "用户【" . ($params['join_device_member_id'] == "" ? $params['join_device_member_id'] : $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1663. } catch (\Exception $e) {
  1664. Db::rollBack();
  1665. dump($e->getMessage());
  1666. return json_fail('绑定更新失败');
  1667. }
  1668. return json_success('绑定更新成功');
  1669. }
  1670. public static function deleteDevice(Request $request)
  1671. {
  1672. try {
  1673. Db::beginTransaction();
  1674. $memberDevice = MemberDevice::where('member_device_id', $request->post('member_device_id'))->first();
  1675. if (!$memberDevice) {
  1676. return json_fail('数据不存在');
  1677. }
  1678. $device = Device::where('device_id', $memberDevice->join_member_device_id)->first();
  1679. $device->device_status = "DISABLED";
  1680. $device->save();
  1681. // Device::where('device_id', $memberDevice->join_member_device_id)->update(['device_status' => 'DISABLED']);
  1682. $memberDevice->delete();
  1683. Db::commit();
  1684. _syslog("解除绑定设备", "用户【" . ($memberDevice->join_device_member_id == "" ? $memberDevice->join_device_member_id : $memberDevice->join_device_master_member_id) . '】,解除绑定设备【' . $device->device_name . '】');
  1685. return json_success('解除绑定成功');
  1686. } catch (\Exception $e) {
  1687. Db::rollBack();
  1688. return json_fail('绑定失败');
  1689. }
  1690. }
  1691. /**
  1692. * 手动创建会员
  1693. */
  1694. public static function createMember($params)
  1695. {
  1696. $memberId = $params['member_id'] ?? '';
  1697. if (!$memberId) {
  1698. throw new BusinessException("参数异常");
  1699. }
  1700. // 主表
  1701. $memberData = [
  1702. 'member_id' => $memberId,
  1703. 'member_is_owner' => 'N',
  1704. 'member_classify' => 'MEMBER',
  1705. 'member_status' => 'ACTIVED',
  1706. 'member_mobile' => $params['mobile'],
  1707. 'member_invite_code' => self::generateInviteCode(),
  1708. 'member_extend_json' => json_encode(['come_from' => 'SYSTEM']),
  1709. 'member_addtimes' => time()
  1710. ];
  1711. Member::insert($memberData);
  1712. // 账户表
  1713. $accountData = [
  1714. 'join_account_member_id' => $memberId,
  1715. 'member_account_classify' => 'POINTS',
  1716. 'member_account_status' => 'ACTIVED',
  1717. 'member_account_category' => 'NORMAL',
  1718. 'member_account_nbr' => $memberId . '-POINTS',
  1719. 'member_account_name' => '积分账户',
  1720. 'member_account_addtimes' => time()
  1721. ];
  1722. // 积分账户
  1723. MemberAccount::insert($accountData);
  1724. $accountData['member_account_classify'] = 'CASH';
  1725. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1726. $accountData['member_account_name'] = '余额账户';
  1727. // 现金/余额账户
  1728. MemberAccount::insert($accountData);
  1729. // info
  1730. $infoData = [
  1731. 'join_info_member_id' => $memberId,
  1732. 'member_info_nickname' => $params['username']
  1733. ];
  1734. MemberInfo::insert($infoData);
  1735. }
  1736. private static function generateMemberId()
  1737. {
  1738. $id = SysSerial::getSerial();
  1739. return "MR" . date('ymdHi') . random_string(4, 'up');
  1740. }
  1741. public static function getMemberName($mobile, $certName, $nickname)
  1742. {
  1743. $name = '';
  1744. if (!empty($certName)) {
  1745. $name = $certName;
  1746. } elseif (!empty($nickname)) {
  1747. $name = $nickname;
  1748. }
  1749. if (!empty($name) && !empty($mobile)) {
  1750. $name .= '-' . $mobile;
  1751. } elseif (!empty($mobile)) {
  1752. $name = $mobile;
  1753. }
  1754. return $name;
  1755. }
  1756. public static function getMemberCertName($mobile, $certName, $nickname)
  1757. {
  1758. $name = '';
  1759. if (!empty($certName)) {
  1760. $name = $certName;
  1761. }
  1762. if (!empty($name) && !empty($mobile)) {
  1763. $name .= '-' . $mobile;
  1764. } elseif (!empty($mobile)) {
  1765. $name = $mobile;
  1766. }
  1767. return $name;
  1768. }
  1769. public static function getMemberNickname($mobile)
  1770. {
  1771. if (empty($mobile)) {
  1772. return '';
  1773. }
  1774. return substr($mobile, -4, 4) . '会员';
  1775. }
  1776. public static function getMemberNameByMemberId($memberId)
  1777. {
  1778. $member = Member::with([
  1779. 'cert' => function ($query) {
  1780. $query->select('join_cert_member_id', 'member_cert_name');
  1781. },
  1782. 'info' => function ($query) {
  1783. $query->select('join_info_member_id', 'member_info_nickname');
  1784. },
  1785. ]
  1786. )->where('member_id', $memberId)
  1787. ->first();
  1788. if (empty($member)) {
  1789. return;
  1790. }
  1791. $member = $member->toArray();
  1792. $mobile = $certName = $nickname = '';
  1793. if (!empty($member['member_mobile'])) {
  1794. $mobile = $member['member_mobile'];
  1795. }
  1796. if (!empty($member['cert']) && !empty($member['cert']['member_cert_name'])) {
  1797. $certName = $member['cert']['member_cert_name'];
  1798. }
  1799. if (!empty($member['info']) && !empty($member['info']['member_info_nickname'])) {
  1800. $nickname = $member['info']['member_info_nickname'];
  1801. }
  1802. return self::getMemberName($mobile, $certName, $nickname);
  1803. }
  1804. /**
  1805. * @Desc 生成推荐码
  1806. * @Author Gorden
  1807. * @Date 2024/9/6 9:05
  1808. *
  1809. * @return string
  1810. */
  1811. public static function generateInviteCode()
  1812. {
  1813. $code = random_string(8, 'number');
  1814. if (Member::where('member_invite_code', $code)->exists()) {
  1815. return self::generateInviteCode();
  1816. } else {
  1817. return $code;
  1818. }
  1819. }
  1820. public static function getAvatarUrl($url)
  1821. {
  1822. if (substr($url, 0, 1) == '.' || substr($url, 0, 1) == '/' || empty($url)) {
  1823. $url = "https://img.wanyuewellness.com.cn/images/avatar_default.png";
  1824. }
  1825. return $url;
  1826. }
  1827. /**
  1828. * @Desc 获取昵称
  1829. * @Author Gorden
  1830. * @Date 2024/10/23 11:50
  1831. *
  1832. * @param $nickname
  1833. * @param $mobile
  1834. * @return mixed|string
  1835. */
  1836. public static function getNickname($nickname, $mobile)
  1837. {
  1838. if (!empty($nickname)) {
  1839. return $nickname;
  1840. } else if (!empty($mobile)) {
  1841. return substr($mobile, -4, 4) . '会员';
  1842. } else {
  1843. return '';
  1844. }
  1845. }
  1846. public static function getRoleName($roleId)
  1847. {
  1848. if (empty($roleId)) {
  1849. return '';
  1850. }
  1851. return MemberRole::where('member_role_id', $roleId)->value('member_role_name');
  1852. }
  1853. /**
  1854. * @Desc 获取身份-合伙人,推荐官
  1855. * @Author Gorden
  1856. * @Date 2024/9/28 13:41
  1857. *
  1858. * @param $isPartner
  1859. * @param $isReferrer
  1860. * @return string
  1861. */
  1862. public static function getIdentity($isPartner, $isReferrer, $isFranchisee)
  1863. {
  1864. if ($isPartner == 'Y') {
  1865. return "PARTNER";
  1866. } elseif ($isReferrer == 'Y') {
  1867. return "REFERRER";
  1868. } elseif ($isFranchisee == 'Y') {
  1869. return "FRANCHISEE";
  1870. } else {
  1871. return 'NON';
  1872. }
  1873. }
  1874. /**
  1875. * @Desc 会员合伙人过期
  1876. * @Author Gorden
  1877. * @Date 2024/10/10 9:50
  1878. *
  1879. * @return void
  1880. */
  1881. public static function partnerExpired()
  1882. {
  1883. $members = Member::where('member_is_partner', 'Y')->get()->toArray();
  1884. foreach ($members as $member) {
  1885. $partnerJson = ['member_id' => $member['member_id']];
  1886. if (!empty($member['member_partner_json'])) {
  1887. $partnerJson = json_decode($member['member_partner_json'], true);
  1888. }
  1889. if (isset($partnerJson['duedate'])) {
  1890. $duedateUnix = strtotime($partnerJson['duedate'] . ' 23:59:59');
  1891. if ($duedateUnix > time()) {
  1892. continue;
  1893. }
  1894. }
  1895. Member::where('member_id', $member['member_id'])->update(['member_is_partner' => 'N', 'member_partner_json' => '[]']);
  1896. _syslog("会员合伙人过期", "取消身份", false, $partnerJson, 1001);
  1897. Log::info("会员【" . $member['member_id'] . '】会员合伙人身份已过期', $partnerJson);
  1898. }
  1899. }
  1900. /**
  1901. * @Desc 康养推荐官过期
  1902. * @Author Gorden
  1903. * @Date 2024/10/10 10:14
  1904. *
  1905. * @return void
  1906. */
  1907. public static function referrerExpired()
  1908. {
  1909. $members = Member::where('member_is_referrer', 'Y')->get()->toArray();
  1910. foreach ($members as $member) {
  1911. $referrerJson = ['member_id' => $member['member_id']];
  1912. if (!empty($member['member_referrer_json'])) {
  1913. $referrerJson = json_decode($member['member_referrer_json'], true);
  1914. }
  1915. if (isset($referrerJson['duedate'])) {
  1916. $duedateUnix = strtotime($referrerJson['duedate'] . ' 23:59:59');
  1917. if ($duedateUnix > time()) {
  1918. continue;
  1919. }
  1920. }
  1921. Member::where('member_id', $member['member_id'])->update(['member_is_referrer' => 'N', 'member_referrer_json' => '[]']);
  1922. _syslog("康养推荐官过期", "取消身份", false, $referrerJson, 1001);
  1923. Log::info("会员【" . $member['member_id'] . '】康养推荐官身份已过期', $referrerJson);
  1924. }
  1925. }
  1926. /**
  1927. * @Desc 会员等级变更计划任务
  1928. * @Author Gorden
  1929. * @Date 2024/10/16 14:46
  1930. *
  1931. * @return void
  1932. */
  1933. public static function roleExpired()
  1934. {
  1935. $members = Member::where('join_member_role_id', '<>', '')->get()->toArray();
  1936. foreach ($members as $member) {
  1937. if (empty($member['member_role_begintime'])) {
  1938. continue;
  1939. }
  1940. $endDateTime = date('Y-m-d 23:59:59', strtotime($member['member_role_begintime'] . ' +364 days'));
  1941. if (strtotime($endDateTime) > time()) {
  1942. continue;
  1943. }
  1944. $memberUpdate = [
  1945. 'join_member_role_id' => '',
  1946. 'member_role_begintime' => '',
  1947. 'member_role_config_json' => '[]'
  1948. ];
  1949. if (!empty($member['member_role_config_json'])) {
  1950. $roleConfigJson = json_decode($member['member_role_config_json'], true);
  1951. if (!empty($roleConfigJson['next'])) {
  1952. $memberUpdate['join_member_role_id'] = $roleConfigJson['next'][0]['member_role_id'];
  1953. $memberUpdate['member_role_begintime'] = $roleConfigJson['next'][0]['member_role_begintime'];
  1954. unset($roleConfigJson['next'][0]);
  1955. $memberUpdate['member_role_config_json'] = json_encode($roleConfigJson);
  1956. }
  1957. }
  1958. Member::where('member_id', $member['member_id'])->update($memberUpdate);
  1959. Log::info("会员过期", ['member_id' => $member['member_id'], 'role_id' => $member['join_member_role_id']]);
  1960. }
  1961. }
  1962. /**
  1963. * @Desc 加盟合伙人过期
  1964. * @Author Gorden
  1965. * @Date 2024/11/13 8:48
  1966. *
  1967. * @return void
  1968. */
  1969. public static function franchiseeExpired()
  1970. {
  1971. $members = Member::where('member_is_franchisee', 'Y')->get()->toArray();
  1972. foreach ($members as $member) {
  1973. $franchiseeJson = ['member_id' => $member['member_id']];
  1974. if (!empty($member['member_franchisee_json'])) {
  1975. $franchiseeJson = json_decode($member['member_franchisee_json'], true);
  1976. }
  1977. if (isset($franchiseeJson['duedate'])) {
  1978. $duedateUnix = strtotime($franchiseeJson['duedate'] . ' 23:59:59');
  1979. if ($duedateUnix > time()) {
  1980. continue;
  1981. }
  1982. }
  1983. Member::where('member_id', $member['member_id'])->update(['member_is_franchisee' => 'N', 'member_franchisee_json' => '[]']);
  1984. _syslog("加盟合伙人过期", "取消身份", false, $franchiseeJson, 1001);
  1985. Log::info("会员【" . $member['member_id'] . '】加盟合伙人身份已过期', $franchiseeJson);
  1986. }
  1987. }
  1988. /**
  1989. * @Desc 加盟合伙人每年发20%
  1990. * @Author Gorden
  1991. * @Date 2024/11/13 9:16
  1992. *
  1993. * @return void
  1994. */
  1995. public static function franchiseeAdded()
  1996. {
  1997. try {
  1998. $members = Member::where('member_is_franchisee', 'Y')->get()->toArray();
  1999. foreach ($members as $member) {
  2000. $franchiseeJson = [];
  2001. if (!empty($member['member_franchisee_json'])) {
  2002. $franchiseeJson = json_decode($member['member_franchisee_json'], true);
  2003. }
  2004. if (isset($franchiseeJson['datetime'])) {
  2005. if (date('Ymd', strtotime($franchiseeJson['datetime'])) == date('Ymd')) {
  2006. continue;
  2007. }
  2008. }
  2009. if (isset($franchiseeJson['duedate'])) {
  2010. $duedateUnix = strtotime($franchiseeJson['duedate']);
  2011. $todayUnix = strtotime(date('Y-m-d'));
  2012. if (in_array(($duedateUnix - $todayUnix) / (3600 * 24 * 365), [1, 2, 3, 4])) {
  2013. $payDetail = PayDetail::where('join_pay_member_id', $member['member_id'])
  2014. ->where('join_pay_order_id', 'FRANCHISEE')
  2015. ->orderBy('pay_addtimes', 'DESC')
  2016. ->first();
  2017. if (date('Ymd') == date('Ymd', strtotime($payDetail->pay_addtimes))) {
  2018. continue;
  2019. }
  2020. $payObjectJson = [];
  2021. if (!empty($payDetail->join_pay_object_json)) {
  2022. $payObjectJson = json_decode($payDetail->join_pay_object_json, true);
  2023. }
  2024. // 创建赠送记录
  2025. PayDetail::insert([
  2026. 'join_pay_member_id' => $payDetail->join_pay_member_id,
  2027. 'join_pay_order_id' => $payDetail->join_pay_order_id,
  2028. 'join_pay_object_json' => $payDetail->join_pay_object_json,
  2029. 'pay_status' => $payDetail->pay_status,
  2030. 'pay_category' => $payDetail->pay_category,
  2031. 'pay_amount' => $payDetail->pay_amount,
  2032. 'pay_paytimes' => date('Y-m-d H:i:s'),
  2033. 'pay_prepayid' => $payDetail->pay_prepayid,
  2034. 'pay_addtimes' => time()
  2035. ]);
  2036. // 赠送加入账户余额
  2037. $account = MemberAccount::where('join_account_member_id', $member['member_id'])
  2038. ->where('member_account_classify', 'CASH')
  2039. ->first();
  2040. $account->member_account_income = $account->member_account_income + $payObjectJson['recharge_config']['nbr'];
  2041. $account->member_account_surplus = $account->member_account_surplus + $payObjectJson['recharge_config']['nbr'];
  2042. $account->save();
  2043. Log::info("加盟合伙人赠送成功");
  2044. _syslog("加盟合伙人", "赠送20%成功", false, ['member_id' => $member['member_id']], 1001);
  2045. }
  2046. }
  2047. }
  2048. } catch (\Exception $e) {
  2049. Log::error("加盟合伙人赠送失败", ['member_id' => $member['member_id'], 'msg' => $e->getMessage()]);
  2050. _syslog("加盟合伙人", "赠送20%失败", false, ['member_id' => $member['member_id'], 'msg' => $e->getMessage()], 1001);
  2051. }
  2052. }
  2053. }