414 lines
15 KiB
PHP
414 lines
15 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\orders;
|
||
|
||
use app\admin\model\Admin;
|
||
use app\admin\model\Order;
|
||
use app\admin\model\Worker;
|
||
use app\common\controller\Backend;
|
||
use think\Db;
|
||
use think\Exception;
|
||
use think\exception\DbException;
|
||
use think\exception\PDOException;
|
||
use think\exception\ValidateException;
|
||
use think\Hook;
|
||
use think\Loader;
|
||
|
||
/**
|
||
* 订单列管理
|
||
*
|
||
* @icon fa fa-circle-o
|
||
*/
|
||
class Configorder extends Backend
|
||
{
|
||
|
||
/**
|
||
* Configorder模型对象
|
||
* @var Order
|
||
*/
|
||
protected $model = null;
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = new Order();
|
||
$this->view->assign("statusList", $this->model->getStatusList());
|
||
$this->view->assign("offlineTotalTypeList", $this->model->getOfflineTotalTypeList());
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 默认生成的控制器所继承的父类中有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();
|
||
$list = $this->model
|
||
->tab(Order::TAB_SETTING)
|
||
->dispatcherauth($this->auth,'dispatch_admin_id')
|
||
//->areaauth($this->auth)
|
||
->with(['dispatchadmin', 'source' => [
|
||
'parent' => function ($q) {
|
||
$q->field('id,title');
|
||
}
|
||
]])
|
||
->where($where);
|
||
|
||
|
||
$list = $list->order($sort, $order)
|
||
->paginate($limit);
|
||
|
||
foreach ($list as $item) {
|
||
if (isset($item->getRelation('source')->parent->title)){
|
||
$item->source_total_name = '【' . $item->getRelation('source')->parent->title . '】' . ($item->getRelation('source')->title??'');
|
||
}else{
|
||
$item->source_total_name = ($item->getRelation('source')->title??'');
|
||
}
|
||
unset($item->source);
|
||
}
|
||
|
||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||
|
||
return json($result);
|
||
}
|
||
|
||
$res = $this->getSource();
|
||
$this->view->assign("sources", $res);
|
||
|
||
return $this->view->fetch();
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 编辑
|
||
*
|
||
* @param $ids
|
||
* @return string
|
||
* @throws DbException
|
||
* @throws \think\Exception
|
||
*/
|
||
public function edit($ids = null)
|
||
{
|
||
$row = $this->model->get($ids,['dispatch']);
|
||
if (!$row) {
|
||
$this->error(__('No Results were found'));
|
||
}
|
||
$adminIds = $this->getDataLimitAdminIds();
|
||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||
$this->error(__('You have no permission'));
|
||
}
|
||
if (false === $this->request->isPost()) {
|
||
$this->view->assign('row', $row);
|
||
//$worker = Worker::where('id',$row->worker_id)->find();
|
||
$worker = Worker::withTrashed()->where('id', $row->worker_id)->find();
|
||
$this->view->assign('worker', $worker);
|
||
return $this->view->fetch();
|
||
}
|
||
$params = $this->request->post('row/a');
|
||
if (empty($params)) {
|
||
$this->error(__('Parameter %s can not be empty', ''));
|
||
}
|
||
$params = $this->preExcludeFields($params);
|
||
|
||
if(!in_array($row->status,[Order::STATUS_CHECKING,Order::STATUS_CHECKONCE])){
|
||
$this->error('订单不允许操作');
|
||
}
|
||
$result = false;
|
||
Db::startTrans();
|
||
try {
|
||
//是否采用模型验证
|
||
if ($this->modelValidate) {
|
||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||
$row->validateFailException()->validate($validate);
|
||
}
|
||
|
||
$data = [
|
||
'status' => Order::STATUS_AUDITING,
|
||
'online_amount' => $params['online_amount'],
|
||
'online_amount_last' => $params['online_amount_last'],
|
||
'offline_amount' => $params['offline_amount'],
|
||
// 'refund_amount' => $params['refund_amount'],
|
||
'cost' => $params['cost'],
|
||
'offline_amount_type'=> $params['offline_amount_type'],
|
||
'amount_images' => $params['amount_images'],
|
||
'material_cost' => $params['material_cost'],
|
||
'cost_rate' => $params['cost_rate'],
|
||
'cost_remark' => $params['cost_remark'] ?? '',
|
||
'material_images' => $params['material_images'] ?? '',
|
||
'off_images' => $params['off_images']?? '',
|
||
];
|
||
|
||
$last_amount = bcadd($params['online_amount_last'],$params['offline_amount'],2);
|
||
|
||
$cost = bcadd($params['cost'],$params['material_cost'],2);
|
||
|
||
$data['total'] = bcadd($params['online_amount'],$last_amount,2);
|
||
$data['real_amount'] = $data['total'];
|
||
$data['performance'] = bcsub($data['real_amount'],$cost,2);
|
||
$result = $row->allowField(true)->save($data);
|
||
|
||
//日志
|
||
$params['order'] = $row;
|
||
$params['role'] = 1;
|
||
$params['auth'] = $this->auth;
|
||
$params['remark'] = '完成订单配置';
|
||
|
||
Hook::listen('order_change', $params);
|
||
|
||
Db::commit();
|
||
} catch (ValidateException|PDOException|Exception $e) {
|
||
Db::rollback();
|
||
$this->error($e->getMessage());
|
||
}
|
||
if (false === $result) {
|
||
$this->error(__('No rows were updated'));
|
||
}
|
||
$this->success();
|
||
}
|
||
|
||
|
||
public function getadminuser(){
|
||
|
||
$groupId = 6;
|
||
|
||
//$adminIds = Db::name('auth_group_access')->where('group_id',$groupId)->column('uid');
|
||
//whereIn('id',$adminIds)
|
||
$adminUsers = Admin::field(['id','nickname as name'])->select();
|
||
|
||
return json($adminUsers);
|
||
}
|
||
|
||
/**
|
||
* 生成查询所需要的条件,排序方式
|
||
* @param mixed $searchfields 快速查询的字段
|
||
* @param boolean $relationSearch 是否关联查询
|
||
* @return array
|
||
*/
|
||
protected function buildparams($searchfields = null, $relationSearch = null)
|
||
{
|
||
$searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
|
||
$relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
|
||
$search = $this->request->get("search", '');
|
||
$filter = $this->request->get("filter", '');
|
||
$op = $this->request->get("op", '', 'trim');
|
||
$sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
|
||
$order = $this->request->get("order", "DESC");
|
||
$offset = max(0, $this->request->get("offset/d", 0));
|
||
$limit = max(0, $this->request->get("limit/d", 0));
|
||
$limit = $limit ?: 999999;
|
||
//新增自动计算页码
|
||
$page = $limit ? intval($offset / $limit) + 1 : 1;
|
||
if ($this->request->has("page")) {
|
||
$page = max(0, $this->request->get("page/d", 1));
|
||
}
|
||
$this->request->get([config('paginate.var_page') => $page]);
|
||
$filter = (array)json_decode($filter, true);
|
||
|
||
$province_id = $filter['province_id']??null;
|
||
$city_id = $filter['city_id']??null;
|
||
$area_id = $filter['city_id']??null;
|
||
unset($filter['city_id']);
|
||
unset($filter['province_id']);
|
||
unset($filter['area_id']);
|
||
|
||
if(!empty($area_id)){
|
||
$filter['area_id'] = $area_id;
|
||
}else{
|
||
if(!empty($city_id)){
|
||
$filter['area_id'] = $city_id;
|
||
}else{
|
||
if(!empty($province_id)){
|
||
$filter['area_id'] = $province_id;
|
||
}
|
||
}
|
||
}
|
||
|
||
$op = (array)json_decode($op, true);
|
||
$filter = $filter ? $filter : [];
|
||
$where = [];
|
||
$alias = [];
|
||
$bind = [];
|
||
$name = '';
|
||
$aliasName = '';
|
||
if (!empty($this->model) && $relationSearch) {
|
||
$name = $this->model->getTable();
|
||
$alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
|
||
$aliasName = $alias[$name] . '.';
|
||
}
|
||
$sortArr = explode(',', $sort);
|
||
foreach ($sortArr as $index => & $item) {
|
||
$item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
|
||
}
|
||
unset($item);
|
||
$sort = implode(',', $sortArr);
|
||
$adminIds = $this->getDataLimitAdminIds();
|
||
if (is_array($adminIds)) {
|
||
$where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
|
||
}
|
||
if ($search) {
|
||
$searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
|
||
foreach ($searcharr as $k => &$v) {
|
||
$v = stripos($v, ".") === false ? $aliasName . $v : $v;
|
||
}
|
||
unset($v);
|
||
$where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
|
||
}
|
||
$index = 0;
|
||
foreach ($filter as $k => $v) {
|
||
if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
|
||
continue;
|
||
}
|
||
$sym = $op[$k] ?? '=';
|
||
if (stripos($k, ".") === false) {
|
||
$k = $aliasName . $k;
|
||
}
|
||
$v = !is_array($v) ? trim($v) : $v;
|
||
$sym = strtoupper($op[$k] ?? $sym);
|
||
//null和空字符串特殊处理
|
||
if (!is_array($v)) {
|
||
if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
|
||
$sym = strtoupper($v);
|
||
}
|
||
if (in_array($v, ['""', "''"])) {
|
||
$v = '';
|
||
$sym = '=';
|
||
}
|
||
}
|
||
|
||
switch ($sym) {
|
||
case '=':
|
||
case '<>':
|
||
$where[] = [$k, $sym, (string)$v];
|
||
break;
|
||
case 'LIKE':
|
||
case 'NOT LIKE':
|
||
case 'LIKE %...%':
|
||
case 'NOT LIKE %...%':
|
||
$where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
|
||
break;
|
||
// ✅ 新增:右匹配(like%)
|
||
case 'LIKE%':
|
||
$where[] = [$k, 'LIKE', "{$v}%"];
|
||
break;
|
||
|
||
// ✅ 新增:左匹配(%like)
|
||
case '%LIKE':
|
||
$where[] = [$k, 'LIKE', "%{$v}"];
|
||
break;
|
||
case '>':
|
||
case '>=':
|
||
case '<':
|
||
case '<=':
|
||
$where[] = [$k, $sym, intval($v)];
|
||
break;
|
||
case 'FINDIN':
|
||
case 'FINDINSET':
|
||
case 'FIND_IN_SET':
|
||
$v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
|
||
$findArr = array_values($v);
|
||
foreach ($findArr as $idx => $item) {
|
||
$bindName = "item_" . $index . "_" . $idx;
|
||
$bind[$bindName] = $item;
|
||
$where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
|
||
}
|
||
break;
|
||
case 'IN':
|
||
case 'IN(...)':
|
||
case 'NOT IN':
|
||
case 'NOT IN(...)':
|
||
$where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
|
||
break;
|
||
case 'BETWEEN':
|
||
case 'NOT BETWEEN':
|
||
$arr = array_slice(explode(',', $v), 0, 2);
|
||
if (stripos($v, ',') === false || !array_filter($arr, function ($v) {
|
||
return $v != '' && $v !== false && $v !== null;
|
||
})) {
|
||
continue 2;
|
||
}
|
||
//当出现一边为空时改变操作符
|
||
if ($arr[0] === '') {
|
||
$sym = $sym == 'BETWEEN' ? '<=' : '>';
|
||
$arr = $arr[1];
|
||
} elseif ($arr[1] === '') {
|
||
$sym = $sym == 'BETWEEN' ? '>=' : '<';
|
||
$arr = $arr[0];
|
||
}
|
||
$where[] = [$k, $sym, $arr];
|
||
break;
|
||
case 'RANGE':
|
||
case 'NOT RANGE':
|
||
$v = str_replace(' - ', ',', $v);
|
||
$arr = array_slice(explode(',', $v), 0, 2);
|
||
if (stripos($v, ',') === false || !array_filter($arr)) {
|
||
continue 2;
|
||
}
|
||
//当出现一边为空时改变操作符
|
||
if ($arr[0] === '') {
|
||
$sym = $sym == 'RANGE' ? '<=' : '>';
|
||
$arr = $arr[1];
|
||
} elseif ($arr[1] === '') {
|
||
$sym = $sym == 'RANGE' ? '>=' : '<';
|
||
$arr = $arr[0];
|
||
}
|
||
$tableArr = explode('.', $k);
|
||
if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias)
|
||
&& !empty($this->model) && $this->relationSearch) {
|
||
//修复关联模型下时间无法搜索的BUG
|
||
$relation = Loader::parseName($tableArr[0], 1, false);
|
||
$alias[$this->model->$relation()->getTable()] = $tableArr[0];
|
||
}
|
||
$where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
|
||
break;
|
||
case 'NULL':
|
||
case 'IS NULL':
|
||
case 'NOT NULL':
|
||
case 'IS NOT NULL':
|
||
$where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
$index++;
|
||
}
|
||
if (!empty($this->model)) {
|
||
$this->model->alias($alias);
|
||
}
|
||
$model = $this->model;
|
||
$where = function ($query) use ($where, $alias, $bind, &$model) {
|
||
if (!empty($model)) {
|
||
$model->alias($alias);
|
||
$model->bind($bind);
|
||
}
|
||
foreach ($where as $k => $v) {
|
||
if (is_array($v)) {
|
||
call_user_func_array([$query, 'where'], $v);
|
||
} else {
|
||
$query->where($v);
|
||
}
|
||
}
|
||
};
|
||
return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
|
||
}
|
||
}
|