Commit 3b92c396 authored by Administrator's avatar Administrator

工单接口

parent b8bbed4a
......@@ -2,6 +2,8 @@
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use App\Models\AlarmOrder;
use Illuminate\Support\Facades\DB;
......@@ -19,8 +21,17 @@ class AlarmOrderController extends Controller
if($this->isadmin()) {
$limit = 20;
$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('device AS d', 'd.id', '=', 'alarm_order.device_id')
->leftjoin('users AS u', 'alarm_order.user_id', '=', 'u.id')
->offset($offset)
->limit($limit)
->get()->toArray();
......@@ -45,18 +56,27 @@ class AlarmOrderController extends Controller
substr($request->input('device_num'), -4).
$request->input('policestatus').
$request->input('shutoff_status');
DB::beginTransaction();
try {
$order = new AlarmOrder();
$insertId = $order->sharedLock()->insertGetId([
$insertId = $order->insertGetId([
'order_num' => $orderNum,
'reportpolice_id' => $reportpolice_id,
'created_at' => Carbon::now(),
]);
// 进度表新增
$orderSchedule = new AlarmOrderSchedule();
$orderSchedule->lockForUpdate();
$orderSchedule->alarm_order_id = $insertId;
$orderSchedule->schedule = 1;
$orderSchedule->created_at = Carbon::now();
$orderSchedule->save();
DB::commit();
} catch (QueryException $exception) {
DB::rollBack();
return $this->jsonErrorData('工单创建失败');
}
if($insertId) {
return $this->jsonSuccessData('已生成工单!');
......@@ -89,12 +109,31 @@ class AlarmOrderController extends Controller
*/
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)
->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) {
$this->jsonSuccessData('工单更新成功');
return $this->jsonSuccessData('工单更新成功');
}else{
$this->jsonErrorData(500, '更新订单失败!');
return $this->jsonErrorData(500, '工单更新失败!');
}
}
......@@ -108,9 +147,9 @@ class AlarmOrderController extends Controller
{
$state = AlarmOrder::destroy($id);
if($state) {
$this->jsonSuccessData('删除成功');
return $this->jsonSuccessData('删除成功');
}else{
$this->jsonErrorData(500, '删除工单失败!');
return $this->jsonErrorData(500, '删除工单失败!');
}
}
}
......@@ -8,6 +8,7 @@ use Illuminate\Support\Carbon;
class AlarmOrder extends Model
{
protected $table = 'alarm_order';
public $timestamps = false;
// 获取报警记录
public function reportpolices()
......@@ -25,4 +26,5 @@ class AlarmOrder extends Model
return Carbon::parse($value)->toDateTimeString();
}
}
......@@ -3,8 +3,20 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
class AlarmOrderSchedule extends Model
{
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 [
|
*/
'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