277 lines
10 KiB
PHP
277 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* Author: 芸众商城 www.yunzshop.com
|
|
* Date: 2018/12/6
|
|
* Time: 6:21 PM
|
|
*/
|
|
|
|
namespace Yunshop\TeamDividend\Common\models;
|
|
|
|
use app\common\facades\Setting;
|
|
use app\common\models\BaseModel;
|
|
use app\common\models\Member;
|
|
use app\common\models\Order;
|
|
use app\common\models\OrderGoods;
|
|
use app\common\models\UniAccount;
|
|
use app\common\services\income\IncomeService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yunshop\TeamDividend\admin\models\MemberChild;
|
|
|
|
class StoreManagerModel extends BaseModel{
|
|
|
|
protected $table = 'yz_store_manager';
|
|
public $timestamps = true;
|
|
protected $hidden = ['updated_at','deleted_at'];
|
|
protected $fillable = [
|
|
'uniacid',
|
|
'uid',
|
|
'status',
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at'
|
|
];
|
|
|
|
/**
|
|
* Common: 列表获取
|
|
* Author: wu-hui
|
|
* Time: 2023/09/15 17:43
|
|
* @param $pageSize
|
|
* @param $search
|
|
* @param string[] $field
|
|
* @return array
|
|
*/
|
|
public function getList($pageSize,$search,$field = ['*']){
|
|
// 条件生成
|
|
$where = [];
|
|
if($search['uid'] > 0) $where[] = ['uid','=',$search['uid']];
|
|
// 列表获取
|
|
$model = self::uniacid()
|
|
->select($field)
|
|
->where($where)
|
|
->when(!empty($search['nickname']),function($query) use ($search){
|
|
// 合伙人 昵称|真实姓名搜索
|
|
$ids = Member::where('nickname','like',"%{$search['nickname']}%")
|
|
->orwhere('realname','like',"%{$search['nickname']}%")
|
|
->pluck('uid');
|
|
if($ids) $ids = $ids->toArray();
|
|
$query->whereIn('uid',(array)$ids);
|
|
})
|
|
->with([
|
|
'member' => function($query){
|
|
$query->select(['uid','nickname','realname','avatar']);
|
|
}
|
|
])
|
|
->withCount(['record as sum_amount'=>function($query){
|
|
$query->select(DB::raw("sum(money) as sum_amount"));
|
|
},'record as sum_total_money'=>function($query){
|
|
$query->select(DB::raw("sum(total_money) as sum_total_money"));
|
|
}])
|
|
->orderBy('created_at','DESC')
|
|
->orderBy('id','DESC');
|
|
$list = $model->paginate($pageSize);
|
|
|
|
return $list ? $list->toArray() : [];
|
|
}
|
|
|
|
|
|
/**
|
|
* Common: 店长信息统计 - 初始化
|
|
* Author: wu-hui
|
|
* Time: 2023/09/18 14:46
|
|
*/
|
|
public function infoStatistics(){
|
|
set_time_limit(0);
|
|
try{
|
|
// 循环平台 进行处理
|
|
$uniAccount = UniAccount::getEnable() ?: [];
|
|
foreach ($uniAccount as $u) {
|
|
Setting::$uniqueAccountId = \YunShop::app()->uniacid = $u->uniacid;
|
|
$set = Setting::get('plugin.team_dividend');
|
|
$isRun = (boolean)$this->isRun($set);
|
|
// 判断:允许下一步 开启店长 店补比例大于0
|
|
if ($isRun && (int)$set['store_manager_switch'] == 1 && (float)$set['store_manager_proportion'] > 0) {
|
|
\Log::debug('--- 经销商 - 店长统计 - 满足条件,开始处理:'.$u->uniacid);
|
|
$this->infoStatisticsHandle($set);
|
|
} else {
|
|
\Log::debug('--- 经销商 - 店长统计 - 不满足条件:'.$u->uniacid);
|
|
}
|
|
}
|
|
}catch(\Exception $e){
|
|
\Log::debug('--- 经销商 - 店长统计 - 错误:'.$e->getMessage());
|
|
}
|
|
}
|
|
/**
|
|
* Common: 店长信息统计 - 判断是否允许执行统计
|
|
* Author: wu-hui
|
|
* Time: 2023/09/18 14:26
|
|
* @param $set
|
|
* @return bool
|
|
*/
|
|
public function isRun($set){
|
|
// 最后执行时间 如果没有最后执行时间则直接返回true
|
|
$endTime = StoreManagerRecordModel::uniacid()->max('end_time');
|
|
if($endTime <= 0) return true;
|
|
// 最后执行时间存在 判断:最后执行时间 + 一个周期 < 当前时间
|
|
$lastRunTime = strtotime(date("Y-m-d H:i:s",$endTime). " +1 month");
|
|
if($set['store_manager_cycle'] == 'yesterday') $lastRunTime = strtotime(date("Y-m-d H:i:s",$endTime). " +1 day");
|
|
else if($set['store_manager_cycle'] == 'last_week') $lastRunTime = strtotime(date("Y-m-d H:i:s",$endTime). " +1 week");
|
|
|
|
// 当前时间 大于 下一个周期执行时间 允许执行,否则不允许
|
|
return time() > $lastRunTime;
|
|
}
|
|
/**
|
|
* Common: 店长信息统计 - 开始处理
|
|
* Author: wu-hui
|
|
* Time: 2023/09/18 14:03
|
|
* @param $set
|
|
*/
|
|
public function infoStatisticsHandle($set){
|
|
// 获取所有店长
|
|
$storeManagerList = self::uniacid()
|
|
->select(['id','uid'])
|
|
->where('status',1)
|
|
->orderBy('id','DESC')
|
|
->get();
|
|
if(!$storeManagerList){
|
|
\Log::debug('--- 经销商 - 店长统计 - 没有店长信息');
|
|
return;
|
|
}
|
|
// 订单查询条件
|
|
$storeManagerCycle = $set['store_manager_cycle'] ?? 'last_month';
|
|
[$startTime,$endTime] = getTimeStamp($storeManagerCycle);
|
|
$where = [
|
|
['yz_order.pay_time','>=',$startTime],
|
|
['yz_order.pay_time','<',$endTime],
|
|
['yz_order_goods.payment_amount','>',0],// 实际支付金额必须大于0
|
|
];
|
|
// 循环处理店长
|
|
$storeIds = array_column($storeManagerList->toArray(),'uid');
|
|
$insertData = [];
|
|
$time = time();
|
|
foreach($storeManagerList as $storeInfo){
|
|
// 获取全部有效下级
|
|
$validChild = $this->getValidChild($storeInfo->uid,$storeIds);
|
|
$validChild[] = $storeInfo->uid;// 包括本人
|
|
// 获取团队全部订单,这里以订单商品的实际支付价格算;统计已支付-待发货,已支付-待收货,已完成订单
|
|
$totalMoney = (float)OrderGoods::uniacid()
|
|
->join('yz_order', 'yz_order_goods.order_id', '=','yz_order.id')
|
|
->where($where)
|
|
->whereIn('yz_order.status',[Order::WAIT_SEND,Order::WAIT_RECEIVE,Order::COMPLETE])
|
|
->whereIn('yz_order.uid',$validChild)
|
|
->sum('yz_order_goods.payment_amount');
|
|
$insertData[] = [
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'uid' => $storeInfo->uid,
|
|
'total_money' => $totalMoney,
|
|
'money' => sprintf("%.2f",$totalMoney * (float)$set['store_manager_proportion'] / 100),
|
|
'proportion' => (float)$set['store_manager_proportion'],
|
|
'start_time' => $startTime,
|
|
'end_time' => $endTime,
|
|
'created_at' => $time,
|
|
];
|
|
}
|
|
// 添加记录
|
|
StoreManagerRecordModel::insert($insertData);
|
|
|
|
return;
|
|
}
|
|
/**
|
|
* Common: 店长信息统计 - 获取店长全部有效下级(去除店长及其团队的全部下级)
|
|
* Author: wu-hui
|
|
* Time: 2023/09/18 13:34
|
|
* @param $uid
|
|
* @param $storeIds
|
|
* @return array
|
|
*/
|
|
public function getValidChild($uid,$storeIds){
|
|
$allChild = MemberChild::uniacid()->where('member_id',$uid)->pluck('child_id')->toArray();
|
|
$diff = array_values(array_intersect($allChild,$storeIds));// 获取交集 判断下级中是否存在店长
|
|
if(count($diff) > 0){
|
|
// 下级中存在店长 进行处理,剔除所有店长及其团队成员
|
|
$subAllChild = MemberChild::uniacid()->whereIn('member_id',$diff)->pluck('child_id')->toArray();
|
|
$delIds = array_values(array_merge($diff,$subAllChild));
|
|
$allChild = array_diff($allChild,$delIds);
|
|
}
|
|
|
|
return $allChild;
|
|
}
|
|
|
|
|
|
/**
|
|
* Common: 店长结算
|
|
* Author: wu-hui
|
|
* Time: 2023/09/21 17:27
|
|
*/
|
|
public function settlement(){
|
|
set_time_limit(0);
|
|
try{
|
|
// 循环平台 进行处理
|
|
$uniAccount = UniAccount::getEnable() ?: [];
|
|
foreach ($uniAccount as $u) {
|
|
Setting::$uniqueAccountId = \YunShop::app()->uniacid = $u->uniacid;
|
|
// 获取未结算列表
|
|
$list = StoreManagerRecordModel::uniacid()
|
|
->select(['id','uid','total_money','money','proportion'])
|
|
->where('is_settlement',0)
|
|
->get();
|
|
if($list){
|
|
$list = $list->toArray();
|
|
$this->settlementHandle($list);
|
|
// 修改为已经结算
|
|
$ids = array_column($list,'id');
|
|
StoreManagerRecordModel::whereIn('id',$ids)->update([
|
|
'is_settlement' => 1
|
|
]);
|
|
}
|
|
}
|
|
}catch(\Exception $e){
|
|
\Log::debug('--- 经销商 - 店长结算 - 错误:'.$e->getMessage());
|
|
}
|
|
}
|
|
/**
|
|
* Common: 店长结算 - 处理结算信息
|
|
* Author: wu-hui
|
|
* Time: 2023/09/21 17:23
|
|
* @param $list
|
|
*/
|
|
public function settlementHandle($list){
|
|
$incomeData = [];
|
|
foreach($list as $item){
|
|
if((float)$item['money'] > 0){
|
|
$incomeData[] = [
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'member_id' => $item['uid'],
|
|
'amount' => $item['money'],
|
|
'detail' => '',
|
|
'dividend_table_id' => $item['id'],
|
|
'dividend_code' => IncomeService::STORE_MANAGER,
|
|
'order_sn' => '',
|
|
];
|
|
}
|
|
}
|
|
IncomeService::insertIncome($incomeData);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Common: 一对一关联 - 用户信息表
|
|
* Author: wu-hui
|
|
* Time: 2023/09/15 17:43
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function member(){
|
|
return $this->hasOne(Member::class, 'uid', 'uid');
|
|
}
|
|
/**
|
|
* Common: 一对一关联 - 店长统计记录
|
|
* Author: wu-hui
|
|
* Time: 2023/09/18 15:03
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function record(){
|
|
return $this->hasMany(StoreManagerRecordModel::class, 'uid', 'uid');
|
|
}
|
|
|
|
|
|
} |