MemberController.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\member\MemberService;
  4. use app\admin\service\notify\RechargeService;
  5. use app\admin\service\order\OrderService;
  6. use app\admin\validate\member\MemberValidate;
  7. use app\model\Coupon;
  8. use app\model\CouponDetail;
  9. use app\model\Family;
  10. use app\model\FamilyMember;
  11. use app\model\Member;
  12. use app\model\MemberAccount;
  13. use app\model\MemberBenefit;
  14. use app\model\MemberQuota;
  15. use app\model\MemberRole;
  16. use app\model\Order;
  17. use app\model\OrderReturn;
  18. use app\model\OrderSheet;
  19. use app\model\PayDetail;
  20. use app\model\SysUser;
  21. use support\Db;
  22. use support\exception\BusinessException;
  23. use support\Log;
  24. use support\Request;
  25. use Tinywan\Jwt\JwtToken;
  26. use function _PHPStan_cc8d35ffb\RingCentral\Psr7\str;
  27. class MemberController
  28. {
  29. public function selectList(Request $request)
  30. {
  31. $keywords = $request->get('keywords');
  32. $isOwner = $request->get('member_is_owner', 'N');
  33. if (!$keywords) {
  34. return json_fail("查询错误");
  35. }
  36. $res = Db::table('member')
  37. ->leftjoin('member_cert', 'member.member_id', '=', 'member_cert.join_cert_member_id')
  38. ->leftjoin('member_info', 'member.member_id', '=', 'member_info.join_info_member_id')
  39. ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name', 'member_info.member_info_nickname')
  40. ->where('member.member_is_owner', $isOwner)
  41. ->where('member_status', 'ACTIVED')
  42. // ->where('member_mobile','<>','0000')
  43. ->where(function ($query) use ($keywords) {
  44. $query->where('member.member_mobile', 'like', '%' . $keywords . '%')
  45. ->orWhere("member.member_id", 'like', '%' . $keywords . '%')
  46. ->orWhere("member_cert.member_cert_name", 'like', '%' . $keywords . '%')
  47. ->orWhere("member_info.member_info_nickname", 'like', '%' . $keywords . '%');
  48. })->get();
  49. $data = [];
  50. foreach ($res as $item) {
  51. $certName = $item->member_cert_name ?? '';
  52. $mobile = $item->member_mobile ?? '';
  53. $data[] = [
  54. 'key' => $item->member_id,
  55. 'value' => MemberService::getMemberCertName($mobile, $certName, ''),
  56. ];
  57. }
  58. return json_success('', $data);
  59. }
  60. public function exportMember(Request $request)
  61. {
  62. return MemberService::exportMember($request);
  63. }
  64. public function selectWriteOffMember()
  65. {
  66. $members = Member::with([
  67. 'cert' => function ($query) {
  68. $query->select('join_cert_member_id', 'member_cert_name');
  69. }
  70. ])->whereRaw("JSON_EXTRACT(`member_json`, '$.user') IS NOT NULL")
  71. ->select('member_id', 'member_mobile', 'member_json')
  72. ->get()
  73. ->toArray();
  74. foreach ($members as &$member) {
  75. // $member['member_mobile'] = substr($member['member_mobile'],0,3).'****'.substr($member['member_mobile'],7);
  76. $member['member_json'] = json_decode($member['member_json'], true);
  77. $member['username'] = $member['member_mobile'];
  78. $member['user_id'] = $member['member_json']['user'] ? $member['member_json']['user']['user_id'] : '';
  79. if (isset($member['cert']['member_cert_name'])) {
  80. $member['username'] = $member['cert']['member_cert_name'] . '-' . $member['username'];
  81. }
  82. }
  83. return json_success('', $members);
  84. }
  85. public function selectFamilyMemberList(Request $request)
  86. {
  87. $keywords = $request->get('keywords');
  88. if (!$keywords) {
  89. return json_fail("查询错误");
  90. }
  91. // 创建的家庭
  92. $family = Family::where('join_family_creator_member_id', $request->get('memberId'))->get()->toArray();
  93. if (!$family) {
  94. return json_success('暂无数据');
  95. }
  96. $familyIds = array_column($family, 'family_id');
  97. $res = FamilyMember::whereIn('join_family_id', $familyIds)
  98. ->where("family_member_name", 'like', '%' . $keywords . '%')
  99. ->get()
  100. ->toArray();
  101. $data = [];
  102. foreach ($res as $item) {
  103. $data[] = [
  104. 'key' => $item['join_family_member_id'],
  105. 'value' => $item['family_member_name']
  106. ];
  107. }
  108. return json_success('', $data);
  109. }
  110. public function selectMemberAllQuota(Request $request)
  111. {
  112. $memberId = $request->get('member_id', '');
  113. $orderId = $request->get('order_id', '');
  114. $goodsId = $request->get('goods_id', '');
  115. $person = $request->get('person', '');
  116. $selectType = $request->get('select_type', 'ALL');
  117. if (!$memberId) {
  118. return json_fail('参数异常');
  119. }
  120. $data = [];
  121. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  122. ->whereColumn('member_benefit_limit_count', '>', 'member_benefit_used_count')
  123. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  124. ->when($orderId != '', function ($query) use ($orderId) {
  125. $query->where('join_benefit_order_id', $orderId);
  126. })->when($goodsId != '', function ($query) use ($goodsId) {
  127. $query->where('join_benefit_goods_id', $goodsId);
  128. })
  129. ->get()
  130. ->toArray();
  131. foreach ($benefit as $item) {
  132. if ($item['total'] - $item['used'] < $person) {
  133. $item['disabled'] = true;
  134. }
  135. $item['cut'] = $item['total'] - $item['used'];
  136. $data[] = $item;
  137. }
  138. if ($selectType == 'BENEFIT') {
  139. return json_success('', $data);
  140. }
  141. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  142. ->when($goodsId != '', function ($query) use ($goodsId) {
  143. $query->whereJsonContains('member_quota_json', ['goods_id' => $goodsId]);
  144. })
  145. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  146. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  147. ->get()
  148. ->toArray();
  149. foreach ($quotas as $quota) {
  150. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  151. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  152. ->where('member_quota_status', 'USED')
  153. ->count('*');
  154. if ($quota['total'] <= $usedNum) {
  155. continue;
  156. }
  157. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  158. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  159. ->select('member_quota_id as id', 'member_quota_name as name')
  160. ->first()
  161. ->toArray();
  162. $memberQuota['total'] = $quota['total'];
  163. $memberQuota['used'] = $usedNum;
  164. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  165. $memberQuota['disabled'] = true;
  166. }
  167. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  168. $data[] = $memberQuota;
  169. }
  170. if ($selectType == 'QUOTA') {
  171. return json_success('', $data);
  172. }
  173. return json_success('', $data);
  174. }
  175. /**
  176. * 预约核销用
  177. */
  178. public function selectMemberAllQuotaByAppointment(Request $request)
  179. {
  180. $memberId = $request->get('member_id', '');
  181. $orderId = $request->get('order_id', '');
  182. $goodsId = $request->get('goods_id', '');
  183. $person = $request->get('person', '');
  184. $selectType = $request->get('select_type', 'ALL');
  185. if (!$memberId) {
  186. return json_fail('参数异常');
  187. }
  188. $data = [];
  189. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  190. ->whereColumn('member_benefit_limit_count', '>', 'member_benefit_used_count')
  191. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  192. ->when($orderId != '', function ($query) use ($orderId) {
  193. $query->where('join_benefit_order_id', $orderId);
  194. })->when($goodsId != '', function ($query) use ($goodsId) {
  195. $query->where('join_benefit_goods_id', $goodsId);
  196. })
  197. ->get()
  198. ->toArray();
  199. $data['benefit'] = [];
  200. foreach ($benefit as $item) {
  201. $item['benefit']['cut'] = $item['total'] - $item['used'];
  202. $data['benefit']['name'] = $item['name'];
  203. $data['benefit']['cut'] += $item['cut'];
  204. $data['benefit']['id'][] = $item['id'];
  205. }
  206. if ($selectType == 'BENEFIT') {
  207. return json_success('', $data);
  208. }
  209. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  210. ->when($goodsId != '', function ($query) use ($goodsId) {
  211. $query->whereJsonContains('member_quota_json', ['goods_id' => $goodsId]);
  212. })
  213. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  214. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  215. ->get()
  216. ->toArray();
  217. $data['quota'] = [];
  218. foreach ($quotas as $quota) {
  219. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  220. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  221. ->where('member_quota_status', 'USED')
  222. ->count('*');
  223. if ($quota['total'] <= $usedNum) {
  224. continue;
  225. }
  226. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  227. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  228. ->select('member_quota_id as id', 'member_quota_name as name')
  229. ->first()
  230. ->toArray();
  231. $memberQuota['total'] = $quota['total'];
  232. $memberQuota['used'] = $usedNum;
  233. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  234. $memberQuota['disabled'] = true;
  235. }
  236. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  237. $data['quota']['name'] = $memberQuota['name'];
  238. $data['quota']['cut'] += $memberQuota['cut'];
  239. $data['quota']['id'][] = $memberQuota['id'];
  240. // $data[] = $memberQuota;
  241. }
  242. if ($selectType == 'QUOTA') {
  243. return json_success('', $data);
  244. }
  245. return json_success('', $data);
  246. }
  247. public function selectMemberAllQuotaByGoods(Request $request)
  248. {
  249. $memberId = $request->get('member_id', '');
  250. $orderId = $request->get('order_id', '');
  251. $person = $request->get('person', '');
  252. $selectType = $request->get('select_type', 'ALL');
  253. if (!$memberId) {
  254. return json_fail('参数异常');
  255. }
  256. $data = [];
  257. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  258. // ->whereColumn('member_benefit_limit_count','>','member_benefit_used_count')
  259. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  260. ->when($orderId != '', function ($query) use ($orderId) {
  261. $query->where('join_benefit_order_id', $orderId);
  262. })
  263. ->get()
  264. ->toArray();
  265. foreach ($benefit as $item) {
  266. if ($item['total'] - $item['used'] < $person) {
  267. $item['disabled'] = true;
  268. }
  269. $item['cut'] = $item['total'] - $item['used'];
  270. $data[] = $item;
  271. }
  272. if ($selectType == 'BENEFIT') {
  273. return json_success('', $data);
  274. }
  275. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  276. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  277. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  278. ->get()
  279. ->toArray();
  280. foreach ($quotas as $quota) {
  281. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  282. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  283. ->where('member_quota_status', 'USED')
  284. ->count('*');
  285. if ($quota['total'] <= $usedNum) {
  286. continue;
  287. }
  288. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  289. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  290. ->select('member_quota_id as id', 'member_quota_name as name')
  291. ->first()
  292. ->toArray();
  293. $memberQuota['total'] = $quota['total'];
  294. $memberQuota['used'] = $usedNum;
  295. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  296. $memberQuota['disabled'] = true;
  297. }
  298. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  299. $data[] = $memberQuota;
  300. }
  301. if ($selectType == 'QUOTA') {
  302. return json_success('', $data);
  303. }
  304. return json_success('', $data);
  305. }
  306. public function list(Request $request)
  307. {
  308. return MemberService::list($request);
  309. }
  310. public function info(Request $request)
  311. {
  312. $memberId = $request->get('member_id', '');
  313. if (!$memberId) {
  314. return json_fail("参数错误");
  315. }
  316. return MemberService::info($memberId);
  317. }
  318. /**
  319. * @Desc 会员统计
  320. * @Author Gorden
  321. * @Date 2024/10/21 16:31
  322. *
  323. * @return \support\Response
  324. */
  325. public function statistics(Request $request)
  326. {
  327. $memberType = $request->get('member_type');
  328. $todayStart = strtotime(date('Y-m-d'));
  329. $todayEnd = strtotime(date('Y-m-d 23:59:59'));
  330. $todayCount = Member::when(empty($memberType), function ($query) use ($todayStart, $todayEnd) {
  331. $query->where('member_addtimes', '>=', $todayStart)->where('member_addtimes', '<=', $todayEnd);
  332. })->when(!empty($memberType), function ($query) use ($memberType, $todayStart, $todayEnd) {
  333. if ($memberType == 'PARTNER') {
  334. $query->where("member_is_partner", 'Y')
  335. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) >= {$todayStart}")
  336. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) <= {$todayEnd}");
  337. } elseif ($memberType == 'REFERRER') {
  338. $query->where('member_is_referrer', 'Y')
  339. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) >= {$todayStart}")
  340. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) <= {$todayEnd}");
  341. }
  342. })->count();
  343. $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 day")));
  344. $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")));
  345. $yesterdayCount = Member::when(empty($memberType), function ($query) use ($yesterdayStart, $yesterdayEnd) {
  346. $query->where('member_addtimes', '>=', $yesterdayStart)->where('member_addtimes', '<=', $yesterdayEnd);
  347. })->when(!empty($memberType), function ($query) use ($memberType, $yesterdayStart, $yesterdayEnd) {
  348. if ($memberType == 'PARTNER') {
  349. $query->where("member_is_partner", 'Y')
  350. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) >= {$yesterdayStart}")
  351. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) <= {$yesterdayEnd}");
  352. } elseif ($memberType == 'REFERRER') {
  353. $query->where('member_is_referrer', 'Y')
  354. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) >= {$yesterdayStart}")
  355. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) <= {$yesterdayEnd}");
  356. }
  357. })->count();
  358. $monthStart = strtotime(date('Y-m-01'));
  359. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  360. $monthCount = Member::when(empty($memberType), function ($query) use ($monthStart, $monthEnd) {
  361. $query->where('member_addtimes', '>=', $monthStart)->where('member_addtimes', '<=', $monthEnd);
  362. })->when(!empty($memberType), function ($query) use ($memberType, $monthStart, $monthEnd) {
  363. if ($memberType == 'PARTNER') {
  364. $query->where("member_is_partner", 'Y')
  365. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) >= {$monthStart}")
  366. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) <= {$monthEnd}");
  367. } elseif ($memberType == 'REFERRER') {
  368. $query->where('member_is_referrer', 'Y')
  369. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) >= {$monthStart}")
  370. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) <= {$monthEnd}");
  371. }
  372. })->count();
  373. $lastMonthStart = strtotime(date('Y-m-01', strtotime("last day of -1 month")));
  374. $lastMonthEnd = strtotime(date('Y-m-t 23:59:59', strtotime("last day of -1 month")));
  375. $lastMonthCount = Member::when(empty($memberType), function ($query) use ($lastMonthStart, $lastMonthEnd) {
  376. $query->where('member_addtimes', '>=', $lastMonthStart)->where('member_addtimes', '<=', $lastMonthEnd);
  377. })->when(!empty($memberType), function ($query) use ($memberType, $lastMonthStart, $lastMonthEnd) {
  378. if ($memberType == 'PARTNER') {
  379. $query->where("member_is_partner", 'Y')
  380. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) >= {$lastMonthStart}")
  381. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_partner_json,'$.datetime'))) as SIGNED) <= {$lastMonthEnd}");
  382. } elseif ($memberType == 'REFERRER') {
  383. $query->where('member_is_referrer', 'Y')
  384. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) >= {$lastMonthStart}")
  385. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_referrer_json,'$.datetime'))) as SIGNED) <= {$lastMonthEnd}");
  386. }
  387. })->count();
  388. return json_success('', [
  389. 'todayCount' => $todayCount,
  390. 'yesterdayCount' => $yesterdayCount,
  391. 'monthCount' => $monthCount,
  392. 'lastMonthCount' => $lastMonthCount
  393. ]);
  394. }
  395. public function vipInfo(Request $request)
  396. {
  397. $memberId = $request->get('member_id', '');
  398. if (!$memberId) {
  399. return json_fail("参数错误");
  400. }
  401. return MemberService::vipInfo($request);
  402. }
  403. /**
  404. * @Desc 储值卡账户列表
  405. * @Author Gorden
  406. * @Date 2024/11/19 11:46
  407. *
  408. * @param Request $request
  409. * @return \support\Response
  410. */
  411. public function cardList(Request $request)
  412. {
  413. $memberId = $request->get('member_id', '');
  414. if (!$memberId) {
  415. return json_fail("参数错误");
  416. }
  417. return MemberService::cardList($request);
  418. }
  419. /**
  420. * @Desc 储值卡账户明细
  421. * @Author Gorden
  422. * @Date 2024/11/19 11:46
  423. *
  424. * @param Request $request
  425. * @return \support\Response
  426. */
  427. public function cardInfo(Request $request)
  428. {
  429. $memberId = $request->get('member_id', '');
  430. if (!$memberId) {
  431. return json_fail("参数错误");
  432. }
  433. return MemberService::cardInfo($request);
  434. }
  435. public function welfareInfo(Request $request)
  436. {
  437. $memberId = $request->get('member_id');
  438. if (!$memberId) {
  439. return json_fail("参数错误");
  440. }
  441. return MemberService::welfareInfo($request);
  442. }
  443. public function commissionList(Request $request)
  444. {
  445. $memberId = $request->get('member_id');
  446. if (!$memberId) {
  447. return json_fail("参数错误");
  448. }
  449. return MemberService::commissionList($request);
  450. }
  451. /**
  452. * @Desc 我的粉丝列表
  453. * @Author Gorden
  454. * @Date 2024/9/20 10:22
  455. *
  456. * @param Request $request
  457. * @return \support\Response
  458. */
  459. public function fansList(Request $request)
  460. {
  461. $memberId = $request->get('member_id');
  462. if (!$memberId) {
  463. return json_fail("参数错误");
  464. }
  465. return MemberService::fansList($request);
  466. }
  467. public function clearWelfare(Request $request)
  468. {
  469. $memberId = $request->post('member_id', '');
  470. if (!$memberId) {
  471. return json_fail("参数错误");
  472. }
  473. return MemberService::clearWelfare($request);
  474. }
  475. public function balanceInfo(Request $request)
  476. {
  477. $memberId = $request->get('member_id', '');
  478. if (!$memberId) {
  479. return json_fail("参数错误");
  480. }
  481. return MemberService::balanceInfo($request);
  482. }
  483. public function pointInfo(Request $request)
  484. {
  485. $memberId = $request->get('member_id', '');
  486. if (!$memberId) {
  487. return json_fail("参数错误");
  488. }
  489. return MemberService::pointInfo($memberId);
  490. }
  491. /**
  492. * @Desc 添加会员
  493. * @Author Gorden
  494. * @Date 2024/3/25 13:40
  495. *
  496. * @param Request $request
  497. * @return \support\Response
  498. */
  499. public function add(Request $request)
  500. {
  501. $validate = new MemberValidate();
  502. if (!$validate->scene('add')->check($request->post())) {
  503. return json_fail($validate->getError());
  504. }
  505. _syslog("添加用户", "用户【" . $request->post('member_cert_name') . '】');
  506. return MemberService::add($request->post());
  507. }
  508. public function update(Request $request)
  509. {
  510. $validate = new MemberValidate();
  511. if (!$validate->scene('update')->check($request->post())) {
  512. return json_fail($validate->getError());
  513. }
  514. _syslog("编辑用户", "用户【" . $request->post('member_cert_name') ?? $request->post('member_id') . '】');
  515. return MemberService::update($request->post());
  516. }
  517. /**
  518. * @Desc 更换手机号
  519. * @Author Gorden
  520. * @Date 2024/11/25 15:54
  521. *
  522. * @param Request $request
  523. * @return \support\Response
  524. */
  525. public function changeMobile(Request $request)
  526. {
  527. $memberId = $request->post('member_id');
  528. $mobile = $request->post('member_mobile');
  529. $ticket = $request->post('ticket');
  530. if (!$memberId || !$mobile || !$ticket) {
  531. return json_fail("参数异常");
  532. }
  533. if (Member::where('member_mobile', $mobile)->exists()) {
  534. return json_fail("新的手机号已存在账户,无法变更,请联系管理员");
  535. }
  536. try {
  537. $member = Member::where('member_id', $memberId)->first();
  538. $oldMobile = $member->member_mobile;
  539. $member->member_mobile = $mobile;
  540. $memberUpdateJson = [];
  541. if (!empty($member->member_update_json)) {
  542. $memberUpdateJson = json_decode($member->member_update_json, true);
  543. }
  544. $memberUpdateJson['update'][] = [
  545. 'time' => date('Y-m-d H:i:s'),
  546. 'user_id' => JwtToken::getCurrentId(),
  547. 'data' => [
  548. [
  549. 'field' => 'member_mobile',
  550. 'old' => $oldMobile,
  551. 'new' => $mobile,
  552. 'media' => [
  553. 'url' => str_replace(getenv("STORAGE_DOMAIN"), '', $ticket)
  554. ]
  555. ]
  556. ]
  557. ];
  558. $member->member_update_json = json_encode($memberUpdateJson);
  559. $member->save();
  560. return json_success("更换手机号成功");
  561. } catch (\Exception $e) {
  562. return json_fail("更换手机号失败");
  563. }
  564. }
  565. /**
  566. * @Desc 会员修改记录
  567. * @Author Gorden
  568. * @Date 2024/11/26 8:41
  569. *
  570. * @param Request $request
  571. * @return \support\Response
  572. */
  573. public function updateLogList(Request $request)
  574. {
  575. $page = $request->get('page', 1);
  576. $pageSize = $request->get('pageSize', 10);
  577. $memberId = $request->get('member_id');
  578. $member = Member::where('member_id', $memberId)->first();
  579. $updateJson = [];
  580. if (!empty($member->member_update_json)) {
  581. $memberUpdateJson = json_decode($member->member_update_json, true);
  582. $updateJson = $memberUpdateJson['update'] ?? [];
  583. }
  584. foreach ($updateJson as &$item) {
  585. if (!empty($item['user_id'])) {
  586. $item['user_name'] = SysUser::where('user_id', $item['user_id'])->value('user_name');
  587. } else {
  588. $item['user_name'] = '会员';
  589. }
  590. // 修改内容
  591. $item['content'] = '';
  592. $item['images'] = '';
  593. $item['imagesOrigin'] = '';
  594. foreach ($item['data'] as $datum) {
  595. if ($datum['field'] == 'member_mobile') {
  596. $item['content'] = '手机号由' . $datum['old'] . '更换为' . $datum['new'] . ';';
  597. if (!empty($datum['media'])) {
  598. $item['images'] = getenv('STORAGE_DOMAIN') . $datum['media']['url'];
  599. $item['imagesOrigin'] = str_replace('/thumb', '', $item['images']);
  600. }
  601. }
  602. }
  603. $item['content'] = rtrim($item['content'], ';');
  604. }
  605. $total = count($updateJson);
  606. $updateJson = array_reverse($updateJson);
  607. $start = ($page - 1) * $pageSize;
  608. $rows = array_slice($updateJson, $start, $pageSize);
  609. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  610. }
  611. /**
  612. * @Desc 绑定的设备
  613. * @Author Gorden
  614. * @Date 2024/4/7 10:20
  615. *
  616. * @param $id
  617. * @return \support\Response
  618. */
  619. public function device($id)
  620. {
  621. return MemberService::deviceList($id);
  622. }
  623. public function addDevice(Request $request)
  624. {
  625. return MemberService::addDevice($request);
  626. }
  627. public function updateDevice(Request $request)
  628. {
  629. return MemberService::updateDevice($request);
  630. }
  631. public function deleteDevice(Request $request)
  632. {
  633. return MemberService::deleteDevice($request);
  634. }
  635. /**
  636. * @Desc 卡券数量
  637. * @Author Gorden
  638. * @Date 2024/8/22 18:51
  639. *
  640. * @param Request $request
  641. * @return \support\Response
  642. */
  643. public function couponCount(Request $request)
  644. {
  645. $memberId = $request->get('member_id', '');
  646. $coupons = CouponDetail::leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  647. ->where('join_coupon_detail_member_id', $memberId)
  648. ->select('coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  649. ->get()
  650. ->toArray();
  651. $data = ['manjian' => 0, 'diyong' => 0, 'zhekou' => 0, 'lijian' => 0, 'zengpin' => 0, 'fuli' => 0, 'nianka' => 0, 'jika' => 0, 'yueka' => 0];
  652. foreach ($coupons as $item) {
  653. if ($item['coupon_classify'] == '满减券') {
  654. $data['manjian'] += 1;
  655. } elseif ($item['coupon_classify'] == '抵用券') {
  656. $data['diyong'] += 1;
  657. } elseif ($item['coupon_classify'] == '折扣券') {
  658. $data['zhekou'] += 1;
  659. } elseif ($item['coupon_classify'] == '立减券') {
  660. $data['lijian'] += 1;
  661. } elseif ($item['coupon_classify'] == '赠品券') {
  662. $data['zengpin'] += 1;
  663. } elseif ($item['coupon_classify'] == '福利券') {
  664. $data['fuli'] += 1;
  665. } elseif ($item['coupon_classify'] == '年卡') {
  666. $data['nianka'] += 1;
  667. } elseif ($item['coupon_classify'] == '季卡') {
  668. $data['jika'] += 1;
  669. } elseif ($item['coupon_classify'] == '月卡') {
  670. $data['yueka'] += 1;
  671. }
  672. }
  673. $data['total'] = $data['manjian'] + $data['diyong'] + $data['zhekou'] + $data['lijian'] + $data['zengpin'] + $data['fuli'] + $data['nianka'] + $data['jika'] + $data['yueka'];
  674. return json_success('', $data);
  675. }
  676. /**
  677. * @Desc 我的卡券
  678. * @Author Gorden
  679. * @Date 2024/8/22 18:45
  680. *
  681. * @param Request $request
  682. * @return \support\Response
  683. */
  684. public function coupon(Request $request)
  685. {
  686. $memberId = $request->get('member_id', '');
  687. $classify = $request->get('classify', '');
  688. $page = $request->get('page', 1);
  689. $pageSize = $request->get('pageSize', 20);
  690. $coupons = CouponDetail::with([
  691. 'member' => function ($query) {
  692. $query->select('member_id', 'member_mobile');
  693. },
  694. 'cert' => function ($query) {
  695. $query->select('join_cert_member_id', 'member_cert_name');
  696. },
  697. 'info' => function ($query) {
  698. $query->select('join_info_member_id', 'member_info_nickname');
  699. }
  700. ])
  701. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  702. ->where('join_coupon_detail_member_id', $memberId)
  703. ->where('coupon.coupon_classify', $classify)
  704. ->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  705. // $total = $coupons->count();
  706. $totalModel = Db::select("select count(a.join_detail_coupon_id) as total from (select join_detail_coupon_id from app_coupon_detail as d left join app_coupon as c ON d.join_detail_coupon_id = c.coupon_id where d.join_coupon_detail_member_id='" . $memberId . "' and c.coupon_classify='" . $classify . "' group by d.join_coupon_detail_member_id,d.join_detail_coupon_id) as a");
  707. $total = 0;
  708. if (!empty($totalModel) && !empty($totalModel[0])) {
  709. $total = $totalModel[0]->total;
  710. }
  711. $rows = $coupons->select('coupon_detail.join_coupon_detail_member_id', 'coupon_detail.join_detail_coupon_id'
  712. , 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  713. ->selectRaw('COUNT(1) as total,
  714. COUNT(IF(app_coupon_detail.coupon_detail_status="USED",1,NULL)) as used_total,
  715. COUNT(IF(app_coupon_detail.coupon_detail_status="ACTIVED" or app_coupon_detail.coupon_detail_status="WAITING",1,NULL)) as unused_total,
  716. MAX(app_coupon_detail.coupon_detail_deadline_datetime) as deadline_datetime')
  717. ->orderBy('deadline_datetime', 'DESC')
  718. ->forPage($page, $pageSize)
  719. ->get()
  720. ->toArray();
  721. foreach ($rows as &$item) {
  722. $mobile = $certName = $nickname = '';
  723. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  724. $mobile = $item['member']['member_mobile'];
  725. }
  726. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  727. $certName = $item['cert']['member_cert_name'];
  728. }
  729. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  730. $nickname = $item['info']['member_info_nickname'];
  731. }
  732. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  733. unset($item['member'], $item['cert'], $item['info']);
  734. $item['invalid_total'] = $item['total'] - $item['used_total'] - $item['unused_total'];
  735. // 优惠券分类
  736. $coupon = Coupon::with('category')->where('coupon_id', $item['join_detail_coupon_id'])
  737. ->select('coupon_id', 'join_coupon_category_id')
  738. ->first();
  739. if (!empty($coupon) && !empty($coupon->category)) {
  740. $item['category_name'] = $coupon->category->category_name;
  741. }
  742. }
  743. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  744. }
  745. /**
  746. * @Desc 我的卡券
  747. * @Author Gorden
  748. * @Date 2024/8/22 18:45
  749. *
  750. * @param Request $request
  751. * @return \support\Response
  752. */
  753. public function allCoupon(Request $request)
  754. {
  755. $memberId = $request->get('member_id', '');
  756. $page = $request->get('page', 1);
  757. $pageSize = $request->get('pageSize', 20);
  758. $coupons = CouponDetail::with([
  759. 'member' => function ($query) {
  760. $query->select('member_id', 'member_mobile');
  761. },
  762. 'cert' => function ($query) {
  763. $query->select('join_cert_member_id', 'member_cert_name');
  764. },
  765. 'info' => function ($query) {
  766. $query->select('join_info_member_id', 'member_info_nickname');
  767. }
  768. ])
  769. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  770. ->where('join_coupon_detail_member_id', $memberId)
  771. ->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  772. // $total = $coupons->count();
  773. $totalModel = Db::select("select count(a.join_detail_coupon_id) as total from (select join_detail_coupon_id from app_coupon_detail as d left join app_coupon as c ON d.join_detail_coupon_id = c.coupon_id where d.join_coupon_detail_member_id='" . $memberId . "' group by d.join_coupon_detail_member_id,d.join_detail_coupon_id) as a");
  774. $total = 0;
  775. if (!empty($totalModel) && !empty($totalModel[0])) {
  776. $total = $totalModel[0]->total;
  777. }
  778. $rows = $coupons->select('coupon_detail.join_coupon_detail_member_id', 'coupon_detail.join_detail_coupon_id'
  779. , 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  780. ->selectRaw('COUNT(1) as total,
  781. COUNT(IF(app_coupon_detail.coupon_detail_status="USED",1,NULL)) as used_total,
  782. COUNT(IF(app_coupon_detail.coupon_detail_status="ACTIVED" or app_coupon_detail.coupon_detail_status="WAITING",1,NULL)) as unused_total,
  783. MAX(app_coupon_detail.coupon_detail_deadline_datetime) as deadline_datetime')
  784. ->orderBy('deadline_datetime', 'DESC')
  785. ->forPage($page, $pageSize)
  786. ->get()
  787. ->toArray();
  788. foreach ($rows as &$item) {
  789. $mobile = $certName = $nickname = '';
  790. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  791. $mobile = $item['member']['member_mobile'];
  792. }
  793. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  794. $certName = $item['cert']['member_cert_name'];
  795. }
  796. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  797. $nickname = $item['info']['member_info_nickname'];
  798. }
  799. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  800. unset($item['member'], $item['cert'], $item['info']);
  801. $item['invalid_total'] = $item['total'] - $item['used_total'] - $item['unused_total'];
  802. // 优惠券分类
  803. $coupon = Coupon::with('category')->where('coupon_id', $item['join_detail_coupon_id'])
  804. ->select('coupon_id', 'join_coupon_category_id')
  805. ->first();
  806. if (!empty($coupon) && !empty($coupon->category)) {
  807. $item['category_name'] = $coupon->category->category_name;
  808. }
  809. }
  810. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  811. }
  812. /**
  813. * @Desc 我的卡券 领取记录
  814. * @Author Gorden
  815. * @Date 2024/8/22 18:45
  816. *
  817. * @param Request $request
  818. * @return \support\Response
  819. */
  820. public function couponDetail(Request $request)
  821. {
  822. $memberId = $request->get('member_id', '');
  823. $couponId = $request->get('coupon_id', '');
  824. $page = $request->get('page', 1);
  825. $pageSize = $request->get('pageSize', 20);
  826. $coupons = CouponDetail::with([
  827. 'member' => function ($query) {
  828. $query->select('member_id', 'member_mobile');
  829. },
  830. 'cert' => function ($query) {
  831. $query->select('join_cert_member_id', 'member_cert_name');
  832. },
  833. 'info' => function ($query) {
  834. $query->select('join_info_member_id', 'member_info_nickname');
  835. }
  836. ])
  837. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  838. ->where('join_coupon_detail_member_id', $memberId)
  839. ->where('coupon.coupon_id', $couponId);
  840. $total = $coupons->count();
  841. $rows = $coupons->select('coupon_detail.*', 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  842. ->orderBy('coupon_detail_deadline_datetime', 'desc')
  843. ->forPage($page, $pageSize)
  844. ->get()
  845. ->toArray();
  846. foreach ($rows as &$item) {
  847. $mobile = $certName = $nickname = '';
  848. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  849. $mobile = $item['member']['member_mobile'];
  850. }
  851. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  852. $certName = $item['cert']['member_cert_name'];
  853. }
  854. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  855. $nickname = $item['info']['member_info_nickname'];
  856. }
  857. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  858. unset($item['member'], $item['cert'], $item['info']);
  859. if (!empty($coupon) && !empty($coupon->category)) {
  860. $item['category_name'] = $coupon->category->category_name;
  861. }
  862. if (!empty($item['coupon_detail_gain_datetime'])) {
  863. $item['coupon_detail_gain_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_gain_datetime']));
  864. }
  865. if (!empty($item['coupon_detail_deadline_datetime'])) {
  866. $item['coupon_detail_deadline_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_deadline_datetime']));
  867. }
  868. }
  869. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  870. }
  871. /**
  872. * @Desc 新会员充值备份记录
  873. * @Author Gorden
  874. * @Date 2024/10/15 10:27
  875. *
  876. * @param Request $request
  877. * @return \support\Response
  878. */
  879. public function rechargeBackupsList(Request $request)
  880. {
  881. $page = $request->get('page', 1);
  882. $pageSize = $request->get('pageSize', 20);
  883. $memberId = $request->get('member_id');
  884. if (!$memberId) {
  885. return json_fail("参数异常");
  886. }
  887. $member = Member::where('member_id', $memberId)
  888. ->select('member_id', 'member_extend_json')
  889. ->first();
  890. $data = [];
  891. if (!empty($member->member_extend_json)) {
  892. $extendJson = json_decode($member->member_extend_json, true);
  893. if (isset($extendJson['snap'])) {
  894. foreach ($extendJson['snap'] as $key => $item) {
  895. $data[] = [
  896. 'recharge_times' => $key,
  897. 'member_account_surplus' => $item['member_account_surplus'],
  898. 'member_account_added' => $item['member_account_added'],
  899. 'pay_amount' => $item['pay_amount'],
  900. 'added_rate' => floatval($item['added_rate']) * 100,
  901. 'added_amount' => $item['added_amount']
  902. ];
  903. }
  904. }
  905. }
  906. $total = count($data);
  907. $start = ($page - 1) * $pageSize;
  908. $rows = array_slice($data, $start, $pageSize);
  909. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  910. }
  911. /**
  912. * @Desc 会员权益使用记录
  913. * @Author Gorden
  914. * @Date 2024/10/15 11:13
  915. *
  916. * @param Request $request
  917. * @return \support\Response
  918. */
  919. public function roleBenefitUsedList(Request $request)
  920. {
  921. $page = $request->get('page', 1);
  922. $pageSize = $request->get('pageSize', 20);
  923. $memberId = $request->get('member_id');
  924. if (!$memberId) {
  925. return json_fail('参数异常');
  926. }
  927. $data = $this->getRoleBenefitUsedList($memberId);
  928. $total = count($data);
  929. $start = ($page - 1) * $pageSize;
  930. $rows = array_slice($data, $start, $pageSize);
  931. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  932. }
  933. public function consumptionList(Request $request)
  934. {
  935. $page = $request->get('page', 1);
  936. $pageSize = $request->get('pageSize', 20);
  937. $memberId = $request->get('member_id');
  938. if (!$memberId) {
  939. return json_fail('参数异常');
  940. }
  941. $member = Member::where('member_id', $memberId)->first();
  942. $firstRechargeTime = 0;
  943. if (!empty($member->member_extend_json)) {
  944. $memberExtendJson = json_decode($member->member_extend_json, true);
  945. if (isset($memberExtendJson['snap'])) {
  946. foreach ($memberExtendJson['snap'] as $key => $value) {
  947. $firstRechargeTime = strtotime($key);
  948. break;
  949. }
  950. }
  951. }
  952. $payDetailIds = PayDetail::where('pay_addtimes', '>', $firstRechargeTime)
  953. ->where('pay_prepayid', $memberId . '-CASH')
  954. ->where('pay_status', 'SUCCESS')
  955. ->pluck('join_pay_order_id')
  956. ->toArray();
  957. $orders = Order::with([
  958. 'payDetail' => function ($query) {
  959. $query->where('pay_prepayid', 'like', '%CASH%');
  960. }
  961. ])->whereIn('order_groupby', $payDetailIds)
  962. ->where('order_addtimes', '>', $firstRechargeTime)
  963. ->whereIn('order_classify', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'DISHES']);
  964. $total = $orders->count();
  965. $rows = $orders->orderBy('order_addtimes', 'DESC')
  966. ->forPage($page, $pageSize)
  967. ->get()
  968. ->toArray();
  969. $data = [];
  970. foreach ($rows as $row) {
  971. $sheets = OrderSheet::with([
  972. 'goods' => function ($query) {
  973. $query->select('goods_id', 'goods_name');
  974. }
  975. ])->where('join_sheet_order_id', $row['order_id'])
  976. ->get()
  977. ->toArray();
  978. $deng = "";
  979. if (count($sheets) > 1) {
  980. $deng = '等';
  981. }
  982. foreach ($row['pay_detail'] as $item) {
  983. if (!empty($item['join_pay_object_json'])) {
  984. $payObjectJson = json_decode($item['join_pay_object_json'], true);
  985. if ($payObjectJson['order_id'] != $row['order_id']) {
  986. continue;
  987. }
  988. }
  989. $data[] = [
  990. 'order_classify' => $row['order_classify'],
  991. 'order_id' => $row['order_id'],
  992. 'goods_name' => !empty($sheets[0]) && !empty($sheets[0]['goods']) ? ($sheets[0]['goods']['goods_name'] . $deng) : '',
  993. 'order_amount_total' => $row['order_amount_total'],
  994. 'order_amount_pay' => $row['order_amount_pay'],
  995. 'pay_paytimes' => $item['pay_paytimes'] ?? "",
  996. 'order_addtimes' => $row['order_addtimes'],
  997. 'pay_amount' => $item['pay_amount']
  998. ];
  999. }
  1000. }
  1001. $rows = $data;
  1002. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  1003. }
  1004. /**
  1005. * @Desc 消费导出
  1006. * @Author Gorden
  1007. * @Date 2024/10/15 17:36
  1008. *
  1009. * @param Request $request
  1010. * @return \support\Response
  1011. */
  1012. public function exportConsumptionList(Request $request)
  1013. {
  1014. $memberId = $request->post('member_id');
  1015. $orderIds = $request->post('order_id', []);
  1016. if (!$memberId) {
  1017. return json_fail('参数异常');
  1018. }
  1019. $member = Member::where('member_id', $memberId)->first();
  1020. $firstRechargeTime = 0;
  1021. if (!empty($member->member_extend_json)) {
  1022. $memberExtendJson = json_decode($member->member_extend_json, true);
  1023. if (isset($memberExtendJson['snap'])) {
  1024. foreach ($memberExtendJson['snap'] as $key => $value) {
  1025. $firstRechargeTime = strtotime($key);
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. if (empty($orderIds)) {
  1031. $payDetailIds = PayDetail::where('pay_addtimes', '>', $firstRechargeTime)
  1032. ->where('pay_prepayid', $memberId . '-CASH')
  1033. ->where('pay_status', 'SUCCESS')
  1034. ->pluck('join_pay_order_id')
  1035. ->toArray();
  1036. } else {
  1037. $payDetailIds = Order::whereIn('order_id', $orderIds)->pluck('order_groupby')->toArray();
  1038. }
  1039. $rows = Order::with([
  1040. 'payDetail' => function ($query) {
  1041. $query->where('pay_prepayid', 'like', '%CASH%');
  1042. }
  1043. ])->whereIn('order_groupby', $payDetailIds)
  1044. ->where('order_addtimes', '>', $firstRechargeTime)
  1045. ->whereIn('order_classify', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'DISHES'])
  1046. ->orderBy('order_addtimes', 'DESC')
  1047. ->get()
  1048. ->toArray();
  1049. $data = [];
  1050. $payAmount = 0;
  1051. foreach ($rows as $row) {
  1052. $sheets = OrderSheet::with([
  1053. 'goods' => function ($query) {
  1054. $query->select('goods_id', 'goods_name');
  1055. }
  1056. ])->where('join_sheet_order_id', $row['order_id'])
  1057. ->get()
  1058. ->toArray();
  1059. $deng = "";
  1060. if (count($sheets) > 1) {
  1061. $deng = '等';
  1062. }
  1063. foreach ($row['pay_detail'] as $item) {
  1064. if (!empty($item['join_pay_object_json'])) {
  1065. $payObjectJson = json_decode($item['join_pay_object_json'], true);
  1066. if ($payObjectJson['order_id'] != $row['order_id']) {
  1067. continue;
  1068. }
  1069. }
  1070. $payAmount += $item['pay_amount'];
  1071. $data[] = [
  1072. 'order_classify' => OrderService::$orderClassify[$row['order_classify']],
  1073. 'order_id' => $row['order_id'],
  1074. 'goods_name' => !empty($sheets[0]) && !empty($sheets[0]['goods']) ? ($sheets[0]['goods']['goods_name'] . $deng) : '',
  1075. 'order_amount_total' => $row['order_amount_total'],
  1076. 'order_amount_pay' => $row['order_amount_pay'],
  1077. 'pay_paytimes' => $item['pay_paytimes'] ?? "",
  1078. 'order_addtimes' => $row['order_addtimes'],
  1079. 'pay_amount' => $item['pay_amount']
  1080. ];
  1081. }
  1082. }
  1083. $data[] = ['pay_amount' => "总计:" . sprintf('%.2f', $payAmount)];
  1084. return json_success('success', $data);
  1085. }
  1086. /**
  1087. * @Desc 获取权益使用记录
  1088. * @Author Gorden
  1089. * @Date 2024/10/15 11:35
  1090. *
  1091. * @param $memberId
  1092. * @param $orderIds
  1093. * @return array
  1094. */
  1095. public function getRoleBenefitUsedList($memberId, $orderIds = [])
  1096. {
  1097. $couponDetails = CouponDetail::where('join_coupon_detail_member_id', $memberId)
  1098. ->where('coupon_detail_status', 'USED')
  1099. ->whereJsonContains('coupon_detail_extend_json->gettype', 'ROLE')
  1100. ->get()
  1101. ->toArray();
  1102. $data = [];
  1103. foreach ($couponDetails as $couponDetail) {
  1104. $orders = Order::where('order_discount_json', 'like', '%' . $couponDetail['coupon_detail_id'] . '%')
  1105. ->whereIn('order_is_complete', ['Y', 'N'])
  1106. ->where('order_status_payment', 'SUCCESS')
  1107. ->when(!empty($orderIds), function ($query) use ($orderIds) {
  1108. $query->whereIn('order_id', $orderIds);
  1109. })->get()
  1110. ->toArray();
  1111. foreach ($orders as $order) {
  1112. $sheets = OrderSheet::with([
  1113. 'goods' => function ($query) {
  1114. $query->select('goods_id', 'goods_name');
  1115. }
  1116. ])->where('join_sheet_order_id', $order['order_id'])
  1117. ->get()
  1118. ->toArray();
  1119. $deng = "";
  1120. if (count($sheets) > 1) {
  1121. $deng = '等';
  1122. }
  1123. $discountJson = json_decode($order['order_discount_json'], true);
  1124. $couponName = '';
  1125. $discountAmount = 0;
  1126. foreach ($discountJson as $item) {
  1127. $discountAmount = $item['coupon_value'];
  1128. $couponName = Coupon::where('coupon_id', $item['coupon_id'])->value('coupon_name');
  1129. }
  1130. $payDetailTimes = PayDetail::whereJsonContains('join_pay_object_json->order_id', $order['order_id'])->value('pay_paytimes');
  1131. $data[] = [
  1132. 'order_id' => $order['order_id'],
  1133. 'goods_name' => !empty($sheets[0]) && !empty($sheets[0]['goods']) ? ($sheets[0]['goods']['goods_name'] . $deng) : '',
  1134. 'order_classify' => $order['order_classify'],
  1135. 'order_amount_total' => $order['order_amount_total'],
  1136. 'order_amount_pay' => $order['order_amount_pay'],
  1137. 'coupon_name' => $couponName,
  1138. 'discountAmount' => $discountAmount,
  1139. 'order_status_system' => $order['order_status_system'],
  1140. 'order_addtimes' => $order['order_addtimes'],
  1141. 'pay_paytimes' => $payDetailTimes
  1142. ];
  1143. }
  1144. }
  1145. return $data;
  1146. }
  1147. public function roleBenefitExport(Request $request)
  1148. {
  1149. $orderIds = $request->post('order_id', []);
  1150. $memberId = $request->post('member_id');
  1151. $applyUsername = $request->post('apply_username', '');
  1152. $applyMobile = $request->post('apply_mobile', '');
  1153. $applyAmount = $request->post('apply_amount', '');
  1154. if (!$memberId) {
  1155. return json_fail("参数异常");
  1156. }
  1157. $data = $this->getRoleBenefitUsedList($memberId, $orderIds);
  1158. $dicountAmount = 0;
  1159. foreach ($data as $key => $item) {
  1160. $data[$key]['order_classify'] = OrderService::$orderClassify[$item['order_classify']];
  1161. $dicountAmount += $item['discountAmount'];
  1162. }
  1163. $returnApplayData['data'] = $data;
  1164. $returnApplayData['amount'] = $applyAmount;
  1165. $returnApplayData['apply_username'] = $applyUsername;
  1166. $returnApplayData['apply_mobile'] = $applyMobile;
  1167. $returnApplayData['reason'] = "会员权益退款";
  1168. $data[] = [
  1169. 'discountAmount' => '优惠合计:¥' . sprintf('%.2f', $dicountAmount)
  1170. ];
  1171. // $data[] = [
  1172. // 'order_classify' => '申请人:' . $applyUsername,
  1173. // ];
  1174. // $data[] = [
  1175. // 'order_classify' => '电话:' . $applyMobile,
  1176. // ];
  1177. // $data[] = [
  1178. // 'order_classify' => '退款金额:' . $applyAmount,
  1179. // ];
  1180. // 产生退款记录
  1181. // $this->refundOrder($memberId, $returnApplayData);
  1182. return json_success('success', $data);
  1183. }
  1184. public function refundOrder(Request $request)
  1185. {
  1186. $params = $request->post();
  1187. Db::beginTransaction();
  1188. try {
  1189. // OrderReturn::where('join_return_member_id', $params['member_id'])
  1190. // ->where('order_return_status', '<>', 'DONE')
  1191. // ->where('order_return_category', '会员退款')
  1192. // ->delete();
  1193. // 验证余额
  1194. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])
  1195. ->where('member_account_classify', 'CASH')
  1196. ->first();
  1197. if ($params['surplus'] > $memberAccount->member_account_surplus) {
  1198. throw new BusinessException("本金退款金额超出账户本金余额");
  1199. }
  1200. if ($params['added'] > $memberAccount->member_account_added) {
  1201. throw new BusinessException("赠送退款金额超出账户赠送余额");
  1202. }
  1203. $returnApplyJson = [
  1204. 'reason' => '会员退款',
  1205. 'order_id' => 'ROLE_REFUND',
  1206. 'classify' => 'ROLE_REFUND',
  1207. 'username' => $params['username'],
  1208. 'mobile' => $params['mobile'],
  1209. 'surplus' => $params['surplus'],
  1210. 'added' => $params['added'],
  1211. 'amount' => $params['bank_amount'],
  1212. 'bank_amount' => $params['bank_amount'],
  1213. 'bank_name' => $params['bank_name'],
  1214. 'bank_acount' => $params['bank_acount'],
  1215. 'bank_cardno' => $params['bank_cardno']
  1216. ];
  1217. // 退款记录
  1218. OrderReturn::insert([
  1219. 'join_order_return_user_id' => JwtToken::getCurrentId(),
  1220. 'join_return_member_id' => $params['member_id'],
  1221. 'join_return_order_id' => 'ROLE_REFUND',
  1222. 'order_return_status' => 'PENDING',
  1223. 'order_return_category' => '会员退款',
  1224. 'order_return_apply_datetime' => date('Y-m-d H:i:s'),
  1225. 'order_return_apply_json' => json_encode($returnApplyJson),
  1226. 'order_return_remark' => $params['remark'] ?? '',
  1227. 'order_return_addtimes' => time()
  1228. ]);
  1229. Db::commit();
  1230. return json_success("success");
  1231. } catch (BusinessException $e) {
  1232. Db::rollBack();
  1233. Log::error("创建退款记录失败:" . $e->getMessage());
  1234. return json_fail($e->getMessage());
  1235. } catch (\Exception $e) {
  1236. Db::rollBack();
  1237. Log::error("创建退款记录失败:" . $e->getMessage());
  1238. return json_fail("创建退款记录失败");
  1239. }
  1240. }
  1241. public function roleBenefitRefund(Request $request)
  1242. {
  1243. $memberId = $request->post('member_id');
  1244. if (!$memberId) {
  1245. return json_fail('参数异常');
  1246. }
  1247. $returnOrder = OrderReturn::where('join_return_member_id', $memberId)
  1248. ->where('order_return_status', 'DOING')
  1249. ->where('order_return_category', '会员权益退款')
  1250. ->first();
  1251. if (!$returnOrder) {
  1252. return json_fail("退款数据异常");
  1253. }
  1254. $memberAccount = MemberAccount::where('join_account_member_id', $memberId)
  1255. ->where('member_account_classify', 'CASH')
  1256. ->where('member_account_status', 'ACTIVED')
  1257. ->first();
  1258. if (!$memberAccount) {
  1259. return json_fail('会员账户异常');
  1260. }
  1261. try {
  1262. Db::beginTransaction();
  1263. $refundApplyJson = json_decode($returnOrder->order_return_apply_json, true);
  1264. $amount = $refundApplyJson['amount'];
  1265. $refundApplyJson['user_id'] = JwtToken::getCurrentId();
  1266. $refundApplyJson['datetime'] = date('Y-m-d H:i:s');
  1267. $returnOrder->order_return_refund_json = json_encode($refundApplyJson);
  1268. $returnOrder->order_return_status = 'DONE';
  1269. $returnOrder->save();
  1270. // 账户剩余
  1271. if ($memberAccount->member_account_surplus + $memberAccount->member_account_added < $amount) {
  1272. throw new BusinessException("账户余额不足");
  1273. }
  1274. // 本金
  1275. if ($memberAccount->member_account_surplus >= $amount) {
  1276. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus - $amount;
  1277. } else {
  1278. $memberAccount->member_account_added = $memberAccount->member_account_added - ($amount - $memberAccount->member_account_surplus);
  1279. $memberAccount->member_account_surplus = 0;
  1280. }
  1281. $memberAccount->member_account_update_user_id = JwtToken::getCurrentId();
  1282. $memberAccount->member_account_updatetimes = time();
  1283. $memberAccount->save();
  1284. // 删除会员身份
  1285. Member::where('member_id', $memberId)
  1286. ->update([
  1287. 'join_member_role_id' => '',
  1288. 'member_role_begintime' => '',
  1289. 'member_role_config_json' => '[]'
  1290. ]);
  1291. // 没用的优惠券失效
  1292. CouponDetail::where('join_coupon_detail_member_id', $memberId)
  1293. ->where('coupon_detail_status', '<>', 'USED')
  1294. ->whereJsonContains('coupon_detail_extend_json->gettype', 'ROLE')
  1295. ->update([
  1296. 'coupon_detail_status' => 'DISABLED'
  1297. ]);
  1298. // 产生一条退款记录 (pay_detail)
  1299. PayDetail::insert([
  1300. 'join_pay_member_id' => $memberId,
  1301. 'join_pay_order_id' => 'ROLE_REFUND',
  1302. 'join_pay_object_json' => '[]',
  1303. 'pay_status' => 'SUCCESS',
  1304. 'pay_category' => 'ROLE_REFUND',
  1305. 'pay_amount' => $amount,
  1306. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1307. 'pay_prepayid' => $memberId . '-CASH',
  1308. 'pay_addtimes' => time()
  1309. ]);
  1310. Db::commit();
  1311. return json_success('success');
  1312. } catch (BusinessException $e) {
  1313. Db::rollBack();
  1314. return json_fail($e->getMessage());
  1315. } catch (\Exception $e) {
  1316. Db::rollBack();
  1317. return json_fail('退款失败');
  1318. }
  1319. }
  1320. public function crownContinue(Request $request)
  1321. {
  1322. $memberId = $request->post('member_id');
  1323. $dataTime = $request->post('datetime', date('Y-m-d'));
  1324. $sendAll = $request->post('send_all', false);
  1325. $orderId = $request->post('order_id');
  1326. if (!$memberId || !$orderId) {
  1327. return json_fail('参数异常');
  1328. }
  1329. $member = Member::where('member_id', $memberId)->first();
  1330. try {
  1331. Db::beginTransaction();
  1332. $roleConfigJson = [];
  1333. if (!empty($member->member_role_config_json)) {
  1334. $roleConfigJson = json_decode($member->member_role_config_json, true);
  1335. if (!empty($roleConfigJson['next'])) {
  1336. unset($roleConfigJson['next']);
  1337. }
  1338. }
  1339. $roleConfigJson['deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($dataTime . ' + 364 days'));
  1340. $member->member_role_config_json = json_encode($roleConfigJson);
  1341. $member->member_role_begintime = date('Y-m-d 00:00:00', strtotime($dataTime));
  1342. $memberRoleId = MemberRole::where('member_role_category', 'COMBINE')
  1343. ->where('member_role_status', 'ACTIVED')
  1344. ->where('member_role_name', 'like', '%皇冠%')
  1345. ->value('member_role_id');
  1346. $member->join_member_role_id = $memberRoleId;
  1347. $member->save();
  1348. if ($sendAll === true) {
  1349. $payDetail = PayDetail::whereJsonContains('join_pay_object_json->order_id', $orderId)
  1350. ->first();
  1351. if (!empty($payDetail->pay_extend_json)) {
  1352. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  1353. $account = MemberAccount::where('join_account_member_id', $memberId)
  1354. ->where('member_account_classify', 'CASH')
  1355. ->first();
  1356. $account->member_account_added = $account->member_account_added + $payExtendJson['added_amount'];
  1357. $account->save();
  1358. }
  1359. // 产生一条支付记录
  1360. PayDetail::insert([
  1361. 'join_pay_member_id' => $memberId,
  1362. 'join_pay_order_id' => 'CROWN_CONTINUE',
  1363. 'join_pay_object_json' => '[]',
  1364. 'pay_status' => 'SUCCESS',
  1365. 'pay_category' => 'CROWN_CONTINUE',
  1366. 'pay_amount' => 0,
  1367. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1368. 'pay_prepayid' => $memberId . '-CASH',
  1369. 'pay_extend_json' => $payDetail->pay_extend_json,
  1370. 'pay_addtimes' => time()
  1371. ]);
  1372. $goodsAttributeJson = RechargeService::getGoodsAttributeJson($orderId);
  1373. // 发券
  1374. $couponParams['order_id'] = $orderId;
  1375. RechargeService::disposeRoleCoupon($goodsAttributeJson, $couponParams, $memberId);
  1376. }
  1377. Db::commit();
  1378. return json_success('success');
  1379. } catch (\Exception $e) {
  1380. dump($e->getTrace());
  1381. Db::rollBack();
  1382. Log::error("续期失败:" . $e->getMessage());
  1383. return json_fail("续期失败");
  1384. }
  1385. }
  1386. }