<?php

namespace app\admin\controller\client;

use app\admin\service\member\MemberService;
use app\controller\Curd;
use app\model\ClientFavorite;
use app\model\ClientPoints;
use app\model\MemberRole;
use support\Request;
use support\Response;

class PointsController extends Curd
{
    public function __construct()
    {
        $this->model = new ClientPoints();
    }

    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'client_points_addtimes';
        if (!empty($request->get('member_mobile'))) {
            $where['member.member_mobile'] = ['like', $request->get('member_mobile')];
        }
        if (!empty($request->get('order_id'))) {
            $where['order.order_id'] = ['like', $request->get('order_id')];
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    protected function doSelect(array $where, string $field = null, string $order = 'desc')
    {
        $model = $this->model->leftJoin('member', 'member.member_id', '=', 'client_points.join_client_points_member_id')
            ->leftJoin('member_info', 'member_info.join_info_member_id', '=', 'client_points.join_client_points_member_id')
            ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'client_points.join_client_points_member_id')
            ->leftJoin('order', 'order.order_id', '=', 'client_points.join_client_points_order_id')
            ->select('member.member_id', 'member.member_mobile', 'member_info.member_info_nickname', 'member_cert.member_cert_name', 'order.order_id', 'order.order_name', 'client_points.*');

        foreach ($where as $column => $value) {
            if (is_array($value)) {
                if ($value[0] === 'like' || $value[0] === 'not like') {
                    $model = $model->where($column, $value[0], "%$value[1]%");
                } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
                    $model = $model->where($column, $value[0], $value[1]);
                } elseif ($value[0] == 'in' && !empty($value[1])) {
                    $valArr = $value[1];
                    if (is_string($value[1])) {
                        $valArr = explode(",", trim($value[1]));
                    }
                    $model = $model->whereIn($column, $valArr);
                } elseif ($value[0] == 'not in' && !empty($value[1])) {
                    $valArr = $value[1];
                    if (is_string($value[1])) {
                        $valArr = explode(",", trim($value[1]));
                    }
                    $model = $model->whereNotIn($column, $valArr);
                } elseif ($value[0] == 'null') {
                    $model = $model->whereNull($column);
                } elseif ($value[0] == 'not null') {
                    $model = $model->whereNotNull($column);
                } elseif ($value[0] !== '' || $value[1] !== '') {
                    $model = $model->whereBetween($column, $value);
                }
            } else {
                $model = $model->where($column, $value);
            }
        }
        if ($field) {
            $model = $model->orderBy($field, $order);
        }
        return $model;
    }

    /**
     * @Desc 积分详情
     * @Author Gorden
     * @Date 2024/9/19 18:16
     *
     * @param Request $request
     * @return Response
     */
    public function info(Request $request): Response
    {
        $pointsId = $request->get('points_id');
        if (!$pointsId) {
            return json_fail('参数异常');
        }

        $points = ClientPoints::with([
            'orders',
            'goods',
            'member' => function ($query) {
                $query->select('member_id','join_member_role_id', 'member_mobile', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
            },
            'memberInfo' => function ($query) {
                $query->select('join_info_member_id', 'member_info_nickname','member_info_headimg');
            },
            'memberCert' => function ($query) {
                $query->select('join_cert_member_id', 'member_cert_name');
            },
        ])->where('client_points_id', $pointsId)
            ->first();

        $mobile = !empty($points->member) && !empty($points->member->member_mobile) ? $points->member->member_mobile : '';
        $nickname = !empty($points->memberInfo) && !empty($points->memberInfo->member_info_nickname) ? $points->memberInfo->member_info_nickname : '';
        $certname = !empty($points->memberCert) && !empty($points->memberCert->member_cert_name) ? $points->memberCert->member_cert_name : '';
        $points->member_name = MemberService::getMemberName($mobile, $certname, $nickname);
        $points->member_info_headimg = MemberService::getAvatarUrl(!empty($points->memberInfo) ? $points->memberInfo->member_info_headimg : '');
        $points->role_name = !empty($points->member) && !empty($points->member_name->join_member_role_id) ? MemberRole::where('member_role_id',$points->member_name->join_member_role_id)->value('member_role_name') : '';

        return json_success("success", $points);
    }
}