MemberService.php 88 KB

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