MemberService.php 64 KB

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