MemberController.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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/4/7 10:20
  504. *
  505. * @param $id
  506. * @return \support\Response
  507. */
  508. public function device($id)
  509. {
  510. return MemberService::deviceList($id);
  511. }
  512. public function addDevice(Request $request)
  513. {
  514. return MemberService::addDevice($request);
  515. }
  516. public function updateDevice(Request $request)
  517. {
  518. return MemberService::updateDevice($request);
  519. }
  520. public function deleteDevice(Request $request)
  521. {
  522. return MemberService::deleteDevice($request);
  523. }
  524. /**
  525. * @Desc 卡券数量
  526. * @Author Gorden
  527. * @Date 2024/8/22 18:51
  528. *
  529. * @param Request $request
  530. * @return \support\Response
  531. */
  532. public function couponCount(Request $request)
  533. {
  534. $memberId = $request->get('member_id', '');
  535. $coupons = CouponDetail::leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  536. ->where('join_coupon_detail_member_id', $memberId)
  537. ->select('coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  538. ->get()
  539. ->toArray();
  540. $data = ['manjian' => 0, 'diyong' => 0, 'zhekou' => 0, 'lijian' => 0, 'zengpin' => 0, 'fuli' => 0, 'nianka' => 0, 'jika' => 0, 'yueka' => 0];
  541. foreach ($coupons as $item) {
  542. if ($item['coupon_classify'] == '满减券') {
  543. $data['manjian'] += 1;
  544. } elseif ($item['coupon_classify'] == '抵用券') {
  545. $data['diyong'] += 1;
  546. } elseif ($item['coupon_classify'] == '折扣券') {
  547. $data['zhekou'] += 1;
  548. } elseif ($item['coupon_classify'] == '立减券') {
  549. $data['lijian'] += 1;
  550. } elseif ($item['coupon_classify'] == '赠品券') {
  551. $data['zengpin'] += 1;
  552. } elseif ($item['coupon_classify'] == '福利券') {
  553. $data['fuli'] += 1;
  554. } elseif ($item['coupon_classify'] == '年卡') {
  555. $data['nianka'] += 1;
  556. } elseif ($item['coupon_classify'] == '季卡') {
  557. $data['jika'] += 1;
  558. } elseif ($item['coupon_classify'] == '月卡') {
  559. $data['yueka'] += 1;
  560. }
  561. }
  562. $data['total'] = $data['manjian'] + $data['diyong'] + $data['zhekou'] + $data['lijian'] + $data['zengpin'] + $data['fuli'] + $data['nianka'] + $data['jika'] + $data['yueka'];
  563. return json_success('', $data);
  564. }
  565. /**
  566. * @Desc 我的卡券
  567. * @Author Gorden
  568. * @Date 2024/8/22 18:45
  569. *
  570. * @param Request $request
  571. * @return \support\Response
  572. */
  573. public function coupon(Request $request)
  574. {
  575. $memberId = $request->get('member_id', '');
  576. $classify = $request->get('classify', '');
  577. $page = $request->get('page', 1);
  578. $pageSize = $request->get('pageSize', 20);
  579. $coupons = CouponDetail::with([
  580. 'member' => function ($query) {
  581. $query->select('member_id', 'member_mobile');
  582. },
  583. 'cert' => function ($query) {
  584. $query->select('join_cert_member_id', 'member_cert_name');
  585. },
  586. 'info' => function ($query) {
  587. $query->select('join_info_member_id', 'member_info_nickname');
  588. }
  589. ])
  590. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  591. ->where('join_coupon_detail_member_id', $memberId)
  592. ->where('coupon.coupon_classify', $classify)
  593. ->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  594. // $total = $coupons->count();
  595. $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");
  596. $total = 0;
  597. if (!empty($totalModel) && !empty($totalModel[0])) {
  598. $total = $totalModel[0]->total;
  599. }
  600. $rows = $coupons->select('coupon_detail.join_coupon_detail_member_id', 'coupon_detail.join_detail_coupon_id'
  601. , 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  602. ->selectRaw('COUNT(1) as total,
  603. COUNT(IF(app_coupon_detail.coupon_detail_status="USED",1,NULL)) as used_total,
  604. COUNT(IF(app_coupon_detail.coupon_detail_status="ACTIVED" or app_coupon_detail.coupon_detail_status="WAITING",1,NULL)) as unused_total,
  605. MAX(app_coupon_detail.coupon_detail_deadline_datetime) as deadline_datetime')
  606. ->orderBy('deadline_datetime', 'DESC')
  607. ->forPage($page, $pageSize)
  608. ->get()
  609. ->toArray();
  610. foreach ($rows as &$item) {
  611. $mobile = $certName = $nickname = '';
  612. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  613. $mobile = $item['member']['member_mobile'];
  614. }
  615. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  616. $certName = $item['cert']['member_cert_name'];
  617. }
  618. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  619. $nickname = $item['info']['member_info_nickname'];
  620. }
  621. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  622. unset($item['member'], $item['cert'], $item['info']);
  623. $item['invalid_total'] = $item['total'] - $item['used_total'] - $item['unused_total'];
  624. // 优惠券分类
  625. $coupon = Coupon::with('category')->where('coupon_id', $item['join_detail_coupon_id'])
  626. ->select('coupon_id', 'join_coupon_category_id')
  627. ->first();
  628. if (!empty($coupon) && !empty($coupon->category)) {
  629. $item['category_name'] = $coupon->category->category_name;
  630. }
  631. }
  632. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  633. }
  634. /**
  635. * @Desc 我的卡券
  636. * @Author Gorden
  637. * @Date 2024/8/22 18:45
  638. *
  639. * @param Request $request
  640. * @return \support\Response
  641. */
  642. public function allCoupon(Request $request)
  643. {
  644. $memberId = $request->get('member_id', '');
  645. $page = $request->get('page', 1);
  646. $pageSize = $request->get('pageSize', 20);
  647. $coupons = CouponDetail::with([
  648. 'member' => function ($query) {
  649. $query->select('member_id', 'member_mobile');
  650. },
  651. 'cert' => function ($query) {
  652. $query->select('join_cert_member_id', 'member_cert_name');
  653. },
  654. 'info' => function ($query) {
  655. $query->select('join_info_member_id', 'member_info_nickname');
  656. }
  657. ])
  658. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  659. ->where('join_coupon_detail_member_id', $memberId)
  660. ->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  661. // $total = $coupons->count();
  662. $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");
  663. $total = 0;
  664. if (!empty($totalModel) && !empty($totalModel[0])) {
  665. $total = $totalModel[0]->total;
  666. }
  667. $rows = $coupons->select('coupon_detail.join_coupon_detail_member_id', 'coupon_detail.join_detail_coupon_id'
  668. , 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  669. ->selectRaw('COUNT(1) as total,
  670. COUNT(IF(app_coupon_detail.coupon_detail_status="USED",1,NULL)) as used_total,
  671. COUNT(IF(app_coupon_detail.coupon_detail_status="ACTIVED" or app_coupon_detail.coupon_detail_status="WAITING",1,NULL)) as unused_total,
  672. MAX(app_coupon_detail.coupon_detail_deadline_datetime) as deadline_datetime')
  673. ->orderBy('deadline_datetime', 'DESC')
  674. ->forPage($page, $pageSize)
  675. ->get()
  676. ->toArray();
  677. foreach ($rows as &$item) {
  678. $mobile = $certName = $nickname = '';
  679. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  680. $mobile = $item['member']['member_mobile'];
  681. }
  682. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  683. $certName = $item['cert']['member_cert_name'];
  684. }
  685. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  686. $nickname = $item['info']['member_info_nickname'];
  687. }
  688. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  689. unset($item['member'], $item['cert'], $item['info']);
  690. $item['invalid_total'] = $item['total'] - $item['used_total'] - $item['unused_total'];
  691. // 优惠券分类
  692. $coupon = Coupon::with('category')->where('coupon_id', $item['join_detail_coupon_id'])
  693. ->select('coupon_id', 'join_coupon_category_id')
  694. ->first();
  695. if (!empty($coupon) && !empty($coupon->category)) {
  696. $item['category_name'] = $coupon->category->category_name;
  697. }
  698. }
  699. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  700. }
  701. /**
  702. * @Desc 我的卡券 领取记录
  703. * @Author Gorden
  704. * @Date 2024/8/22 18:45
  705. *
  706. * @param Request $request
  707. * @return \support\Response
  708. */
  709. public function couponDetail(Request $request)
  710. {
  711. $memberId = $request->get('member_id', '');
  712. $couponId = $request->get('coupon_id', '');
  713. $page = $request->get('page', 1);
  714. $pageSize = $request->get('pageSize', 20);
  715. $coupons = CouponDetail::with([
  716. 'member' => function ($query) {
  717. $query->select('member_id', 'member_mobile');
  718. },
  719. 'cert' => function ($query) {
  720. $query->select('join_cert_member_id', 'member_cert_name');
  721. },
  722. 'info' => function ($query) {
  723. $query->select('join_info_member_id', 'member_info_nickname');
  724. }
  725. ])
  726. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  727. ->where('join_coupon_detail_member_id', $memberId)
  728. ->where('coupon.coupon_id', $couponId);
  729. $total = $coupons->count();
  730. $rows = $coupons->select('coupon_detail.*', 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  731. ->orderBy('coupon_detail_deadline_datetime', 'desc')
  732. ->forPage($page, $pageSize)
  733. ->get()
  734. ->toArray();
  735. foreach ($rows as &$item) {
  736. $mobile = $certName = $nickname = '';
  737. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  738. $mobile = $item['member']['member_mobile'];
  739. }
  740. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  741. $certName = $item['cert']['member_cert_name'];
  742. }
  743. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  744. $nickname = $item['info']['member_info_nickname'];
  745. }
  746. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  747. unset($item['member'], $item['cert'], $item['info']);
  748. if (!empty($coupon) && !empty($coupon->category)) {
  749. $item['category_name'] = $coupon->category->category_name;
  750. }
  751. if (!empty($item['coupon_detail_gain_datetime'])) {
  752. $item['coupon_detail_gain_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_gain_datetime']));
  753. }
  754. if (!empty($item['coupon_detail_deadline_datetime'])) {
  755. $item['coupon_detail_deadline_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_deadline_datetime']));
  756. }
  757. }
  758. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  759. }
  760. /**
  761. * @Desc 新会员充值备份记录
  762. * @Author Gorden
  763. * @Date 2024/10/15 10:27
  764. *
  765. * @param Request $request
  766. * @return \support\Response
  767. */
  768. public function rechargeBackupsList(Request $request)
  769. {
  770. $page = $request->get('page', 1);
  771. $pageSize = $request->get('pageSize', 20);
  772. $memberId = $request->get('member_id');
  773. if (!$memberId) {
  774. return json_fail("参数异常");
  775. }
  776. $member = Member::where('member_id', $memberId)
  777. ->select('member_id', 'member_extend_json')
  778. ->first();
  779. $data = [];
  780. if (!empty($member->member_extend_json)) {
  781. $extendJson = json_decode($member->member_extend_json, true);
  782. if (isset($extendJson['snap'])) {
  783. foreach ($extendJson['snap'] as $key => $item) {
  784. $data[] = [
  785. 'recharge_times' => $key,
  786. 'member_account_surplus' => $item['member_account_surplus'],
  787. 'member_account_added' => $item['member_account_added'],
  788. 'pay_amount' => $item['pay_amount'],
  789. 'added_rate' => floatval($item['added_rate']) * 100,
  790. 'added_amount' => $item['added_amount']
  791. ];
  792. }
  793. }
  794. }
  795. $total = count($data);
  796. $start = ($page - 1) * $pageSize;
  797. $rows = array_slice($data, $start, $pageSize);
  798. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  799. }
  800. /**
  801. * @Desc 会员权益使用记录
  802. * @Author Gorden
  803. * @Date 2024/10/15 11:13
  804. *
  805. * @param Request $request
  806. * @return \support\Response
  807. */
  808. public function roleBenefitUsedList(Request $request)
  809. {
  810. $page = $request->get('page', 1);
  811. $pageSize = $request->get('pageSize', 20);
  812. $memberId = $request->get('member_id');
  813. if (!$memberId) {
  814. return json_fail('参数异常');
  815. }
  816. $data = $this->getRoleBenefitUsedList($memberId);
  817. $total = count($data);
  818. $start = ($page - 1) * $pageSize;
  819. $rows = array_slice($data, $start, $pageSize);
  820. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  821. }
  822. public function consumptionList(Request $request)
  823. {
  824. $page = $request->get('page', 1);
  825. $pageSize = $request->get('pageSize', 20);
  826. $memberId = $request->get('member_id');
  827. if (!$memberId) {
  828. return json_fail('参数异常');
  829. }
  830. $member = Member::where('member_id', $memberId)->first();
  831. $firstRechargeTime = 0;
  832. if (!empty($member->member_extend_json)) {
  833. $memberExtendJson = json_decode($member->member_extend_json, true);
  834. if (isset($memberExtendJson['snap'])) {
  835. foreach ($memberExtendJson['snap'] as $key => $value) {
  836. $firstRechargeTime = strtotime($key);
  837. break;
  838. }
  839. }
  840. }
  841. $payDetailIds = PayDetail::where('pay_addtimes', '>', $firstRechargeTime)
  842. ->where('pay_prepayid', $memberId . '-CASH')
  843. ->where('pay_status', 'SUCCESS')
  844. ->pluck('join_pay_order_id')
  845. ->toArray();
  846. $orders = Order::with([
  847. 'payDetail' => function ($query) {
  848. $query->where('pay_prepayid', 'like', '%CASH%');
  849. }
  850. ])->whereIn('order_groupby', $payDetailIds)
  851. ->where('order_addtimes', '>', $firstRechargeTime)
  852. ->whereIn('order_classify', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'DISHES']);
  853. $total = $orders->count();
  854. $rows = $orders->orderBy('order_addtimes', 'DESC')
  855. ->forPage($page, $pageSize)
  856. ->get()
  857. ->toArray();
  858. $data = [];
  859. foreach ($rows as $row) {
  860. $sheets = OrderSheet::with([
  861. 'goods' => function ($query) {
  862. $query->select('goods_id', 'goods_name');
  863. }
  864. ])->where('join_sheet_order_id', $row['order_id'])
  865. ->get()
  866. ->toArray();
  867. $deng = "";
  868. if (count($sheets) > 1) {
  869. $deng = '等';
  870. }
  871. foreach ($row['pay_detail'] as $item) {
  872. if (!empty($item['join_pay_object_json'])) {
  873. $payObjectJson = json_decode($item['join_pay_object_json'], true);
  874. if ($payObjectJson['order_id'] != $row['order_id']) {
  875. continue;
  876. }
  877. }
  878. $data[] = [
  879. 'order_classify' => $row['order_classify'],
  880. 'order_id' => $row['order_id'],
  881. 'goods_name' => !empty($sheets[0]) && !empty($sheets[0]['goods']) ? ($sheets[0]['goods']['goods_name'] . $deng) : '',
  882. 'order_amount_total' => $row['order_amount_total'],
  883. 'order_amount_pay' => $row['order_amount_pay'],
  884. 'pay_paytimes' => $item['pay_paytimes'] ?? "",
  885. 'order_addtimes' => $row['order_addtimes'],
  886. 'pay_amount' => $item['pay_amount']
  887. ];
  888. }
  889. }
  890. $rows = $data;
  891. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  892. }
  893. /**
  894. * @Desc 消费导出
  895. * @Author Gorden
  896. * @Date 2024/10/15 17:36
  897. *
  898. * @param Request $request
  899. * @return \support\Response
  900. */
  901. public function exportConsumptionList(Request $request)
  902. {
  903. $memberId = $request->post('member_id');
  904. $orderIds = $request->post('order_id', []);
  905. if (!$memberId) {
  906. return json_fail('参数异常');
  907. }
  908. $member = Member::where('member_id', $memberId)->first();
  909. $firstRechargeTime = 0;
  910. if (!empty($member->member_extend_json)) {
  911. $memberExtendJson = json_decode($member->member_extend_json, true);
  912. if (isset($memberExtendJson['snap'])) {
  913. foreach ($memberExtendJson['snap'] as $key => $value) {
  914. $firstRechargeTime = strtotime($key);
  915. break;
  916. }
  917. }
  918. }
  919. if (empty($orderIds)) {
  920. $payDetailIds = PayDetail::where('pay_addtimes', '>', $firstRechargeTime)
  921. ->where('pay_prepayid', $memberId . '-CASH')
  922. ->where('pay_status', 'SUCCESS')
  923. ->pluck('join_pay_order_id')
  924. ->toArray();
  925. } else {
  926. $payDetailIds = Order::whereIn('order_id', $orderIds)->pluck('order_groupby')->toArray();
  927. }
  928. $rows = Order::with([
  929. 'payDetail' => function ($query) {
  930. $query->where('pay_prepayid', 'like', '%CASH%');
  931. }
  932. ])->whereIn('order_groupby', $payDetailIds)
  933. ->where('order_addtimes', '>', $firstRechargeTime)
  934. ->whereIn('order_classify', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'DISHES'])
  935. ->orderBy('order_addtimes', 'DESC')
  936. ->get()
  937. ->toArray();
  938. $data = [];
  939. $payAmount = 0;
  940. foreach ($rows as $row) {
  941. $sheets = OrderSheet::with([
  942. 'goods' => function ($query) {
  943. $query->select('goods_id', 'goods_name');
  944. }
  945. ])->where('join_sheet_order_id', $row['order_id'])
  946. ->get()
  947. ->toArray();
  948. $deng = "";
  949. if (count($sheets) > 1) {
  950. $deng = '等';
  951. }
  952. foreach ($row['pay_detail'] as $item) {
  953. if (!empty($item['join_pay_object_json'])) {
  954. $payObjectJson = json_decode($item['join_pay_object_json'], true);
  955. if ($payObjectJson['order_id'] != $row['order_id']) {
  956. continue;
  957. }
  958. }
  959. $payAmount += $item['pay_amount'];
  960. $data[] = [
  961. 'order_classify' => OrderService::$orderClassify[$row['order_classify']],
  962. 'order_id' => $row['order_id'],
  963. 'goods_name' => !empty($sheets[0]) && !empty($sheets[0]['goods']) ? ($sheets[0]['goods']['goods_name'] . $deng) : '',
  964. 'order_amount_total' => $row['order_amount_total'],
  965. 'order_amount_pay' => $row['order_amount_pay'],
  966. 'pay_paytimes' => $item['pay_paytimes'] ?? "",
  967. 'order_addtimes' => $row['order_addtimes'],
  968. 'pay_amount' => $item['pay_amount']
  969. ];
  970. }
  971. }
  972. $data[] = ['pay_amount' => "总计:" . sprintf('%.2f', $payAmount)];
  973. return json_success('success', $data);
  974. }
  975. /**
  976. * @Desc 获取权益使用记录
  977. * @Author Gorden
  978. * @Date 2024/10/15 11:35
  979. *
  980. * @param $memberId
  981. * @param $orderIds
  982. * @return array
  983. */
  984. public function getRoleBenefitUsedList($memberId, $orderIds = [])
  985. {
  986. $couponDetails = CouponDetail::where('join_coupon_detail_member_id', $memberId)
  987. ->where('coupon_detail_status', 'USED')
  988. ->whereJsonContains('coupon_detail_extend_json->gettype', 'ROLE')
  989. ->get()
  990. ->toArray();
  991. $data = [];
  992. foreach ($couponDetails as $couponDetail) {
  993. $orders = Order::where('order_discount_json', 'like', '%' . $couponDetail['coupon_detail_id'] . '%')
  994. ->whereIn('order_is_complete', ['Y', 'N'])
  995. ->where('order_status_payment', 'SUCCESS')
  996. ->when(!empty($orderIds), function ($query) use ($orderIds) {
  997. $query->whereIn('order_id', $orderIds);
  998. })->get()
  999. ->toArray();
  1000. foreach ($orders as $order) {
  1001. $sheets = OrderSheet::with([
  1002. 'goods' => function ($query) {
  1003. $query->select('goods_id', 'goods_name');
  1004. }
  1005. ])->where('join_sheet_order_id', $order['order_id'])
  1006. ->get()
  1007. ->toArray();
  1008. $deng = "";
  1009. if (count($sheets) > 1) {
  1010. $deng = '等';
  1011. }
  1012. $discountJson = json_decode($order['order_discount_json'], true);
  1013. $couponName = '';
  1014. $discountAmount = 0;
  1015. foreach ($discountJson as $item) {
  1016. $discountAmount = $item['coupon_value'];
  1017. $couponName = Coupon::where('coupon_id', $item['coupon_id'])->value('coupon_name');
  1018. }
  1019. $payDetailTimes = PayDetail::whereJsonContains('join_pay_object_json->order_id', $order['order_id'])->value('pay_paytimes');
  1020. $data[] = [
  1021. 'order_id' => $order['order_id'],
  1022. 'goods_name' => !empty($sheets[0]) && !empty($sheets[0]['goods']) ? ($sheets[0]['goods']['goods_name'] . $deng) : '',
  1023. 'order_classify' => $order['order_classify'],
  1024. 'order_amount_total' => $order['order_amount_total'],
  1025. 'order_amount_pay' => $order['order_amount_pay'],
  1026. 'coupon_name' => $couponName,
  1027. 'discountAmount' => $discountAmount,
  1028. 'order_status_system' => $order['order_status_system'],
  1029. 'order_addtimes' => $order['order_addtimes'],
  1030. 'pay_paytimes' => $payDetailTimes
  1031. ];
  1032. }
  1033. }
  1034. return $data;
  1035. }
  1036. public function roleBenefitExport(Request $request)
  1037. {
  1038. $orderIds = $request->post('order_id', []);
  1039. $memberId = $request->post('member_id');
  1040. $applyUsername = $request->post('apply_username', '');
  1041. $applyMobile = $request->post('apply_mobile', '');
  1042. $applyAmount = $request->post('apply_amount', '');
  1043. if (!$memberId) {
  1044. return json_fail("参数异常");
  1045. }
  1046. $data = $this->getRoleBenefitUsedList($memberId, $orderIds);
  1047. $dicountAmount = 0;
  1048. foreach ($data as $key => $item) {
  1049. $data[$key]['order_classify'] = OrderService::$orderClassify[$item['order_classify']];
  1050. $dicountAmount += $item['discountAmount'];
  1051. }
  1052. $returnApplayData['data'] = $data;
  1053. $returnApplayData['amount'] = $applyAmount;
  1054. $returnApplayData['apply_username'] = $applyUsername;
  1055. $returnApplayData['apply_mobile'] = $applyMobile;
  1056. $returnApplayData['reason'] = "会员权益退款";
  1057. $data[] = [
  1058. 'discountAmount' => '优惠合计:¥' . sprintf('%.2f', $dicountAmount)
  1059. ];
  1060. // $data[] = [
  1061. // 'order_classify' => '申请人:' . $applyUsername,
  1062. // ];
  1063. // $data[] = [
  1064. // 'order_classify' => '电话:' . $applyMobile,
  1065. // ];
  1066. // $data[] = [
  1067. // 'order_classify' => '退款金额:' . $applyAmount,
  1068. // ];
  1069. // 产生退款记录
  1070. // $this->refundOrder($memberId, $returnApplayData);
  1071. return json_success('success', $data);
  1072. }
  1073. public function refundOrder(Request $request)
  1074. {
  1075. $params = $request->post();
  1076. Db::beginTransaction();
  1077. try {
  1078. // OrderReturn::where('join_return_member_id', $params['member_id'])
  1079. // ->where('order_return_status', '<>', 'DONE')
  1080. // ->where('order_return_category', '会员退款')
  1081. // ->delete();
  1082. // 验证余额
  1083. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])
  1084. ->where('member_account_classify', 'CASH')
  1085. ->first();
  1086. if ($params['surplus'] + $params['bank_amount'] > $memberAccount->member_account_surplus) {
  1087. throw new BusinessException("本金退款金额超出账户本金余额");
  1088. }
  1089. if ($params['added'] > $memberAccount->member_account_added) {
  1090. throw new BusinessException("赠送退款金额超出账户赠送余额");
  1091. }
  1092. $returnApplyJson = [
  1093. 'reason' => '会员退款',
  1094. 'order_id' => 'ROLE_REFUND',
  1095. 'classify' => 'ROLE_REFUND',
  1096. 'username' => $params['username'],
  1097. 'mobile' => $params['mobile'],
  1098. 'surplus' => $params['surplus'],
  1099. 'added' => $params['added'],
  1100. 'amount' => $params['bank_amount'],
  1101. 'bank_amount' => $params['bank_amount'],
  1102. 'bank_name' => $params['bank_name'],
  1103. 'bank_acount' => $params['bank_acount'],
  1104. 'bank_cardno' => $params['bank_cardno']
  1105. ];
  1106. // 退款记录
  1107. OrderReturn::insert([
  1108. 'join_order_return_user_id' => JwtToken::getCurrentId(),
  1109. 'join_return_member_id' => $params['member_id'],
  1110. 'join_return_order_id' => 'ROLE_REFUND',
  1111. 'order_return_status' => 'PENDING',
  1112. 'order_return_category' => '会员退款',
  1113. 'order_return_apply_datetime' => date('Y-m-d H:i:s'),
  1114. 'order_return_apply_json' => json_encode($returnApplyJson),
  1115. 'order_return_remark' => $params['remark'] ?? '',
  1116. 'order_return_addtimes' => time()
  1117. ]);
  1118. Db::commit();
  1119. return json_success("success");
  1120. } catch (BusinessException $e) {
  1121. Db::rollBack();
  1122. Log::error("创建退款记录失败:" . $e->getMessage());
  1123. return json_fail($e->getMessage());
  1124. } catch (\Exception $e) {
  1125. Db::rollBack();
  1126. Log::error("创建退款记录失败:" . $e->getMessage());
  1127. return json_fail("创建退款记录失败");
  1128. }
  1129. }
  1130. public function roleBenefitRefund(Request $request)
  1131. {
  1132. $memberId = $request->post('member_id');
  1133. if (!$memberId) {
  1134. return json_fail('参数异常');
  1135. }
  1136. $returnOrder = OrderReturn::where('join_return_member_id', $memberId)
  1137. ->where('order_return_status', 'DOING')
  1138. ->where('order_return_category', '会员权益退款')
  1139. ->first();
  1140. if (!$returnOrder) {
  1141. return json_fail("退款数据异常");
  1142. }
  1143. $memberAccount = MemberAccount::where('join_account_member_id', $memberId)
  1144. ->where('member_account_classify', 'CASH')
  1145. ->where('member_account_status', 'ACTIVED')
  1146. ->first();
  1147. if (!$memberAccount) {
  1148. return json_fail('会员账户异常');
  1149. }
  1150. try {
  1151. Db::beginTransaction();
  1152. $refundApplyJson = json_decode($returnOrder->order_return_apply_json, true);
  1153. $amount = $refundApplyJson['amount'];
  1154. $refundApplyJson['user_id'] = JwtToken::getCurrentId();
  1155. $refundApplyJson['datetime'] = date('Y-m-d H:i:s');
  1156. $returnOrder->order_return_refund_json = json_encode($refundApplyJson);
  1157. $returnOrder->order_return_status = 'DONE';
  1158. $returnOrder->save();
  1159. // 账户剩余
  1160. if ($memberAccount->member_account_surplus + $memberAccount->member_account_added < $amount) {
  1161. throw new BusinessException("账户余额不足");
  1162. }
  1163. // 本金
  1164. if ($memberAccount->member_account_surplus >= $amount) {
  1165. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus - $amount;
  1166. } else {
  1167. $memberAccount->member_account_added = $memberAccount->member_account_added - ($amount - $memberAccount->member_account_surplus);
  1168. $memberAccount->member_account_surplus = 0;
  1169. }
  1170. $memberAccount->member_account_update_user_id = JwtToken::getCurrentId();
  1171. $memberAccount->member_account_updatetimes = time();
  1172. $memberAccount->save();
  1173. // 删除会员身份
  1174. Member::where('member_id', $memberId)
  1175. ->update([
  1176. 'join_member_role_id' => '',
  1177. 'member_role_begintime' => '',
  1178. 'member_role_config_json' => '[]'
  1179. ]);
  1180. // 没用的优惠券失效
  1181. CouponDetail::where('join_coupon_detail_member_id', $memberId)
  1182. ->where('coupon_detail_status', '<>', 'USED')
  1183. ->whereJsonContains('coupon_detail_extend_json->gettype', 'ROLE')
  1184. ->update([
  1185. 'coupon_detail_status' => 'DISABLED'
  1186. ]);
  1187. // 产生一条退款记录 (pay_detail)
  1188. PayDetail::insert([
  1189. 'join_pay_member_id' => $memberId,
  1190. 'join_pay_order_id' => 'ROLE_REFUND',
  1191. 'join_pay_object_json' => '[]',
  1192. 'pay_status' => 'SUCCESS',
  1193. 'pay_category' => 'ROLE_REFUND',
  1194. 'pay_amount' => $amount,
  1195. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1196. 'pay_prepayid' => $memberId . '-CASH',
  1197. 'pay_addtimes' => time()
  1198. ]);
  1199. Db::commit();
  1200. return json_success('success');
  1201. } catch (BusinessException $e) {
  1202. Db::rollBack();
  1203. return json_fail($e->getMessage());
  1204. } catch (\Exception $e) {
  1205. Db::rollBack();
  1206. return json_fail('退款失败');
  1207. }
  1208. }
  1209. public function crownContinue(Request $request)
  1210. {
  1211. $memberId = $request->post('member_id');
  1212. $dataTime = $request->post('datetime', date('Y-m-d'));
  1213. $sendAll = $request->post('send_all', false);
  1214. $orderId = $request->post('order_id');
  1215. if (!$memberId || !$orderId) {
  1216. return json_fail('参数异常');
  1217. }
  1218. $member = Member::where('member_id', $memberId)->first();
  1219. try {
  1220. Db::beginTransaction();
  1221. $roleConfigJson = [];
  1222. if (!empty($member->member_role_config_json)) {
  1223. $roleConfigJson = json_decode($member->member_role_config_json, true);
  1224. if (!empty($roleConfigJson['next'])) {
  1225. unset($roleConfigJson['next']);
  1226. }
  1227. }
  1228. $roleConfigJson['deadline_datetime'] = date('Y-m-d 23:59:59', strtotime($dataTime . ' + 364 days'));
  1229. $member->member_role_config_json = json_encode($roleConfigJson);
  1230. $member->member_role_begintime = date('Y-m-d 00:00:00', strtotime($dataTime));
  1231. $memberRoleId = MemberRole::where('member_role_category', 'COMBINE')
  1232. ->where('member_role_status', 'ACTIVED')
  1233. ->where('member_role_name', 'like', '%皇冠%')
  1234. ->value('member_role_id');
  1235. $member->join_member_role_id = $memberRoleId;
  1236. $member->save();
  1237. if ($sendAll === true) {
  1238. $payDetail = PayDetail::whereJsonContains('join_pay_object_json->order_id', $orderId)
  1239. ->first();
  1240. if (!empty($payDetail->pay_extend_json)) {
  1241. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  1242. $account = MemberAccount::where('join_account_member_id', $memberId)
  1243. ->where('member_account_classify', 'CASH')
  1244. ->first();
  1245. $account->member_account_added = $account->member_account_added + $payExtendJson['added_amount'];
  1246. $account->save();
  1247. }
  1248. // 产生一条支付记录
  1249. PayDetail::insert([
  1250. 'join_pay_member_id' => $memberId,
  1251. 'join_pay_order_id' => 'CROWN_CONTINUE',
  1252. 'join_pay_object_json' => '[]',
  1253. 'pay_status' => 'SUCCESS',
  1254. 'pay_category' => 'CROWN_CONTINUE',
  1255. 'pay_amount' => 0,
  1256. 'pay_paytimes' => date('Y-m-d H:i:s'),
  1257. 'pay_prepayid' => $memberId . '-CASH',
  1258. 'pay_extend_json' => $payDetail->pay_extend_json,
  1259. 'pay_addtimes' => time()
  1260. ]);
  1261. $goodsAttributeJson = RechargeService::getGoodsAttributeJson($orderId);
  1262. // 发券
  1263. $couponParams['order_id'] = $orderId;
  1264. RechargeService::disposeRoleCoupon($goodsAttributeJson, $couponParams, $memberId);
  1265. }
  1266. Db::commit();
  1267. return json_success('success');
  1268. } catch (\Exception $e) {
  1269. dump($e->getTrace());
  1270. Db::rollBack();
  1271. Log::error("续期失败:" . $e->getMessage());
  1272. return json_fail("续期失败");
  1273. }
  1274. }
  1275. }