2021-11-18 09:57:04 +00:00
< ? php
namespace app\repository ;
use app\exception\RepositoryException ;
use app\model\Business ;
2021-11-29 08:34:25 +00:00
use app\model\BusinessCircle ;
2021-11-25 10:11:50 +00:00
use app\model\BusinessFlow ;
2021-11-18 09:57:04 +00:00
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 = [])
{
2021-11-29 05:56:26 +00:00
$q = $this -> model -> alias ( " a " )
-> join ( " account b " , " a.user_code = b.user_code " )
-> join ( " business c " , " a.business_code = c.code " )
-> field ([ " c.code as business_code " , " b.avatar_url " , " b.nick_name as account_nick_name " , " c.business_name " , " c.total_recharge " , " c.id as basiness_id " , " c.balance " ]);
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 ;
}
/**
* 优惠券列表
*
* @ 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 " ])
{
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
/* 获取所有的商家*/
public function getBusinessAll ()
{
2021-11-29 05:56:26 +00:00
return BusinessFlow :: alias ( " a " )
-> join ( " account b " , " a.user_code = b.user_code " )
-> join ( " business c " , " a.business_code = c.code " )
-> field ( " c.code , c.business_name " )
-> order ( " a.id desc " )
-> select ();
2021-11-25 10:11:50 +00:00
}
/**
* 获取单个商家详情
* @ param $businessCode
* @ param bool $lock
* @ return BusinessFlow | array | Model | null
* @ throws \think\db\exception\DataNotFoundException
* @ throws \think\db\exception\DbException
* @ throws \think\db\exception\ModelNotFoundException
*/
public function getBusinessAccount ( $businessCode , bool $lock = false )
{
$Flow = BusinessFlow :: with ([ " account " , " business " ]) -> where ( " business_code " , $businessCode ) -> when ( $lock , function ( $q ){
$q -> lock ( true );
}) -> find ();
if ( empty ( $Flow ) || empty ( $Flow -> account ) || empty ( $Flow -> business )) {
return null ;
}
return $Flow ;
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-18 09:57:04 +00:00
}