allocatr/application/admin/controller/orders/Dispatch2.php
2025-04-17 16:55:10 +08:00

395 lines
14 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 app\admin\controller\orders;
use app\admin\model\Order;
use app\admin\model\OrderDispatch;
use app\admin\model\OrderDispatchLog;
use app\common\controller\Backend;
use app\common\Logic\OrderLogic;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Hook;
/**
* 派单列管理
*
* @icon fa fa-circle-o
*/
class Dispatch2 extends Backend
{
/**
* OrderDispatch模型对象
* @var \app\admin\model\OrderDispatch
*/
protected $model = null;
protected $searchFields = 'order.id,order.order_no,order.tel,order.customer,worker_name,worker_tel';
public function _initialize()
{
parent::_initialize();
//加载当前控制器语言包
$this->model = new \app\admin\model\OrderDispatch;
$this->view->assign("typeList", $this->model->getTypeList());
$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 = 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();
$list = $this->model
->with(['order'])
->auth($this->auth)
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as &$row) {
$row->btn_edit = (in_array($row->status, $this->model->btnActiveStatusList('btn_edit'))) ? true : false;
$row->btn_cancel = (in_array($row->status, $this->model->btnActiveStatusList('btn_cancel'))) ? true : false;
$row->btn_abnormal = (in_array($row->status, $this->model->btnActiveStatusList('btn_abnormal'))) ? true : false;
$row->btn_finished = (in_array($row->status, $this->model->btnActiveStatusList('btn_finished'))) ? true : false;
$row->is_disabled = (in_array($row->status, $this->model->btnActiveStatusList('disabled_status'))) ? true : false;
$row->btn_record = (in_array($row->status, $this->model->btnActiveStatusList('btn_record'))) ? true : false;
// $row->btn_income = (in_array($row->status, $this->model->btnActiveStatusList('btn_income')) && in_array($row->order->status, $orderModel->incomeBtnStatus())) ? true : false;
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
/**
* 编辑
*
* @param $ids
* @return string
* @throws DbException
* @throws \think\Exception
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$order = Order::where('id',$row->id)->with(['area'])->find();
if(empty($order)){
$this->error(__('未找到关联订单'));
}
$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);
$this->view->assign('order', $order);
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if(isset($params['action']) && $params['action'] == 'finish'){
return $this->finish($ids);
}
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
if(!in_array($row->status ,$this->model->btnActiveStatusList('btn_edit'))){
$this->error(__('任务状态已变更,请刷新后再操作'));
}
$params = $this->preExcludeFields($params);
$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);
}
$workerChange = false;
$remark = '';
if($row->worker_id != $params['worker_id']){ //更换了师傅
$worker = model('worker')->where('id',$params['worker_id'])->find();
$params ['worker_name'] = $worker->name;
$params ['worker_tel'] = $worker->tel;
//记录日志
$remark = '师傅由 ('.$row->worker_id.')'.$row->worker_name.' 更换为('.$params['worker_id'].')'.$worker->name;
$workerChange = true;
}
$result = $row->allowField(true)->save($params);
if($workerChange){
$hookParams = [
'dispatch' => $row,
'remark' => $remark,
];
Hook::listen('order_dispatch_change', $hookParams);
}
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
$this->success();
}
/**
* 编辑
*
* @param $ids
* @return string
* @throws DbException
* @throws \think\Exception
*/
public function finish($ids = null)
{
$row = $this->model->get($ids);
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'));
}
$order = Order::where('id',$row->order_id)->find();
if(empty($order)){
$this->error(__('未找到关联订单'));
}
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
$this->view->assign('order', $order);
$this->view->assign('action','finish');
return $this->view->fetch('edit');
}
$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, $this->model->btnActiveStatusList('btn_finished'))){
$this->error('任务状态变更,请刷新后操作');
}
if($order->status != Order::STATUS_DISPATCHED){
$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);
}
$params['status'] = OrderDispatch::STATUS_FINISH;
$params['finish_time'] = date('Y-m-d H:i:s');
$params['follow'] = 2;
$result = $row->allowField(true)->save($params);
//修改订单状态
$OrderLogic = new OrderLogic();
$OrderLogic -> dispachFinishAfter($row,['role'=>1,'auth'=>$this->auth,'remark'=>'后台操作完成任务,操作人:'.$this->auth->nickname]);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
$this->success();
}
/**
* 取消派单
*
* @param $ids
* @return void
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
// public function del($ids = null)
// {
// if (false === $this->request->isPost()) {
// $this->error(__("Invalid parameters"));
// }
// $ids = $ids ?: $this->request->post("ids");
// if (empty($ids)) {
// $this->error(__('Parameter %s can not be empty', 'ids'));
// }
// $pk = $this->model->getPk();
// $adminIds = $this->getDataLimitAdminIds();
// if (is_array($adminIds)) {
// $this->model->where($this->dataLimitField, 'in', $adminIds);
// }
// $list = $this->model->where($pk, 'in', $ids)->whereIn('status', $this->model->deleteStatusList())->select();
//
// $remark = $this->request->param('remark', '');
//
// $count = 0;
// Db::startTrans();
// try {
// foreach ($list as $item) {
// //$count += $item->delete();
// $order = Order::where('id', $item->order_id)->where('status', Order::STATUS_DISPATCHED)->find();
// if (!$order) {
// $this->error('订单状态已变更,请刷新后操作');
// }
// //取消
// $item->save(['status' => OrderDispatch::STATUS_CANCEL, 'remark' => $remark]);
// //回退订单状态
// $order->allowField(true)->save(['status' => Order::STATUS_DISPATCHING]);
//
// $params['order'] = $order->ref;
// $params['role'] = 1;
// $params['auth'] = $this->auth;
// $params['remark'] = '派单被取消[ID' . $item->id . '],订单状态回退';
// if (!empty($remark)) {
// $params['remark'] .= ',操作备注:' . $remark;
// }
// Hook::listen('order_change', $params);
// }
// Db::commit();
// } catch (PDOException | Exception $e) {
// Db::rollback();
// throw $e;
// $this->error($e->getMessage());
// }
//
// $this->error(__('取消成功'));
// }
/**
* 取消派单
*
* @param $ids
* @return void
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function del($ids=null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $this->request->post("ids/a");
$reason = $this->request->post("reason");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->whereIn('status', $this->model->deleteStatusList())->select();
if(count($list) != count($ids)){
$this->error(__("任务状态不可操作,请刷新页面后再操作"));
}
$remark = $reason;
$count = 0;
$OrderLogic = new OrderLogic();
Db::startTrans();
try {
foreach ($list as $item) {
//$count += $item->delete();
$order = Order::where('id', $item->order_id)->where('status', Order::STATUS_DISPATCHED)->find();
if (!$order) {
$this->error('订单状态已变更,请刷新后操作');
}
//取消
// $item->allowField(true)->save(['status' => OrderDispatch::STATUS_CANCEL, 'remark' => $remark]);
//回退订单状态
//$order->allowField(true)->save(['status' => Order::STATUS_DISPATCHING]);
/* Hook::listen('order_change', $params);
$hookParams = [
'dispatch' => $item,
'remark' => '后台取消,adminId:'.$this->auth->id
];
Hook::listen('order_dispatch_change', $hookParams);*/
$OrderLogic->cancelOrderDispatch($item,$this->auth,$remark);
}
Db::commit();
} catch (PDOException | Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
/* if ($count) {
$this->success();
}*/
$this->success(__('取消成功'));
}
}