<?php
|
|
namespace app\common\model\supplier;
|
|
use app\common\model\BaseModel;
|
use app\common\model\shop\Role as RoleModel;
|
use app\common\model\supplier\member\Member as MemberModel;
|
use app\common\model\supplier\PointsLog as PointsLogModel;
|
use app\shop\model\auth\UserRole as UserRoleModel;
|
|
/**
|
* 商家供应商模型
|
*/
|
class Supplier extends BaseModel
|
{
|
protected $name = 'supplier';
|
protected $pk = 'shop_supplier_id';
|
|
|
|
/**
|
* 关联应用表
|
*/
|
public function app()
|
{
|
return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
|
}
|
|
/**
|
* 关联logo
|
*/
|
public function logo()
|
{
|
return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'logo_id');
|
}
|
/**
|
* 关联企业微信二维码
|
*/
|
public function qyQrcode()
|
{
|
return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'qy_qrcode_id');
|
}
|
/**
|
* 关联品牌类型
|
*/
|
public function category()
|
{
|
return $this->hasOne('app\\common\\model\\supplier\\Category', 'category_id', 'category_id');
|
}
|
/**
|
* 关联business
|
*/
|
public function business()
|
{
|
return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'business_id');
|
}
|
/**
|
* 关联超管
|
*/
|
public function superUser()
|
{
|
return $this->hasOne('app\\common\\model\\supplier\\User', 'shop_supplier_id', 'shop_supplier_id')
|
->where('is_super','=', 1);
|
}
|
/**
|
* 详情
|
*/
|
public static function detail($shop_supplier_id, $with = [])
|
{
|
return (new static())->with($with)->find($shop_supplier_id);
|
}
|
|
/**
|
* 累积供应商结算金额 (批量)
|
*/
|
public function onBatchIncSupplierMoney($data)
|
{
|
foreach ($data as $supplierId => $supplierMoney) {
|
$this->where(['shop_supplier_id' => $supplierId])
|
->inc('total_money', $supplierMoney)
|
->inc('money', $supplierMoney)
|
->update();
|
}
|
return true;
|
}
|
|
/**
|
* 累积商户的可用积分
|
*/
|
public function setIncPoints($points, $describe, $decPoints = 0)
|
{
|
// 新增积分变动明细
|
PointsLogModel::add([
|
'shop_supplier_id' => $this['shop_supplier_id'],
|
'value' => $points,
|
'describe' => $describe,
|
'app_id' => $this['app_id'],
|
]);
|
|
// 更新用户可用积分
|
$data['points'] = ($this['points'] + $points + $decPoints <= 0) ? 0 : $this['points'] + $points + $decPoints;
|
$this->where('branch_id', '=', $this['branch_id'])->update($data);
|
|
return true;
|
}
|
|
//判断营业时间
|
public function supplierStatus($shop_supplier_id)
|
{
|
$supplier = $this->where('shop_supplier_id', '=', $shop_supplier_id)
|
->where('is_delete', '=', 0)
|
->find();
|
//是否开启营业
|
if (!$supplier || $supplier['status'] != 0) {
|
return false;
|
}
|
//判断是否设置了营业时间
|
$business_start_time= $supplier['business_start_time'];
|
$business_end_time = $supplier['business_end_time'];
|
if (empty($business_start_time) || empty($business_end_time)) {
|
return true;
|
}
|
|
//营业时间
|
$start_time = strtotime($business_start_time);
|
$end_time = strtotime($business_end_time);
|
if($end_time >= $start_time){
|
//结束时间大于开始时间
|
if (!(time() >= $start_time && time() <= $end_time)) {
|
return false;
|
}
|
}else{
|
//开始的时间大于结束时间,即是营业到凌晨
|
if (time() >= $end_time && time() <= $start_time) {
|
return false;
|
}
|
}
|
|
return true;
|
}
|
|
/**
|
* 通过角色管理的区域获取商户id
|
*/
|
public static function getSupplierIdsByUser($user)
|
{
|
|
$model = new static();
|
//获取用户所有的角色
|
$user_role_ids = (new UserRoleModel())::getRoleIds($user['shop_user_id']);
|
if(!empty($user_role_ids)){
|
//获取所有角色的权限
|
$area_arr =RoleModel::getAreaIdsByRoleIds($user_role_ids);
|
if(!empty($area_arr)){
|
$area_ids = [];
|
foreach ($area_arr as $val){
|
if(!is_array($val)){
|
$val = json_decode($val,true);
|
}
|
if(empty($area_ids)){
|
//第一个角色所管理的区域
|
$area_ids = $val;
|
}else{
|
//合并角色管理的区域
|
$area_ids = array_merge($area_ids, $val);
|
}
|
}
|
if(!empty($area_ids)){
|
//获取加盟商入驻时所选的区域
|
return $model->where('area_id',"in",$area_ids)
|
->column("shop_supplier_id");
|
}
|
}
|
|
}
|
return [];
|
}
|
|
/**
|
* 获取供应商当前有效的年卡
|
*/
|
public function getCurrentMember()
|
{
|
return MemberModel::getCurrentMember($this['shop_supplier_id'], $this['app_id']);
|
}
|
|
/**
|
* 获取角色管理的区域
|
*/
|
public static function getAreaIdsByUser($user)
|
{
|
//获取用户所有的角色
|
$user_role_ids = (new UserRoleModel())::getRoleIds($user['shop_user_id']);
|
if(!empty($user_role_ids)){
|
//获取所有角色的权限
|
$area_arr =RoleModel::getAreaIdsByRoleIds($user_role_ids);
|
if(!empty($area_arr)){
|
$area_ids = [];
|
foreach ($area_arr as $val){
|
if(!is_array($val)){
|
$val = json_decode($val,true);
|
}
|
if(empty($area_ids)){
|
//第一个角色所管理的区域
|
$area_ids = $val;
|
}else{
|
//合并角色管理的区域
|
$area_ids = array_merge($area_ids, $val);
|
}
|
}
|
return $area_ids;
|
}
|
|
}
|
return [];
|
}
|
|
//获取自营的商户 by yj 2024.1.9
|
public static function getSupplierSelfSupport()
|
{
|
$model = new static();
|
$supplier = $model->where('store_type', '=', 20)
|
->where('is_delete', '=', 0)
|
->column("shop_supplier_id");
|
return $supplier;
|
}
|
|
//获取名称获取准确的商户 by yj 2024.2.28
|
public static function getSupplierIdByName($name)
|
{
|
$model = new static();
|
$supplier = $model->where('name', '=', $name)
|
->where('is_delete', '=', 0)
|
->find();
|
return empty($supplier) ? '-1' : $supplier["shop_supplier_id"];
|
}
|
|
/**
|
* 商户独立收款是否开启 by yj 2024.8.6
|
*/
|
public static function getIndependentOpen($shop_supplier_id)
|
{
|
$model = new static();
|
$data = $model->where('shop_supplier_id', '=', $shop_supplier_id)
|
->where('is_delete', '=', 0)
|
->find();
|
return empty($data['is_independent']) ? 0 : $data['is_independent'];
|
}
|
/**
|
* 冻结用户资金
|
*/
|
public function freezeMoney($money)
|
{
|
$this->save(['money' => $this['money'] - $money,'freeze_money'=>$this['freeze_money']+$money]);
|
}
|
/**
|
* 直推供应商多少人
|
*/
|
public function refereeSupplierCount ($referee_id)
|
{
|
$count = $this->where('referee_id', '=', $referee_id)
|
->where('is_delete', '=', 0)
|
->count();
|
return $count;
|
}
|
|
/**
|
* 下级商户数量
|
* @param $user_id
|
* @return int
|
*/
|
public function getSubordinateNum($user_id)
|
{
|
return (new static())->where('is_delete', '=', 0)
|
->where('referee_id', 'in', $user_id)
|
->count();
|
}
|
|
}
|