Commit 3b92c396 authored by Administrator's avatar Administrator

工单接口

parent b8bbed4a
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\AlarmOrder; use App\Models\AlarmOrder;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
...@@ -19,8 +21,17 @@ class AlarmOrderController extends Controller ...@@ -19,8 +21,17 @@ class AlarmOrderController extends Controller
if($this->isadmin()) { if($this->isadmin()) {
$limit = 20; $limit = 20;
$offset = $limit * ($request->input('page') - 1); $offset = $limit * ($request->input('page') - 1);
$orders = AlarmOrder::selectRaw('FROM_UNIXTIME(r.starttime) AS start_time, FROM_UNIXTIME(r.endtime) AS end_time, alarm_order.*, r.devicenumber, r.concentration, r.location, r.policestatus, r.shutoff_status') $orders = AlarmOrder::selectRaw('
FROM_UNIXTIME(r.starttime) AS start_time,
FROM_UNIXTIME(r.endtime) AS end_time,
alarm_order.*,
r.devicenumber, r.concentration, r.location, r.policestatus, r.shutoff_status,
d.devicecoord, d.username,
u.name, u.phone_number
')
->leftjoin('reportpolice AS r', 'r.id', '=', 'alarm_order.reportpolice_id') ->leftjoin('reportpolice AS r', 'r.id', '=', 'alarm_order.reportpolice_id')
->leftjoin('device AS d', 'd.id', '=', 'alarm_order.device_id')
->leftjoin('users AS u', 'alarm_order.user_id', '=', 'u.id')
->offset($offset) ->offset($offset)
->limit($limit) ->limit($limit)
->get()->toArray(); ->get()->toArray();
...@@ -45,18 +56,27 @@ class AlarmOrderController extends Controller ...@@ -45,18 +56,27 @@ class AlarmOrderController extends Controller
substr($request->input('device_num'), -4). substr($request->input('device_num'), -4).
$request->input('policestatus'). $request->input('policestatus').
$request->input('shutoff_status'); $request->input('shutoff_status');
DB::beginTransaction();
try {
$order = new AlarmOrder(); $order = new AlarmOrder();
$insertId = $order->sharedLock()->insertGetId([ $insertId = $order->insertGetId([
'order_num' => $orderNum, 'order_num' => $orderNum,
'reportpolice_id' => $reportpolice_id, 'reportpolice_id' => $reportpolice_id,
'created_at' => Carbon::now(),
]); ]);
// 进度表新增 // 进度表新增
$orderSchedule = new AlarmOrderSchedule(); $orderSchedule = new AlarmOrderSchedule();
$orderSchedule->lockForUpdate();
$orderSchedule->alarm_order_id = $insertId; $orderSchedule->alarm_order_id = $insertId;
$orderSchedule->schedule = 1; $orderSchedule->schedule = 1;
$orderSchedule->created_at = Carbon::now();
$orderSchedule->save(); $orderSchedule->save();
DB::commit();
} catch (QueryException $exception) {
DB::rollBack();
return $this->jsonErrorData('工单创建失败');
}
if($insertId) { if($insertId) {
return $this->jsonSuccessData('已生成工单!'); return $this->jsonSuccessData('已生成工单!');
...@@ -89,12 +109,31 @@ class AlarmOrderController extends Controller ...@@ -89,12 +109,31 @@ class AlarmOrderController extends Controller
*/ */
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$data = $request->all();
if(isset($data['schedule'])) {
DB::beginTransaction();
try {
AlarmOrderSchedule::insert(array_merge(
$data['schedule'],
['created_at' => Carbon::now(), 'alarm_order_id' => $id]));
unset($data['schedule']);
$order = AlarmOrder::where('id', $id) $order = AlarmOrder::where('id', $id)
->update($request->all()); ->update(array_merge($data, ['updated_at' => Carbon::now()]));
DB::commit();
} catch (QueryException $exception) {
DB::rollBack();
return $this->jsonErrorData(500, '工单更新失败!');
}
}else{
$order = AlarmOrder::where('id', $id)
->update(array_merge($data, ['updated_at' => Carbon::now()]));
}
if($order) { if($order) {
$this->jsonSuccessData('工单更新成功'); return $this->jsonSuccessData('工单更新成功');
}else{ }else{
$this->jsonErrorData(500, '更新订单失败!'); return $this->jsonErrorData(500, '工单更新失败!');
} }
} }
...@@ -108,9 +147,9 @@ class AlarmOrderController extends Controller ...@@ -108,9 +147,9 @@ class AlarmOrderController extends Controller
{ {
$state = AlarmOrder::destroy($id); $state = AlarmOrder::destroy($id);
if($state) { if($state) {
$this->jsonSuccessData('删除成功'); return $this->jsonSuccessData('删除成功');
}else{ }else{
$this->jsonErrorData(500, '删除工单失败!'); return $this->jsonErrorData(500, '删除工单失败!');
} }
} }
} }
...@@ -8,6 +8,7 @@ use Illuminate\Support\Carbon; ...@@ -8,6 +8,7 @@ use Illuminate\Support\Carbon;
class AlarmOrder extends Model class AlarmOrder extends Model
{ {
protected $table = 'alarm_order'; protected $table = 'alarm_order';
public $timestamps = false;
// 获取报警记录 // 获取报警记录
public function reportpolices() public function reportpolices()
...@@ -25,4 +26,5 @@ class AlarmOrder extends Model ...@@ -25,4 +26,5 @@ class AlarmOrder extends Model
return Carbon::parse($value)->toDateTimeString(); return Carbon::parse($value)->toDateTimeString();
} }
} }
...@@ -3,8 +3,20 @@ ...@@ -3,8 +3,20 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
class AlarmOrderSchedule extends Model class AlarmOrderSchedule extends Model
{ {
protected $table = 'alarm_order_schedule'; protected $table = 'alarm_order_schedule';
public $timestamps = false;
public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->toDateTimeString();
}
public function getUpdatedAtAttribute($value)
{
return Carbon::parse($value)->toDateTimeString();
}
} }
...@@ -67,7 +67,7 @@ return [ ...@@ -67,7 +67,7 @@ return [
| |
*/ */
'timezone' =>'UTC', 'timezone' =>'PRC',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment