306 lines
10 KiB
PHP
306 lines
10 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\workers;
|
||
|
||
use app\admin\model\Area;
|
||
use app\admin\model\AuthGroup;
|
||
use app\admin\model\Item;
|
||
use app\admin\model\Order;
|
||
use app\common\controller\Backend;
|
||
use fast\Tree;
|
||
use think\Db;
|
||
use think\exception\PDOException;
|
||
use think\exception\ValidateException;
|
||
|
||
/**
|
||
* 师傅列管理
|
||
*
|
||
* @icon fa fa-circle-o
|
||
*/
|
||
class Worker extends Backend
|
||
{
|
||
|
||
/**
|
||
* Worker模型对象
|
||
* @var \app\admin\model\Worker
|
||
*/
|
||
protected $model = null;
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = new \app\admin\model\Worker;
|
||
$this->view->assign("statusList", $this->model->getStatusList());
|
||
|
||
|
||
$items = model('item')->getAll();
|
||
|
||
Tree::instance()->init($items)->icon = [' ', ' ', ' '];
|
||
// dd($ruleList);
|
||
$res = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
|
||
|
||
foreach ($res as &$v) {
|
||
$v = $v->toArray();
|
||
$v['state'] = ['selected' => false];
|
||
$v['parent'] = $v['pid'] ?: '#';
|
||
$v['text'] = $v['title'];
|
||
unset($v['pid']);
|
||
unset($v['title']);
|
||
unset($v['level']);
|
||
unset($v['key_word']);
|
||
unset($v['sort']);
|
||
unset($v['status']);
|
||
}
|
||
$this->tree = $res;
|
||
$this->view->assign("tree", $res);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||
*/
|
||
|
||
|
||
/**
|
||
* 查看
|
||
*/
|
||
public function index()
|
||
{
|
||
//当前是否为关联查询
|
||
$this->relationSearch = true;
|
||
//设置过滤方法
|
||
$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();
|
||
$area_code = request()->get('area_id');
|
||
$item_id = request()->get('item_id');
|
||
$keyword = request()->get('keyword');
|
||
$build = $this->model
|
||
->with(['area','admin' => function($q){
|
||
$q->withField(['id','username']);
|
||
}])
|
||
->where($where)
|
||
->field('worker.id,admin_id,type,name,tel,area_id,create_time,deposit_amount,update_time,status,star')
|
||
->order($sort, $order);
|
||
if ($keyword) {
|
||
$build->where(function ($q) use ($keyword) {
|
||
$q->where('name', 'like', '%' . $keyword . '%')->whereOr('tel', 'like', '%' . $keyword . '%');
|
||
});
|
||
}
|
||
if ($item_id) {
|
||
$build->join('worker_item', 'worker_item.worker_id = worker.id', 'left');
|
||
$build->where('worker_item.item_id', $item_id);
|
||
}
|
||
|
||
if ($area_code) {
|
||
$build->where('area_id', 'like', $this->getSelectAreaCode($area_code) . '%');
|
||
}
|
||
|
||
$list = $build
|
||
->paginate($limit);
|
||
|
||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||
|
||
return json($result);
|
||
}
|
||
|
||
$items = Db::name('item')
|
||
->where('status', 1)
|
||
->field(['id', 'title', 'key_word', 'pid'])
|
||
->order('pid', 'asc')
|
||
->order('sort', 'desc')
|
||
->select();
|
||
$filtered = array_filter($items, function ($item) {
|
||
return $item['pid'] == 0;
|
||
});
|
||
|
||
$pid_map = array_column($filtered, null, 'id');
|
||
$res_items = [];
|
||
foreach ($items as $item) {
|
||
if ($item['pid'] != 0 && isset($pid_map[$item['pid']])) {
|
||
$res_items [] = [
|
||
...$item, 'ptitle' => $pid_map[$item['pid']]['title']
|
||
];
|
||
}
|
||
}
|
||
$this->view->assign('items', $res_items);
|
||
return $this->view->fetch();
|
||
}
|
||
|
||
|
||
public function add()
|
||
{
|
||
if (false === $this->request->isPost()) {
|
||
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 ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||
$params[$this->dataLimitField] = $this->auth->id;
|
||
}
|
||
$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 . '.add' : $name) : $this->modelValidate;
|
||
$this->model->validateFailException()->validate($validate);
|
||
}
|
||
$params['admin_id'] = $this->auth->id;
|
||
|
||
if(!empty($params['area_id'])){
|
||
|
||
$area = Area::getByCode($params['area_id']);
|
||
if($area){
|
||
$location = getLocation($area->merge_name);
|
||
if(!empty($location)){
|
||
$params['lng'] = $location['lng'];
|
||
$params['lat'] = $location['lat'];
|
||
}
|
||
}
|
||
}
|
||
$result = $this->model->allowField(true)->save($params);
|
||
$item_map = model('item')->getAll();
|
||
$item_map = array_column($item_map, 'level', 'id');
|
||
if ($result) {
|
||
$items = explode(',', $params['rules']);
|
||
if ($items) {
|
||
$insert = [];
|
||
foreach ($items as $item) {
|
||
$insert [] = [
|
||
'worker_id' => $result,
|
||
'item_id' => $item,
|
||
'item_path_id' => $item_map[$item] ?? 0
|
||
];
|
||
}
|
||
model('WorkerItem')->saveAll($insert);
|
||
}
|
||
}
|
||
|
||
Db::commit();
|
||
} catch (ValidateException | PDOException | Exception $e) {
|
||
Db::rollback();
|
||
$this->error($e->getMessage());
|
||
}
|
||
if ($result === false) {
|
||
$this->error(__('No rows were inserted'));
|
||
}
|
||
$this->success();
|
||
}
|
||
|
||
|
||
public function edit($ids = null)
|
||
{
|
||
|
||
if (!$ids) {
|
||
if (request()->isPost()) {
|
||
$ids = input('id');
|
||
if (!$ids) {
|
||
$this->error('缺少订单ID');
|
||
}
|
||
} else {
|
||
$this->error('缺少订单ID');
|
||
}
|
||
}
|
||
|
||
// 获取当前ID对应的订单信息
|
||
$worker = $this->model->get($ids);
|
||
if (!$worker) {
|
||
$this->error('订单不存在');
|
||
}
|
||
|
||
// 判断是否为POST请求,进行更新操作
|
||
if (request()->isPost()) {
|
||
// 获取表单提交的数据
|
||
$params = input('post.row/a');
|
||
|
||
$item_map = model('item')->getAll();
|
||
$item_map = array_column($item_map, 'level', 'id');
|
||
if ($ids) {
|
||
$items = explode(',', $params['rules']);
|
||
if ($items) {
|
||
$insert = [];
|
||
foreach ($items as $item) {
|
||
$insert [] = [
|
||
'worker_id' => $ids,
|
||
'item_id' => $item,
|
||
'item_path_id' => $item_map[$item] ?? 0
|
||
];
|
||
}
|
||
model('WorkerItem')->where('worker_id', $ids)->delete();
|
||
model('WorkerItem')->insertAll($insert);
|
||
}
|
||
}
|
||
unset($params['rules']);
|
||
unset($params['address']);
|
||
$worker->save($params);
|
||
|
||
// 返回成功信息
|
||
$this->success('更新成功', 'index');
|
||
}
|
||
$area_name = model('area')->getNameByCode($worker->area_id);
|
||
$worker->area_name = str_replace(',', '/', $area_name);
|
||
|
||
|
||
$select_ids = model('WorkerItem')->where('worker_id', $ids)->field('item_id')
|
||
->select();
|
||
$select_ids = array_column($select_ids, 'item_id');
|
||
foreach ($this->tree as $index => $item) {
|
||
if (in_array($item['parent'], $select_ids)) {
|
||
$this->tree[$index]['state']['selected'] = true;
|
||
}
|
||
}
|
||
// dd($area_name);
|
||
// 将订单数据传递到视图
|
||
$this->assign('row', $worker);
|
||
$this->assign("tree", $this->tree);
|
||
// 渲染编辑页面
|
||
return $this->fetch();
|
||
}
|
||
|
||
public function dispatchList()
|
||
{
|
||
|
||
$area_id = request()->get('area_id');
|
||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||
|
||
$build = model('worker')
|
||
->where('status', 1)
|
||
->with(['area'])
|
||
->withCount(['myorder' => function ($query) {
|
||
$query->where('status', Order::STATUS_FINISHED);
|
||
return 'finish_order';
|
||
},])
|
||
->withCount(['myorder' => function ($query) {
|
||
$query->where('status', '<', Order::STATUS_FINISHED)
|
||
->where('status', '>=', Order::STATUS_DRAFT);
|
||
return 'doing_order';
|
||
}])
|
||
->field(['id', 'name', 'tel', 'area_id', 'lng', 'lat']);
|
||
|
||
if ($area_id) {
|
||
$code = $this->getSelectAreaCode(substr_replace($area_id, '00', -2));
|
||
$build->where('area_id', 'like', $code . '%');
|
||
}
|
||
$list = $build
|
||
->paginate($limit);
|
||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||
|
||
return json($result);
|
||
|
||
}
|
||
|
||
}
|