MemberService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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\SysSerial;
  18. use support\Db;
  19. use support\Request;
  20. class MemberService
  21. {
  22. public static function list(Request $request)
  23. {
  24. $page = $request->get('page', 1);
  25. $pageSize = $request->get('pageSize', 10);
  26. $name = $request->get('name', '');
  27. $mobile = $request->get('mobile', '');
  28. $level = $request->get('level','');
  29. $isAuth = $request->get('is_auth','');
  30. $addtime = $request->get('member_addtimes',[]);
  31. $rows = Member::with('account')
  32. ->where('member_is_owner', 'N')
  33. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  34. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  35. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  36. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  37. // ->where('member_account.member_account_classify', 'CASH')
  38. ->when($name != '', function ($query) use ($name) {
  39. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  40. })->when($mobile != '', function ($query) use ($mobile) {
  41. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  42. })->when($level != '', function ($query) use ($level) {
  43. if($level == 'other'){
  44. $query->where('member.join_member_role_id',NULL)->orWhere('member.join_member_role_id','');
  45. }else{
  46. $query->where('member_role.member_role_id', $level);
  47. }
  48. })->when($isAuth != '', function ($query) use ($isAuth) {
  49. if($isAuth == 'Y'){
  50. $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','<>','');
  51. }else{
  52. $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','');
  53. }
  54. })->when(!empty($addtime), function ($query) use ($addtime) {
  55. $addtime[0] = strtotime($addtime[0]);
  56. $addtime[1] = strtotime($addtime[1]);
  57. $query->whereBetween('member.member_addtimes', $addtime);
  58. })
  59. ->select('member_id', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  60. 'member_info.member_info_nickname', 'member_info.member_info_headimg',
  61. 'member_role.member_role_id', 'member_role.member_role_name',
  62. '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',
  63. // 'member_account.member_account_surplus'
  64. );
  65. // 总条数
  66. $total = $rows->count('member_id');
  67. $rows = $rows->orderBy('member_addtimes', 'DESC')
  68. ->forPage($page, $pageSize)
  69. ->get()
  70. ->toArray();
  71. foreach ($rows as &$row) {
  72. $row['info'] = [
  73. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'],-4,4).'用户',
  74. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : ''
  75. ];
  76. $row['cert'] = [
  77. 'member_cert_birth' => $row['member_cert_birth'],
  78. 'member_cert_gender' => $row['member_cert_gender'],
  79. 'member_cert_name' => $row['member_cert_name'],
  80. 'member_cert_nbr' => $row['member_cert_nbr'],
  81. 'member_cert_province' => !empty($row['member_cert_province']) && is_json($row['member_cert_province']) ? json_decode($row['member_cert_province']) : '',
  82. 'member_cert_addr' => $row['member_cert_addr'],
  83. 'member_cert_face' => !empty($row['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_face'] : '',
  84. 'member_cert_photo' => !empty($row['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_photo'] : '',
  85. 'member_cert_nation' => $row['member_cert_nation'],
  86. ];
  87. $account = [];
  88. if (!empty($row['account'])) {
  89. foreach ($row['account'] as $item) {
  90. if ($item['member_account_classify'] == 'POINTS') {
  91. $account['points'] = [
  92. 'member_account_name' => $item['member_account_name'],
  93. 'member_account_income' => $item['member_account_income'],
  94. 'member_account_expend' => $item['member_account_expend'],
  95. 'member_account_surplus' => $item['member_account_surplus'],
  96. 'member_account_added' => $item['member_account_added'],
  97. ];
  98. } else if ($item['member_account_classify'] == 'CASH') {
  99. $account['cash'] = [
  100. 'member_account_name' => $item['member_account_name'],
  101. 'member_account_income' => $item['member_account_income'],
  102. 'member_account_expend' => $item['member_account_expend'],
  103. 'member_account_surplus' => $item['member_account_surplus'],
  104. 'member_account_added' => $item['member_account_added'],
  105. ];
  106. } else if ($item['member_account_classify'] == 'CARD') {
  107. $account['card'] = [
  108. 'member_account_name' => $item['member_account_name'],
  109. 'member_account_income' => $item['member_account_income'],
  110. 'member_account_expend' => $item['member_account_expend'],
  111. 'member_account_surplus' => $item['member_account_surplus'],
  112. 'member_account_added' => $item['member_account_added'],
  113. ];
  114. }
  115. }
  116. }
  117. $row['account'] = $account;
  118. $row['is_auth'] = "未认证";
  119. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  120. $row['is_auth'] = "已认证";
  121. }
  122. $row['source'] = "APP";
  123. if (!empty($row['member_extend_json'])) {
  124. $extendJson = json_decode($row['member_extend_json'], true);
  125. if (isset($extendJson['come_from'])) {
  126. $row['source'] = $extendJson['come_from'] ?? '';
  127. }
  128. }
  129. }
  130. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  131. }
  132. public static function exportMember(Request $request)
  133. {
  134. $memberIds = $request->get('member_id','');
  135. $name = $request->get('name', '');
  136. $mobile = $request->get('mobile', '');
  137. $level = $request->get('level','');
  138. $isAuth = $request->get('is_auth','');
  139. $addtime = $request->get('member_addtimes',[]);
  140. $rows = Member::with('account')
  141. ->where('member_is_owner', 'N')
  142. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  143. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  144. // ->leftjoin('member_account', 'member.member_id', '=', 'member_account.join_account_member_id')
  145. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  146. // ->where('member_account.member_account_classify', 'CASH')
  147. ->when($memberIds != '', function ($query) use ($memberIds) {
  148. $query->whereIn('member.member_id', $memberIds);
  149. })->when($name != '', function ($query) use ($name) {
  150. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  151. })->when($mobile != '', function ($query) use ($mobile) {
  152. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  153. })->when($level != '', function ($query) use ($level) {
  154. if($level == 'other'){
  155. $query->where('member.join_member_role_id',NULL)->orWhere('member.join_member_role_id','');
  156. }else{
  157. $query->where('member_role.member_role_id', $level);
  158. }
  159. })->when($isAuth != '', function ($query) use ($isAuth) {
  160. if($isAuth == 'Y'){
  161. $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','<>','');
  162. }else{
  163. $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','');
  164. }
  165. })->when(!empty($addtime), function ($query) use ($addtime) {
  166. $addtime[0] = strtotime($addtime[0]);
  167. $addtime[1] = strtotime($addtime[1]);
  168. $query->whereBetween('member.member_addtimes', $addtime);
  169. })->select('member_id', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  170. 'member_info.member_info_nickname', 'member_info.member_info_headimg',
  171. 'member_role.member_role_id', 'member_role.member_role_name',
  172. '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',
  173. // 'member_account.member_account_surplus'
  174. )->orderBy('member_addtimes', 'DESC')
  175. ->get()
  176. ->toArray();
  177. $exportData = [];
  178. foreach ($rows as $row) {
  179. $row['info'] = [
  180. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'],-4,4).'用户',
  181. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : ''
  182. ];
  183. $row['cert'] = [
  184. 'member_cert_name' => $row['member_cert_name'],
  185. ];
  186. $account = [];
  187. if (!empty($row['account'])) {
  188. foreach ($row['account'] as $item) {
  189. if ($item['member_account_classify'] == 'POINTS') {
  190. $account['points'] = [
  191. 'member_account_surplus' => $item['member_account_surplus'],
  192. 'member_account_added' => $item['member_account_added'],
  193. ];
  194. } else if ($item['member_account_classify'] == 'CASH') {
  195. $account['cash'] = [
  196. 'member_account_surplus' => $item['member_account_surplus'],
  197. 'member_account_added' => $item['member_account_added'],
  198. ];
  199. } else if ($item['member_account_classify'] == 'CARD') {
  200. $account['card'] = [
  201. 'member_account_surplus' => $item['member_account_surplus'],
  202. 'member_account_added' => $item['member_account_added'],
  203. ];
  204. }
  205. }
  206. }
  207. // $row['account'] = $account;
  208. $row['is_auth'] = "未认证";
  209. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  210. $row['is_auth'] = "已认证";
  211. }
  212. $row['source'] = "APP";
  213. if (!empty($row['member_extend_json'])) {
  214. $extendJson = json_decode($row['member_extend_json'], true);
  215. if (isset($extendJson['come_from'])) {
  216. $row['source'] = $extendJson['come_from'] ?? '';
  217. }
  218. }
  219. $sourceStr = ['APP'=>"APP",'CUSTOMER'=>"客户管理",'SYSTEM'=>"后台"];
  220. $exportData[] = [
  221. 'member_id'=>$row['member_id'],
  222. 'member_info_nickname'=>$row['info']['member_info_nickname'] ?? '',
  223. 'member_mobile'=>$row['member_mobile'] ?? '',
  224. 'member_cert_name'=>$row['cert']['member_cert_name'] ?? '',
  225. 'source'=> in_array($row['source'],$sourceStr) ? $sourceStr[$row['source']] : 'APP',
  226. 'cash'=>isset($account['cash']) ? $account['cash']['member_account_surplus'] + $account['cash']['member_account_added'] : 0,
  227. 'points'=>isset($account['points']) ? $account['points']['member_account_surplus'] + $account['points']['member_account_added'] : 0,
  228. 'is_auth' => $row['is_auth'],
  229. 'level' => $row['member_role_name'] ?? '普通用户',
  230. 'member_addtimes'=>$row['member_addtimes']
  231. ];
  232. }
  233. return json_success('',$exportData);
  234. }
  235. public static function info($memberId)
  236. {
  237. $member = Member::with('cert')
  238. ->where('member_id', $memberId)
  239. ->first();
  240. return json_success('', $member);
  241. }
  242. public static function balanceInfo(Request $request)
  243. {
  244. $memberId = $request->get('member_id', '');
  245. $page = $request->get('page', 1);
  246. $pageSize = $request->get('pageSize', 20);
  247. $account = MemberAccount::where('join_account_member_id', $memberId)
  248. ->where('member_account_classify', 'CASH')
  249. ->first();
  250. $balance = $account->member_account_surplus + $account->member_account_added;
  251. $info = PayDetail::where(function ($query) use ($memberId) {
  252. $query->where('join_pay_member_id', $memberId)
  253. // ->whereIn('pay_prepayid', ['W06', 'A01'])
  254. ->where('pay_category', 'RECHARGE')
  255. ->where('pay_status', 'SUCCESS');
  256. })->orWhere(function ($query) use ($memberId) {
  257. $query->where('join_pay_member_id', $memberId)
  258. ->whereNotIn('pay_prepayid', ['W06', 'A01'])
  259. ->where('pay_category', '<>', 'RECHARGE')
  260. ->where('pay_status', 'SUCCESS');
  261. })->orderByDesc('pay_addtimes')
  262. ->get()
  263. ->toArray();
  264. $data = [];
  265. foreach ($info as $key => $item) {
  266. $categoryIds = [];
  267. if(!empty($item['join_pay_order_id'])){
  268. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id');
  269. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id');
  270. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id');
  271. }
  272. $categoryName = [];
  273. foreach ($categoryIds as $id) {
  274. $category = SysCategory::where('category_id', $id)->first();
  275. if (!empty($category->category_super_path)) {
  276. $path = explode('#', $category->category_super_path);
  277. $ids = array_filter($path);
  278. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  279. } else {
  280. $categoryName[] = $category->category_name;
  281. }
  282. }
  283. $item['goods_category'] = implode(',', $categoryName);
  284. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  285. $item['balance_type'] = 'add';
  286. } else {
  287. $item['balance_type'] = 'cut';
  288. }
  289. if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  290. if (!empty($item['pay_extend_json'])) {
  291. $extendJson = json_decode($item['pay_extend_json'], true);
  292. if (!isset($extendJson['added_amount'])) {
  293. $extendJson['added_amount'] = 0;
  294. }
  295. // 计划任务还没跑
  296. if ($item['pay_category'] == 'RECHARGE' && (!isset($extendJson['notify']) || $extendJson['notify'] != 'success')) {
  297. continue;
  298. }
  299. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  300. $data[] = [
  301. 'join_pay_member_id' => $item['join_pay_member_id'],
  302. 'pay_addtimes' => $item['pay_addtimes'],
  303. 'pay_amount' => $extendJson['added_amount'],
  304. 'pay_category' => 'ADDED',
  305. 'balance' => number_format($balance, 2, '.', ''),
  306. 'balance_type' => 'add',
  307. ];
  308. }
  309. $balance = $balance - $extendJson['added_amount'];
  310. }
  311. $item['balance'] = number_format($balance, 2, '.', '');
  312. $balance = $balance - $item['pay_amount'];
  313. $data[] = $item;
  314. continue;
  315. }
  316. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  317. if (!empty($item['pay_extend_json'])) {
  318. $extendJson = json_decode($item['pay_extend_json'], true);
  319. if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
  320. $data[] = [
  321. 'join_pay_member_id' => $item['join_pay_member_id'],
  322. 'pay_addtimes' => $item['pay_addtimes'],
  323. 'pay_amount' => $extendJson['added_amount'],
  324. 'pay_category' => 'ADDED',
  325. 'balance' => number_format($balance, 2, '.', ''),
  326. 'balance_type' => 'add',
  327. ];
  328. $balance = $balance - $extendJson['added_amount'];
  329. }
  330. }
  331. $item['balance'] = number_format($balance, 2, '.', '');
  332. $balance = $balance - $item['pay_amount'];
  333. } else {
  334. $item['balance'] = number_format($balance, 2, '.', '');
  335. $balance = $balance + $item['pay_amount'];
  336. }
  337. $data[] = $item;
  338. }
  339. $total = count($data);
  340. $start = ($page-1)*$pageSize;
  341. $rows = array_slice($data,$start,$pageSize);
  342. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  343. }
  344. public static function pointInfo($memberId)
  345. {
  346. $account = MemberAccount::where('join_account_member_id', $memberId)
  347. ->where('member_account_classify', 'POINTS')
  348. ->first();
  349. $balance = $account->member_account_surplus + $account->member_account_added;
  350. $points = ClientPoints::where('join_client_points_member_id', $memberId)->get()->toArray();
  351. $nowPoint = 0;
  352. foreach ($points as $key => &$point) {
  353. $point['goods_category'] = '其他';
  354. if (!empty($point['join_client_points_goods_id'])) {
  355. $categoryId = Goods::where('goods_id', $point['join_client_points_goods_id'])->pluck('join_goods_category_id');
  356. $category = SysCategory::where('category_id', $categoryId)->first();
  357. if (!empty($category->category_super_path)) {
  358. $path = explode('#', $category->category_super_path);
  359. $ids = array_filter($path);
  360. $point['goods_category'] = SysCategory::where('category_id', current($ids))->value('category_name');
  361. } else {
  362. $point['goods_category'] = $category->category_name;
  363. }
  364. }
  365. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送"])) {
  366. $point['balance_type'] = 'add';
  367. } else {
  368. $point['balance_type'] = 'cut';
  369. }
  370. if ($key == 0) {
  371. $point['balance'] = number_format($balance, 2, '.', '');
  372. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送"])) {
  373. $balance = $balance - $point['client_points_nbr'];
  374. } else {
  375. $balance = $balance + $point['client_points_nbr'];
  376. }
  377. continue;
  378. }
  379. if (in_array($point['client_points_category'], ["评价", "消费", "活动", "赠送"])) {
  380. $point['balance'] = number_format($balance, 2, '.', '');
  381. $balance = $balance - $point['client_points_nbr'];
  382. // $point['balance'] = $balance = $nowPoint = number_format($balance - $point['client_points_nbr'], 2, '.', '');
  383. } else {
  384. $point['balance'] = number_format($balance, 2, '.', '');
  385. $balance = $balance + $point['client_points_nbr'];
  386. // $point['balance'] = $balance = $nowPoint = number_format($balance + $point['client_points_nbr'], 2, '.', '');
  387. }
  388. }
  389. return json_success('', $points);
  390. }
  391. /**
  392. * @Desc 添加会员
  393. * @Author Gorden
  394. * @Date 2024/3/25 13:40
  395. *
  396. * @param $params
  397. * @return \support\Response
  398. */
  399. public static function add($params)
  400. {
  401. Db::beginTransaction();
  402. try {
  403. $memberId = self::generateMemberId();
  404. // 先写主表,要ID
  405. $memberData = [
  406. 'member_id' => $memberId,
  407. 'member_is_owner' => $params['is_owner'],
  408. 'member_classify' => $params['classify'],
  409. 'member_status' => $params['status'],
  410. 'member_mobile' => $params['mobile'],
  411. // 'member_from' => $params['source'],
  412. 'member_extend_json' => json_encode(['come_from' => $params['source']]),
  413. 'member_addtimes' => time(),
  414. ];
  415. Member::insert($memberData);
  416. // 账户表
  417. $accountData = [
  418. 'join_account_member_id' => $memberId,
  419. 'member_account_classify' => 'POINTS',
  420. 'member_account_status' => 'ACTIVED',
  421. 'member_account_category' => 'NORMAL',
  422. 'member_account_nbr' => $memberId . '-POINTS',
  423. 'member_account_name' => '积分账户',
  424. 'member_account_addtimes' => time()
  425. ];
  426. // 积分账户
  427. MemberAccount::insert($accountData);
  428. $accountData['member_account_classify'] = 'CASH';
  429. $accountData['member_account_nbr'] = $memberId . '-CASH';
  430. $accountData['member_account_name'] = '余额账户';
  431. // 现金/余额账户
  432. MemberAccount::insert($accountData);
  433. // member cert
  434. $certData = [
  435. 'join_cert_member_id' => $memberId,
  436. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  437. 'member_cert_name' => $params['cert_name'] ?? '',
  438. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  439. 'member_cert_birth' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  440. 'member_cert_gender' => $params['gender'] ?? '',
  441. 'member_cert_province' => $params['province'] ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  442. 'member_cert_city' => $params['city'] ?? '',
  443. 'member_cert_addr' => $params['addr'] ?? '',
  444. // 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  445. // 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  446. ];
  447. MemberCert::insert($certData);
  448. // member info
  449. $infoData = [
  450. 'join_info_member_id' => $memberId,
  451. 'member_info_nickname' => $params['account_name'],
  452. 'member_info_headimg' => getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar'])
  453. ];
  454. MemberInfo::insert($infoData);
  455. Db::commit();
  456. } catch (\Exception $e) {
  457. Db::rollBack();
  458. dump($e->getMessage() . ';line:' . $e->getLine());
  459. return json_fail("数据添加失败");
  460. }
  461. return json_success('数据添加成功');
  462. }
  463. /**
  464. * @Desc 编辑会员/ 业主
  465. * @Author Gorden
  466. * @Date 2024/4/20 15:18
  467. *
  468. * @param $params
  469. * @return \support\Response
  470. */
  471. public static function update($params)
  472. {
  473. Db::beginTransaction();
  474. try {
  475. $memberId = $params['member_id'];
  476. $member = Member::where('member_id', $params['member_id'])->first();
  477. if (!$member) {
  478. return json_fail("暂无数据");
  479. }
  480. $extendJson = [];
  481. if (!empty($member->member_extend_json)) {
  482. $extendJson = json_decode($member->member_extend_json, true);
  483. }
  484. $extendJson['come_from'] = $params['source'];
  485. // 先写主表,要ID
  486. $memberData = [
  487. 'member_id' => $memberId,
  488. 'member_is_owner' => $params['is_owner'],
  489. 'member_classify' => $params['classify'],
  490. 'member_status' => $params['status'],
  491. 'member_mobile' => $params['mobile'],
  492. // 'member_from' => $params['source'],
  493. 'member_extend_json' => json_encode($extendJson)
  494. ];
  495. Member::where('member_id', $params['member_id'])->update($memberData);
  496. // member cert
  497. $certData = [
  498. 'join_cert_member_id' => $memberId,
  499. 'member_cert_nation' => $params['member_cert_nation'] ?? '',
  500. 'member_cert_name' => $params['cert_name'] ?? '',
  501. 'member_cert_nbr' => $params['cert_nbr'] ?? '',
  502. 'member_cert_birth' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
  503. 'member_cert_gender' => $params['gender'] ?? '',
  504. 'member_cert_province' => $params['province'] ? json_encode($params['province'], JSON_UNESCAPED_UNICODE) : '[]',
  505. 'member_cert_city' => $params['city'] ?? '',
  506. 'member_cert_addr' => $params['addr'] ?? '',
  507. 'member_cert_face' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_face']),
  508. 'member_cert_photo' => str_replace(getenv('STORAGE_DOMAIN'), '', $params['member_cert_photo']),
  509. ];
  510. if (!MemberCert::where('join_cert_member_id', $memberId)->exists()) {
  511. MemberCert::insert($certData);
  512. } else {
  513. MemberCert::where('join_cert_member_id', $params['member_id'])->update($certData);
  514. }
  515. // member info
  516. if (strpos($params['avatar'], getenv('APP_IMAGE_DOMAIN')) !== false || strpos($params['avatar'], getenv('SYSTEM_AVATAR_DOMAIN')) !== false) {
  517. $avatar = $params['avatar'];
  518. } else {
  519. $avatar = getenv('SYSTEM_AVATAR_DOMAIN') . str_replace(getenv('STORAGE_DOMAIN'), '', $params['avatar']);
  520. }
  521. $infoData = [
  522. 'join_info_member_id' => $memberId,
  523. 'member_info_nickname' => $params['account_name'],
  524. 'member_info_headimg' => $avatar
  525. ];
  526. if (!MemberInfo::where('join_info_member_id', $params['member_id'])->exists()) {
  527. MemberInfo::insert($infoData);
  528. } else {
  529. MemberInfo::where('join_info_member_id', $params['member_id'])->update($infoData);
  530. }
  531. Db::commit();
  532. } catch (\Exception $e) {
  533. Db::rollBack();
  534. dump($e->getMessage() . ';line:' . $e->getLine());
  535. return json_fail("数据更新失败");
  536. }
  537. return json_success('数据更新成功');
  538. }
  539. /**
  540. * @Desc 会员关联设备列表
  541. * @Author Gorden
  542. * @Date 2024/4/7 9:52
  543. *
  544. * @param $id
  545. * @return \support\Response
  546. */
  547. public static function deviceList($id)
  548. {
  549. $family = Family::where('join_family_creator_member_id', $id)->get()->toArray();
  550. $familyMemberIds = '';
  551. if ($family) {
  552. $familyIds = array_column($family, 'family_id');
  553. $familyMember = FamilyMember::whereIn('join_family_id', $familyIds)->get()->toArray();
  554. $familyMemberIds = array_column($familyMember, 'join_family_member_id');
  555. }
  556. $rows = MemberDevice::leftJoin('device', 'device.device_id', '=', 'member_device.join_member_device_id')
  557. ->leftJoin('member', 'member.member_id', '=', 'member_device.join_device_member_id')
  558. ->leftJoin('family_member', 'family_member.join_family_member_id', '=', 'member_device.join_device_member_id')
  559. ->select('device.device_category', 'device.device_name', 'device.device_type', 'member.member_mobile', 'member_device.*', 'family_member.family_member_name')
  560. ->when(!empty($familyMemberIds), function ($query) use ($familyMemberIds) {
  561. $query->whereIn('member_device.join_device_master_member_id', $familyMemberIds);
  562. })
  563. ->when(empty($familyMemberIds), function ($query) use ($id) {
  564. $query->where('member_device.join_device_master_member_id', $id);
  565. })->orderBy('member_device_addtimes', 'desc')
  566. ->get()
  567. ->toArray();
  568. return json_success('', ['rows' => $rows]);
  569. }
  570. /**
  571. * @Desc 绑定设备
  572. * @Author Gorden
  573. * @Date 2024/4/7 10:27
  574. *
  575. * @param Request $request
  576. * @return \support\Response
  577. */
  578. public static function addDevice(Request $request)
  579. {
  580. $params = $request->post();
  581. Db::beginTransaction();
  582. try {
  583. $data = [
  584. 'join_device_member_id' => $params['join_device_member_id'] ?? '',
  585. 'join_device_master_member_id' => $params['join_device_master_member_id'],
  586. 'join_member_device_id' => $params['join_member_device_id'],
  587. 'member_device_status' => $params['member_device_status'],
  588. 'member_device_key' => $params['member_device_key'] ?? '',
  589. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  590. 'member_device_remark' => $params['member_device_remark'] ?? null,
  591. 'member_device_addtimes' => time(),
  592. ];
  593. MemberDevice::insert($data);
  594. $device = Device::where('device_id', $params['join_member_device_id'])->whereIn('device_status', ['PROCESSING', 'PENDING'])->first();
  595. // 激活设备
  596. if (!$device) {
  597. Db::rollBack();
  598. return json_fail('当前设备不是可激活状态');
  599. }
  600. $device->device_status = 'ACTIVED';
  601. $device->save();
  602. Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => 'ACTIVED']);
  603. _syslog("绑定设备", "用户【" . ($params['join_device_member_id'] ?? $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  604. Db::commit();
  605. } catch (\Exception $e) {
  606. Db::rollBack();
  607. dump($e->getMessage());
  608. return json_fail('绑定失败');
  609. }
  610. return json_success('绑定成功');
  611. }
  612. public static function updateDevice(Request $request)
  613. {
  614. $params = $request->post();
  615. Db::beginTransaction();
  616. try {
  617. $data = [
  618. 'member_device_status' => $params['member_device_status'],
  619. 'member_device_key' => $params['member_device_key'],
  620. 'join_device_member_id' => $params['join_device_member_id'],
  621. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  622. 'member_device_remark' => $params['member_device_remark'] ?? null,
  623. ];
  624. MemberDevice::where('member_device_id', $request->post('member_device_id'))->update($data);
  625. $device = Device::where('device_id', $params['join_member_device_id'])->first();
  626. if ($device->device_status != $params['member_device_status']) {
  627. $device->device_status = $params['member_device_status'];
  628. $device->save();
  629. }
  630. // Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => $params['member_device_status']]);
  631. Db::commit();
  632. _syslog("更新绑定设备", "用户【" . ($params['join_device_member_id'] == "" ? $params['join_device_member_id'] : $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  633. } catch (\Exception $e) {
  634. Db::rollBack();
  635. dump($e->getMessage());
  636. return json_fail('绑定更新失败');
  637. }
  638. return json_success('绑定更新成功');
  639. }
  640. public static function deleteDevice(Request $request)
  641. {
  642. try {
  643. Db::beginTransaction();
  644. $memberDevice = MemberDevice::where('member_device_id', $request->post('member_device_id'))->first();
  645. if (!$memberDevice) {
  646. return json_fail('数据不存在');
  647. }
  648. $device = Device::where('device_id', $memberDevice->join_member_device_id)->first();
  649. $device->device_status = "DISABLED";
  650. $device->save();
  651. // Device::where('device_id', $memberDevice->join_member_device_id)->update(['device_status' => 'DISABLED']);
  652. $memberDevice->delete();
  653. Db::commit();
  654. _syslog("解除绑定设备", "用户【" . ($memberDevice->join_device_member_id == "" ? $memberDevice->join_device_member_id : $memberDevice->join_device_master_member_id) . '】,解除绑定设备【' . $device->device_name . '】');
  655. return json_success('解除绑定成功');
  656. } catch (\Exception $e) {
  657. Db::rollBack();
  658. return json_fail('绑定失败');
  659. }
  660. }
  661. private static function generateMemberId()
  662. {
  663. $id = SysSerial::getSerial();
  664. return "ME" . str_pad($id, 16, "0", STR_PAD_LEFT) . random_string(6, 'up');
  665. }
  666. }