MemberService.php 66 KB

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