MemberService.php 65 KB

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