MemberService.php 107 KB

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