PowerRepository.php
5.06 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Created by PhpStorm.
* User: machengjun
* Date: 2018/2/7
* Time: 下午3:58
*/
namespace App\Repositories\Customers;
use App\Exceptions\Api\ApiException;
use App\Modules\Enums\ErrorCode;
use App\Modules\Models\Power\Power;
use App\Modules\Repositories\BaseRepository;
use Illuminate\Support\Facades\Log;
/**
* Class EloquentHistoryRepository.
*/
class PowerRepository extends BaseRepository
{
function makePowerStatusSaled($powers)
{
$update = [
'status' => 3
];
$powers_id = array_column($powers, 'id');
try {
Power::whereIn('id', $powers_id)->Update($update);
} catch (Exception $e) {
throw new ApiException(ErrorCode::DATABASE_ERROR, trans('api.error.database_error'));
}
return true;
}
function takeOutSuccess($power_no)
{
$where = [
['power_no', '=', $power_no],
// ['status', '=', 3],
];
$power_obj = Power::where($where)->whereIn('status', [2, 3])->first();
if ($power_obj->status == 3) {
$power_obj->status = 4;
} elseif ($power_obj->status == 2) {
$power_obj->status = 1;
}
// $power_obj->status = 4;
$power_obj->machine_id = null;
$power_obj->hatch_no = null;
$power_obj->save();
return $power_obj;
}
function setPowerStatus($machine_id,$hatch_no,$power_no,$has_power, $pType, $season){
$power_obj = Power::where('power_no',$power_no)->first();
if($power_obj == null){
//库里不存在充电宝,自动入库
$this->onenetAddPower($power_no,$machine_id,$hatch_no,$has_power, $pType, $season);
return true;
}
$this->updatePowerStatus($power_obj,$machine_id,$hatch_no,$has_power, $pType, $season);
return true;
}
function onenetAddPower($power_no,$machine_id,$hatch_no,$has_power, $pType, $season){
$power_obj = new Power();
$power_obj->power_no = $power_no;
$power_obj->has_power = $has_power;
$power_obj->status = 2;
$power_obj->machine_id = $machine_id;
$power_obj->hatch_no = $hatch_no;
$power_obj->power_type = $pType;
$power_obj->season = $season;
$power_obj->save();
}
function updatePowerStatus($power_obj,$machine_id,$hatch_no,$has_power, $pType, $season)
{
if($power_obj->status == 1){
$power_obj->status = 2;
}
$power_obj->has_power = $has_power;
$power_obj->machine_id = $machine_id;
$power_obj->hatch_no = $hatch_no;
$power_obj->power_type = $pType;
$power_obj->season = $season;
$power_obj->save();
}
function doReturn($machine_obj,$power_no,$hatch_no,$has_power, $isT){
$where = [
['power_no', '=', $power_no],
// ['status', '=', 4],
];
// $power_obj = Power::where($where)->first();
$power_obj = Power::where($where)->whereIn('status', [1, 4])->first();
if($power_obj == null){
throw new ApiException(ErrorCode::SALE_POWER_NOT_EXIST, trans('api.error.sale_power_not_exist'));
}
$on = true;
if($power_obj->status == 1){
$on = false;
$power_obj->status = 2;
}else{
if($isT == 0){
$power_obj->status = 1;
}else{
$power_obj->status = 2;
}
}
// $power_obj->status = 2;
$pType = substr($power_no, 2, 2);//获取讲解器类型
$season = $this->getSeason($pType);
$power_obj->machine_id = $machine_obj->id;
$power_obj->hatch_no = $hatch_no;
$power_obj->has_power = $has_power;
$power_obj->power_type = $pType;
$power_obj->season = $season;
$power_obj->save();
if($on){
return $power_obj;
}else{
return null;
}
}
public function getInfo($hatch_no)
{
$where = [
'hatch_no'=>$hatch_no
];
$info = Power::where($where)->first();
Log::info($info);
if($info == null){
throw new ApiException(ErrorCode::SALE_POWER_NOT_EXIST, trans('api.error.sale_power_not_exist'));
}
return $info;
}
public function getPowerInfo($hatch_no, $mac_no)
{
$where = [
'hatch_no'=>$hatch_no,
'machine_id'=>$mac_no
];
$info = Power::where($where)->orderBy('status', 'desc')->first();
Log::info($info);
if($info == null){
throw new ApiException(ErrorCode::SALE_POWER_NOT_EXIST, trans('api.error.sale_power_not_exist'));
}
return $info;
}
public function getSeason($pType)
{
$datas = [
['A', 'E', 'I', 'M'],
['B', 'F', 'J', 'N'],
['C', 'G', 'K', 'O'],
['D', 'H', 'L', 'P'],
];
foreach ($datas as $k=>$dada){
if(in_array($pType, $dada)){
return $k + 1;
}
}
return 0;
}
}