<?php

namespace app\api\controller;

use app\api\model\dealer\User as DealerUserModel;
use app\api\model\Invite\InviteLog;
use app\api\model\wholesaler\Apply;
use app\api\service\User as UserService;
use app\common\enum\WholesalerEnum;
use app\common\model\invite\InviteConfig;
use cores\exception\BaseException;
use think\db\exception\DbException;
use think\response\Json;

class Invite extends Controller
{
    /**
     * @notes:基本信息
     * @return Json
     * @throws BaseException
     * @author: wanghousheng
     */
    public function index(): Json
    {
        $info = InviteConfig::detail([], ['coupon']);
        $data = [];
        if ($info) {
            $data['one_order_rate'] = $info['one_order_rate'];
            $data['integral'] = $info['integral'];
            $data['coupon_name'] = $info['coupon']['name'];
            $model = new InviteLog();
            $data['money'] = $model->sumMoney();
            $data['count_people'] = $model->countPeople();
        }
        return $this->renderSuccess($data);
    }

    /**
     * @notes:邀请记录
     * @throws BaseException
     * @throws DbException
     * @author: wanghousheng
     */
    public function getList(): Json
    {
        $model = new InviteLog();
        $list = $model->getList();
        return $this->renderSuccess(compact('list'));
    }

    /**
     * @notes:
     * @return Json
     * @throws BaseException
     * @author: wanghousheng
     */
    public function statistics(): Json
    {
        $refuse_num = 0;
        $adoption_num = 0;
        $money = 0;
        $model = new InviteLog();
        $user_ids = $model->inviteeUserIds();
        if ($user_ids) {
            $applyModel = new Apply();
            //审核通过
            $adoption_num = $applyModel->whereIn('user_id', $user_ids)->where(['status' => WholesalerEnum::ADOPT])->count();
            //审核拒绝
            $refuse_num = $applyModel->whereIn('user_id', $user_ids)->where(['status' => WholesalerEnum::REFUSE])->count();
        }
        // 当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        // 分销商用户详情
        $dealer = DealerUserModel::detail($userId, []);
        if (!empty($dealer->freeze_money)) {
            $money = $dealer->freeze_money;
        }
        $data = compact('refuse_num', 'adoption_num', 'money');
        return $this->renderSuccess($data);
    }

    /**
     * @notes:邀请采购商列表
     * @return Json
     * @throws BaseException
     * @throws DbException
     * @author: wanghousheng
     */
    public function inviteWholesalerList(): Json
    {
        $data['list'] = [];
        $data['total'] = 0;
        $data['adoption_num'] = 0;
        $data['refuse_num'] = 0;
        $model = new InviteLog();
        $user_ids = $model->inviteeUserIds();
        $status = intval($this->request->post('status', 10));
        if ($user_ids) {
            $applyModel = new Apply();
            $list = $applyModel->whereIn('user_id', $user_ids)
                ->with(['avatarImg'])
                ->where(['status' => $status])
                ->field(['username', 'mobile', 'avatar_id', 'create_time'])
                ->order('id desc')
                ->paginate(15);
            $data['list'] = $list->items();
            if (!empty($data['list'])) {
                foreach ($data['list'] as &$item) {
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                }
            }
            $data['total'] = $list->total();
            //审核通过
            $data['adoption_num'] = $applyModel->whereIn('user_id', $user_ids)->where(['status' => WholesalerEnum::ADOPT])->count();
            //审核拒绝
            $data['refuse_num'] = $applyModel->whereIn('user_id', $user_ids)->where(['status' => WholesalerEnum::REFUSE])->count();
        }
        return $this->renderSuccess($data);
    }
}