MemberController.php 57 KB

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