MemberService.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. return json_fail("暂无数据");
  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 (\Exception $e) {
  964. Db::rollBack();
  965. dump($e->getMessage() . ';line:' . $e->getLine());
  966. return json_fail("数据更新失败");
  967. }
  968. return json_success('数据更新成功');
  969. }
  970. /**
  971. * @Desc 会员关联设备列表
  972. * @Author Gorden
  973. * @Date 2024/4/7 9:52
  974. *
  975. * @param $id
  976. * @return \support\Response
  977. */
  978. public static function deviceList($id)
  979. {
  980. $family = Family::where('join_family_creator_member_id', $id)->get()->toArray();
  981. $familyMemberIds = '';
  982. if ($family) {
  983. $familyIds = array_column($family, 'family_id');
  984. $familyMember = FamilyMember::whereIn('join_family_id', $familyIds)->get()->toArray();
  985. $familyMemberIds = array_column($familyMember, 'join_family_member_id');
  986. }
  987. $rows = MemberDevice::leftJoin('device', 'device.device_id', '=', 'member_device.join_member_device_id')
  988. ->leftJoin('member', 'member.member_id', '=', 'member_device.join_device_member_id')
  989. ->leftJoin('family_member', 'family_member.join_family_member_id', '=', 'member_device.join_device_member_id')
  990. ->select('device.device_category', 'device.device_name', 'device.device_type', 'member.member_mobile', 'member_device.*', 'family_member.family_member_name')
  991. ->when(!empty($familyMemberIds), function ($query) use ($familyMemberIds) {
  992. $query->whereIn('member_device.join_device_master_member_id', $familyMemberIds);
  993. })
  994. ->when(empty($familyMemberIds), function ($query) use ($id) {
  995. $query->where('member_device.join_device_master_member_id', $id);
  996. })->orderBy('member_device_addtimes', 'desc')
  997. ->get()
  998. ->toArray();
  999. return json_success('', ['rows' => $rows]);
  1000. }
  1001. /**
  1002. * @Desc 绑定设备
  1003. * @Author Gorden
  1004. * @Date 2024/4/7 10:27
  1005. *
  1006. * @param Request $request
  1007. * @return \support\Response
  1008. */
  1009. public static function addDevice(Request $request)
  1010. {
  1011. $params = $request->post();
  1012. Db::beginTransaction();
  1013. try {
  1014. $data = [
  1015. 'join_device_member_id' => $params['join_device_member_id'] ?? '',
  1016. 'join_device_master_member_id' => $params['join_device_master_member_id'],
  1017. 'join_member_device_id' => $params['join_member_device_id'],
  1018. 'member_device_status' => $params['member_device_status'],
  1019. 'member_device_key' => $params['member_device_key'] ?? '',
  1020. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1021. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1022. 'member_device_addtimes' => time(),
  1023. ];
  1024. MemberDevice::insert($data);
  1025. $device = Device::where('device_id', $params['join_member_device_id'])->whereIn('device_status', ['PROCESSING', 'PENDING'])->first();
  1026. // 激活设备
  1027. if (!$device) {
  1028. Db::rollBack();
  1029. return json_fail('当前设备不是可激活状态');
  1030. }
  1031. $device->device_status = 'ACTIVED';
  1032. $device->save();
  1033. Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => 'ACTIVED']);
  1034. _syslog("绑定设备", "用户【" . ($params['join_device_member_id'] ?? $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1035. Db::commit();
  1036. } catch (\Exception $e) {
  1037. Db::rollBack();
  1038. dump($e->getMessage());
  1039. return json_fail('绑定失败');
  1040. }
  1041. return json_success('绑定成功');
  1042. }
  1043. public static function updateDevice(Request $request)
  1044. {
  1045. $params = $request->post();
  1046. Db::beginTransaction();
  1047. try {
  1048. $data = [
  1049. 'member_device_status' => $params['member_device_status'],
  1050. 'member_device_key' => $params['member_device_key'],
  1051. 'join_device_member_id' => $params['join_device_member_id'],
  1052. 'member_device_config_json' => !empty($params['member_device_config_json']) ? $params['member_device_config_json'] : null,
  1053. 'member_device_remark' => $params['member_device_remark'] ?? null,
  1054. ];
  1055. MemberDevice::where('member_device_id', $request->post('member_device_id'))->update($data);
  1056. $device = Device::where('device_id', $params['join_member_device_id'])->first();
  1057. if ($device->device_status != $params['member_device_status']) {
  1058. $device->device_status = $params['member_device_status'];
  1059. $device->save();
  1060. }
  1061. // Device::where('device_id', $params['join_member_device_id'])->update(['device_status' => $params['member_device_status']]);
  1062. Db::commit();
  1063. _syslog("更新绑定设备", "用户【" . ($params['join_device_member_id'] == "" ? $params['join_device_member_id'] : $params['join_device_master_member_id']) . '】,绑定设备【' . $device->device_name . '】');
  1064. } catch (\Exception $e) {
  1065. Db::rollBack();
  1066. dump($e->getMessage());
  1067. return json_fail('绑定更新失败');
  1068. }
  1069. return json_success('绑定更新成功');
  1070. }
  1071. public static function deleteDevice(Request $request)
  1072. {
  1073. try {
  1074. Db::beginTransaction();
  1075. $memberDevice = MemberDevice::where('member_device_id', $request->post('member_device_id'))->first();
  1076. if (!$memberDevice) {
  1077. return json_fail('数据不存在');
  1078. }
  1079. $device = Device::where('device_id', $memberDevice->join_member_device_id)->first();
  1080. $device->device_status = "DISABLED";
  1081. $device->save();
  1082. // Device::where('device_id', $memberDevice->join_member_device_id)->update(['device_status' => 'DISABLED']);
  1083. $memberDevice->delete();
  1084. Db::commit();
  1085. _syslog("解除绑定设备", "用户【" . ($memberDevice->join_device_member_id == "" ? $memberDevice->join_device_member_id : $memberDevice->join_device_master_member_id) . '】,解除绑定设备【' . $device->device_name . '】');
  1086. return json_success('解除绑定成功');
  1087. } catch (\Exception $e) {
  1088. Db::rollBack();
  1089. return json_fail('绑定失败');
  1090. }
  1091. }
  1092. /**
  1093. * 手动创建会员
  1094. */
  1095. public static function createMember($params)
  1096. {
  1097. $memberId = $params['member_id'] ?? '';
  1098. if (!$memberId) {
  1099. throw new BusinessException("参数异常");
  1100. }
  1101. // 主表
  1102. $memberData = [
  1103. 'member_id' => $memberId,
  1104. 'member_is_owner' => 'N',
  1105. 'member_classify' => 'MEMBER',
  1106. 'member_status' => 'ACTIVED',
  1107. 'member_mobile' => $params['mobile'],
  1108. 'member_invite_code' => self::generateInviteCode(),
  1109. 'member_extend_json' => json_encode(['come_from' => 'SYSTEM']),
  1110. 'member_addtimes' => time()
  1111. ];
  1112. Member::insert($memberData);
  1113. // 账户表
  1114. $accountData = [
  1115. 'join_account_member_id' => $memberId,
  1116. 'member_account_classify' => 'POINTS',
  1117. 'member_account_status' => 'ACTIVED',
  1118. 'member_account_category' => 'NORMAL',
  1119. 'member_account_nbr' => $memberId . '-POINTS',
  1120. 'member_account_name' => '积分账户',
  1121. 'member_account_addtimes' => time()
  1122. ];
  1123. // 积分账户
  1124. MemberAccount::insert($accountData);
  1125. $accountData['member_account_classify'] = 'CASH';
  1126. $accountData['member_account_nbr'] = $memberId . '-CASH';
  1127. $accountData['member_account_name'] = '余额账户';
  1128. // 现金/余额账户
  1129. MemberAccount::insert($accountData);
  1130. // info
  1131. $infoData = [
  1132. 'join_info_member_id' => $memberId,
  1133. 'member_info_nickname' => $params['username']
  1134. ];
  1135. MemberInfo::insert($infoData);
  1136. }
  1137. private static function generateMemberId()
  1138. {
  1139. $id = SysSerial::getSerial();
  1140. return "MR" . date('ymdHi') . random_string(4, 'up');
  1141. }
  1142. public static function getMemberName($mobile, $certName, $nickname)
  1143. {
  1144. $name = '';
  1145. if (!empty($certName)) {
  1146. $name = $certName;
  1147. } elseif (!empty($nickname)) {
  1148. $name = $nickname;
  1149. }
  1150. if (!empty($name) && !empty($mobile)) {
  1151. $name .= '-' . $mobile;
  1152. } elseif (!empty($mobile)) {
  1153. $name = $mobile;
  1154. }
  1155. return $name;
  1156. }
  1157. public static function getMemberNameByMemberId($memberId)
  1158. {
  1159. $member = Member::with([
  1160. 'cert' => function ($query) {
  1161. $query->select('join_cert_member_id', 'member_cert_name');
  1162. },
  1163. 'info' => function ($query) {
  1164. $query->select('join_info_member_id', 'member_info_nickname');
  1165. },
  1166. ]
  1167. )->where('member_id', $memberId)
  1168. ->first();
  1169. if (empty($member)) {
  1170. return;
  1171. }
  1172. $member = $member->toArray();
  1173. $mobile = $certName = $nickname = '';
  1174. if (!empty($member['member_mobile'])) {
  1175. $mobile = $member['member_mobile'];
  1176. }
  1177. if (!empty($member['cert']) && !empty($member['cert']['member_cert_name'])) {
  1178. $certName = $member['cert']['member_cert_name'];
  1179. }
  1180. if (!empty($member['info']) && !empty($member['info']['member_info_nickname'])) {
  1181. $nickname = $member['info']['member_info_nickname'];
  1182. }
  1183. return self::getMemberName($mobile, $certName, $nickname);
  1184. }
  1185. /**
  1186. * @Desc 生成推荐码
  1187. * @Author Gorden
  1188. * @Date 2024/9/6 9:05
  1189. *
  1190. * @return string
  1191. */
  1192. public static function generateInviteCode()
  1193. {
  1194. $code = random_string(8, 'number');
  1195. if (Member::where('member_invite_code', $code)->exists()) {
  1196. return self::generateInviteCode();
  1197. } else {
  1198. return $code;
  1199. }
  1200. }
  1201. }