MemberService.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. <?php
  2. namespace app\admin\service\member;
  3. use app\model\ClientPoints;
  4. use app\model\Device;
  5. use app\model\Family;
  6. use app\model\FamilyMember;
  7. use app\model\Goods;
  8. use app\model\Member;
  9. use app\model\MemberAccount;
  10. use app\model\MemberCert;
  11. use app\model\MemberDevice;
  12. use app\model\MemberInfo;
  13. use app\model\Order;
  14. use app\model\OrderSheet;
  15. use app\model\PayDetail;
  16. use app\model\SysCategory;
  17. use app\model\SysDept;
  18. use app\model\SysSerial;
  19. use app\model\SysUser;
  20. use support\Db;
  21. use support\exception\BusinessException;
  22. use support\Request;
  23. class MemberService
  24. {
  25. public static function list(Request $request)
  26. {
  27. $page = $request->get('page', 1);
  28. $pageSize = $request->get('pageSize', 10);
  29. $name = $request->get('name', '');
  30. $mobile = $request->get('mobile', '');
  31. $level = $request->get('level', '');
  32. $isAuth = $request->get('is_auth', '');
  33. $addtime = $request->get('member_addtimes', []);
  34. $type = $request->get('type', '');
  35. $rows = Member::with([
  36. 'account' => function ($query) {
  37. $query->where('member_account_status', 'ACTIVED');
  38. }
  39. ])
  40. ->where('member_is_owner', 'N')
  41. ->where('member_mobile', '<>', '0000')
  42. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  43. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  44. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  45. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  46. // ->where('member_account.member_account_classify', 'CASH')
  47. ->when($name != '', function ($query) use ($name) {
  48. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  49. })->when($mobile != '', function ($query) use ($mobile) {
  50. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  51. })->when($level != '', function ($query) use ($level) {
  52. if ($level == 'other') {
  53. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  54. } else {
  55. $query->where('member_role.member_role_id', $level);
  56. }
  57. })->when($isAuth != '', function ($query) use ($isAuth) {
  58. if ($isAuth == 'Y') {
  59. $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', '<>', '');
  60. } else {
  61. $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', '');
  62. }
  63. })->when(!empty($addtime), function ($query) use ($addtime) {
  64. $addtime[0] = strtotime($addtime[0]);
  65. $addtime[1] = strtotime($addtime[1]);
  66. $query->whereBetween('member.member_addtimes', $addtime);
  67. })->when(!empty($type), function ($query) use ($type) {
  68. if ($type == 'todayAdd') {
  69. $addtime[0] = strtotime(date('Y-m-d') . ' 00:00:00');
  70. $addtime[1] = strtotime(date('Y-m-d') . ' 23:59:59');
  71. } else if ($type == 'monthAdd') {
  72. $addtime[0] = strtotime(date('Y-m-01') . ' 00:00:00');
  73. $addtime[1] = strtotime(date('Y-m-t') . ' 23:59:59');
  74. }
  75. $query->whereBetween('member.member_addtimes', $addtime);
  76. })
  77. ->select('member_id', 'member_is_vip', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  78. 'member_info.member_info_nickname', 'member_info.member_info_headimg',
  79. 'member_role.member_role_id', 'member_role.member_role_name',
  80. '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',
  81. // 'member_account.member_account_surplus'
  82. );
  83. // 总条数
  84. $total = $rows->count('member_id');
  85. $rows = $rows->orderBy('member_addtimes', 'DESC')
  86. ->forPage($page, $pageSize)
  87. ->get()
  88. ->toArray();
  89. foreach ($rows as &$row) {
  90. $row['member_mobile'] = substr($row['member_mobile'], 0, 3) . '****' . substr($row['member_mobile'], 7);
  91. $row['info'] = [
  92. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '用户',
  93. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : ''
  94. ];
  95. $row['cert'] = [
  96. 'member_cert_birth' => $row['member_cert_birth'],
  97. 'member_cert_gender' => $row['member_cert_gender'],
  98. 'member_cert_name' => $row['member_cert_name'],
  99. 'member_cert_nbr' => $row['member_cert_nbr'],
  100. 'member_cert_province' => !empty($row['member_cert_province']) && is_json($row['member_cert_province']) ? json_decode($row['member_cert_province']) : '',
  101. 'member_cert_addr' => $row['member_cert_addr'],
  102. 'member_cert_face' => !empty($row['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_face'] : '',
  103. 'member_cert_photo' => !empty($row['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_photo'] : '',
  104. 'member_cert_nation' => $row['member_cert_nation'],
  105. ];
  106. $account = [];
  107. if (!empty($row['account'])) {
  108. foreach ($row['account'] as $item) {
  109. if ($item['member_account_classify'] == 'WELFARE') {
  110. $account['welfare'] = [
  111. 'member_account_name' => $item['member_account_name'],
  112. 'member_account_income' => $item['member_account_income'],
  113. 'member_account_expend' => $item['member_account_expend'],
  114. 'member_account_surplus' => $item['member_account_surplus'],
  115. 'member_account_added' => $item['member_account_added'],
  116. ];
  117. } else if ($item['member_account_classify'] == 'POINTS') {
  118. $account['points'] = [
  119. 'member_account_name' => $item['member_account_name'],
  120. 'member_account_income' => $item['member_account_income'],
  121. 'member_account_expend' => $item['member_account_expend'],
  122. 'member_account_surplus' => $item['member_account_surplus'],
  123. 'member_account_added' => $item['member_account_added'],
  124. ];
  125. } else if ($item['member_account_classify'] == 'CASH') {
  126. $account['cash'] = [
  127. 'member_account_name' => $item['member_account_name'],
  128. 'member_account_income' => $item['member_account_income'],
  129. 'member_account_expend' => $item['member_account_expend'],
  130. 'member_account_surplus' => $item['member_account_surplus'],
  131. 'member_account_added' => $item['member_account_added'],
  132. ];
  133. } else if ($item['member_account_classify'] == 'CARD') {
  134. $account['card'] = [
  135. 'member_account_name' => $item['member_account_name'],
  136. 'member_account_income' => $item['member_account_income'],
  137. 'member_account_expend' => $item['member_account_expend'],
  138. 'member_account_surplus' => $item['member_account_surplus'],
  139. 'member_account_added' => $item['member_account_added'],
  140. ];
  141. } else if ($item['member_account_classify'] == 'VIP') {
  142. $account['vip'] = [
  143. 'member_account_name' => $item['member_account_name'],
  144. 'member_account_income' => $item['member_account_income'],
  145. 'member_account_expend' => $item['member_account_expend'],
  146. 'member_account_surplus' => $item['member_account_surplus'],
  147. 'member_account_added' => $item['member_account_added'],
  148. ];
  149. }
  150. }
  151. }
  152. $row['account'] = $account;
  153. $row['is_auth'] = "未认证";
  154. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  155. $row['is_auth'] = "已认证";
  156. }
  157. $row['source'] = "APP";
  158. $row['belong'] = [];
  159. if (!empty($row['member_extend_json'])) {
  160. $extendJson = json_decode($row['member_extend_json'], true);
  161. if (isset($extendJson['come_from'])) {
  162. $row['source'] = $extendJson['come_from'] ?? '';
  163. }
  164. if (isset($extendJson['belong'])) {
  165. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  166. if ($user) {
  167. $row['belong']['user_id'] = $extendJson['belong']['user_id'];
  168. $row['belong']['username'] = $user->user_name;
  169. $row['belong']['user_mobile'] = $user->user_mobile;
  170. }
  171. $row['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  172. }
  173. }
  174. }
  175. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  176. }
  177. public static function exportMember(Request $request)
  178. {
  179. $memberIds = $request->get('member_id', '');
  180. $name = $request->get('name', '');
  181. $mobile = $request->get('mobile', '');
  182. $level = $request->get('level', '');
  183. $isAuth = $request->get('is_auth', '');
  184. $addtime = $request->get('member_addtimes', []);
  185. $rows = Member::with('account')
  186. ->where('member_is_owner', 'N')
  187. ->where('member_mobile', '<>', '0000')
  188. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  189. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  190. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  191. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  192. // ->where('member_account.member_account_classify', 'CASH')
  193. ->when($memberIds != '', function ($query) use ($memberIds) {
  194. $query->whereIn('member.member_id', $memberIds);
  195. })->when($name != '', function ($query) use ($name) {
  196. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  197. })->when($mobile != '', function ($query) use ($mobile) {
  198. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  199. })->when($level != '', function ($query) use ($level) {
  200. if ($level == 'other') {
  201. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  202. } else {
  203. $query->where('member_role.member_role_id', $level);
  204. }
  205. })->when($isAuth != '', function ($query) use ($isAuth) {
  206. if ($isAuth == 'Y') {
  207. $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', '<>', '');
  208. } else {
  209. $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', '');
  210. }
  211. })->when(!empty($addtime), function ($query) use ($addtime) {
  212. $addtime[0] = strtotime($addtime[0]);
  213. $addtime[1] = strtotime($addtime[1]);
  214. $query->whereBetween('member.member_addtimes', $addtime);
  215. })->select('member_id', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  216. 'member_info.member_info_nickname', 'member_info.member_info_headimg',
  217. 'member_role.member_role_id', 'member_role.member_role_name',
  218. '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',
  219. // 'member_account.member_account_surplus'
  220. )->orderBy('member_addtimes', 'DESC')
  221. ->get()
  222. ->toArray();
  223. $exportData = [];
  224. foreach ($rows as $row) {
  225. $row['info'] = [
  226. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '用户',
  227. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : ''
  228. ];
  229. $row['cert'] = [
  230. 'member_cert_name' => $row['member_cert_name'],
  231. ];
  232. $account = [];
  233. if (!empty($row['account'])) {
  234. foreach ($row['account'] as $item) {
  235. if ($item['member_account_classify'] == 'POINTS') {
  236. $account['points'] = [
  237. 'member_account_surplus' => $item['member_account_surplus'],
  238. 'member_account_added' => $item['member_account_added'],
  239. ];
  240. } else if ($item['member_account_classify'] == 'CASH') {
  241. $account['cash'] = [
  242. 'member_account_surplus' => $item['member_account_surplus'],
  243. 'member_account_added' => $item['member_account_added'],
  244. ];
  245. } else if ($item['member_account_classify'] == 'CARD') {
  246. $account['card'] = [
  247. 'member_account_surplus' => $item['member_account_surplus'],
  248. 'member_account_added' => $item['member_account_added'],
  249. ];
  250. }
  251. }
  252. }
  253. // $row['account'] = $account;
  254. $row['is_auth'] = "未认证";
  255. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  256. $row['is_auth'] = "已认证";
  257. }
  258. $row['source'] = "APP";
  259. if (!empty($row['member_extend_json'])) {
  260. $extendJson = json_decode($row['member_extend_json'], true);
  261. if (isset($extendJson['come_from'])) {
  262. $row['source'] = $extendJson['come_from'] ?? '';
  263. }
  264. }
  265. $sourceStr = ['APP' => "APP", 'CUSTOMER' => "客户管理", 'SYSTEM' => "后台"];
  266. $exportData[] = [
  267. 'member_id' => $row['member_id'],
  268. 'member_info_nickname' => $row['info']['member_info_nickname'] ?? '',
  269. 'member_mobile' => $row['member_mobile'] ?? '',
  270. 'member_cert_name' => $row['cert']['member_cert_name'] ?? '',
  271. 'source' => in_array($row['source'], $sourceStr) ? $sourceStr[$row['source']] : 'APP',
  272. 'cash' => isset($account['cash']) ? $account['cash']['member_account_surplus'] + $account['cash']['member_account_added'] : 0,
  273. 'points' => isset($account['points']) ? $account['points']['member_account_surplus'] + $account['points']['member_account_added'] : 0,
  274. 'is_auth' => $row['is_auth'],
  275. 'level' => $row['member_role_name'] ?? '普通用户',
  276. 'member_addtimes' => $row['member_addtimes']
  277. ];
  278. }
  279. return json_success('', $exportData);
  280. }
  281. public static function info($memberId)
  282. {
  283. $member = Member::with('cert', 'info')
  284. ->where('member_id', $memberId)
  285. ->first();
  286. if (!$member) {
  287. return json_fail('查询错误');
  288. }
  289. $member = $member->toArray();
  290. $member['info'] = [
  291. 'member_info_nickname' => !empty($member['info']) && !empty($member['info']['member_info_nickname']) ? $member['info']['member_info_nickname'] : substr($member['member_mobile'], -4, 4) . '用户',
  292. 'member_info_headimg' => !empty($member['info']) && !empty($member['info']['member_info_headimg']) ? $member['info']['member_info_headimg'] : ''
  293. ];
  294. $member['cert'] = [
  295. 'member_cert_birth' => $member['cert']['member_cert_birth'] ?? '',
  296. 'member_cert_gender' => $member['cert']['member_cert_gender'] ?? '',
  297. 'member_cert_name' => $member['cert']['member_cert_name'] ?? '',
  298. 'member_cert_nbr' => $member['cert']['member_cert_nbr'] ?? '',
  299. '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']) : '',
  300. 'member_cert_addr' => $member['cert']['member_cert_addr'] ?? '',
  301. 'member_cert_face' => !empty($member['cert']) && !empty($member['cert']['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $member['cert']['member_cert_face'] : '',
  302. 'member_cert_photo' => !empty($member['cert']) && !empty($member['cert']['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $member['cert']['member_cert_photo'] : '',
  303. 'member_cert_nation' => $member['cert']['member_cert_nation'] ?? '',
  304. ];
  305. $member['is_auth'] = "未认证";
  306. if (!empty($member['cert']['member_cert_nbr']) && !empty($member['cert']['member_cert_name'])) {
  307. $member['is_auth'] = "已认证";
  308. }
  309. $member['source'] = "APP";
  310. $member['belong'] = [];
  311. if (!empty($member['member_extend_json'])) {
  312. $extendJson = json_decode($member['member_extend_json'], true);
  313. if (isset($extendJson['come_from'])) {
  314. $member['source'] = $extendJson['come_from'] ?? '';
  315. }
  316. if (isset($extendJson['belong'])) {
  317. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  318. if ($user) {
  319. $member['belong']['user_id'] = $extendJson['belong']['user_id'];
  320. $member['belong']['username'] = $user->user_name;
  321. $member['belong']['user_mobile'] = $user->user_mobile;
  322. }
  323. $member['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  324. }
  325. }
  326. return json_success('', $member);
  327. }
  328. /**
  329. * VIP账户明细
  330. */
  331. public static function vipInfo(Request $request)
  332. {
  333. $memberId = $request->get('member_id', '');
  334. $page = $request->get('page', 1);
  335. $pageSize = $request->get('pageSize', 20);
  336. $account = MemberAccount::where('join_account_member_id', $memberId)
  337. ->where('member_account_classify', 'VIP')
  338. ->first();
  339. $balance = $account->member_account_surplus + $account->member_account_added;
  340. $info = PayDetail::where(function ($query) use ($memberId) {
  341. $query->where('join_pay_member_id', $memberId)
  342. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  343. ->where('pay_category', 'VIP')
  344. ->where('pay_status', 'SUCCESS');
  345. })->orWhere(function ($query) use ($memberId) {
  346. $query->where('join_pay_member_id', $memberId)
  347. ->where('pay_prepayid', 'like', '%VIP%')
  348. ->where('pay_status', 'SUCCESS');
  349. })->orderByDesc('pay_addtimes')
  350. ->get()
  351. ->toArray();
  352. $data = [];
  353. foreach ($info as $key => $item) {
  354. $categoryIds = [];
  355. if (!empty($item['join_pay_order_id'])) {
  356. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  357. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  358. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  359. }
  360. $categoryName = [];
  361. foreach ($categoryIds as $id) {
  362. $category = SysCategory::where('category_id', $id)->first();
  363. if (!empty($category->category_super_path)) {
  364. $path = explode('#', $category->category_super_path);
  365. $ids = array_filter($path);
  366. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  367. } else {
  368. $categoryName[] = $category->category_name;
  369. }
  370. }
  371. $item['goods_category'] = implode(',', array_unique($categoryName));
  372. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  373. $item['goods_category'] = '自定义金额充值';
  374. }
  375. if (in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  376. $item['balance_type'] = 'add';
  377. } else {
  378. $item['balance_type'] = 'cut';
  379. }
  380. if ($key == 0 && in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  381. if (!empty($item['pay_extend_json'])) {
  382. $extendJson = json_decode($item['pay_extend_json'], true);
  383. if (!isset($extendJson['added_amount'])) {
  384. $extendJson['added_amount'] = 0;
  385. }
  386. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  387. $data[] = [
  388. 'join_pay_member_id' => $item['join_pay_member_id'],
  389. 'pay_addtimes' => $item['pay_addtimes'],
  390. 'pay_amount' => $extendJson['added_amount'],
  391. 'pay_category' => 'ADDED',
  392. 'balance' => number_format($balance, 2, '.', ''),
  393. 'balance_type' => 'add',
  394. ];
  395. }
  396. $balance = $balance - $extendJson['added_amount'];
  397. }
  398. $item['balance'] = number_format($balance, 2, '.', '');
  399. $balance = $balance - $item['pay_amount'];
  400. $data[] = $item;
  401. continue;
  402. }
  403. if (in_array($item['pay_category'], ['VIP', 'REFUND'])) {
  404. if (!empty($item['pay_extend_json'])) {
  405. $extendJson = json_decode($item['pay_extend_json'], true);
  406. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  407. $data[] = [
  408. 'join_pay_member_id' => $item['join_pay_member_id'],
  409. 'pay_addtimes' => $item['pay_addtimes'],
  410. 'pay_amount' => $extendJson['added_amount'],
  411. 'pay_category' => 'ADDED',
  412. 'balance' => number_format($balance, 2, '.', ''),
  413. 'balance_type' => 'add',
  414. ];
  415. $balance = $balance - $extendJson['added_amount'];
  416. }
  417. }
  418. $item['balance'] = number_format($balance, 2, '.', '');
  419. $balance = $balance - $item['pay_amount'];
  420. } else {
  421. $item['balance'] = number_format($balance, 2, '.', '');
  422. $balance = $balance + $item['pay_amount'];
  423. }
  424. $data[] = $item;
  425. }
  426. $total = count($data);
  427. $start = ($page - 1) * $pageSize;
  428. $rows = array_slice($data, $start, $pageSize);
  429. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  430. }
  431. public static function welfareInfo(Request $request)
  432. {
  433. $memberId = $request->get('member_id', '');
  434. $page = $request->get('page', 1);
  435. $pageSize = $request->get('pageSize', 20);
  436. $account = MemberAccount::where('join_account_member_id', $memberId)
  437. ->where('member_account_classify', 'WELFARE')
  438. ->first();
  439. $balance = $account->member_account_surplus + $account->member_account_added;
  440. $info = PayDetail::where(function ($query) use ($memberId) {
  441. $query->where('join_pay_member_id', $memberId)
  442. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  443. ->where('pay_category', 'RECHARGE')
  444. ->where('pay_prepayid', 'WELFARE')
  445. ->where('pay_status', 'SUCCESS');
  446. })->orWhere(function ($query) use ($memberId) {
  447. $query->where('join_pay_member_id', $memberId)
  448. ->where('pay_prepayid', 'like', '%WELFARE%')
  449. ->where('pay_status', 'SUCCESS');
  450. })->orderByDesc('pay_addtimes')
  451. ->get()
  452. ->toArray();
  453. $data = [];
  454. foreach ($info as $key => $item) {
  455. $categoryIds = [];
  456. if (!empty($item['join_pay_order_id'])) {
  457. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  458. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  459. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  460. }
  461. $categoryName = [];
  462. foreach ($categoryIds as $id) {
  463. $category = SysCategory::where('category_id', $id)->first();
  464. if (!empty($category->category_super_path)) {
  465. $path = explode('#', $category->category_super_path);
  466. $ids = array_filter($path);
  467. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  468. } else {
  469. $categoryName[] = $category->category_name;
  470. }
  471. }
  472. $item['goods_category'] = implode(',', array_unique($categoryName));
  473. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  474. $item['goods_category'] = '自定义金额充值';
  475. }
  476. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  477. $item['balance_type'] = 'add';
  478. } else {
  479. $item['balance_type'] = 'cut';
  480. }
  481. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  482. if (!empty($item['pay_extend_json'])) {
  483. $extendJson = json_decode($item['pay_extend_json'], true);
  484. if (!isset($extendJson['added_amount'])) {
  485. $extendJson['added_amount'] = 0;
  486. }
  487. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  488. $data[] = [
  489. 'join_pay_member_id' => $item['join_pay_member_id'],
  490. 'pay_addtimes' => $item['pay_addtimes'],
  491. 'pay_amount' => $extendJson['added_amount'],
  492. 'pay_category' => 'ADDED',
  493. 'balance' => number_format($balance, 2, '.', ''),
  494. 'balance_type' => 'add',
  495. ];
  496. }
  497. $balance = $balance - $extendJson['added_amount'];
  498. }
  499. $item['balance'] = number_format($balance, 2, '.', '');
  500. $balance = $balance - $item['pay_amount'];
  501. $data[] = $item;
  502. continue;
  503. }
  504. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  505. if (!empty($item['pay_extend_json'])) {
  506. $extendJson = json_decode($item['pay_extend_json'], true);
  507. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  508. $data[] = [
  509. 'join_pay_member_id' => $item['join_pay_member_id'],
  510. 'pay_addtimes' => $item['pay_addtimes'],
  511. 'pay_amount' => $extendJson['added_amount'],
  512. 'pay_category' => 'ADDED',
  513. 'balance' => number_format($balance, 2, '.', ''),
  514. 'balance_type' => 'add',
  515. ];
  516. $balance = $balance - $extendJson['added_amount'];
  517. }
  518. }
  519. $item['balance'] = number_format($balance, 2, '.', '');
  520. $balance = $balance - $item['pay_amount'];
  521. } else {
  522. $item['balance'] = number_format($balance, 2, '.', '');
  523. $balance = $balance + $item['pay_amount'];
  524. }
  525. $data[] = $item;
  526. }
  527. $total = count($data);
  528. $start = ($page - 1) * $pageSize;
  529. $rows = array_slice($data, $start, $pageSize);
  530. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  531. }
  532. public static function balanceInfo(Request $request)
  533. {
  534. $memberId = $request->get('member_id', '');
  535. $page = $request->get('page', 1);
  536. $pageSize = $request->get('pageSize', 20);
  537. $account = MemberAccount::where('join_account_member_id', $memberId)
  538. ->where('member_account_classify', 'CASH')
  539. ->first();
  540. $balance = $account->member_account_surplus + $account->member_account_added;
  541. $info = PayDetail::where(function ($query) use ($memberId) {
  542. $query->where('join_pay_member_id', $memberId)
  543. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  544. ->where('pay_category', 'RECHARGE')
  545. ->where('pay_prepayid', '<>', $memberId . '-WELFARE')
  546. ->where('pay_status', 'SUCCESS');
  547. })->orWhere(function ($query) use ($memberId) {
  548. $query->where('join_pay_member_id', $memberId)
  549. ->where('pay_prepayid', 'like', '%CASH%')
  550. ->where('pay_status', 'SUCCESS');
  551. })->orderByDesc('pay_addtimes')
  552. ->get()
  553. ->toArray();
  554. $data = [];
  555. foreach ($info as $key => $item) {
  556. $categoryIds = [];
  557. if (!empty($item['join_pay_order_id'])) {
  558. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  559. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  560. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  561. }
  562. $categoryName = [];
  563. foreach ($categoryIds as $id) {
  564. $category = SysCategory::where('category_id', $id)->first();
  565. if (!empty($category->category_super_path)) {
  566. $path = explode('#', $category->category_super_path);
  567. $ids = array_filter($path);
  568. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  569. } else {
  570. $categoryName[] = $category->category_name;
  571. }
  572. }
  573. $item['goods_category'] = implode(',', array_unique($categoryName));
  574. if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
  575. $item['goods_category'] = '自定义金额充值';
  576. }
  577. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  578. $item['balance_type'] = 'add';
  579. } else {
  580. $item['balance_type'] = 'cut';
  581. }
  582. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  583. if (!empty($item['pay_extend_json'])) {
  584. $extendJson = json_decode($item['pay_extend_json'], true);
  585. if (!isset($extendJson['added_amount'])) {
  586. $extendJson['added_amount'] = 0;
  587. }
  588. // 计划任务还没跑
  589. if ($item['pay_category'] == 'RECHARGE' && (!isset($extendJson['notify']) || $extendJson['notify'] != 'success')) {
  590. continue;
  591. }
  592. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  593. $data[] = [
  594. 'join_pay_member_id' => $item['join_pay_member_id'],
  595. 'pay_addtimes' => $item['pay_addtimes'],
  596. 'pay_amount' => $extendJson['added_amount'],
  597. 'pay_category' => 'ADDED',
  598. 'balance' => number_format($balance, 2, '.', ''),
  599. 'balance_type' => 'add',
  600. ];
  601. }
  602. $balance = $balance - $extendJson['added_amount'];
  603. }
  604. $item['balance'] = number_format($balance, 2, '.', '');
  605. $balance = $balance - $item['pay_amount'];
  606. $data[] = $item;
  607. continue;
  608. }
  609. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  610. if (!empty($item['pay_extend_json'])) {
  611. $extendJson = json_decode($item['pay_extend_json'], true);
  612. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  613. $data[] = [
  614. 'join_pay_member_id' => $item['join_pay_member_id'],
  615. 'pay_addtimes' => $item['pay_addtimes'],
  616. 'pay_amount' => $extendJson['added_amount'],
  617. 'pay_category' => 'ADDED',
  618. 'balance' => number_format($balance, 2, '.', ''),
  619. 'balance_type' => 'add',
  620. ];
  621. $balance = $balance - $extendJson['added_amount'];
  622. }
  623. }
  624. $item['balance'] = number_format($balance, 2, '.', '');
  625. $balance = $balance - $item['pay_amount'];
  626. } else {
  627. $item['balance'] = number_format($balance, 2, '.', '');
  628. $balance = $balance + $item['pay_amount'];
  629. }
  630. $data[] = $item;
  631. }
  632. $total = count($data);
  633. $start = ($page - 1) * $pageSize;
  634. $rows = array_slice($data, $start, $pageSize);
  635. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  636. }
  637. public static function pointInfo($memberId)
  638. {
  639. $account = MemberAccount::where('join_account_member_id', $memberId)
  640. ->where('member_account_classify', 'POINTS')
  641. ->first();
  642. $balance = $account->member_account_surplus + $account->member_account_added;
  643. $points = ClientPoints::where('join_client_points_member_id', $memberId)->get()->toArray();
  644. $nowPoint = 0;
  645. foreach ($points as $key => &$point) {
  646. $point['goods_category'] = '其他';
  647. if (!empty($point['join_client_points_goods_id'])) {
  648. $categoryId = Goods::where('goods_id', $point['join_client_points_goods_id'])->pluck('join_goods_category_id');
  649. $category = SysCategory::where('category_id', $categoryId)->first();
  650. if (!empty($category->category_super_path)) {
  651. $path = explode('#', $category->category_super_path);
  652. $ids = array_filter($path);
  653. $point['goods_category'] = SysCategory::where('category_id', current($ids))->value('category_name');
  654. } else {
  655. $point['goods_category'] = $category->category_name;
  656. }
  657. }
  658. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送"])) {
  659. $point['balance_type'] = 'add';
  660. } else {
  661. $point['balance_type'] = 'cut';
  662. }
  663. if ($key == 0) {
  664. $point['balance'] = number_format($balance, 2, '.', '');
  665. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送"])) {
  666. $balance = $balance - $point['client_points_nbr'];
  667. } else {
  668. $balance = $balance + $point['client_points_nbr'];
  669. }
  670. continue;
  671. }
  672. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送"])) {
  673. $point['balance'] = number_format($balance, 2, '.', '');
  674. $balance = $balance - $point['client_points_nbr'];
  675. // $point['balance'] = $balance = $nowPoint = number_format($balance - $point['client_points_nbr'], 2, '.', '');
  676. } else {
  677. $point['balance'] = number_format($balance, 2, '.', '');
  678. $balance = $balance + $point['client_points_nbr'];
  679. // $point['balance'] = $balance = $nowPoint = number_format($balance + $point['client_points_nbr'], 2, '.', '');
  680. }
  681. }
  682. return json_success('', $points);
  683. }
  684. /**
  685. * @Desc 添加会员
  686. * @Author Gorden
  687. * @Date 2024/3/25 13:40
  688. *
  689. * @param $params
  690. * @return \support\Response
  691. */
  692. public static function add($params)
  693. {
  694. Db::beginTransaction();
  695. try {
  696. $memberId = self::generateMemberId();
  697. // 先写主表,要ID
  698. $memberData = [
  699. 'member_id' => $memberId,
  700. 'member_is_owner' => $params['is_owner'] ?? 'N',
  701. 'member_classify' => $params['classify'] ?? '',
  702. 'member_status' => $params['status'] ?? 'PENDING',
  703. 'member_mobile' => $params['mobile'] ?? '',
  704. 'member_category' => $params['member_category'] ?? '后台添加',
  705. // 'member_from' => $params['source'],
  706. // 'member_extend_json' => json_encode(['come_from' => $params['source']]),
  707. 'member_addtimes' => time(),
  708. ];
  709. $extendJson['come_from'] = $params['source'];
  710. if (!empty($params['user_id'])) {
  711. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  712. if (!$deptId) {
  713. throw new BusinessException('请先关联业务员和门店');
  714. }
  715. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  716. }
  717. $memberData['member_extend_json'] = json_encode($extendJson);
  718. Member::insert($memberData);
  719. // 账户表
  720. $accountData = [
  721. 'join_account_member_id' => $memberId,
  722. 'member_account_classify' => 'POINTS',
  723. 'member_account_status' => 'ACTIVED',
  724. 'member_account_category' => 'NORMAL',
  725. 'member_account_nbr' => $memberId . '-POINTS',
  726. 'member_account_name' => '积分账户',
  727. 'member_account_addtimes' => time()
  728. ];
  729. // 积分账户
  730. MemberAccount::insert($accountData);
  731. $accountData['member_account_classify'] = 'CASH';
  732. $accountData['member_account_nbr'] = $memberId . '-CASH';
  733. $accountData['member_account_name'] = '余额账户';
  734. // 现金/余额账户
  735. MemberAccount::insert($accountData);
  736. // 福利账户
  737. // if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY'){
  738. // $welfareData = [
  739. // 'join_account_member_id'=>$memberId,
  740. // 'member_account_classify'=>'WELFARE',
  741. // 'member_account_status'=>'ACTIVED',
  742. // 'member_account_category'=>'NORMAL',
  743. // 'member_account_nbr'=>$memberId.'-WELFARE',
  744. // 'member_account_name'=>'福利账户',
  745. // 'member_account_addtimes'=>time()
  746. // ];
  747. // MemberAccount::insert($welfareData);
  748. // }
  749. // member cert
  750. $certData = [
  751. 'join_cert_member_id' => $memberId,
  752. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  753. 'member_cert_name' => $params['cert_name'] ?? '',
  754. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  755. 'member_cert_birth' => !empty($params['birth']) ? date('Y-m-d', strtotime($params['birth'])) : '',
  756. 'member_cert_gender' => $params['gender'] ?? '',
  757. 'member_cert_province' => !empty($params['province']) ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  758. 'member_cert_city' => $params['city'] ?? '',
  759. 'member_cert_addr' => $params['addr'] ?? '',
  760. // 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  761. // 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  762. ];
  763. MemberCert::insert($certData);
  764. // member info
  765. $infoData = [
  766. 'join_info_member_id' => $memberId,
  767. 'member_info_nickname' => $params['account_name'] ?? '',
  768. 'member_info_headimg' => !empty($params['avatar']) ? getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']) : ''
  769. ];
  770. MemberInfo::insert($infoData);
  771. Db::commit();
  772. } catch (BusinessException $e) {
  773. Db::rollBack();
  774. return json_fail($e->getMessage());
  775. } catch (\Exception $e) {
  776. Db::rollBack();
  777. dump($e->getMessage() . ';line:' . $e->getLine());
  778. return json_fail("数据添加失败");
  779. }
  780. return json_success('数据添加成功', ['member_id' => $memberId]);
  781. }
  782. /**
  783. * @Desc 编辑会员/ 业主
  784. * @Author Gorden
  785. * @Date 2024/4/20 15:18
  786. *
  787. * @param $params
  788. * @return \support\Response
  789. */
  790. public static function update($params)
  791. {
  792. Db::beginTransaction();
  793. try {
  794. $memberId = $params['member_id'];
  795. $member = Member::where('member_id', $params['member_id'])->first();
  796. if (!$member) {
  797. return json_fail("暂无数据");
  798. }
  799. $extendJson = [];
  800. if (!empty($member->member_extend_json)) {
  801. $extendJson = json_decode($member->member_extend_json, true);
  802. }
  803. $extendJson['come_from'] = $params['source'];
  804. // 门店、业务员
  805. if (!empty($params['user_id'])) {
  806. $deptId = SysUser::where('user_id', $params['user_id'])->value('join_user_dept_id');
  807. if (!$deptId) {
  808. throw new BusinessException('请先关联业务员和门店');
  809. }
  810. $extendJson['belong'] = ['user_id' => $params['user_id'], 'premises' => $deptId];
  811. }
  812. // 先写主表,要ID
  813. $memberData = [
  814. 'member_id' => $memberId,
  815. 'member_is_owner' => $params['is_owner'],
  816. 'member_classify' => $params['member_classify'],
  817. 'member_status' => $params['status'],
  818. // 'member_mobile' => $params['mobile'],
  819. // 'member_from' => $params['source'],
  820. 'member_extend_json' => json_encode($extendJson)
  821. ];
  822. Member::where('member_id', $params['member_id'])->update($memberData);
  823. // member cert
  824. $certData = [
  825. 'join_cert_member_id' => $memberId,
  826. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  827. 'member_cert_name' => $params['cert_name'] ?? '',
  828. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  829. 'member_cert_birth' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  830. 'member_cert_gender' => $params['gender'] ?? '',
  831. 'member_cert_province' => $params['province'] ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  832. 'member_cert_city' => $params['city'] ?? '',
  833. 'member_cert_addr' => $params['addr'] ?? '',
  834. 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  835. 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  836. ];
  837. if (!MemberCert::where('join_cert_member_id', $memberId)->exists()) {
  838. MemberCert::insert($certData);
  839. } else {
  840. MemberCert::where('join_cert_member_id', $params['member_id'])->update($certData);
  841. }
  842. // member info
  843. $avatar = '';
  844. if ($params['avatar'] && (strpos($params['avatar'], getenv('APP_IMAGE_DOMAIN')) !== false || strpos($params['avatar'], getenv('SYSTEM_AVATAR_DOMAIN')) !== false)) {
  845. $avatar = $params['avatar'];
  846. } else if ($params['avatar']) {
  847. $avatar = getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']);
  848. }
  849. $infoData = [
  850. 'join_info_member_id' => $memberId,
  851. 'member_info_nickname' => $params['account_name'],
  852. 'member_info_headimg' => $avatar
  853. ];
  854. if (!MemberInfo::where('join_info_member_id', $params['member_id'])->exists()) {
  855. MemberInfo::insert($infoData);
  856. } else {
  857. MemberInfo::where('join_info_member_id', $params['member_id'])->update($infoData);
  858. }
  859. // 福利账户
  860. // if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY' && !MemberAccount::where('member_account_nbr',$memberId.'-WELFARE')->exists()){
  861. // $welfareData = [
  862. // 'join_account_member_id'=>$memberId,
  863. // 'member_account_classify'=>'WELFARE',
  864. // 'member_account_status'=>'ACTIVED',
  865. // 'member_account_category'=>'NORMAL',
  866. // 'member_account_nbr'=>$memberId.'-WELFARE',
  867. // 'member_account_name'=>'福利账户',
  868. // 'member_account_addtimes'=>time()
  869. // ];
  870. // MemberAccount::insert($welfareData);
  871. // }
  872. Db::commit();
  873. } catch (\Exception $e) {
  874. Db::rollBack();
  875. dump($e->getMessage() . ';line:' . $e->getLine());
  876. return json_fail("数据更新失败");
  877. }
  878. return json_success('数据更新成功');
  879. }
  880. /**
  881. * @Desc 会员关联设备列表
  882. * @Author Gorden
  883. * @Date 2024/4/7 9:52
  884. *
  885. * @param $id
  886. * @return \support\Response
  887. */
  888. public static function deviceList($id)
  889. {
  890. $family = Family::where('join_family_creator_member_id', $id)->get()->toArray();
  891. $familyMemberIds = '';
  892. if ($family) {
  893. $familyIds = array_column($family, 'family_id');
  894. $familyMember = FamilyMember::whereIn('join_family_id', $familyIds)->get()->toArray();
  895. $familyMemberIds = array_column($familyMember, 'join_family_member_id');
  896. }
  897. $rows = MemberDevice::leftJoin('device', 'device.device_id', '=', 'member_device.join_member_device_id')
  898. ->leftJoin('member', 'member.member_id', '=', 'member_device.join_device_member_id')
  899. ->leftJoin('family_member', 'family_member.join_family_member_id', '=', 'member_device.join_device_member_id')
  900. ->select('device.device_category', 'device.device_name', 'device.device_type', 'member.member_mobile', 'member_device.*', 'family_member.family_member_name')
  901. ->when(!empty($familyMemberIds), function ($query) use ($familyMemberIds) {
  902. $query->whereIn('member_device.join_device_master_member_id', $familyMemberIds);
  903. })
  904. ->when(empty($familyMemberIds), function ($query) use ($id) {
  905. $query->where('member_device.join_device_master_member_id', $id);
  906. })->orderBy('member_device_addtimes', 'desc')
  907. ->get()
  908. ->toArray();
  909. return json_success('', ['rows' => $rows]);
  910. }
  911. /**
  912. * @Desc 绑定设备
  913. * @Author Gorden
  914. * @Date 2024/4/7 10:27
  915. *
  916. * @param Request $request
  917. * @return \support\Response
  918. */
  919. public static function addDevice(Request $request)
  920. {
  921. $params = $request->post();
  922. Db::beginTransaction();
  923. try {
  924. $data = [
  925. 'join_device_member_id' => $params['join_device_member_id'] ?? '',
  926. 'join_device_master_member_id' => $params['join_device_master_member_id'],
  927. 'join_member_device_id' => $params['join_member_device_id'],
  928. 'member_device_status' => $params['member_device_status'],
  929. 'member_device_key' => $params['member_device_key'] ?? '',
  930. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  931. 'member_device_remark' => $params['member_device_remark'] ?? null,
  932. 'member_device_addtimes' => time(),
  933. ];
  934. MemberDevice::insert($data);
  935. $device = Device::where('device_id', $params['join_member_device_id'])->whereIn('device_status', ['PROCESSING', 'PENDING'])->first();
  936. // 激活设备
  937. if (!$device) {
  938. Db::rollBack();
  939. return json_fail('当前设备不是可激活状态');
  940. }
  941. $device->device_status = 'ACTIVED';
  942. $device->save();
  943. Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => 'ACTIVED']);
  944. _syslog("绑定设备", "用户【" . ($params['join_device_member_id'] ?? $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  945. Db::commit();
  946. } catch (\Exception $e) {
  947. Db::rollBack();
  948. dump($e->getMessage());
  949. return json_fail('绑定失败');
  950. }
  951. return json_success('绑定成功');
  952. }
  953. public static function updateDevice(Request $request)
  954. {
  955. $params = $request->post();
  956. Db::beginTransaction();
  957. try {
  958. $data = [
  959. 'member_device_status' => $params['member_device_status'],
  960. 'member_device_key' => $params['member_device_key'],
  961. 'join_device_member_id' => $params['join_device_member_id'],
  962. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  963. 'member_device_remark' => $params['member_device_remark'] ?? null,
  964. ];
  965. MemberDevice::where('member_device_id', $request->post('member_device_id'))->update($data);
  966. $device = Device::where('device_id', $params['join_member_device_id'])->first();
  967. if ($device->device_status != $params['member_device_status']) {
  968. $device->device_status = $params['member_device_status'];
  969. $device->save();
  970. }
  971. // Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => $params['member_device_status']]);
  972. Db::commit();
  973. _syslog("更新绑定设备", "用户【" . ($params['join_device_member_id'] == "" ? $params['join_device_member_id'] : $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  974. } catch (\Exception $e) {
  975. Db::rollBack();
  976. dump($e->getMessage());
  977. return json_fail('绑定更新失败');
  978. }
  979. return json_success('绑定更新成功');
  980. }
  981. public static function deleteDevice(Request $request)
  982. {
  983. try {
  984. Db::beginTransaction();
  985. $memberDevice = MemberDevice::where('member_device_id', $request->post('member_device_id'))->first();
  986. if (!$memberDevice) {
  987. return json_fail('数据不存在');
  988. }
  989. $device = Device::where('device_id', $memberDevice->join_member_device_id)->first();
  990. $device->device_status = "DISABLED";
  991. $device->save();
  992. // Device::where('device_id', $memberDevice->join_member_device_id)->update(['device_status' => 'DISABLED']);
  993. $memberDevice->delete();
  994. Db::commit();
  995. _syslog("解除绑定设备", "用户【" . ($memberDevice->join_device_member_id == "" ? $memberDevice->join_device_member_id : $memberDevice->join_device_master_member_id) . '】,解除绑定设备【' . $device->device_name . '】');
  996. return json_success('解除绑定成功');
  997. } catch (\Exception $e) {
  998. Db::rollBack();
  999. return json_fail('绑定失败');
  1000. }
  1001. }
  1002. /**
  1003. * 手动创建会员
  1004. */
  1005. public static function createMember($params)
  1006. {
  1007. $memberId = $params['member_id'] ?? '';
  1008. if (!$memberId) {
  1009. throw new BusinessException("参数异常");
  1010. }
  1011. // 主表
  1012. $memberData = [
  1013. 'member_id' => $memberId,
  1014. 'member_is_owner' => 'N',
  1015. 'member_classify' => 'MEMBER',
  1016. 'member_status' => 'ACTIVED',
  1017. 'member_mobile' => $params['mobile'],
  1018. 'member_extend_json' => json_encode(['come_from' => 'SYSTEM']),
  1019. 'member_addtimes' => time()
  1020. ];
  1021. Member::insert($memberData);
  1022. // 账户表
  1023. $accountData = [
  1024. 'join_account_member_id' => $memberId,
  1025. 'member_account_classify' => 'POINTS',
  1026. 'member_account_status' => 'ACTIVED',
  1027. 'member_account_category' => 'NORMAL',
  1028. 'member_account_nbr' => $memberId . '-POINTS',
  1029. 'member_account_name' => '积分账户',
  1030. 'member_account_addtimes' => time()
  1031. ];
  1032. // 积分账户
  1033. MemberAccount::insert($accountData);
  1034. $accountData['member_account_classify'] = 'CASH';
  1035. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1036. $accountData['member_account_name'] = '余额账户';
  1037. // 现金/余额账户
  1038. MemberAccount::insert($accountData);
  1039. // info
  1040. $infoData = [
  1041. 'join_info_member_id' => $memberId,
  1042. 'member_info_nickname' => $params['username']
  1043. ];
  1044. MemberInfo::insert($infoData);
  1045. }
  1046. private static function generateMemberId()
  1047. {
  1048. $id = SysSerial::getSerial();
  1049. return "MR" . str_pad($id, 16, "0", STR_PAD_LEFT) . random_string(6, 'up');
  1050. }
  1051. public static function getMemberName($mobile, $certName, $nickname)
  1052. {
  1053. $name = '';
  1054. if (!empty($certName)){
  1055. $name = $certName;
  1056. }elseif (!empty($nickname)){
  1057. $name = $nickname;
  1058. }
  1059. if (!empty($name) && !empty($mobile)){
  1060. $name .= '-'.$mobile;
  1061. }elseif (!empty($mobile)){
  1062. $name = $mobile;
  1063. }
  1064. return $name;
  1065. }
  1066. }