MachineRepository.php
4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/2/5
* Time: 13:17
*/
namespace App\Repositories\Backend\Machine;
use App\Modules\Models\Machine\Machine;
use App\Modules\Onenet\Onenet;
use Illuminate\Support\Facades\DB;
use App\Modules\Repositories\Machine\BaseMachineRepository;
use Grimzy\LaravelMysqlSpatial\Types\Point;
class MachineRepository extends BaseMachineRepository
{
public function getForDataTable()
{
return $this->query()->get();
}
public function getinfo()
{
return $this->query()->select('machine.id', 'machine.life as mac_status', 'machine.mac_no','machine.free_time','machine.type','machine.hatch_number','machine.one_day_price','spot.spotname','business.business_name','machine.address','machine.deposit')
->leftjoin('spot','machine.spot_id','=','spot.id')
->leftjoin('business','machine.business_id','=','business.id')
->get();;
}
public function create($input)
{
$data = $input['data'];
$machine = $this->createUserStub($data);
DB::transaction(function () use ($machine, $data) {
if ($machine->save($data)) {
//Attach new roles
// event(new BusinessCreated($business));
return true;
}
throw new GeneralException(trans('exceptions.backend.machine.create_error'));
});
}
protected function createUserStub($input)
{
$mac_no=Machine::num();
//获取玻所的编号
$devid = new Onenet();
$he_cloud_device=$devid->deviceAdd($mac_no);
if($he_cloud_device){
$he_cloud_device_id = $he_cloud_device['he_cloud_device_id'];
}else{
throw new GeneralException(trans('alerts.backend.machine.wait_error'));
die();
}
//经纬度
$lng= $input['lat'] ;
$lat = $input['lng'];
$machine = self::MODEL;
$machine = new $machine;
$machine->mac_no = $mac_no;
$machine->one_day_price = $input['one_day_price']; //分为单位
$machine->deposit = $input['deposit']; //分为单位
$machine->free_time = $input['free_time'];
$machine->business_id = $input['business_id'];
$machine->address = $input['address'];
$machine->spot_id = $input['spot_id'];
$machine->type = $input['type'];
$machine->position = new Point($lat, $lng);
//todo 仓口数量 默认 1大机柜 2小机柜
// $machine->hatch_number = $input['hatch_number'];
if($input['type'] == 1){
$machine->hatch_number = 60;
}elseif($input['type'] == 2){
$machine->hatch_number = 10;
}
$machine->he_cloud_device_id = $he_cloud_device_id;
$machine->wx_mini_program = "mac_no=".$mac_no;
$machine->ali_mini_program ="mac_no=".$mac_no;;
return $machine;
}
public function change(Machine $machine,$input)
{
$input['position']= new Point($input['lng'] , $input['lat']);
unset($input['lat']);
unset($input['lng']);
if($input['type'] == 1){
$input['hatch_number'] = 60;
}elseif($input['type'] == 2){
$input['hatch_number'] = 10;
}
DB::transaction(function () use ($machine, $input) {
if ($machine->update($input)) {
// event(new BusinessUpdated($machine));
return true;
}
throw new GeneralException(trans('exceptions.backend.business.update_error'));
});
}
function get($id, $t = false)
{
if($t){
$where = [
['mac_no', '=', $id]
];
}else{
$where = [
['id', '=', $id]
];
}
$machine = Machine::where($where)->first();
if ($machine == null) {
return false;
// throw new ApiException(ErrorCode::MACHINE_NOT_EXIST, trans('api.error.machine_not_exist'));
}
return $machine;
}
}