bztang-admin/plugins/area-dividend/src/services/UpgradeService.php

272 lines
8.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Yunshop\AreaDividend\services;
use app\common\exceptions\ShopException;
use app\common\facades\Setting;
use app\common\models\Member;
use app\common\models\member\MemberParent;
use app\common\models\Order;
use app\common\models\OrderGoods;
use Yunshop\AreaDividend\models\AreaDividendAgent;
use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
use Yunshop\TeamDividend\models\TeamDividendLevelModel;
class UpgradeService
{
protected $setting;
public $upgrade;
protected $memberId;
protected $member;
public function __construct($memberId = null)
{
$this->setting = Setting::get('plugin.area_dividend');
//升级条件的设置(不是总设置)
$this->upgrade = $this->setting['upgrade'];
$this->memberId = $memberId;
$this->member = Member::find($memberId);
}
/**
* 是否为区域代理
* @return bool
*/
protected function hasDividendAgent()
{
return AreaDividendAgent::uniacid()->where('status', 1)->where('member_id', $this->memberId)->exists();
}
/**
* 订单支付后
* @return void
*/
public function afterPay()
{
if ($this->upgrade['event_type'] == 1) {
\Log::debug('--区域分红升级条件开始--afterPay');
if ($this->verification()) {
$this->sendMessage();
}
\Log::debug('--区域分红升级条件--结束');
}
}
/**
* 订单完成后
* @return void
*/
public function afterComplete()
{
if ($this->upgrade['event_type'] == 2) {
\Log::debug('--区域分红升级条件开始--afterComplete');
if ($this->verification()) {
$this->sendMessage();
}
\Log::debug('--区域分红升级条件--结束');
}
}
/**
* 校验升级条件
* @return bool
*/
public function verification(): bool
{
\Log::debug('--区域分红升级条件--升级设置', $this->upgrade);
$result = $this->check();
if ($result['status'] == 0) {
\Log::debug('--区域分红升级条件--失败', $result['msg']);
return false;
}
$one_result = $this->hasConditionOne();
$two_result = $this->hasConditionTwo();
if (!$two_result['status'] && !$one_result['status']) {
\Log::debug('--UpgradeService--hasConditionTwo', $two_result['msg']);
\Log::debug('--UpgradeService--hasConditionOne', $one_result['msg']);
return false;
}
return true;
}
/**
* @return bool
*/
public function hasApply()
{
if ($this->hasDividendAgent()) {
return true;
}
$one_result = $this->hasConditionOne();
$two_result = $this->hasConditionTwo();
if ($one_result['status'] || $two_result['status']) {
return true;
}
return false;
}
protected function sendMessage()
{
try {
if (!$this->upgrade['template_id']) {
\Log::debug('--区域分红升级条件--没有设置模板');
return false;
} else {
MessageService::meetUpgradeMessage($this->upgrade['template_id'], $this->member, $this->member->uniacid);
}
} catch (ShopException $e) {
\Log::debug('--区域分红升级条件--模板信息发送失败:', $e->getMessage());
return false;
}
}
/**
* 升级条件1
* @return array
*/
public function hasConditionOne()
{
if (!$this->upgrade['push_level']) {
return $this->error('没有设置经销商等级!');
}
if (!$this->upgrade['push_num']) {
return $this->error('没有设置直推数量!');
}
if (!$this->upgrade['price']) {
return $this->error('没有设置消费金额!');
}
if (!$this->upgrade['level_type']) {
return $this->error('没有设置等级类型!');
}
if (!$this->upgrade['push_level']) {
return $this->error('没有设置经销商等级!');
}
$push_count = $this->getAgencyCount();
if ($push_count < $this->upgrade['push_num']) {
return $this->error('经销商直推数量不符合条件!--' . ($this->upgrade['level_type'] ? '本级' : '以上') . '--经销商数量=' . $push_count);
}
$self_price = $this->getSelfPrice();
if ($self_price < $this->upgrade['price']) {
return $this->error("没有设置累计消费!--self_price={$self_price}--upgrade_price={$this->upgrade['price']}");
}
return $this->success();
}
/**
* 升级条件2
* @return array
*/
public function hasConditionTwo()
{
if (empty($this->upgrade['goods_ids'])) {
return $this->error('没有设置指定商品');
}
if (!$this->hasBuyGoods()) {
return $this->error('不符合购买指定商品');
}
return $this->success();
}
/**
* 自身团队已消费金额
* @return float
*/
public function getSelfPrice()
{
$child_ids = MemberParent::where('parent_id', $this->memberId)->pluck('member_id');
return Order::whereIn('status', [1, 2, 3])->whereIn('uid', $child_ids)->sum('price');
}
/**
* 是否有购买指定商品
* @return mixed
*/
public function hasBuyGoods()
{
return OrderGoods::where('uid', $this->memberId)
->whereIn('goods_id', $this->upgrade['goods_ids'] ?: [])
->whereHas('hasOneOrder', function ($order) {
if ($this->upgrade['event_type'] == 1) {
$order->where('status', ">=", 1);
} elseif ($this->upgrade['event_type'] == 2) {
$order->where('status', 3);
}
})
->exists();
}
/**
* 经销商人数
* @return int
*/
public function getAgencyCount()
{
$child_ids = MemberParent::where('parent_id', $this->memberId)->where('level', 1)->pluck('member_id');
//经销商
$agency_list = TeamDividendAgencyModel::whereIn('uid', $child_ids)->where('is_black', 0)->get();
$push_count = 0;
if ($this->upgrade['level_type'] == 1) {
$push_count = $agency_list->filter(function ($agency) {
return $this->upgrade['push_level'] == $agency->level;
})->count();
} elseif ($this->upgrade['level_type'] == 2) {
$start_level = TeamDividendLevelModel::where('id', $this->upgrade['push_level'])->value('level_weight');
if ($start_level) {
//获取设置等级以上的等级
$level_ids = TeamDividendLevelModel::where('level_weight', '>=', $start_level)->pluck('id')->toArray();
$push_count = $agency_list->filter(function ($agency) use ($level_ids) {
return in_array($agency->level, $level_ids);
})->count();
}
}
return $push_count;
}
protected function success($data = null, $mgs = 'success')
{
return ['status' => 1, 'msg' => $mgs, 'data' => $data];
}
protected function error($mgs = 'error', $data = null)
{
return ['status' => 0, 'msg' => $mgs, 'data' => $data];
}
public function check()
{
//未开启区域分红 || 未开启升级条件设置
if ($this->setting['is_area_dividend'] != 1 || $this->upgrade['is_open'] != 1) {
return $this->error('未开启相关设置!');
}
if (!isset($this->memberId)) {
return $this->error('缺少会员id');
}
if (!app('plugins')->isEnabled('team-dividend')) {
return $this->error('未开启经销商管理插件!');
}
if ($this->hasDividendAgent()) {
return $this->error('已成为区域代理!');
}
return $this->success();
}
}