MemberController.php 61 KB

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