Files
oil/server-tp6/app/store/controller/Partners.php
T

158 lines
4.6 KiB
PHP

<?php
declare (strict_types=1);
namespace app\store\controller;
use app\store\controller\Controller;
use app\store\model\Partners as PartnersModel;
use app\store\model\partners\Order as OrderModel;
use app\store\model\store\User;
use think\response\Json;
/**
* 商户控制器
* Class StoreUser
* @package app\store\controller
*/
class Partners extends Controller
{
/**
* 商户列表
* @return mixed
* @throws \think\exception\DbException
*/
public function index()
{
$model = new PartnersModel;
$getData = $this->request->param();
$data = [];
if (isset($getData['name']) && !empty($getData['name'])) {
$data[] = ['name', 'like', "%{$getData['name']}%"];
}
$user = $this->store['user'];
if($user['is_super'] == 0) {
$data[] = ['store_user_id', 'in', User::getScope($user)];
}
/*if(!empty($this->store['user']['partners_id']) && $this->store['user']['is_super'] == 0){
$filter[] = ['partners_id', 'in', json_decode($this->store['user']['partners_id'],true)];
}*/
$list = $model->getList($data)->toArray();
$newList = [];
$OrderModel = new OrderModel();
$date = date("Y-m-d");
foreach ($list['data'] as $val){
$val['day_money'] =$OrderModel->getOrderTotalPrice($date, $date, $val);
$val['total_money'] =$OrderModel->getOrderTotalPrice(null, null, $val);
$newList[] = $val;
}
$list['data'] = $newList;
return $this->renderSuccess(['list'=>$list,'user'=>$this->store['user']]);
}
/**
* 获取所有商户
* @return mixed
* @throws \think\exception\DbException
*/
public function all(): Json
{
$user = $this->store['user'];
$list = PartnersModel::getAll($user);
return $this->renderSuccess(compact('list','user'));
}
/**
* 添加商户
* @return array|mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function add()
{
$model = new PartnersModel;
$data = $this->postForm();
// 新增记录
if ($model->add($data,$this->store['user'])) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* 商户详情
* @param int $partnersId
* @return Json
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \cores\exception\BaseException
*/
public function detail(int $partnersId): Json
{
$partnersInfo = PartnersModel::detail($partnersId);
return $this->renderSuccess(compact('partnersInfo'));
}
/**
* 编辑商户
* @param int $partnersId
* @return Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \cores\exception\BaseException
*/
public function edit(int $partnersId): Json
{
// 商品详情
$model = PartnersModel::detail($partnersId);
$data = $this->postForm();
// 更新记录
if ($model->edit($data)) {
return $this->renderSuccess('更新成功');
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* 用户充值
* @param int $partnersId
* @param string $target
* @return array
*/
public function recharge(int $partnersId, string $target)
{
// 用户详情
$model = PartnersModel::detail($partnersId);
if ($model->recharge($target, $this->postForm())) {
return $this->renderSuccess('操作成功');
}
return $this->renderError($model->getError() ?: '操作失败');
}
/**
* 删除商户
* @return Json
* @throws \think\exception\DbException
*/
public function delete(): Json
{
$arr = $this->postData();
$userModel = new User();
foreach ((array)$arr as $val){
$model = PartnersModel::detail($val);
if ($model->setDelete()) {
$userModel->where([
['level','=',1],
['partners_id','like','%'.$val.'%'],
])->delete();
}
}
return $this->renderSuccess('删除成功');
}
}