@@ -0,0 +1,362 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use think\Db;
|
||||
use app\admin\controller\shopro\Base;
|
||||
use addons\shopro\library\commission\Log as LogLibrary;
|
||||
use addons\shopro\exception\Exception;
|
||||
|
||||
/**
|
||||
* 分销商管理
|
||||
*
|
||||
*/
|
||||
class Agent extends Base
|
||||
{
|
||||
|
||||
// protected $relationSearch = true;
|
||||
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\shopro\commission\Agent
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\commission\Agent;
|
||||
$this->userModel = new \app\admin\model\shopro\user\User;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
$nobuildfields = ['nickname', 'mobile'];
|
||||
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
|
||||
|
||||
$total = $this->buildSearch()
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
|
||||
$list = $this->buildSearch()
|
||||
->with(['user' => function ($query) {
|
||||
return $query->withField('id, avatar, nickname, mobile, child_user_count, child_user_count_1, total_consume');
|
||||
}, 'agent_level' => function ($query) {
|
||||
return $query->withField('image, name, level, commission_rules');
|
||||
}, 'parent_agent' => function ($query) {
|
||||
return $query->withField('id, avatar, nickname');
|
||||
}])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
$this->success('查看分销商', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查看团队
|
||||
*/
|
||||
public function team()
|
||||
{
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
$filter = json_decode($this->request->get("filter", ''), true);
|
||||
$parent_user_id = $filter && isset($filter['parent_user_id']) ? $filter['parent_user_id'] : 0;
|
||||
|
||||
$nobuildfields = ['level'];
|
||||
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
|
||||
|
||||
$sort = $sort == 'user_id' ? 'id' : $sort; // 这里用的是 userModel , sort 会自动取 this->model 的主键(user_id)
|
||||
$total = $this->buildUserSearch()
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
|
||||
$list = $this->buildUserSearch()
|
||||
->with(['agent.agentLevel'])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
$parentUser = $this->userModel->with(['agent.agentLevel'])->where('id', $parent_user_id)->find();
|
||||
|
||||
$result = array("total" => $total, "rows" => $list, 'parent_user' => $parentUser);
|
||||
|
||||
$this->success('查看分销商', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 分销商详情
|
||||
*/
|
||||
public function profile($id)
|
||||
{
|
||||
$row = $this->model->with(['user' => function ($query) {
|
||||
return $query->withField('id, avatar, nickname, mobile, child_user_count, child_user_count_1, child_user_count_2, total_consume');
|
||||
}, 'agent_level' => function ($query) {
|
||||
return $query->withField('image, name, level, commission_rules');
|
||||
}, 'parent_agent' => function ($query) {
|
||||
return $query->withField('id, avatar, nickname');
|
||||
}])->where('user_id', $id)->find();
|
||||
if (!$row) {
|
||||
$this->error('未找到分销商');
|
||||
}
|
||||
if ($this->request->isAjax()) {
|
||||
$this->success('分销商详情', null, $row);
|
||||
}
|
||||
$this->assignconfig('row', $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新信息
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$agent = new \addons\shopro\library\commission\Agent($id);
|
||||
if (!$agent->agent) {
|
||||
$this->error('未找到分销商');
|
||||
}
|
||||
$key = $this->request->post('key');
|
||||
$value = $this->request->post('value');
|
||||
$data = null;
|
||||
$runUpgradeAgent = true; // 升级当前分销商
|
||||
$runUpgradeLastAgentId = 0; // 升级原分销商
|
||||
$result = Db::transaction(function () use ($agent, $key, $value, &$data, &$runUpgradeAgent, &$runUpgradeLastAgentId) {
|
||||
switch ($key) {
|
||||
case 'status': // 变更分销商状态
|
||||
$data = $value;
|
||||
if ($value === 'normal' && $agent->user->parent_user_id === 0 && $agent->agent->parent_agent_id !== 0) {
|
||||
$agent->user->parent_user_id = $agent->agent->parent_agent_id;
|
||||
$agent->user->save();
|
||||
\addons\shopro\library\commission\Log::write($agent->agent->parent_agent_id, 'share', ['user' => $agent->user], ['oper_type' => 'user', 'oper_id' => $agent->user->id]);
|
||||
}
|
||||
break;
|
||||
case 'level': // 变更分销商等级
|
||||
$data = \app\admin\model\shopro\commission\Level::get($value);
|
||||
if (!$data) {
|
||||
new Exception('未找到该等级');
|
||||
}
|
||||
break;
|
||||
case 'level_status': // 审核分销商升级
|
||||
if ($agent->agent->level_status == 0) {
|
||||
new Exception('该分销商没有审核升级条件');
|
||||
} else {
|
||||
$level = \app\admin\model\shopro\commission\Level::get(['level' => $agent->agent->level_status]);
|
||||
if ($value != 0) {
|
||||
if ($level) {
|
||||
$data['level'] = $level;
|
||||
$data['action'] = 'agree';
|
||||
$agent->agent->level = $level->level;
|
||||
$agent->agent->save();
|
||||
} else {
|
||||
new Exception('该等级不存在');
|
||||
}
|
||||
} else {
|
||||
$data['action'] = 'reject';
|
||||
}
|
||||
$value = 0;
|
||||
}
|
||||
break;
|
||||
case 'parent_agent_id': // 变更分销商上级
|
||||
$data = null;
|
||||
if ($agent->agent->parent_agent_id == $value) {
|
||||
new Exception('请勿重复选择');
|
||||
}
|
||||
if ($agent->agent->user_id == $value) {
|
||||
new Exception('不能绑定本人');
|
||||
}
|
||||
if ($value != 0) {
|
||||
$data = \app\admin\model\shopro\user\User::get($value);
|
||||
if (!$data) {
|
||||
new Exception('未找到该分销商');
|
||||
}
|
||||
if (!$this->checkChangeParentAgent($agent->agent->user_id, $value)) {
|
||||
new Exception('不能绑定该分销商');
|
||||
}
|
||||
}
|
||||
$runUpgradeLastAgentId = $agent->agent->parent_agent_id;
|
||||
$agent->user->parent_user_id = $value;
|
||||
$agent->user->save();
|
||||
break;
|
||||
case 'upgrade_lock':
|
||||
case 'apply_info':
|
||||
case 'info_status':
|
||||
if ($value === '') {
|
||||
$value = null;
|
||||
}
|
||||
$data = $value;
|
||||
$runUpgradeAgent = false;
|
||||
break;
|
||||
}
|
||||
return $agent->agent->allowField('status,level,apply_info,parent_agent_id,upgrade_lock,info_status,level_status')->save([
|
||||
$key => $value
|
||||
]);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
if ($runUpgradeAgent) {
|
||||
$agent = new \addons\shopro\library\commission\Agent($agent->agent->user_id);
|
||||
$agent->asyncAgentUpgrade($agent->agent->user_id);
|
||||
|
||||
|
||||
if ($runUpgradeLastAgentId) {
|
||||
$agent->asyncAgentUpgrade($runUpgradeLastAgentId);
|
||||
}
|
||||
}
|
||||
LogLibrary::write($agent->agent->user_id, 'agent', ['type' => $key, $key => $data]);
|
||||
return $this->success('更新成功', null, $agent->agent);
|
||||
} else {
|
||||
return $this->error('更新失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
$searchWhere = $this->request->request('searchWhere');
|
||||
$total = $this->model
|
||||
->where($where)
|
||||
->whereOr('id', '=', $searchWhere)
|
||||
->whereOr('nickname', 'like', "%$searchWhere%")
|
||||
->whereOr('mobile', 'like', "%$searchWhere%")
|
||||
->order($sort, $order)
|
||||
->field('id, nickname, mobile, avatar')
|
||||
->count();
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->whereOr('id', '=', $searchWhere)
|
||||
->whereOr('nickname', 'like', "%$searchWhere%")
|
||||
->whereOr('mobile', 'like', "%$searchWhere%")
|
||||
->order($sort, $order)
|
||||
->field('id, nickname, mobile, avatar')
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
$this->success('选择用户', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分销商搜索
|
||||
*/
|
||||
public function buildSearch()
|
||||
{
|
||||
$filter = $this->request->get("filter", '');
|
||||
$filter = (array)json_decode($filter, true);
|
||||
$filter = $filter ? $filter : [];
|
||||
|
||||
$nickname = isset($filter['nickname']) ? $filter['nickname'] : '';
|
||||
$mobile = isset($filter['mobile']) ? $filter['mobile'] : '';
|
||||
|
||||
$name = $this->model->getQuery()->getTable();
|
||||
$tableName = $name . '.';
|
||||
|
||||
$agents = $this->model;
|
||||
|
||||
if ($nickname || $mobile) {
|
||||
$agents = $agents->whereExists(function ($query) use ($nickname, $mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . 'user_id');
|
||||
|
||||
if ($nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$nickname}%");
|
||||
}
|
||||
|
||||
if ($mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
return $agents;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 用户 team 搜索
|
||||
*/
|
||||
public function buildUserSearch()
|
||||
{
|
||||
$filter = $this->request->get("filter", '');
|
||||
$filter = (array)json_decode($filter, true);
|
||||
$filter = $filter ? $filter : [];
|
||||
|
||||
$level = isset($filter['level']) ? $filter['level'] : '';
|
||||
|
||||
$name = $this->userModel->getQuery()->getTable();
|
||||
$tableName = $name . '.';
|
||||
|
||||
$users = $this->userModel;
|
||||
|
||||
if ($level) {
|
||||
$users = $users->whereExists(function ($query) use ($level, $tableName) {
|
||||
$agentTableName = (new \app\admin\model\shopro\commission\Agent())->getQuery()->getTable();
|
||||
|
||||
$query = $query->table($agentTableName)->where($tableName . 'id=' . $agentTableName . '.user_id')
|
||||
->where('level', $level);
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
|
||||
// 递归往上找推荐人,防止出现推荐闭环
|
||||
private function checkChangeParentAgent($agentId, $parentAgentId)
|
||||
{
|
||||
if ($agentId === $parentAgentId) {
|
||||
return false;
|
||||
}
|
||||
if ($parentAgentId == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$parentAgent = \app\admin\model\shopro\commission\Agent::get($parentAgentId);
|
||||
if ($parentAgent) {
|
||||
if ($parentAgent->parent_agent_id === 0) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->checkChangeParentAgent($agentId, $parentAgent->parent_agent_id);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* 分销配置
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Config extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* 分销设置模型对象
|
||||
* @var \app\admin\model\shopro\commission\Config
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\commission\Config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if($this->request->isAjax()) {
|
||||
$data = [];
|
||||
if (checkEnv('commission', false)) {
|
||||
$data = $this->model->column('name, value');
|
||||
}
|
||||
|
||||
$this->success('分销设置', null, $data);
|
||||
}
|
||||
|
||||
$this->assignconfig('is_upgrade', checkEnv('commission', false) ? false : true);
|
||||
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
checkEnv('commission');
|
||||
|
||||
$params = $this->request->post();
|
||||
foreach($params as $k => $p) {
|
||||
$this->model->where('name', $k)->update([
|
||||
'value' => $p
|
||||
]);
|
||||
}
|
||||
$this->success('更新成功');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* 分销商管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Goods extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Goods模型对象
|
||||
* @var \app\admin\model\shopro\goods\Goods
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
$this->loadlang('shopro/goods/goods');
|
||||
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\goods\Goods;
|
||||
$this->view->assign("typeList", $this->model->getTypeList());
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//当前是否为关联查询
|
||||
$this->relationSearch = false;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
// list($where, $sort, $order, $offset, $limit) = $this->buildparams('title');
|
||||
$sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
|
||||
$sort = $sort == 'price' ? 'convert(`price`, DECIMAL(10, 2))' : $sort;
|
||||
$order = $this->request->get("order", "DESC");
|
||||
$offset = $this->request->get("offset", 0);
|
||||
$limit = $this->request->get("limit", 0);
|
||||
$activity_type = $this->request->get("activity_type", 'all'); // 活动类型
|
||||
|
||||
$total = $this->buildSearchOrder()->count();
|
||||
|
||||
// 构建查询数据条件
|
||||
$list = $this->buildSearchOrder();
|
||||
|
||||
$subsql = \app\admin\model\shopro\goods\SkuPrice::where('status', 'up')->field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
|
||||
$goodsTableName = $this->model->getQuery()->getTable();
|
||||
// 关联规格表,获取总库存
|
||||
$list = $list->join([$subsql => 'w'], $goodsTableName . '.id = w.sku_goods_id', 'left');
|
||||
|
||||
// 关联查询当前商品的活动,一个商品可能存在多条活动记录,使用 group_concat 搜集所有活动类型,关联条件 只有 find_in_set 会存在一个商品出现多次,所以使用 group
|
||||
$actSubSql = \app\admin\model\shopro\activity\Activity::where('starttime', '<=', time())->where('endtime', '>=', time())->buildSql();
|
||||
$list = $list->join([$actSubSql => 'act'], "(find_in_set(" . $goodsTableName . ".id, act.goods_ids) or act.goods_ids = '')", 'left');
|
||||
|
||||
// 关联查询当前商品是否设置有积分
|
||||
$scoreSubSql = \app\admin\model\shopro\app\ScoreSkuPrice::field("'score' as app_type, goods_id as score_goods_id")->group('score_goods_id')->buildSql();
|
||||
$list = $list->join([$scoreSubSql => 'score'], $goodsTableName . '.id = score.score_goods_id', 'left');
|
||||
|
||||
// 关闭 sql mode 的 ONLY_FULL_GROUP_BY
|
||||
$oldModes = closeStrict(['ONLY_FULL_GROUP_BY']);
|
||||
|
||||
$list = $list->field("$goodsTableName.*, w.*,score.*,group_concat(act.type) as activity_type, act.goods_ids")
|
||||
->group('id')
|
||||
->orderRaw($sort . ' ' . $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
// 恢复 sql mode
|
||||
recoverStrict($oldModes);
|
||||
|
||||
foreach ($list as $row) {
|
||||
$row->commission = \app\admin\model\shopro\commission\Goods::get($row->id);
|
||||
$row->visible(['id', 'type', 'activity_id', 'activity_type', 'is_sku', 'app_type', 'title', 'status', 'weigh', 'category_ids', 'image', 'price', 'likes', 'views', 'sales', 'stock', 'show_sales', 'dispatch_type', 'updatetime', 'commission']);
|
||||
}
|
||||
$list = collection($list)->toArray();
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
if ($this->request->get("page_type") == 'select') {
|
||||
return json($result);
|
||||
}
|
||||
|
||||
return $this->success('操作成功', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post();
|
||||
$goods_ids = explode(',', $params['ids']);
|
||||
unset($params['ids']);
|
||||
if ($params) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach($goods_ids as $goods_id) {
|
||||
$commissionGoods = \app\admin\model\shopro\commission\Goods::get($goods_id);
|
||||
if($commissionGoods) {
|
||||
$commissionGoods->allowField(true)->save($params);
|
||||
}else {
|
||||
$params['goods_id'] = $goods_id;
|
||||
\app\admin\model\shopro\commission\Goods::insert($params);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
} catch (ValidateException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (PDOException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success('编辑成功');
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}else{
|
||||
if(!$ids) {
|
||||
$ids = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->where('id', 'in', $ids)->select();
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if(count($row) == 1) {
|
||||
$row[0]->commission = \app\admin\model\shopro\commission\Goods::get($row[0]->id);
|
||||
$row[0]->sku_price = \app\admin\model\shopro\goods\SkuPrice::all(['goods_id' => $row[0]->id]);
|
||||
|
||||
}
|
||||
$this->assignconfig('row', $row);
|
||||
$this->assignconfig('defaultCommissionConfig', \app\admin\model\shopro\commission\Config::where([
|
||||
'name' => ['in', ['commission_level', 'self_buy', 'commission_price_type', 'commission_event']]
|
||||
])->column('value', 'name'));
|
||||
return $this->view->fetch();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//参与分销
|
||||
public function commission_status()
|
||||
{
|
||||
$params = $this->request->post();
|
||||
\app\admin\model\shopro\commission\Goods::where('goods_id', 'in', $params['ids'])->update([
|
||||
'status' => $params['status']
|
||||
]);
|
||||
$this->success('更新成功');
|
||||
}
|
||||
|
||||
// 构建查询条件
|
||||
private function buildSearchOrder()
|
||||
{
|
||||
$goods_title = $this->request->get("goods_title", ''); // 关键字
|
||||
$status = $this->request->get("status", 'all');
|
||||
$goods_type = $this->request->get("goods_type", 'all');
|
||||
$activity_type = $this->request->get("activity_type", 'all');
|
||||
$app_type = $this->request->get("app_type", 'all');
|
||||
$min_price = $this->request->get("min_price", "");
|
||||
$max_price = $this->request->get("max_price", "");
|
||||
$category_id = $this->request->get('category_id', 0);
|
||||
$self_rules = $this->request->get('self_rules', 'all');
|
||||
$commission_goods_status = $this->request->get('commission_goods_status', 'all');
|
||||
|
||||
$tableName = $this->model->getQuery()->getTable();
|
||||
$tableName = $tableName . '.';
|
||||
|
||||
$goods = $this->model;
|
||||
|
||||
if ($goods_title) {
|
||||
// 模糊搜索字段
|
||||
$searcharr = ['title'];
|
||||
foreach ($searcharr as $k => &$v) {
|
||||
$v = stripos($v, ".") === false ? $tableName . $v : $v;
|
||||
}
|
||||
unset($v);
|
||||
$goods = $goods->where(function ($query) use ($searcharr, $goods_title, $tableName) {
|
||||
$query->where(implode("|", $searcharr), "LIKE", "%{$goods_title}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($goods_type != 'all') {
|
||||
$goods = $goods->where($tableName . 'type', $goods_type);
|
||||
}
|
||||
|
||||
$goods_ids = [];
|
||||
// 活动
|
||||
if ($activity_type != 'all') {
|
||||
// 同一请求,会组装两次请求条件,缓存 10 秒
|
||||
$activities = \app\admin\model\shopro\activity\Activity::cache(10)->where('type', $activity_type)->column('goods_ids');
|
||||
foreach ($activities as $key => $goods_id) {
|
||||
$ids = explode(',', $goods_id);
|
||||
$goods_ids = array_merge($goods_ids, $ids);
|
||||
}
|
||||
}
|
||||
|
||||
// 积分
|
||||
if ($app_type == 'score') {
|
||||
$score_goods_ids = \app\admin\model\shopro\app\ScoreSkuPrice::cache(10)->group('goods_id')->column('goods_id');
|
||||
$goods_ids = array_merge($goods_ids, $score_goods_ids);
|
||||
}
|
||||
|
||||
$goods_ids = array_filter(array_unique($goods_ids));
|
||||
if ($goods_ids) {
|
||||
$goods = $goods->where($tableName . 'id', 'in', $goods_ids);
|
||||
} else {
|
||||
if ($activity_type != 'all' || $app_type != 'all') {
|
||||
// 搜了活动,但是 goods_ids 为空,这时候搜索结果应该为空
|
||||
$goods = $goods->where($tableName . 'id', 'in', $goods_ids);
|
||||
}
|
||||
}
|
||||
|
||||
// 价格
|
||||
if ($min_price != '') {
|
||||
$goods = $goods->where('convert(`price`, DECIMAL(10, 2)) >= ' . round($min_price, 2));
|
||||
}
|
||||
if ($max_price != '') {
|
||||
$goods = $goods->where('convert(`price`, DECIMAL(10, 2)) <= ' . round($max_price, 2));
|
||||
}
|
||||
|
||||
// 商品状态
|
||||
if ($status != 'all') {
|
||||
$goods = $goods->where('status', $status);
|
||||
}
|
||||
|
||||
if(isset($category_id) && $category_id != 0) {
|
||||
$category_ids = [];
|
||||
// 查询分类所有子分类,包括自己
|
||||
$category_ids = \addons\shopro\model\Category::getCategoryIds($category_id);
|
||||
|
||||
|
||||
$goods = $goods->where(function ($query) use ($category_ids) {
|
||||
// 所有子分类使用 find_in_set or 匹配,亲测速度并不慢
|
||||
foreach($category_ids as $key => $category_id) {
|
||||
$query->whereOrRaw("find_in_set($category_id, category_ids)");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 分销规则
|
||||
if ($self_rules != 'all' && ($commission_goods_status == 'all' || $commission_goods_status == 1)) {
|
||||
// 这肯定是参与的,或者全部
|
||||
$goods = $goods->whereExists(function ($query) use ($self_rules, $tableName) {
|
||||
$commissionGoodsName = (new \app\admin\model\shopro\commission\Goods())->getQuery()->getTable();
|
||||
$query = $query->table($commissionGoodsName)->where($commissionGoodsName . '.goods_id=' . $tableName . 'id');
|
||||
|
||||
$query = $query->where('self_rules', $self_rules);
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
// 查询参与分销的
|
||||
if ($commission_goods_status != 'all') {
|
||||
$goods = $goods->where(function ($query) use ($commission_goods_status, $tableName) {
|
||||
$query->whereExists(function ($query) use ($commission_goods_status, $tableName) {
|
||||
$commissionGoodsName = (new \app\admin\model\shopro\commission\Goods())->getQuery()->getTable();
|
||||
$query = $query->table($commissionGoodsName)->where($commissionGoodsName . '.goods_id=' . $tableName . 'id');
|
||||
|
||||
$query = $query->where('status', $commission_goods_status);
|
||||
|
||||
return $query;
|
||||
});
|
||||
|
||||
// 查询不参与分销的(存在 commission goods 并且 status = 1 或者 不存在 commission_goods)
|
||||
if ($commission_goods_status == 0) {
|
||||
$query = $query->whereOr(function ($query) use ($commission_goods_status, $tableName) {
|
||||
$query = $query->whereNotExists(function ($query) use ($commission_goods_status, $tableName) {
|
||||
$commissionGoodsName = (new \app\admin\model\shopro\commission\Goods())->getQuery()->getTable();
|
||||
$query = $query->table($commissionGoodsName)->where($commissionGoodsName . '.goods_id=' . $tableName . 'id');
|
||||
return $query;
|
||||
});
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
return $goods;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use Exception;
|
||||
use app\admin\model\shopro\commission\Level as LevelModel;
|
||||
|
||||
|
||||
/**
|
||||
* 分销商等级管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Level extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Level模型对象
|
||||
* @var \app\admin\model\shopro\commission\Level
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new LevelModel();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$defaultLevel = $this->model->get(['level' => 1]);
|
||||
if(!$defaultLevel) {
|
||||
$this->model->save([
|
||||
'name' => '默认等级',
|
||||
'level' => 1,
|
||||
'commission_rules' => '{"commission_1":"0.00","commission_2":"0.00","commission_3":"0.00"}'
|
||||
]);
|
||||
}
|
||||
if($this->request->isAjax()) {
|
||||
$data = $this->model->order('level asc')->select();
|
||||
$this->success('分销商等级', null, $data);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post();
|
||||
if ($params) {
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
Db::commit();
|
||||
} catch (ValidateException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (PDOException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$this->getExistLevel();
|
||||
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($level = null)
|
||||
{
|
||||
if(!$level) {
|
||||
$level = $this->request->get('level');
|
||||
}
|
||||
$row = $this->model->get($level);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post();
|
||||
if ($params) {
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
if($level != $params['new_level']) {
|
||||
$params['level'] = $params['new_level'];
|
||||
$result = LevelModel::strict(false)->insert($params);
|
||||
$result = $row->delete();
|
||||
}else {
|
||||
$result = $row->allowField(true)->save($params);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (ValidateException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (PDOException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$this->getExistLevel();
|
||||
$this->assignconfig('row', $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
private function getExistLevel()
|
||||
{
|
||||
$existLevel = $this->model->column('level');
|
||||
$this->assignconfig('existLevel', $existLevel);
|
||||
return $existLevel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use app\admin\controller\shopro\Base;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* 分销动态
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Log extends Base
|
||||
{
|
||||
|
||||
/**
|
||||
* Order模型对象
|
||||
* @var \app\admin\model\shopro\commission\Log
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $noNeedRight = ['getEventAll'];
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\commission\Log;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
$nobuildfields = [
|
||||
'agent_nickname', 'agent_mobile',
|
||||
'oper_nickname', 'oper_mobile',
|
||||
];
|
||||
|
||||
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
|
||||
|
||||
$total = $this->buildSearch()
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
|
||||
// 查询分页数据
|
||||
$list = $this->buildSearch()
|
||||
->with(['agent', 'oper']) // 默认oper 只关联用户,下面循环关联管理员
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
$list = collection($list)->toArray();
|
||||
foreach ($list as $key => $log) {
|
||||
if ($log['oper_type'] == 'admin') {
|
||||
$list[$key]['oper'] = $log['oper_id'] ? \app\admin\model\Admin::field('id,username,nickname,avatar')->where('id', $log['oper_id'])->find() : null;
|
||||
}
|
||||
}
|
||||
|
||||
$result = [
|
||||
"total" => $total,
|
||||
"rows" => $list,
|
||||
];
|
||||
|
||||
$this->success('分销动态', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
// 获取所有 event
|
||||
public function getEventAll() {
|
||||
$event = $this->model->getEventList();
|
||||
|
||||
$newData[] = ['name' => '全部', 'type' => 'all'];
|
||||
|
||||
foreach ($event as $k => $v) {
|
||||
$newData[] = [
|
||||
'name' => $v,
|
||||
'type' => $k
|
||||
];
|
||||
}
|
||||
|
||||
return $this->success('获取成功', null, $newData);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function buildSearch()
|
||||
{
|
||||
$filter = $this->request->get("filter", '');
|
||||
$filter = (array)json_decode($filter, true);
|
||||
$filter = $filter ? $filter : [];
|
||||
|
||||
// 操作人
|
||||
$oper_type = isset($filter['oper_type']) ? $filter['oper_type'] : 'all';
|
||||
$oper_nickname = isset($filter['oper_nickname']) ? $filter['oper_nickname'] : '';
|
||||
$oper_mobile = isset($filter['oper_mobile']) ? $filter['oper_mobile'] : '';
|
||||
// 分销商
|
||||
$agent_nickname = isset($filter['agent_nickname']) ? $filter['agent_nickname'] : '';
|
||||
$agent_mobile = isset($filter['agent_mobile']) ? $filter['agent_mobile'] : '';
|
||||
|
||||
// 当前表名
|
||||
$tableName = $this->model->getQuery()->getTable();
|
||||
|
||||
$logs = $this->model;
|
||||
|
||||
// 分销商
|
||||
if ($agent_nickname || $agent_mobile) {
|
||||
$logs = $logs->whereExists(function ($query) use ($agent_nickname, $agent_mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.agent_id');
|
||||
|
||||
if ($agent_nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$agent_nickname}%");
|
||||
}
|
||||
|
||||
if ($agent_mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$agent_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
if ($oper_type != 'all') {
|
||||
if ($oper_type == 'user') {
|
||||
if ($oper_nickname || $oper_mobile) {
|
||||
$logs = $logs->whereExists(function ($query) use ($oper_nickname, $oper_mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.oper_id');
|
||||
|
||||
if ($oper_nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$oper_nickname}%");
|
||||
}
|
||||
|
||||
if ($oper_mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$oper_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
} else if ($oper_type == 'admin') {
|
||||
if ($oper_nickname) {
|
||||
$logs = $logs->whereExists(function ($query) use ($oper_nickname, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.oper_id');
|
||||
|
||||
if ($oper_nickname) {
|
||||
$query = $query->where(function ($query) use ($oper_nickname) {
|
||||
$query->where('nickname', 'like', "%{$oper_nickname}%")->whereOr('username', 'like', "%{$oper_nickname}%");
|
||||
});
|
||||
}
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return $logs;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use app\admin\controller\shopro\Base;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* 分销订单管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Order extends Base
|
||||
{
|
||||
|
||||
/**
|
||||
* Order模型对象
|
||||
* @var \app\admin\model\shopro\commission\Order
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
$this->loadlang('shopro/order/order_item');
|
||||
$this->loadlang('shopro/order/order');
|
||||
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\commission\Order;
|
||||
$this->rewardModel = new \app\admin\model\shopro\commission\Reward;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
$nobuildfields = [
|
||||
'buyer_nickname','buyer_mobile',
|
||||
'agent_nickname','agent_mobile',
|
||||
'commission_user_id', 'commission_nickname', 'commission_mobile',
|
||||
'order_createtime', 'order_sn','goods_title',
|
||||
];
|
||||
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
|
||||
|
||||
// 查询所有数据,计算统计数据
|
||||
$total = $this->buildSearch()
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
// 查询分页数据
|
||||
$list = $this->buildSearch()
|
||||
->with(['order' => function ($query) {
|
||||
$query->removeOption('soft_delete')->field('id,type,order_sn,user_id,activity_type,goods_amount,status,total_amount,score_amount,total_fee,pay_fee,score_fee,pay_type,paytime,platform,ext,createtime,updatetime');
|
||||
}, 'item' => function ($query) {
|
||||
$query->field('id,user_id,order_id,goods_id,goods_type,activity_id,activity_type,goods_sku_text,goods_title,goods_image,goods_original_price,discount_fee,goods_price,goods_num,dispatch_status,dispatch_fee,dispatch_type,aftersale_status,comment_status,refund_status,refund_fee,ext,createtime,updatetime');
|
||||
}, 'agent', 'reward.agent', 'buyer' => function ($query) {
|
||||
$query->field('id,nickname,avatar,mobile,prevtime,logintime,jointime');
|
||||
}])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
$list = collection($list)->toArray();
|
||||
|
||||
// 处理分页数据格式
|
||||
$newList = [];
|
||||
foreach ($list as $key => $data) {
|
||||
$rewards = $data['reward'];
|
||||
|
||||
$commission_total = 0;
|
||||
$commissioned_total = 0;
|
||||
foreach ($rewards as $reward) {
|
||||
if ($reward['status'] == 1) {
|
||||
$commissioned_total += $reward['commission'];
|
||||
}
|
||||
$commission_total += $reward['commission'];
|
||||
}
|
||||
$data['commission_total'] = number_format($commission_total, 2, '.', '');
|
||||
$data['commissioned_total'] = number_format($commissioned_total, 2, '.', '');
|
||||
$newList[] = $data;
|
||||
}
|
||||
|
||||
// 处理顶部统计数据
|
||||
$amount_total = 0; // 商品结算总金额
|
||||
$commission_total = 0; // 分佣总金额
|
||||
$commission_waiting_total = 0; // 待结算
|
||||
$commissioned_total = 0; // 已结算
|
||||
$commission_back_total = 0; // 已退回
|
||||
$commission_cancel_total = 0; // 已取消
|
||||
|
||||
$total_page = intval(ceil($total / 1000));
|
||||
for ($i = 0; $i < $total_page; $i++) {
|
||||
$totalList = $this->buildSearch()
|
||||
->with(['reward' => function ($query) {
|
||||
$query->field('id,status,commission,commission_order_id');
|
||||
}])
|
||||
->field('id,amount,commission_reward_status,commission_event')
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit(($i * 1000), 1000)
|
||||
->select();
|
||||
|
||||
foreach ($totalList as $key => $li) {
|
||||
$amount_total += $li['amount'];
|
||||
foreach ($li['reward'] as $reward) {
|
||||
if ($reward['status'] == 1) {
|
||||
$commissioned_total += $reward['commission'];
|
||||
} else if ($reward['status'] == -1) {
|
||||
$commission_back_total += $reward['commission'];
|
||||
} else if ($reward['status'] == 0) {
|
||||
$commission_waiting_total += $reward['commission'];
|
||||
} else if ($reward['status'] == -2) {
|
||||
$commission_cancel_total += $reward['commission'];
|
||||
}
|
||||
$commission_total += $reward['commission'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = [
|
||||
"total" => $total,
|
||||
"rows" => $newList,
|
||||
"amount_total" => number_format($amount_total, 2, '.', ''),
|
||||
"commission_total" => number_format($commission_total, 2, '.', ''),
|
||||
"commissioned_total" => number_format($commissioned_total, 2, '.', ''),
|
||||
"commission_back_total" => number_format($commission_back_total, 2, '.', ''),
|
||||
"commission_waiting_total" => number_format($commission_waiting_total, 2, '.', ''),
|
||||
"commission_cancel_total" => number_format($commission_cancel_total, 2, '.', '')
|
||||
];
|
||||
|
||||
$this->success('分销订单', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分销订单结算
|
||||
*
|
||||
* @param int $id 分销订单ID
|
||||
*/
|
||||
public function runCommission($id = null) {
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
$result = Db::transaction(function () use ($row) {
|
||||
return (new \addons\shopro\library\commission\Reward)->runCommissionRewardByOrder('admin', $row);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('结算成功');
|
||||
} else {
|
||||
$this->error('订单已结算或没有要结算的佣金');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分销订单退回
|
||||
*
|
||||
* @param int $id 分销订单ID
|
||||
* @param int $delete 是否删除业绩
|
||||
*/
|
||||
public function backCommission($id = null, $delete = 0)
|
||||
{
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
$result = Db::transaction(function () use ($row, $delete) {
|
||||
return (new \addons\shopro\library\commission\Reward)->backCommissionRewardByOrder('admin', $row, $delete);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('退回成功');
|
||||
} else {
|
||||
$this->error('订单已退回或未结算');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分销订单取消
|
||||
*
|
||||
* @param int $id 分销订单ID
|
||||
* @param int $delete 是否删除业绩
|
||||
*/
|
||||
public function cancelCommission($id = null, $delete = 0)
|
||||
{
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
$result = Db::transaction(function () use ($row, $delete) {
|
||||
return (new \addons\shopro\library\commission\Reward)->backCommissionRewardByOrder('admin', $row, $delete);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('退回成功');
|
||||
} else {
|
||||
$this->error('订单已退回或未结算');
|
||||
}
|
||||
}
|
||||
|
||||
private function buildSearch()
|
||||
{
|
||||
$filter = $this->request->get("filter", '');
|
||||
$filter = (array)json_decode($filter, true);
|
||||
$filter = $filter ? $filter : [];
|
||||
|
||||
// 用户'parent_nickname','parent_mobile',
|
||||
// 购买人
|
||||
$buyer_nickname = isset($filter['buyer_nickname']) ? $filter['buyer_nickname'] : '';
|
||||
$buyer_mobile = isset($filter['buyer_mobile']) ? $filter['buyer_mobile'] : '';
|
||||
// 推广用户
|
||||
$agent_nickname = isset($filter['agent_nickname']) ? $filter['agent_nickname'] : '';
|
||||
$agent_mobile = isset($filter['agent_mobile']) ? $filter['agent_mobile'] : '';
|
||||
// 分销用户
|
||||
$commission_user_id = isset($filter['commission_user_id']) ? $filter['commission_user_id'] : 0;
|
||||
$commission_nickname = isset($filter['commission_nickname']) ? $filter['commission_nickname'] : '';
|
||||
$commission_mobile = isset($filter['commission_mobile']) ? $filter['commission_mobile'] : '';
|
||||
|
||||
// 时间区间
|
||||
$order_createtime = isset($filter['order_createtime']) ? $filter['order_createtime'] : '';
|
||||
$order_createtime = array_filter(explode(' - ', $order_createtime));
|
||||
// 订单 & 商品
|
||||
$order_sn = isset($filter['order_sn']) ? $filter['order_sn'] : '';
|
||||
$goods_title = isset($filter['goods_title']) ? $filter['goods_title'] : '';
|
||||
|
||||
// 当前表名
|
||||
$tableName = $this->model->getQuery()->getTable();
|
||||
|
||||
$orders = $this->model;
|
||||
|
||||
// 购买人查询
|
||||
if ($buyer_nickname || $buyer_mobile) {
|
||||
$orders = $orders->whereExists(function ($query) use ($buyer_nickname, $buyer_mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.buyer_id');
|
||||
|
||||
if ($buyer_nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$buyer_nickname}%");
|
||||
}
|
||||
|
||||
if ($buyer_mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$buyer_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
// 推广人昵称,手机号
|
||||
if ($agent_nickname || $agent_mobile) {
|
||||
$orders = $orders->whereExists(function ($query) use ($agent_nickname, $agent_mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.agent_id');
|
||||
|
||||
if ($agent_nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$agent_nickname}%");
|
||||
}
|
||||
|
||||
if ($agent_mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$agent_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 分销用户查询
|
||||
if ($commission_user_id || $commission_nickname || $commission_mobile) {
|
||||
$orders = $orders->whereExists(function ($query) use ($commission_user_id, $commission_nickname, $commission_mobile, $tableName) {
|
||||
$rewardTableName = (new \app\admin\model\shopro\commission\Reward())->getQuery()->getTable();
|
||||
|
||||
$query = $query->table($rewardTableName)->where($rewardTableName . '.commission_order_id=' . $tableName . '.id');
|
||||
|
||||
$query->where(function ($query) use ($commission_user_id, $commission_nickname, $commission_mobile, $rewardTableName) {
|
||||
$is_or = false;
|
||||
if ($commission_user_id) {
|
||||
$is_or = true;
|
||||
$query = $query->where('agent_id', $commission_user_id);
|
||||
}
|
||||
|
||||
if ($commission_nickname || $commission_mobile) {
|
||||
$query->whereOr(function ($query) use ($commission_nickname, $commission_mobile, $rewardTableName) {
|
||||
$query->whereExists(function ($query) use ($commission_nickname, $commission_mobile, $rewardTableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $rewardTableName . '.agent_id');
|
||||
|
||||
if ($commission_nickname) {
|
||||
$query = $query->whereOr('nickname', 'like', "%{$commission_nickname}%");
|
||||
}
|
||||
|
||||
if ($commission_mobile) {
|
||||
$query = $query->whereOr('mobile', 'like', "%{$commission_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 订单号查询
|
||||
if ($order_sn || $order_createtime) {
|
||||
$orders = $orders->whereExists(function ($query) use ($order_sn, $order_createtime, $tableName) {
|
||||
$orderTableName = (new \app\admin\model\shopro\order\Order())->getQuery()->getTable();
|
||||
$query = $query->table($orderTableName)->where($orderTableName . '.id=' . $tableName . '.order_id');
|
||||
|
||||
if ($order_sn) {
|
||||
$query = $query->where('order_sn', 'like', "%{$order_sn}%");
|
||||
}
|
||||
|
||||
if ($order_createtime) {
|
||||
$query = $query->where('createtime', 'BETWEEN time', $order_createtime);
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
// 商品名称
|
||||
if ($goods_title) {
|
||||
$orders = $orders->whereExists(function ($query) use ($goods_title, $tableName) {
|
||||
$orderTableName = (new \app\admin\model\shopro\order\OrderItem())->getQuery()->getTable();
|
||||
$query = $query->table($orderTableName)->where($orderTableName . '.id=' . $tableName . '.order_item_id');
|
||||
|
||||
$query = $query->where('goods_title', 'like', "%{$goods_title}%");
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\commission;
|
||||
|
||||
use app\admin\controller\shopro\Base;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* 佣金明细
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Reward extends Base
|
||||
{
|
||||
|
||||
/**
|
||||
* Order模型对象
|
||||
* @var \app\admin\model\shopro\commission\Reward
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\shopro\commission\Reward;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
$nobuildfields = [
|
||||
'buyer_nickname','buyer_mobile',
|
||||
'agent_nickname','agent_mobile',
|
||||
'order_sn',
|
||||
];
|
||||
|
||||
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
|
||||
|
||||
$total = $this->buildSearch()
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
|
||||
// 查询分页数据
|
||||
$list = $this->buildSearch()
|
||||
->with(['order' => function ($query) {
|
||||
$query->removeOption('soft_delete')->field('id,type,order_sn,user_id,activity_type,goods_amount,status,total_amount,score_amount,total_fee,pay_fee,score_fee,pay_type,paytime,platform,ext,createtime,updatetime');
|
||||
}, 'buyer', 'agent'])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
// 处理顶部统计数据
|
||||
$commission_total = 0;
|
||||
$commissioned_total = 0;
|
||||
$delay_total = 0;
|
||||
$commission_back_total = 0;
|
||||
|
||||
$total_page = intval(ceil($total / 1000));
|
||||
for ($i = 0; $i < $total_page; $i++) {
|
||||
$totalList = $this->buildSearch()
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit(($i * 1000), 1000)
|
||||
->select();
|
||||
|
||||
foreach ($totalList as $key => $reward) {
|
||||
if ($reward['status'] == 1) {
|
||||
$commissioned_total += $reward['commission'];
|
||||
} else if ($reward['status'] == -1) {
|
||||
$commission_back_total += $reward['commission'];
|
||||
} else if ($reward['status'] == 0) {
|
||||
$delay_total += $reward['commission'];
|
||||
}
|
||||
$commission_total += $reward['commission'];
|
||||
}
|
||||
}
|
||||
|
||||
$result = [
|
||||
"total" => $total,
|
||||
"rows" => $list,
|
||||
"commission_total" => number_format($commission_total, 2, '.', ''),
|
||||
"commissioned_total" => number_format($commissioned_total, 2, '.', ''),
|
||||
"delay_total" => number_format($delay_total, 2, '.', ''),
|
||||
"commission_back_total" => number_format($commission_back_total, 2, '.', '')
|
||||
];
|
||||
|
||||
$this->success('佣金明细', null, $result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改佣金金额
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function edit ($id = null) {
|
||||
$params = $this->request->post();
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
|
||||
if ($row['status'] != 0) {
|
||||
$this->error('当前状态不可修改');
|
||||
}
|
||||
|
||||
$row->commission = $params['commission'];
|
||||
$row->save();
|
||||
|
||||
$this->success('修改成功', null, $row);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分佣
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function runCommission($id = null)
|
||||
{
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
$result = \think\Db::transaction(function () use ($row) {
|
||||
return (new \addons\shopro\library\commission\Reward)->runCommissionReward('admin', $row);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('结算成功');
|
||||
} else {
|
||||
$this->error('佣金已结算,请不要重复结算');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 佣金退回
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function backCommission($id = null)
|
||||
{
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
$result = \think\Db::transaction(function () use ($row) {
|
||||
return (new \addons\shopro\library\commission\Reward)->backCommissionReward('admin', $row);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('退回成功');
|
||||
} else {
|
||||
$this->error('佣金已退回或未结算');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消佣金
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function cancelCommission($id = null)
|
||||
{
|
||||
if (!$id) {
|
||||
$id = $this->request->get('id');
|
||||
}
|
||||
$row = $this->model->get($id);
|
||||
|
||||
$result = \think\Db::transaction(function () use ($row) {
|
||||
return (new \addons\shopro\library\commission\Reward)->cancelCommissionReward('admin', $row);
|
||||
});
|
||||
|
||||
if ($result) {
|
||||
$this->success('退回成功');
|
||||
} else {
|
||||
$this->error('佣金已退回或未结算');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function buildSearch()
|
||||
{
|
||||
$filter = $this->request->get("filter", '');
|
||||
$filter = (array)json_decode($filter, true);
|
||||
$filter = $filter ? $filter : [];
|
||||
|
||||
// 用户'parent_nickname','parent_mobile',
|
||||
// 购买人
|
||||
$buyer_nickname = isset($filter['buyer_nickname']) ? $filter['buyer_nickname'] : '';
|
||||
$buyer_mobile = isset($filter['buyer_mobile']) ? $filter['buyer_mobile'] : '';
|
||||
// 分销用户
|
||||
$agent_nickname = isset($filter['agent_nickname']) ? $filter['agent_nickname'] : '';
|
||||
$agent_mobile = isset($filter['agent_mobile']) ? $filter['agent_mobile'] : '';
|
||||
// 订单 & 商品
|
||||
$order_sn = isset($filter['order_sn']) ? $filter['order_sn'] : '';
|
||||
|
||||
// 当前表名
|
||||
$tableName = $this->model->getQuery()->getTable();
|
||||
|
||||
$orders = $this->model;
|
||||
|
||||
// 购买人查询
|
||||
if ($buyer_nickname || $buyer_mobile) {
|
||||
$orders = $orders->whereExists(function ($query) use ($buyer_nickname, $buyer_mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.buyer_id');
|
||||
|
||||
if ($buyer_nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$buyer_nickname}%");
|
||||
}
|
||||
|
||||
if ($buyer_mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$buyer_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
// 分销用户昵称,手机号
|
||||
if ($agent_nickname || $agent_mobile) {
|
||||
$orders = $orders->whereExists(function ($query) use ($agent_nickname, $agent_mobile, $tableName) {
|
||||
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
|
||||
$query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.agent_id');
|
||||
|
||||
if ($agent_nickname) {
|
||||
$query = $query->where('nickname', 'like', "%{$agent_nickname}%");
|
||||
}
|
||||
|
||||
if ($agent_mobile) {
|
||||
$query = $query->where('mobile', 'like', "%{$agent_mobile}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
// 订单号查询
|
||||
if ($order_sn) {
|
||||
$orders = $orders->whereExists(function ($query) use ($order_sn, $tableName) {
|
||||
$orderTableName = (new \app\admin\model\shopro\order\Order())->getQuery()->getTable();
|
||||
$query = $query->table($orderTableName)->where($orderTableName . '.id=' . $tableName . '.order_id');
|
||||
|
||||
if ($order_sn) {
|
||||
$query = $query->where('order_sn', 'like', "%{$order_sn}%");
|
||||
}
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户