<?php

namespace app\repository;

use app\exception\RepositoryException;
use app\model\Business;
use app\model\CouponMain;
use app\model\Deduction;
use app\model\Recharge;
use app\service\Repository;
use think\Collection;
use think\Model;

/**
 * 商家域 相关操作
 *
 * Class BusinessRepository
 * @package app\repository
 * @method self getInstance(Model $model = null) static
 */
class BusinessRepository extends Repository
{
    /**
     * 根据条件查询列表
     *
     * @param array $where 查询条件
     * @param int $page 默认第一页 0不限制
     * @param int $limit 限制条数 0不限制
     * @param array $order
     * @return array
     * @throws RepositoryException
     */
    public function businessList(array $where = [], int $page = 1, int $limit = 0, array $order = [])
    {
        $q = $this->model->alias("a")->field("id")->withJoin(["account" => ["avatar_url", "nick_name"], "business"]);
        $data = [
            'total' => 0,
            'current' => $page,
            'size' => $limit,
            'list' => new Collection(),
        ];


        if (count($where)) {
            $q = $q->where($where);
        }
        if (count($order)) {
            $q = $q->order($order);
        }
        $data['total'] = $q->count();
        if ($data['total']) {
            if ($limit) {
                if ($page) {
                    $q = $q->page($page);
                }
                $q = $q->limit($limit);
            }

            $data['list'] = $q->select();
        }

        return $data;
    }

    /**
     * 优惠券列表
     *
     * @param string $businessCode 商家code
     * @param int $page 默认第一页 0不限制
     * @param int $limit 限制条数 0不限制
     * @param array $order
     * @return array
     * @throws RepositoryException
     */
    public function businessCouponList($where, int $page = 1, int $limit = 0, array $order = ["create_time" => "desc", "id" => "desc"])
    {
        return CouponMain::findList($where, [], $page, $limit, null, $order);
    }

    /**
     * 扣费记录
     *
     * @param string $businessCode 商家code
     * @param int $page 默认第一页 0不限制
     * @param int $limit 限制条数 0不限制
     * @param array $order
     * @return array
     * @throws RepositoryException
     */
    public function businessDeductionList($where, int $page = 1, int $limit = 0, array $order = ["create_time" => "desc", "id" => "desc"])
    {
        return Deduction::findList($where, [], $page, $limit, null, $order);
    }

    /**
     * 充值记录
     *
     * @param string $businessCode 商家code
     * @param int $page 默认第一页 0不限制
     * @param int $limit 限制条数 0不限制
     * @param array $order
     * @return array
     * @throws RepositoryException
     */
    public function businessRechargeList($where, int $page = 1, int $limit = 0, array $order = ["create_time" => "desc", "id" => "desc"])
    {
        return Recharge::findList($where, [], $page, $limit, function ($q){
            return $q->withJoin(["business"=>["business_name"]]);
        }, $order);
    }
    /* 获取所有不是代理商的商家*/
    public function getBusinessAll(){
        return   Business:: where("state", Business::state_on)->select();
    }
}