MemberService.php 37 KB

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