coupon-admin/app/repository/BusinessRepository.php

267 lines
7.7 KiB
PHP
Raw Normal View History

2021-11-18 09:57:04 +00:00
<?php
2021-11-30 10:31:58 +00:00
2021-11-18 09:57:04 +00:00
namespace app\repository;
use app\exception\RepositoryException;
2021-12-22 07:09:53 +00:00
use app\model\Account;
2021-11-18 09:57:04 +00:00
use app\model\Business;
use app\model\CouponMain;
use app\model\Deduction;
2022-04-07 06:31:17 +00:00
use app\model\Member;
2021-11-18 09:57:04 +00:00
use app\model\Recharge;
use app\service\Repository;
2021-12-10 10:25:14 +00:00
use app\traits\account\ApplyStaffTrait;
2021-11-30 07:26:38 +00:00
use app\traits\CouponBillTrait;
2021-12-06 10:56:23 +00:00
use app\traits\CouponMainTrait;
2021-11-18 09:57:04 +00:00
use think\Collection;
use think\Model;
/**
* 商家域 相关操作
*
* Class BusinessRepository
* @package app\repository
* @method self getInstance(Model $model = null) static
*/
class BusinessRepository extends Repository
{
2021-11-30 07:26:38 +00:00
use CouponBillTrait;
2021-12-06 10:56:23 +00:00
use CouponMainTrait;
2021-12-10 10:25:14 +00:00
use ApplyStaffTrait;
2021-11-30 10:31:58 +00:00
2021-11-18 09:57:04 +00:00
/**
* 根据条件查询列表
*
* @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 = [])
{
2021-11-29 05:56:26 +00:00
$q = $this->model->alias("a")
2021-11-30 10:31:58 +00:00
->join("account b", "a.code = b.business_code")
2021-11-30 07:26:38 +00:00
->field(["a.code as business_code",
"b.avatar_url",
"b.nick_name as account_nick_name",
"a.business_name",
"a.total_recharge",
"a.id as basiness_id",
2021-12-01 08:53:44 +00:00
"a.balance",
"a.contact_name",
"a.contact_phone",
"a.business_address",
"a.business_license",
"a.state",
2022-01-13 02:48:36 +00:00
"a.type_name",
2021-12-01 08:53:44 +00:00
])
2021-11-30 10:31:58 +00:00
->group("a.id");
2021-11-18 09:57:04 +00:00
$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;
}
/**
2022-03-31 06:42:56 +00:00
* 签到券列表
2021-11-18 09:57:04 +00:00
*
* @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"])
{
2021-11-30 10:31:58 +00:00
return Deduction::findList($where, [], $page, $limit, function ($q) {
2021-12-20 09:53:03 +00:00
return $q->with(["couponMain","bill","account"]);
2021-11-30 10:31:58 +00:00
}, $order);
2021-11-18 09:57:04 +00:00
}
/**
* 充值记录
*
* @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"])
{
2021-11-25 10:11:50 +00:00
return Recharge::findList($where, [], $page, $limit, function ($q) {
return $q->withJoin(["business" => ["business_name"]]);
2021-11-18 09:57:04 +00:00
}, $order);
}
2021-11-25 10:11:50 +00:00
/* 获取所有的商家*/
2022-04-07 06:31:17 +00:00
public function getBusinessAll($roles = "",$businessCode ="")
2021-11-25 10:11:50 +00:00
{
2022-04-07 06:31:17 +00:00
$roles = array_filter( explode(",",$roles));
$isAdmin = true;
//如果是 渠道商或者工作人员
if(
in_array(Member::ANENT_ROLE_ID,$roles)
||
in_array(Member::STAFF_ROLE_ID,$roles)
){
$isAdmin=false;
}
2021-11-30 10:31:58 +00:00
return Business::alias("a")
->join("account b", "a.code = b.business_code")
2021-12-23 03:02:00 +00:00
->field("a.code , a.business_name , a.contact_name")
2022-04-07 06:31:17 +00:00
->when(!$isAdmin,function ($q)use($businessCode){
$q->where("a.agency_code",$businessCode);
})
2021-11-30 10:31:58 +00:00
->order("a.id desc")
->group("a.id")
->select();
2021-11-25 10:11:50 +00:00
}
/**
* 获取单个商家详情
* @param $businessCode
* @param bool $lock
2021-11-30 10:31:58 +00:00
* @return array|Model|null
2021-11-25 10:11:50 +00:00
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
2021-11-30 10:31:58 +00:00
public function getBusinessAccount($businessCode, bool $lock = false)
2021-11-25 10:11:50 +00:00
{
2021-12-17 03:41:27 +00:00
$business = Business::where("code", $businessCode)->when($lock, function ($q) {
2021-11-25 10:11:50 +00:00
$q->lock(true);
})->find();
2021-12-17 03:41:27 +00:00
if (empty($business)) {
2021-11-25 10:11:50 +00:00
return null;
}
2021-12-17 03:41:27 +00:00
return $business;
2021-11-18 09:57:04 +00:00
}
2021-11-29 05:59:29 +00:00
2021-11-30 10:52:02 +00:00
/**
* 格式化前端输出商户信息
* @param array $data
* @param array $formats
* @return array
*/
public function formatFrontBusinessInfo(array $data, array $formats=[]): array
{
$fields = ['id', 'code', 'business_name', 'business_subtitle', 'business_license',
'lat', 'lng', 'business_address', 'contact_name', 'contact_phone',
'create_time', 'type', 'type_name', 'characteristic', 'intro',
'business_circle', 'business_circle_id', 'background', 'score',
];
if (in_array(1, $formats)) {
// 返回审批相关字段
$fields = array_merge($fields, ['state', 'reason']);
}
if (in_array(2, $formats)) {
// 返回关联的平台商相关字段
$fields = array_merge($fields, [ 'is_agency', 'agency_code']);
}
return arrayKeysFilter($data, $fields);
}
2021-11-30 07:26:38 +00:00
/**
* 查看指定商圈下的商家
* @param $businessCircleId
2021-11-30 10:31:58 +00:00
* @return Business[]|array|Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
2021-11-30 07:26:38 +00:00
*/
public function getByCircleId($businessCircleId)
{
2021-11-30 10:31:58 +00:00
return Business::where("business_circle_id", $businessCircleId)->select();
}
/**
* 平台商下有多少商家
* @param $agencyCode
* @return int
*/
public function agencyHasBusinessCount($agencyCode)
{
2022-04-07 06:40:32 +00:00
if(empty($agencyCode)){
return 0;
}
2021-11-30 10:31:58 +00:00
return Business::where("agency_code", $agencyCode)->count();
}
/**
2022-03-31 06:42:56 +00:00
* 平台商下商家 所持有的签到券数量
2021-11-30 10:31:58 +00:00
* @param $agencyCode
* @return int
*/
public function agencyHasCouponCount($agencyCode)
{
2022-04-07 06:40:32 +00:00
if(empty($agencyCode)){
return 0;
}
2021-11-30 10:31:58 +00:00
return Business::alias("a")
->join("coupon_main b", "a.code =b.business_code")
->where("a.agency_code", $agencyCode)
->sum("b.count");
2021-11-30 07:26:38 +00:00
}
2022-03-30 07:52:49 +00:00
/**
2022-03-31 06:42:56 +00:00
* 统计某个时间段内 商家发布的签到券个数
2022-03-30 07:52:49 +00:00
* @param $businessId
* @param $startTime
* @param $endTime
*/
public function getTimeListModeReleaseCount($businessCode,$startTime,$endTime)
{
return CouponMain::where("business_code",$businessCode)
->whereBetweenTime("create_time",$startTime,$endTime)
->order("id")
->count();
}
2021-11-18 09:57:04 +00:00
}