<?php

namespace app\admin\controller\member;

use app\admin\service\member\MemberService;
use app\admin\service\member\OwnerService;
use app\admin\validate\member\MemberValidate;
use support\Db;
use support\Request;

class OwnerController
{
    public function selectList(Request $request)
    {
        $keywords = $request->get('keywords');
        if (!$keywords) {
            return json_fail("查询错误");
        }

        $res = Db::table('member')
            ->join('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','Y')
            ->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,
            ];
        }

        return json_success('',$data);
    }

    public function list(Request $request)
    {
        return OwnerService::list($request);
    }

    /**
     * @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());
        }

        return OwnerService::add($request->post());
    }
}