123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- namespace app\admin\controller\member;
- use app\admin\service\member\MemberService;
- use app\admin\validate\member\MemberValidate;
- use app\model\Family;
- use app\model\FamilyMember;
- use app\model\Member;
- use app\model\MemberAccount;
- use app\model\MemberBenefit;
- use app\model\MemberQuota;
- use app\model\PayDetail;
- use support\Db;
- use support\Request;
- class MemberController
- {
- public function selectList(Request $request)
- {
- $keywords = $request->get('keywords');
- $isOwner = $request->get('member_is_owner', 'N');
- if (!$keywords) {
- return json_fail("查询错误");
- }
- $res = Db::table('member')
- ->leftjoin('member_cert', 'member.member_id', '=', 'member_cert.join_cert_member_id')
- ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name')
- ->where('member.member_is_owner', $isOwner)
- ->where(function ($query) use ($keywords) {
- $query->where('member.member_mobile', 'like', '%' . $keywords . '%')
- ->orWhere("member.member_id", 'like', '%' . $keywords . '%')
- ->orWhere("member_cert.member_cert_name", 'like', '%' . $keywords . '%');
- })->get();
- $data = [];
- foreach ($res as $item) {
- $data[] = [
- 'key' => $item->member_id,
- 'value' => !empty($item->member_cert_name) ? $item->member_cert_name . '-' . $item->member_mobile : $item->member_mobile,
- ];
- }
- return json_success('', $data);
- }
- public function exportMember(Request $request){
- return MemberService::exportMember($request);
- }
- public function selectWriteOffMember()
- {
- $members = Member::with([
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- }
- ])->whereRaw("JSON_EXTRACT(`member_json`, '$.user') IS NOT NULL")
- ->select('member_id', 'member_mobile', 'member_json')
- ->get()
- ->toArray();
- foreach ($members as &$member) {
- $member['member_json'] = json_decode($member['member_json'], true);
- $member['username'] = $member['member_mobile'];
- $member['user_id'] = $member['member_json']['user'] ? $member['member_json']['user']['user_id'] : '';
- if (isset($member['cert']['member_cert_name'])) {
- $member['username'] = $member['cert']['member_cert_name'] . '-' . $member['username'];
- }
- }
- return json_success('', $members);
- }
- public function selectFamilyMemberList(Request $request)
- {
- $keywords = $request->get('keywords');
- if (!$keywords) {
- return json_fail("查询错误");
- }
- // 创建的家庭
- $family = Family::where('join_family_creator_member_id', $request->get('memberId'))->get()->toArray();
- if (!$family) {
- return json_success('暂无数据');
- }
- $familyIds = array_column($family, 'family_id');
- $res = FamilyMember::whereIn('join_family_id', $familyIds)
- ->where("family_member_name", 'like', '%' . $keywords . '%')
- ->get()
- ->toArray();
- $data = [];
- foreach ($res as $item) {
- $data[] = [
- 'key' => $item['join_family_member_id'],
- 'value' => $item['family_member_name']
- ];
- }
- return json_success('', $data);
- }
- public function selectMemberAllQuota(Request $request)
- {
- $memberId = $request->get('member_id','');
- $orderId = $request->get('order_id', '');
- $goodsId = $request->get('goods_id','');
- $person = $request->get('person', '');
- $selectType = $request->get('select_type', 'ALL');
- if (!$memberId) {
- return json_fail('参数异常');
- }
- $data = [];
- $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
- ->whereColumn('member_benefit_limit_count', '>', 'member_benefit_used_count')
- ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
- ->when($orderId != '', function ($query) use ($orderId) {
- $query->where('join_benefit_order_id', $orderId);
- })->when($goodsId != '', function ($query) use ($goodsId) {
- $query->where('join_benefit_goods_id', $goodsId);
- })
- ->get()
- ->toArray();
- foreach ($benefit as $item) {
- if ($item['total'] - $item['used'] < $person) {
- $item['disabled'] = true;
- }
- $item['cut'] = $item['total'] - $item['used'];
- $data[] = $item;
- }
- if ($selectType == 'BENEFIT') {
- return json_success('', $data);
- }
- $quotas = MemberQuota::where('join_quota_member_id', $memberId)
- ->when($goodsId != '', function ($query) use ($goodsId) {
- $query->whereJsonContains('member_quota_json', ['goods_id' => $goodsId]);
- })
- ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
- ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
- ->get()
- ->toArray();
- foreach ($quotas as $quota) {
- $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
- ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
- ->where('member_quota_status', 'USED')
- ->count('*');
- if ($quota['total'] <= $usedNum) {
- continue;
- }
- $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
- ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
- ->select('member_quota_id as id', 'member_quota_name as name')
- ->first()
- ->toArray();
- $memberQuota['total'] = $quota['total'];
- $memberQuota['used'] = $usedNum;
- if ($memberQuota['total'] - $memberQuota['used'] < $person) {
- $memberQuota['disabled'] = true;
- }
- $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
- $data[] = $memberQuota;
- }
- if ($selectType == 'QUOTA') {
- return json_success('', $data);
- }
- return json_success('', $data);
- }
- public function selectMemberAllQuotaByGoods(Request $request)
- {
- $memberId = $request->get('member_id', '');
- $orderId = $request->get('order_id', '');
- $person = $request->get('person', '');
- $selectType = $request->get('select_type', 'ALL');
- if (!$memberId) {
- return json_fail('参数异常');
- }
- $data = [];
- $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
- // ->whereColumn('member_benefit_limit_count','>','member_benefit_used_count')
- ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
- ->when($orderId != '', function ($query) use ($orderId) {
- $query->where('join_benefit_order_id', $orderId);
- })
- ->get()
- ->toArray();
- foreach ($benefit as $item) {
- if ($item['total'] - $item['used'] < $person) {
- $item['disabled'] = true;
- }
- $item['cut'] = $item['total'] - $item['used'];
- $data[] = $item;
- }
- if ($selectType == 'BENEFIT') {
- return json_success('', $data);
- }
- $quotas = MemberQuota::where('join_quota_member_id', $memberId)
- ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
- ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
- ->get()
- ->toArray();
- foreach ($quotas as $quota) {
- $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
- ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
- ->where('member_quota_status', 'USED')
- ->count('*');
- if ($quota['total'] <= $usedNum) {
- continue;
- }
- $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
- ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
- ->select('member_quota_id as id', 'member_quota_name as name')
- ->first()
- ->toArray();
- $memberQuota['total'] = $quota['total'];
- $memberQuota['used'] = $usedNum;
- if ($memberQuota['total'] - $memberQuota['used'] < $person) {
- $memberQuota['disabled'] = true;
- }
- $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
- $data[] = $memberQuota;
- }
- if ($selectType == 'QUOTA') {
- return json_success('', $data);
- }
- return json_success('', $data);
- }
- public function list(Request $request)
- {
- return MemberService::list($request);
- }
- public function info(Request $request)
- {
- $memberId = $request->get('member_id', '');
- if (!$memberId) {
- return json_fail("参数错误");
- }
- return MemberService::info($memberId);
- }
- public function balanceInfo(Request $request)
- {
- $memberId = $request->get('member_id', '');
- if (!$memberId) {
- return json_fail("参数错误");
- }
- return MemberService::balanceInfo($request);
- }
- public function pointInfo(Request $request)
- {
- $memberId = $request->get('member_id', '');
- if (!$memberId) {
- return json_fail("参数错误");
- }
- return MemberService::pointInfo($memberId);
- }
- /**
- * @Desc 添加会员
- * @Author Gorden
- * @Date 2024/3/25 13:40
- *
- * @param Request $request
- * @return \support\Response
- */
- public function add(Request $request)
- {
- $validate = new MemberValidate();
- if (!$validate->scene('add')->check($request->post())) {
- return json_fail($validate->getError());
- }
- _syslog("添加用户", "用户【" . $request->post('member_cert_name') . '】');
- return MemberService::add($request->post());
- }
- public function update(Request $request)
- {
- $validate = new MemberValidate();
- if (!$validate->scene('update')->check($request->post())) {
- return json_fail($validate->getError());
- }
- _syslog("编辑用户", "用户【" . $request->post('member_cert_name') ?? $request->post('member_id') . '】');
- return MemberService::update($request->post());
- }
- /**
- * @Desc 绑定的设备
- * @Author Gorden
- * @Date 2024/4/7 10:20
- *
- * @param $id
- * @return \support\Response
- */
- public function device($id)
- {
- return MemberService::deviceList($id);
- }
- public function addDevice(Request $request)
- {
- return MemberService::addDevice($request);
- }
- public function updateDevice(Request $request)
- {
- return MemberService::updateDevice($request);
- }
- public function deleteDevice(Request $request)
- {
- return MemberService::deleteDevice($request);
- }
- }
|