DeviceRepository.php
4.83 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
<?php
namespace domain\device;
use common\helpers\Log as AppLog;
use domain\device\models\CreateBatch as CreateBatchModel;
use domain\device\models\Device as DeviceModel;
use domain\device\models\DeviceStats;
class DeviceRepository
{
const DEBUG = false;
private static function myLog($str)
{
if (false == self::DEBUG) {
return '';
}
AppLog::DEBUG($str);
}
/**
* @param $where
* @return array|\yii\db\ActiveRecord[]
*/
static function getList($where, $limit = 0, $offset = 0)
{
$deviceFind = DeviceModel::find();
$deviceFind->alias('a');
$deviceFind->select(['a.*','b.batch_no', 'm.name as manufacture','p.name as project','pd.name as production','mo.name as model']);
$deviceFind->leftJoin(CreateBatchModel::tableName().' b','b.id = a.batch_id');
$deviceFind->leftJoin('manufacture as m', 'm.id = b.manufacture_id');
$deviceFind->leftJoin('project as p', 'p.id = b.project_id');
$deviceFind->leftJoin('model as mo', 'mo.id = b.model_id');
$deviceFind->leftJoin('production as pd', 'pd.id = b.production_id');
$deviceFind->where($where);
$deviceFind->orderBy('created_at desc');
$deviceFind->asArray();
if ($offset) {
$deviceFind->offset($offset);
}
if ($limit) {
$deviceFind->limit($limit);
}
$all = $deviceFind->all();
return $all;
}
/**
* @param $where
* @return int|string
*/
static function getListCount($where)
{
$deviceFind = DeviceModel::find();
$deviceFind->alias('a');
$deviceFind->leftJoin(CreateBatchModel::tableName().' b','b.id = a.batch_id');
$deviceFind->leftJoin('manufacture as m', 'm.id = b.manufacture_id');
$deviceFind->leftJoin('project as p', 'p.id = b.project_id');
$deviceFind->leftJoin('model as mo', 'mo.id = b.model_id');
$deviceFind->leftJoin('production as pd', 'pd.id = b.production_id');
$deviceFind->where($where);
$all = $deviceFind->count();
return $all;
}
/**
* @param $condition
* @return null|static
*/
static function findOne($condition)
{
return DeviceModel::findOne($condition);
}
/**
* @param $condition
* @return array|\yii\db\ActiveRecord[]
*/
static function findAll($condition)
{
$deviceModel = DeviceModel::find();
$deviceModel->where($condition);
$list = $deviceModel->all();
return $list;
}
/**
* @param $condition
* @return int|string
*/
static function rowsCount($condition)
{
$deviceModel = DeviceModel::find();
$deviceModel->where($condition);
$count = $deviceModel->count();
self::myLog('SQL:'.$deviceModel->createCommand()->rawSql);
return $count;
}
/**
* 判断是否要登记到授权失败表
* @param $batchId
* @param $batchNum
* @return bool
*/
static function checkNeedRecordFailRecord($batchId, $batchNum)
{
// 第1个情况,全部授权满了
$authCount = self::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
if (($authCount *1) >= ($batchNum * 1)) {
return true;
}
// 第2个情况,授权数目 + 删除后恢复数满了
$where = ['and',
['=', 'batch_id', $batchId],
['is not', 'device_id', null],
['in', 'status', [DeviceStatus::NO_AUTH, DeviceStatus::FAIL_AUTH]],
['=', 'is_delete', 0],
];
$restoreCount = self::rowsCount($where);
if (($restoreCount *1 + $authCount) >= ($batchNum * 1)) {
return true;
}
return false;
}
/**
* 判断是否要自动产生序列号
* @param $batchId
* @param $batchNum
* @return bool
*/
static function checkNeedAutoGen($batchId, $batchNum)
{
$where = [
'and',
['=', 'batch_id' ,$batchId],
['=', 'is_delete', 0],
['=', 'status', DeviceStatus::NO_AUTH],
['is', 'device_id', null]
];
$authCount = self::rowsCount($where);
self::myLog('checkNeedAutoGen data:'.$authCount.' __'. $batchNum);
if (empty($authCount)) {
return true;
}
return false;
}
/**
* @param $batchId
* @return array|null|\yii\db\ActiveRecord
*/
static function findBatchRandOne($batchId)
{
$deviceFind = DeviceModel::find();
$deviceFind->where(['device_id' => null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
$deviceFind->orderBy("rand()");
$nullDevice = $deviceFind->one();
return $nullDevice;
}
}