MemberController.php 60 KB

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