MemberController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\member\MemberService;
  4. use app\admin\validate\member\MemberValidate;
  5. use app\model\Family;
  6. use app\model\FamilyMember;
  7. use app\model\Member;
  8. use app\model\MemberAccount;
  9. use app\model\MemberBenefit;
  10. use app\model\MemberQuota;
  11. use app\model\PayDetail;
  12. use support\Db;
  13. use support\Request;
  14. class MemberController
  15. {
  16. public function selectList(Request $request)
  17. {
  18. $keywords = $request->get('keywords');
  19. $isOwner = $request->get('member_is_owner', 'N');
  20. if (!$keywords) {
  21. return json_fail("查询错误");
  22. }
  23. $res = Db::table('member')
  24. ->leftjoin('member_cert', 'member.member_id', '=', 'member_cert.join_cert_member_id')
  25. ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name')
  26. ->where('member.member_is_owner', $isOwner)
  27. ->where(function ($query) use ($keywords) {
  28. $query->where('member.member_mobile', 'like', '%' . $keywords . '%')
  29. ->orWhere("member.member_id", 'like', '%' . $keywords . '%')
  30. ->orWhere("member_cert.member_cert_name", 'like', '%' . $keywords . '%');
  31. })->get();
  32. $data = [];
  33. foreach ($res as $item) {
  34. $data[] = [
  35. 'key' => $item->member_id,
  36. 'value' => !empty($item->member_cert_name) ? $item->member_cert_name . '-' . $item->member_mobile : $item->member_mobile,
  37. ];
  38. }
  39. return json_success('', $data);
  40. }
  41. public function exportMember(Request $request){
  42. return MemberService::exportMember($request);
  43. }
  44. public function selectWriteOffMember()
  45. {
  46. $members = Member::with([
  47. 'cert' => function ($query) {
  48. $query->select('join_cert_member_id', 'member_cert_name');
  49. }
  50. ])->whereRaw("JSON_EXTRACT(`member_json`, '$.user') IS NOT NULL")
  51. ->select('member_id', 'member_mobile', 'member_json')
  52. ->get()
  53. ->toArray();
  54. foreach ($members as &$member) {
  55. $member['member_json'] = json_decode($member['member_json'], true);
  56. $member['username'] = $member['member_mobile'];
  57. $member['user_id'] = $member['member_json']['user'] ? $member['member_json']['user']['user_id'] : '';
  58. if (isset($member['cert']['member_cert_name'])) {
  59. $member['username'] = $member['cert']['member_cert_name'] . '-' . $member['username'];
  60. }
  61. }
  62. return json_success('', $members);
  63. }
  64. public function selectFamilyMemberList(Request $request)
  65. {
  66. $keywords = $request->get('keywords');
  67. if (!$keywords) {
  68. return json_fail("查询错误");
  69. }
  70. // 创建的家庭
  71. $family = Family::where('join_family_creator_member_id', $request->get('memberId'))->get()->toArray();
  72. if (!$family) {
  73. return json_success('暂无数据');
  74. }
  75. $familyIds = array_column($family, 'family_id');
  76. $res = FamilyMember::whereIn('join_family_id', $familyIds)
  77. ->where("family_member_name", 'like', '%' . $keywords . '%')
  78. ->get()
  79. ->toArray();
  80. $data = [];
  81. foreach ($res as $item) {
  82. $data[] = [
  83. 'key' => $item['join_family_member_id'],
  84. 'value' => $item['family_member_name']
  85. ];
  86. }
  87. return json_success('', $data);
  88. }
  89. public function selectMemberAllQuota(Request $request)
  90. {
  91. $memberId = $request->get('member_id','');
  92. $orderId = $request->get('order_id', '');
  93. $goodsId = $request->get('goods_id','');
  94. $person = $request->get('person', '');
  95. $selectType = $request->get('select_type', 'ALL');
  96. if (!$memberId) {
  97. return json_fail('参数异常');
  98. }
  99. $data = [];
  100. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  101. ->whereColumn('member_benefit_limit_count', '>', 'member_benefit_used_count')
  102. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  103. ->when($orderId != '', function ($query) use ($orderId) {
  104. $query->where('join_benefit_order_id', $orderId);
  105. })->when($goodsId != '', function ($query) use ($goodsId) {
  106. $query->where('join_benefit_goods_id', $goodsId);
  107. })
  108. ->get()
  109. ->toArray();
  110. foreach ($benefit as $item) {
  111. if ($item['total'] - $item['used'] < $person) {
  112. $item['disabled'] = true;
  113. }
  114. $item['cut'] = $item['total'] - $item['used'];
  115. $data[] = $item;
  116. }
  117. if ($selectType == 'BENEFIT') {
  118. return json_success('', $data);
  119. }
  120. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  121. ->when($goodsId != '', function ($query) use ($goodsId) {
  122. $query->whereJsonContains('member_quota_json', ['goods_id' => $goodsId]);
  123. })
  124. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  125. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  126. ->get()
  127. ->toArray();
  128. foreach ($quotas as $quota) {
  129. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  130. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  131. ->where('member_quota_status', 'USED')
  132. ->count('*');
  133. if ($quota['total'] <= $usedNum) {
  134. continue;
  135. }
  136. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  137. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  138. ->select('member_quota_id as id', 'member_quota_name as name')
  139. ->first()
  140. ->toArray();
  141. $memberQuota['total'] = $quota['total'];
  142. $memberQuota['used'] = $usedNum;
  143. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  144. $memberQuota['disabled'] = true;
  145. }
  146. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  147. $data[] = $memberQuota;
  148. }
  149. if ($selectType == 'QUOTA') {
  150. return json_success('', $data);
  151. }
  152. return json_success('', $data);
  153. }
  154. public function selectMemberAllQuotaByGoods(Request $request)
  155. {
  156. $memberId = $request->get('member_id', '');
  157. $orderId = $request->get('order_id', '');
  158. $person = $request->get('person', '');
  159. $selectType = $request->get('select_type', 'ALL');
  160. if (!$memberId) {
  161. return json_fail('参数异常');
  162. }
  163. $data = [];
  164. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  165. // ->whereColumn('member_benefit_limit_count','>','member_benefit_used_count')
  166. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  167. ->when($orderId != '', function ($query) use ($orderId) {
  168. $query->where('join_benefit_order_id', $orderId);
  169. })
  170. ->get()
  171. ->toArray();
  172. foreach ($benefit as $item) {
  173. if ($item['total'] - $item['used'] < $person) {
  174. $item['disabled'] = true;
  175. }
  176. $item['cut'] = $item['total'] - $item['used'];
  177. $data[] = $item;
  178. }
  179. if ($selectType == 'BENEFIT') {
  180. return json_success('', $data);
  181. }
  182. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  183. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  184. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  185. ->get()
  186. ->toArray();
  187. foreach ($quotas as $quota) {
  188. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  189. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  190. ->where('member_quota_status', 'USED')
  191. ->count('*');
  192. if ($quota['total'] <= $usedNum) {
  193. continue;
  194. }
  195. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  196. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  197. ->select('member_quota_id as id', 'member_quota_name as name')
  198. ->first()
  199. ->toArray();
  200. $memberQuota['total'] = $quota['total'];
  201. $memberQuota['used'] = $usedNum;
  202. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  203. $memberQuota['disabled'] = true;
  204. }
  205. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  206. $data[] = $memberQuota;
  207. }
  208. if ($selectType == 'QUOTA') {
  209. return json_success('', $data);
  210. }
  211. return json_success('', $data);
  212. }
  213. public function list(Request $request)
  214. {
  215. return MemberService::list($request);
  216. }
  217. public function info(Request $request)
  218. {
  219. $memberId = $request->get('member_id', '');
  220. if (!$memberId) {
  221. return json_fail("参数错误");
  222. }
  223. return MemberService::info($memberId);
  224. }
  225. public function balanceInfo(Request $request)
  226. {
  227. $memberId = $request->get('member_id', '');
  228. if (!$memberId) {
  229. return json_fail("参数错误");
  230. }
  231. return MemberService::balanceInfo($request);
  232. }
  233. public function pointInfo(Request $request)
  234. {
  235. $memberId = $request->get('member_id', '');
  236. if (!$memberId) {
  237. return json_fail("参数错误");
  238. }
  239. return MemberService::pointInfo($memberId);
  240. }
  241. /**
  242. * @Desc 添加会员
  243. * @Author Gorden
  244. * @Date 2024/3/25 13:40
  245. *
  246. * @param Request $request
  247. * @return \support\Response
  248. */
  249. public function add(Request $request)
  250. {
  251. $validate = new MemberValidate();
  252. if (!$validate->scene('add')->check($request->post())) {
  253. return json_fail($validate->getError());
  254. }
  255. _syslog("添加用户", "用户【" . $request->post('member_cert_name') . '】');
  256. return MemberService::add($request->post());
  257. }
  258. public function update(Request $request)
  259. {
  260. $validate = new MemberValidate();
  261. if (!$validate->scene('update')->check($request->post())) {
  262. return json_fail($validate->getError());
  263. }
  264. _syslog("编辑用户", "用户【" . $request->post('member_cert_name') ?? $request->post('member_id') . '】');
  265. return MemberService::update($request->post());
  266. }
  267. /**
  268. * @Desc 绑定的设备
  269. * @Author Gorden
  270. * @Date 2024/4/7 10:20
  271. *
  272. * @param $id
  273. * @return \support\Response
  274. */
  275. public function device($id)
  276. {
  277. return MemberService::deviceList($id);
  278. }
  279. public function addDevice(Request $request)
  280. {
  281. return MemberService::addDevice($request);
  282. }
  283. public function updateDevice(Request $request)
  284. {
  285. return MemberService::updateDevice($request);
  286. }
  287. public function deleteDevice(Request $request)
  288. {
  289. return MemberService::deleteDevice($request);
  290. }
  291. }