UserdeviceController.php
87.1 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
<?php
namespace app\ht\modules\device\controllers;
use Yii;
use yii\base\Exception;
use yii\db\StaleObjectException;
use yii\data\Pagination;
use yii\log\Logger;
use yii\db\Query;
use app\ht\controllers\BaseController;
use app\ht\helpers\QrcodeUtil;
use app\ht\helpers\Sort;
use common\components\adminlog\AdminLogs;
use common\exts\wechat\Http;
use common\helpers\Distance;
use common\helpers\Log as AppLog;
use common\helpers\ImageManager;
use common\models\EngineerProfile as EngineerProfileModel;
use common\models\Address as AddressModel;
use common\models\BindDeviceApply as BindDeviceApplyModel;
use common\models\BindDeviceApplyImg as BindDeviceApplyImgModel;
use common\models\BindDeviceApplyLog as BindDeviceApplyLogModel;
use common\models\Owner as OwnerModel;
use common\models\Qrcode as QrcodeModel;
use common\models\Model as ModelModel;
use common\models\RepairOrderLog as RepairOrderLogModel;
use common\models\SysSetting as SysSettingModel;
use common\models\SysUser as SysUserModel;
use common\models\UserDeviceLog as UserDeviceLogModel;
use common\models\SysUserProfile as SysUserProfileModel;
use common\models\User as UserModel;
use common\models\UserDevice as UserDeviceModel;
use domain\device\models\Device as DeviceModel;
use common\services\redis\RedisConfig;
use domain\device\BrandRepository;
use domain\device\DeviceCatRepository;
use domain\device\UserDeviceRepository;
use domain\engineer\EngineerRole;
use domain\marketing\subsidy\ApplyAwardRule;
use domain\trade\RepairOrderStatus;
use domain\trade\RepairOrderRepository;
use domain\marketing\freecommission\EngineerFreeCommission;
use stdClass;
use function intval;
use function file_exists;
use function mkdir;
use function filemtime;
use function time;
use function getimagesize;
use function image_type_to_extension;
use function stripos;
use function preg_match;
use function strtolower;
use function sizeof;
use function implode;
use function strtotime;
use function count;
use function strtoupper;
/**
* 用户设备管理
* @package app\ht\modules\device\controllers
*/
class UserdeviceController extends BaseController
{
const REDIS_LIST_DEL_COUNT = 5; // redis中删除对应值的数量
const SEARCH_MODEL_COUNT = 10; // 自动补全查询的条数
private $isDownloadImgToLocalServer = 0; // 如果等于1时,条形码审核接口访问本地服务器的图片,防止跨域问题
public function actionIndex()
{
// 获取父级分类
$deviceCatParentList = DeviceCatRepository::getTopParentCats();//find()->select("id,name,parent_id")->where(["parent_id" => 0])->asArray()->all();
$req = Yii::$app->request;
$pId = $req->get('deviceParentCat');
if (empty($pId)) {
$pId = intval($deviceCatParentList[0]['id']);
}
// 获取子级分类
// $deviceCatChildList = DeviceCat::find()->select("id,name,parent_id")->where(["parent_id" => $pId])->asArray()->all();
$params = array();
$params = $this->dataList();
$params['deviceCatParentList'] = $deviceCatParentList;
// $params['deviceCatChildList'] = $deviceCatChildList;
$params['catPid'] = 0;
$params['catId'] = 0;
$params['brandId'] = 0;
$params['brandList'] = BrandRepository::getAllAvailableBrands();
$params['search_model_count'] = self::SEARCH_MODEL_COUNT;
return $this->render("index", $params);
}
public function actionInfo()
{
$req = Yii::$app->request;
$id = $req->get('id');
//$userDeviceModel = new UserDeviceModel();
$userDevice = UserDeviceRepository::getUserDeviceInfoById(intval($id));
// 读取历史维修信息
$repalirLogList = RepairOrderRepository::getRepairLogList(intval($id), 1);
// 获取二维码
$img_path = QrcodeUtil::previewQrcodeImage($userDevice['number'] . ".png", $userDevice['number']);
$userDeviceLogs = UserDeviceLogModel::getUserDeviceLog($id);
foreach ($userDeviceLogs as $k => $item) {
if ($item['operator_type'] == UserDeviceLogModel::OPERATOR_TYPE_ADMIN) {
$operator = SysUserModel::findOne($item['operator_id']);
$userDeviceLogs[$k]['operator_name'] = $operator->username;
} elseif ($item['operator_type'] == UserDeviceLogModel::OPERATOR_TYPE_ENGINEER) {
$operator = EngineerProfileModel::findOne(["engineer_id" => $item['operator_id']]);
if (!empty($operator)) {
$userDeviceLogs[$k]['operator_name'] = ($operator->firstname . $operator->lastname) ? ($operator->firstname . $operator->lastname) : $operator->nickname;
} else {
$userDeviceLogs[$k]['operator_name'] = "";
}
} elseif ($item['operator_type'] == UserDeviceLogModel::OPERATOR_TYPE_USER) {
$user = UserModel::findOne($item['operator_id']);
$userDeviceLogs[$k]['operator_name'] = (isset($user) && !empty($user["name"])) ? $user["name"] : "";
}
}
$params = array();
$params['userDevice'] = $userDevice;
$params['repalirLogList'] = $repalirLogList;
$params['qrcode_img'] = $img_path;
$params['userDeviceLogs'] = $userDeviceLogs;
return $this->render("info", $params);
}
public function actionApply()
{
$req = Yii::$app->request;
$type = empty($req->get('type')) ? 1 : intval($req->get('type'));
// 获取父级分类
$deviceCatParentList = DeviceCatRepository::getTopParentCats();
// 获取子级分类
//$deviceCatChildList = DeviceCat::find()->select("id,name,parent_id")->where(["parent_id"=>intval($deviceCatParentList[0]['id'])])->asArray()->all();
$params = array();
$params = $this->getApplyDataList();
$params['deviceCatParentList'] = $deviceCatParentList;
//$params['deviceCatChildList'] = $deviceCatChildList;
$params['type'] = $type;
$params['catPid'] = 0;
$params['catId'] = 0;
$params['brandId'] = 0;
$params['brandList'] = BrandRepository::getAllAvailableBrands();
$params['roles'] = EngineerRole::getRoles('all');
return $this->render("apply", $params);
}
// 导出审核通过的
public function actionExportApply()
{
$req = Yii::$app->request;
$type = empty($req->get('type')) ? 1 : intval($req->get('type'));
//获取父级分类
$deviceCatParentList = DeviceCatRepository::getTopParentCats();//
//获取子级分类
//$deviceCatChildList = DeviceCat::find()->select("id,name,parent_id")->where(["parent_id"=>intval($deviceCatParentList[0]['id'])])->asArray()->all();
$params = array();
$params = $this->getApplyDataList(1);
$params['deviceCatParentList'] = $deviceCatParentList;
//$params['deviceCatChildList'] = $deviceCatChildList;
$params['type'] = $type;
$params['catPid'] = 0;
$params['catId'] = 0;
$params['brandId'] = 0;
$params['brandList'] = BrandRepository::getAllAvailableBrands();
/**
* 后台操作日志
*/
AdminLogs::doExport('贴码审核通过的', count($params['userDeviceList']));
return $this->renderPartial("exportApprove", $params);
}
/**
* @return string
* 成功
*/
public function actionSuccess()
{
$req = Yii::$app->request;
$id = $req->get('id');
$status = $req->get('status'); // 获取申请列表跳转过来的状态
// 读取设备申请信息
$userDeviceModel = new UserDeviceModel();
$userDevice = $userDeviceModel->getBindDeviceApplyUserDeviceById(intval($id));
// 转换成OSS服务器的图片url路径
$bindDeviceApplyImgModel = BindDeviceApplyImgModel::find();
$bindDeviceApplyImgModel->where(['bind_device_apply_id' => $userDevice['bind_device_apply_id']]);
$images = $bindDeviceApplyImgModel->all();
$imageArr = array();
if ($images) {
foreach ($images as $k => $v) {
if (isset($v->path) && $v->path != null && $v->path != '') {
$imageArr[] = ImageManager::getUrl($v->path);
}
}
}
// 读取申请操作日志
$logResult = BindDeviceApplyLogModel::find()->where(["bind_device_apply_id" => intval($id)])->asArray()->all();
$img_path = QrcodeUtil::previewQrcodeImage($userDevice['qrcode_number'] . ".png", $userDevice['qrcode_number']);
// 日志操作人类型:0用户、1工程师、2管理员
$data = array();
foreach ($logResult as $log) {
$tmp = array();
$tmp['log_at'] = $log['log_at'];
$tmp['content'] = $log['content'];
if ($log['operator_type'] == RepairOrderLogModel::OPERATOR_TYPE_USER) {
$user = UserModel::findOne(intval($log['operator_id']));
if (!empty($user)) {
$tmp['operator_user'] = "客户-" . $user->name;
}
} else if ($log['operator_type'] == RepairOrderLogModel::OPERATOR_TYPE_ENGINEER) {
$user = EngineerProfileModel::findOne(["engineer_id" => intval($log['operator_id'])]);
if (!empty($user)) {
$tmp['operator_user'] = "工程师-" . $user->firstname . $user->lastname;
}
} else if ($log['operator_type'] == RepairOrderLogModel::OPERATOR_TYPE_ADMIN) {
$user = SysUserProfileModel::findOne(["sys_user_id" => intval($log['operator_id'])]);
if (!empty($user)) {
$tmp['operator_user'] = "管理员-" . $user->realname;
}
} else if ($log['operator_type'] == RepairOrderLogModel::OPERATOR_TYPE_SYSTEM) {
$tmp['operator_user'] = "系统";
}
$tmp['operator_user'] = empty($tmp['operator_user']) ? "" : $tmp['operator_user'];
$data[] = $tmp;
}
return $this->render("success", array(
"userDevice" => $userDevice,
"operatorList" => $data,
'images' => $imageArr,
"qrcode_img" => $img_path,
"status" => $status)
);
}
/**
* @return string
* 失败
*/
public function actionAuth()
{
$req = Yii::$app->request;
$redis = Yii::$app->redis;
$id = $req->get('id');
$status = $req->get('status'); // 获取申请列表跳转过来的状态
$userId = Yii::$app->user->id;
$redis->lrem(RedisConfig::TODO_BIND_DEVICE_APPLYS, self::REDIS_LIST_DEL_COUNT, $id); //删除redis中对应值的list中的值
$inProcessing = 0;
if ($redis->hexists(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $id)) {
$str = $redis->hget(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $id);
$values = explode("-", $str);
if (isset($values[1]) && $userId != $values[1]) {
$inProcessing = 1;
}
} else {
$redis->hset(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $id, $id . "-" . $userId);
}
// 读取设备申请信息
$userDeviceModel = new UserDeviceModel();
$userDevice = $userDeviceModel->getBindDeviceApplyUserDeviceById(intval($id));
if ($userDevice['status'] != BindDeviceApplyModel::STATUS_APPLYING
&& $userDevice['status'] != BindDeviceApplyModel::STATUS_REVIEW) {
return $this->redirect(["success", "id" => $id, "status" => $userDevice['status']]);
}
// 读取申请失败操作日志
$logResult = BindDeviceApplyLogModel::find()->where(["bind_device_apply_id" => intval($id), "type" => 4])->asArray()->all();
$img_path = QrcodeUtil::previewQrcodeImage($userDevice['qrcode_number'] . ".png", $userDevice['qrcode_number']);
$bindDeviceApplyImgModel = BindDeviceApplyImgModel::find();
$bindDeviceApplyImgModel->where(['bind_device_apply_id' => $userDevice['bind_device_apply_id']]);
$images = $bindDeviceApplyImgModel->all();
$imageArr = array();
// 当条形码需要执行识别本地图片时,使用这个开关
if ($this->isDownloadImgToLocalServer == 1) {
$barcodePath = Yii::getAlias('@webroot') . "/tmp/barcode/";
if (!file_exists($barcodePath)) {
mkdir($barcodePath, 0777, true);
}
if ($images) {
foreach ($images as $k => $v) {
if (isset($v->path) && $v->path != null && $v->path != '') {
$image = ImageManager::getUrl($v->path);
$imageArr[] = $image;
$imageName = $this->getImageName(ImageManager::getUrl($v->path));
$imagePath = $barcodePath . $imageName;
// 判断文件是否已经存在
if (file_exists($imagePath)) {
// 判断文件最后修改时间,如果超过一个小时则重新下载图片,防止遇到名字一样的图片却不能覆盖的情况
$filemtime = filemtime($imagePath);
if (time() - $filemtime > 3600) {
Http::download($image, $imagePath);
}
} else {
Http::download($image, $imagePath);
}
}
}
}
} else {
if ($images) {
foreach ($images as $k => $v) {
if (isset($v->path) && $v->path != null && $v->path != '') {
$image = ImageManager::getUrl($v->path);
$imageArr[] = $image;
}
}
}
}
$params = array();
if ($userDevice['apply_by_type'] == BindDeviceApplyModel::APPLY_TYPE_BY_ENGINEER) {
$deviceOrders = BindDeviceApplyModel::bindDeviceApplyHandlingOrder($userDevice['user_device_id'], array('status' => RepairOrderStatus::getHandleStatus()));
$params['orders'] = $deviceOrders;
}
$bindApplyModel = BindDeviceApplyModel::findOne($id);
$params['userDevice'] = $userDevice;
$params['bindApplyInfo'] = $bindApplyModel ? $bindApplyModel->toArray() : null;
$params['images'] = $imageArr;
$params['brandList'] = BrandRepository::getAllAvailableBrands();
$params["status"] = $status;
$params["img_path"] = $img_path;
$params["inProcessing"] = $inProcessing;
$params['reasons'] = $this->getAuthFailReasons();
$params['reviewReasons'] = $this->getReviewReasons();
$params['isDownloadImgToLocalServer'] = $this->isDownloadImgToLocalServer;
$params['logResult'] = $logResult;
return $this->render("auth", $params);
}
// 审核不通过原因
private function getAuthFailReasons()
{
/*$arr = array(array('id' => 1, 'content' => '请上传正确的设备序列号图片'), array('id' => 2, 'content' => '请上传正确的设备型号图片'),
array('id' => 3, 'content' => '请上传清晰的设备序列号图片'), array('id' => 4, 'content' => '请上传清晰的设备型号图片'),
array('id' => 5, 'content' => '该设备不参与本次活动。(办公打印机/复印)'), array('id' => 6, 'content' => '设备正反面图片不属于同一台机器.'),
array('id' => 7, 'content' => '二次绑定'), array('id' => 'others', 'content' => '其他'),
*/
$arr = array(
array('id' => 1, 'content' => '无法识别设备序列号'), array('id' => 2, 'content' => '无法识别设备型号'),
array('id' => 3, 'content' => '该设备不在业务范围内(传真机/工业机等)'), array('id' => 4, 'content' => '设备正反面图片不属于同一台机器'),
array('id' => 5, 'content' => '整机照片不够完整/看不到所帖二维码'), array('id' => 6, 'content' => '图片二维码与系统二维码不相符.'),
array('id' => 7, 'content' => '图文印刷店/照相馆/知名房地产公司不采集'), array('id' => 8, 'content' => '生产日期超过15年'),
array('id' => 9, 'content' => '贴码位置有误'), array('id' => 10, 'content' => '二次绑定'),
array('id' => 'others', 'content' => '其他'),
);
return $arr;
}
// 审核接口
public function actionDoAuth()
{
$post = Yii::$app->request->post();
$req = Yii::$app->request;
$redis = Yii::$app->redis;
$auth = intval($post['auth']);
$device_apply_id = $post['device_apply_id'];
$over_write = $req->post('over_write');
// 从设备申请表读取申请资料,如果不存在,则表明操作无效
$deviceApply = BindDeviceApplyModel::findOne($device_apply_id);
if (!$deviceApply) {
return $this->renderJson(array("status" => 0, "msg" => "该贴码申请不存在"));
exit();
}
// 待审核/待复核才允许提交审核操作
if ($deviceApply->status != BindDeviceApplyModel::STATUS_APPLYING
&& $deviceApply->status != BindDeviceApplyModel::STATUS_REVIEW) {
return $this->renderJson(array("status" => 0, "msg" => "该贴码申请不可审核"));
exit();
}
if ($auth == 1) {
if ($over_write == 1) {
$result = $this->overWriteReview($deviceApply);
} else {
$result = $this->approveReview($deviceApply);
}
} else {
$result = $this->rejectReview($deviceApply);
}
// redis队列中值处理
if ($result['status'] == 1) {
//删除redis中对应的值
$redis->lrem(RedisConfig::TODO_BIND_DEVICE_APPLYS, self::REDIS_LIST_DEL_COUNT, $device_apply_id); //删除redis中对应值的list中的值
//删除正在处理的订单
if ($redis->hexists(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $device_apply_id)) {
$redis->hdel(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $device_apply_id);
}
} else {
// 先删除redis中对应值,然后再添加一条审核中的
$redis->lrem(RedisConfig::TODO_BIND_DEVICE_APPLYS, self::REDIS_LIST_DEL_COUNT, $device_apply_id); //删除redis中对应值的list中的值
//$redis->lpush(RedisConfig::TODO_BIND_DEVICE_APPLYS,$device_apply_id);
}
return $this->renderJson($result);
}
/**
* 判断是不是真正的图片
*/
private function isImage($filename)
{
$types = '.gif|.jpeg|.png|.bmp|.jpg'; // 定义检查的图片类型
if (file_exists($filename)) {
if ($info = @getimagesize($filename)) return 0;
$ext = image_type_to_extension($info['2']);
return stripos($types, $ext);
} else {
return false;
}
}
// 获取图片文件名
public function getImageName($path)
{
if (!preg_match('/\/([^\/]+\.[a-z]{3,4})$/i', $path, $matches))
die('Use image please');
$image_name = strtolower($matches[1]);
return $image_name;
}
// 覆盖操作 $deviceApply 为新的 apply 记录
private function overWriteReview($deviceApply)
{
$post = Yii::$app->request->post();
$status = ($post['status'] === '' || empty($post['status'])) ? 0 : $post['status']; // 获取申请列表跳转过来的状态
$device_apply_id = $post['device_apply_id'];
$deviceChildCat = $post['cat_id'];
$brand_id = $post['brand_id'];
$model_id = $post['model'];
$factory_time = $post['factory_time'];
$serial_no = $post['serial_no'];
$userId = Yii::$app->user->id;
$owner_id = $deviceApply->owner_id;
$address_id = $deviceApply->address_id;
$engineer_id = $deviceApply->engineer_id;
$qrcode_no = $deviceApply->qrcode_number;
// 根据型号获取 型号id
$model = ModelModel::find()->where(["brand_id" => $brand_id, "device_cat_id" => intval($deviceChildCat), "id" => $model_id])->asArray()->one();
if (sizeof($model) == 0) {
return array("status" => 0, "msg" => "型号不存在,请先补充型号信息");
}
$model_id = $model['id'];
$user_device_id = null;
// 根据二维码编号,获取二维码id
$qrcode = QrcodeModel::find()->where(["number" => $qrcode_no])->asArray()->one();
if (sizeof($qrcode) == 0) {
return array("status" => 0, "msg" => "无效的二维码编号,请确认");
}
// 旧的 user_device 记录
$oldUserDevice = UserDeviceModel::findOne(["serial_no" => $serial_no]);
if (empty($oldUserDevice)) {
return array("status" => 70, "msg" => "替换失败,找不到对应序列号");
}
// 旧二维码ID
$oldDeviceQrcodeId = $oldUserDevice->device_qrcode_id;
$oldBindDeviceApplyId = $oldUserDevice->bind_device_apply_id;
$newUserDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $deviceApply->id]);
if (empty($newUserDeviceModel)) {
return array("status" => 71, "msg" => "找不到对应用户设备");
}
// 是否能覆盖
$canOperate = BindDeviceApplyModel::canOperateDevice($newUserDeviceModel->id);
if (!$canOperate) {
return array("status" => 72, "msg" => "该设备还有订单在处理,不能覆盖");
}
// 获取设备信息
$device = DeviceModel::find()->where(["device_cat_id" => intval($deviceChildCat), "model_id" => $model_id])->asArray()->one();
if (sizeof($device) == 0) {
return array("status" => 0, "msg" => "此分类和型号下,无对应设备信息,请确认");
}
$transaction = Yii::$app->getDb()->beginTransaction();
try {
$device_id = $device['id'];
$qrcode_id = $qrcode['id'];
//记录数据到userdevice
$result = 0;
$oldUserDevice->owner_id = $owner_id;
$oldUserDevice->engineer_id = $engineer_id;
$oldUserDevice->device_id = $device_id;
$oldUserDevice->address_id = $address_id;
$oldUserDevice->device_qrcode_id = $qrcode_id;
$oldUserDevice->bind_device_apply_id = $device_apply_id;
$oldUserDevice->factory_time = $factory_time;
$oldUserDevice->updated_at = time();
$result = $oldUserDevice->save();
if ($oldBindDeviceApplyId != $device_apply_id) {
$tt = time();
BindDeviceApplyModel::updateOldBindDeviceApplyOrders($oldUserDevice->id, $deviceChildCat);
BindDeviceApplyModel::updateNewBindDeviceApplyOrders($newUserDeviceModel->id, $deviceChildCat, $oldUserDevice->id);
$dbConnection = Yii::$app->db;
// 之前旧的申请记录作废
$oldBindDeviceApplyModel = BindDeviceApplyModel::findOne(['id' => $oldBindDeviceApplyId]);
if ($oldBindDeviceApplyModel) {
$dbConnection->createCommand()
->insert('bind_device_apply_tmp', [
'id' => $oldBindDeviceApplyModel->id,
'ver' => $oldBindDeviceApplyModel->ver,
'engineer_id' => $oldBindDeviceApplyModel->engineer_id,
'owner_id' => $oldBindDeviceApplyModel->owner_id,
'address_id' => $oldBindDeviceApplyModel->address_id,
'qrcode_number' => $oldBindDeviceApplyModel->qrcode_number,
'status' => $oldBindDeviceApplyModel->status,
'reject_desc' => $oldBindDeviceApplyModel->reject_desc,
'apply_at' => $oldBindDeviceApplyModel->apply_at,
'apply_by_type' => $oldBindDeviceApplyModel->apply_by_type,
'updated_at' => $oldBindDeviceApplyModel->updated_at,
'pass_at' => $oldBindDeviceApplyModel->pass_at,
'cover_at' => $tt,
'old_user_device_id' => $oldUserDevice->id
])->execute();
$oldBindDeviceApplyModel->delete();
}
// 新的userDevice 作废
if ($newUserDeviceModel) {
//$discardUserDevice = array('updated_at'=>$tt,'device_id'=>0,'device_qrcode_id'=>0,'bind_device_apply_id'=>0); //'bind_device_apply_id'=>$oldBindDeviceApplyId
//$newUserDeviceModel->updateAttributes($discardUserDevice);
$dbConnection->createCommand()
->insert('user_device_tmp', [
'id' => $newUserDeviceModel->id,
'device_id' => $newUserDeviceModel->device_id,
'owner_id' => $newUserDeviceModel->owner_id,
'engineer_id' => $newUserDeviceModel->engineer_id,
'address_id' => $newUserDeviceModel->address_id,
'device_qrcode_id' => $newUserDeviceModel->device_qrcode_id,
'bind_device_apply_id' => $newUserDeviceModel->bind_device_apply_id,
'serial_no' => $newUserDeviceModel->serial_no,
'factory_time' => $newUserDeviceModel->factory_time,
'created_at' => $newUserDeviceModel->created_at,
'updated_at' => $newUserDeviceModel->updated_at
])->execute();
$newUserDeviceModel->delete();
}
}
// 更新之前旧的二维码资料为失效状态
$mQrcode = QrcodeModel::findOne(["id" => $oldDeviceQrcodeId]);
$mQrcode->status = QrcodeModel::STATUS_INVALID;
$mQrcode->save();
if ($result > 0) {
// 更新为绑定成功
$deviceApply->status = BindDeviceApplyModel::STATUS_APPLY_PASS;
$deviceApply->pass_at = time();
$deviceApply->save();
// 写入日志
$bindDeviceApplyLog = new BindDeviceApplyLogModel();
$bindDeviceApplyLog->bind_device_apply_id = $device_apply_id;
$bindDeviceApplyLog->operator_id = $userId;
$bindDeviceApplyLog->operator_type = 2;
$bindDeviceApplyLog->type = BindDeviceApplyLogModel::TYPE_CORVER_SUCCESS;
$bindDeviceApplyLog->content = "覆盖绑定成功";
$bindDeviceApplyLog->insert();
//更新二维码绑定状态
$modelQrcode = QrcodeModel::findOne(["id" => $qrcode_id]);
$modelQrcode->status = QrcodeModel::STATUS_BIND;
$modelQrcode->save();
/* 工程师贴码奖励 */
ApplyAwardRule::engineerAddPasteAward($deviceApply);
// 普通工程师直接贴码, 触发贴码免佣优惠券处理逻辑
if ($deviceApply->apply_by_type == BindDeviceApplyModel::APPLY_TYPE_BY_DIRECT) {
EngineerFreeCommission::onBindDeviceApplySuccess($engineer_id);
}
// 提交事务
$transaction->commit();
// 记录用户设备日志
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $device_apply_id]);
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => $oldBindDeviceApplyId,
'content' => "覆盖绑定成功",
'type' => UserDeviceLogModel::TYPE_OVERWRITE_SUCCESS]);
}
// 获取下一个
$nextid = $this->getNextData($status, $device_apply_id);
return array("status" => 1, "msg" => "绑定成功", "nextid" => $nextid);
} else {
// 记录用户设备日志
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $device_apply_id]);
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => $oldBindDeviceApplyId,
'content' => "覆盖绑定失败",
'type' => UserDeviceLogModel::TYPE_OVERWRITE_FAIL]);
}
return array("status" => 0, "msg" => "写入绑定信息失败");
}
} catch (Exception $exception) {
$transaction->rollBack();
Yii::getLogger()->log($exception->getTraceAsString(), Logger::LEVEL_ERROR);
if ($exception instanceof StaleObjectException) {
return array("status" => 0, "msg" => "该申请已被其他操作员审核完毕");
}
return array("status" => 0, "msg" => "数据库操作失败");
}
}
// 审核通过
private function approveReview($deviceApply)
{
$post = Yii::$app->request->post();
$status = ($post['status'] === '' || empty($post['status'])) ? 0 : $post['status']; // 获取申请列表跳转过来的状态
$device_apply_id = $post['device_apply_id'];
$deviceChildCat = $post['cat_id'];
$brand_id = $post['brand_id'];
$model_id = $post['model'];
$factory_time = $post['factory_time'];
$serial_no = $post['serial_no'];
$userId = Yii::$app->user->id;
$owner_id = $deviceApply->owner_id;
$address_id = $deviceApply->address_id;
$engineer_id = $deviceApply->engineer_id;
$qrcode_no = $deviceApply->qrcode_number;
// 根据型号获取 型号id, 这里不能直接靠 model_id 来判断 要加入传入的 brand_id 和device_cat_id
$modelM = ModelModel::find()->where(["brand_id" => $brand_id, "device_cat_id" => intval($deviceChildCat), "id" => $model_id]);
$model = $modelM->asArray()->one();
if (sizeof($model) == 0) {
return array("status" => 0, "msg" => "型号不存在,请先补充型号信息");
}
$model_id = $model['id'];
$user_device_id = null;
// 根据二维码编号,获取二维码id
$qrCodeModel = QrcodeModel::find()->where(["number" => $qrcode_no])->asArray()->one();
if (sizeof($qrCodeModel) == 0) {
return array("status" => 0, "msg" => "无效的二维码编号,请确认");
}
// 记录的 UserDevice
$userDevice = UserDeviceModel::findOne(['bind_device_apply_id' => $device_apply_id]);
if (empty($userDevice)) {
return array("status" => 100, "msg" => "找不到对应用户设备");
}
/*
$canOperate = BindDeviceApply::canOperateDevice($userDevice->id);
if (!$canOperate) {
return array("status" => 101, "msg" => "该设备还有订单在处理,不能提交审核");
}*/
$userDeviceSerialCheck = UserDeviceModel::find()->where(["serial_no" => $serial_no])->asArray()->one();
if (sizeof($userDeviceSerialCheck) > 0) {
return array("status" => 99, "msg" => "此设备序列号已经存在,重新版定型号", "data" => $this->getSuccessUserDeviceInfo($userDeviceSerialCheck['id']));
}
// 获取设备信息
$device = DeviceModel::find()->where(["device_cat_id" => intval($deviceChildCat), "model_id" => $model_id])->asArray()->one();
if (sizeof($device) == 0) {
return array("status" => 0, "msg" => "此分类和型号下,无对应设备信息,请确认");
}
$transaction = Yii::$app->getDb()->beginTransaction();
try {
$device_id = $device['id'];
$qrcode_id = $qrCodeModel['id'];
$result = 0;
if ($userDevice) {
$userDevice->device_id = $device_id;
$userDevice->owner_id = $owner_id;
$userDevice->engineer_id = $engineer_id;
$userDevice->address_id = $address_id;
$userDevice->device_qrcode_id = $qrcode_id;
$userDevice->serial_no = strtoupper($serial_no);
$userDevice->factory_time = $factory_time;
$result = $userDevice->save();
$user_device_id = $userDevice->id;
} else {
// 再次预防 user device 记录 为空
$userDevice = new UserDeviceModel();
$userDevice->device_id = $device_id;
$userDevice->owner_id = $owner_id;
$userDevice->engineer_id = $engineer_id;
$userDevice->address_id = $address_id;
$userDevice->device_qrcode_id = $qrcode_id;
$userDevice->bind_device_apply_id = $device_apply_id;
$userDevice->serial_no = strtoupper($serial_no);
$userDevice->factory_time = $factory_time;
$result = $userDevice->insert();
$user_device_id = $userDevice->id;
}
if ($result > 0) {
// 更新旧设备订单分类,订单名称暂时不改
BindDeviceApplyModel::updateOldBindDeviceApplyOrders($user_device_id, $deviceChildCat);
// 更新为绑定成功
$deviceApply->status = BindDeviceApplyModel::STATUS_APPLY_PASS;
$deviceApply->pass_at = time();
$deviceApply->save();
// 写入日志
$bindDeviceApplyLog = new BindDeviceApplyLogModel();
$bindDeviceApplyLog->bind_device_apply_id = $device_apply_id;
$bindDeviceApplyLog->operator_id = $userId;
$bindDeviceApplyLog->operator_type = 2;
$bindDeviceApplyLog->type = BindDeviceApplyLogModel::TYPE_APPLY_PASS;
$bindDeviceApplyLog->content = "绑定成功";
$bindDeviceApplyLog->insert();
// 更新二维码绑定状态
$mQrcode = QrcodeModel::findOne(["id" => $qrcode_id]);
$mQrcode->status = QrcodeModel::STATUS_BIND;
$mQrcode->save();
/* 工程师贴码奖励 */
ApplyAwardRule::engineerAddPasteAward($deviceApply);
// 普通工程师直接贴码, 触发贴码免佣优惠券处理逻辑
if ($deviceApply->apply_by_type == BindDeviceApplyModel::APPLY_TYPE_BY_DIRECT) {
EngineerFreeCommission::onBindDeviceApplySuccess($engineer_id);
}
// 提交事务
$transaction->commit();
// 记录用户设备日志
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $device_apply_id]);
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "审核绑定成功",
'type' => UserDeviceLogModel::TYPE_BIND_SUCCESS]);
}
// 获取下一个
$nextid = $this->getNextData($status, $device_apply_id);
return array("status" => 1, "msg" => "绑定成功", "nextid" => $nextid);
} else {
// 记录用户设备日志
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $device_apply_id]);
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "审核绑定失败",
'type' => UserDeviceLogModel::TYPE_BIND_FAIL]);
}
return array("status" => 0, "msg" => "写入绑定信息失败");
}
} catch (Exception $exception) {
$transaction->rollBack();
Yii::getLogger()->log($exception->getTraceAsString(), Logger::LEVEL_ERROR);
AppLog::DEBUG('= approveReview ='.$exception->getTraceAsString());
if ($exception instanceof StaleObjectException) {
return array("status" => 0, "msg" => "该申请已被其他操作员审核完毕");
}
return array("status" => 0, "msg" => "数据库操作失败");
}
}
/**
* @param $id
* @return array
*/
private function getSuccessUserDeviceInfo($id)
{
//$userDeviceModel = new UserDeviceModel();
$userDevice = UserDeviceRepository::getUserDeviceInfoById(intval($id));
// 转换成OSS服务器的图片url路径
$bindDeviceApplyImgModel = BindDeviceApplyImgModel::find();
$bindDeviceApplyImgModel->where(['bind_device_apply_id' => $userDevice['bind_device_apply_id']]);
$images = $bindDeviceApplyImgModel->all();
$imageArr = array();
if ($images) {
foreach ($images as $k => $v) {
if (isset($v->path) && $v->path != null && $v->path != '') {
$imageArr[] = ImageManager::getUrl($v->path);
}
}
}
$userDevice['apply_at'] = date("Y-m-d H:i:s", $userDevice['apply_at']);
// 读取申请操作日志
$logResult = BindDeviceApplyLogModel::find()->where(["bind_device_apply_id" => intval($id)])->asArray()->all();
$img_path = QrcodeUtil::previewQrcodeImage($userDevice['number'] . ".png", $userDevice['number']);
return array("userdevice" => $userDevice, "images" => $imageArr, "logresult" => $logResult, "qrcodeimg" => $img_path);
}
/**
* 审核拒绝处理
* @param $deviceApply
* @return array
* @throws \Exception
*/
private function rejectReview($deviceApply)
{
$post = Yii::$app->request->post();
$status = ($post['status'] === '' || empty($post['status'])) ? 0 : $post['status']; // 获取申请列表跳转过来的状态
$transaction = Yii::$app->getDb()->beginTransaction();
try {
$device_apply_id = $post['device_apply_id'];
$fail_content = $post['fail_content'];
// 贴码审核拒绝
$deviceApply->status = BindDeviceApplyModel::STATUS_APPLY_FAIL;
$deviceApply->reject_desc = $fail_content; // 保存拒绝原因
$deviceApply->save();
// 写入日志
$bindDeviceApplyLog = new BindDeviceApplyLogModel();
$bindDeviceApplyLog->bind_device_apply_id = $device_apply_id;
$bindDeviceApplyLog->operator_id = Yii::$app->user->id;
$bindDeviceApplyLog->operator_type = 2;
$bindDeviceApplyLog->type = BindDeviceApplyLogModel::TYPE_APPLY_FAIL;
$bindDeviceApplyLog->content = '绑定失败: ' . $fail_content;
$bindDeviceApplyLog->log_at = time();
$bindDeviceApplyLog->insert();
/**
* 发送模板消息: 绑定申请审核不通过
*/
if (!YII_ENV_DEV) {
// $engineerModel = Engineer::findOne($deviceApply->engineer_id);
// if ($engineerModel) {
// TemplateMessage::bindDevice($deviceApply, '未知设备', $engineerModel);
// }
/**
* 发送短信通知: 绑定申请审核不通过
*/
//SmsMessage::sendBindDeviceFailMsg($engineerModel->phone, '未知设备'); // 绑定设备模板消息足够
}
// 提交事务
$transaction->commit();
// 记录用户设备日志
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $device_apply_id]);
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "贴码审核拒绝",
'type' => UserDeviceLogModel::TYPE_REVIEW_REJECT]);
}
// 获取下一个
$nextid = $this->getNextData($status, $device_apply_id);
return array("status" => 1, "msg" => "操作成功", "nextid" => $nextid);
} catch (Exception $exception) {
$transaction->rollBack();
Yii::getLogger()->log($exception->getTraceAsString(), Logger::LEVEL_ERROR);
if ($exception instanceof StaleObjectException) {
return array("status" => 0, "msg" => "该申请已被其他操作员审核完毕");
}
return array("status" => 0, "msg" => "数据库操作失败");
}
}
/**
* 获取下一条数据
*/
private function getNextData($status, $bindDeviceApplyId)
{
$redis = Yii::$app->redis;
if ($status == BindDeviceApplyModel::STATUS_REVIEW) {
//$findnex = BindDeviceApply::find()->where(['status' => $status])->orderBy("id desc")->one();
$redis->lrem(RedisConfig::TOVIEW_BIND_DEVICE_APPLYS, self::REDIS_LIST_DEL_COUNT, $bindDeviceApplyId);
$findnex = $redis->rpop(RedisConfig::TOVIEW_BIND_DEVICE_APPLYS);
} else if ($status == BindDeviceApplyModel::STATUS_APPLYING) {
//$findnex= BindDeviceApply::find()->where(['status' => $status])->orderBy("id desc")->one();
$redis->lrem(RedisConfig::TODO_BIND_DEVICE_APPLYS, self::REDIS_LIST_DEL_COUNT, $bindDeviceApplyId);
$findnex = $redis->rpop(RedisConfig::TODO_BIND_DEVICE_APPLYS);
}
if ($findnex) {
return $findnex;
} else {
return 0;
}
}
/**
* 同步待处理审核申请队列
*/
public function actionSetDataToRedis()
{
$redis = Yii::$app->redis;
$applyList = BindDeviceApplyModel::find()->where("status in (0,3)")->orderBy("id asc")->all();
$redis->del(RedisConfig::TODO_BIND_DEVICE_APPLYS);
$redis->del(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS);
$redis->del(RedisConfig::TOVIEW_BIND_DEVICE_APPLYS);
foreach ($applyList as $applyItem) {
if ($applyItem) {
if ($applyItem->status == 0) {
$redis->lpush(RedisConfig::TODO_BIND_DEVICE_APPLYS, $applyItem->id);
} else if ($applyItem->status == 3) {
$redis->lpush(RedisConfig::TOVIEW_BIND_DEVICE_APPLYS, $applyItem->id);
}
}
}
$e = new stdClass();
$e->status = true;
return $this->renderJson($e);
}
/**
* 判断该申请是否有人在处理
*/
public function actionApplyIsProcessing()
{
$redis = Yii::$app->redis;
$post = Yii::$app->request->post();
$e = new stdClass();
$e->status = false;
if (empty($post['device_apply_id'])) {
$e->msg = '缺少必要参数';
return $this->renderJson($e);
}
$device_apply_id = $post['device_apply_id'];
$applyItem = BindDeviceApplyModel::findOne($device_apply_id);
if (empty($applyItem)) {
$e->msg = "该申请数据不存在";
return $this->renderJson($e);
}
$userId = Yii::$app->user->id;
if ($redis->hexists(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $device_apply_id)) {
$str = $redis->hget(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $device_apply_id);
$values = explode("-", $str);
if (isset($values[1]) && $userId != $values[1]) {
$userInfo = SysUserModel::findOne($values[1]);
$e->status = 2;
$e->msg = $userInfo->username . "正在处理中";
return $this->renderJson($e);
}
}
$e->status = 1;
$e->msg = "跳转正常";
return $this->renderJson($e);
}
/**
* 根据品牌/型号/分类生成设备名称
* 格式: 品牌-型号-类型
* @param $brandId
* @param $modelId
* @param $deviceCatId
* @return string
*/
static public function generateDeviceName($brandId, $modelId, $deviceCatId)
{
$deviceName = '';
$brandModel = BrandRepository::selectOne($brandId);
if ($brandModel) {
$deviceName = $brandModel->chinese_name;
}
if ($modelId) {
$model = ModelModel::findOne($modelId);
if ($model) {
$deviceName = $deviceName . '-' . $model->model;
}
}
$catModel = DeviceCatRepository::selectOne($deviceCatId);
if ($catModel) {
$deviceName = $deviceName . '-' . $catModel->name;
}
return $deviceName;
}
protected function dataList()
{
$req = Yii::$app->request;
$cat_pid = $req->get('deviceParentCat');//设备一级分类
$cat_id = $req->get('deviceChildCat');//设备二级分类
#$brand = $req->get('brand'); 暂不支持基于品牌查询
$engineer = $req->get('engineer');//绑定工程师
$time_begin = $req->get('time_begin');//绑定时间(起)
$time_end = $req->get('time_end');//绑定时间(止)
$owner = $req->get('owner');//设备拥有者
$serial_no = $req->get('serial_no');//序列号
$model_no = $req->get('model_no'); //型号
$qrcode_no = $req->get('qrcode_no'); //二维码编号
$get = array();
$get['cat_pid'] = empty($cat_pid) ? 0 : $cat_pid;
$get['cat_id'] = empty($cat_id) ? 0 : $cat_id;
#可以构建一个解析器。。
$map = array();
// 过滤设备分类
if ($cat_pid > 0) {
if ($cat_id > 0) { // 选择了二级分类则精准查找
$map[] = array("=", "d.device_cat_id", $cat_id, "I");
} else { // 仅选择了一级分类, 则查询一级分类下所有子分类的数据
$deviceCatIdArray = array();
$childrens = DeviceCatRepository::getSubCatsByParentId($cat_pid, 'sort_order ASC', false);
foreach ($childrens as $child) {
$deviceCatIdArray[] = $child->id;
}
// 父节点
$deviceCatIdArray[] = $cat_pid;
$deviceCatIds = '(' . implode(",", $deviceCatIdArray) . ')';
$map[] = array("in", "d.device_cat_id", $deviceCatIds, "I"); // 转换为sql: id IN (1,2,3)
}
}
if (!empty($engineer)) {
$map[] = array(" like ", "concat(p.firstname,p.lastname)", "%" . $engineer . "%", "S"); #I : int , S:string
$get['engineer'] = $engineer;
}
if (!empty($serial_no)) {
$map[] = array(" like ", "ud.serial_no", "%" . $serial_no . "%", "S");
$get['serial_no'] = $serial_no;
}
if (!empty($model_no)) {
$map[] = array(" like ", "m.model", "%" . $model_no . "%", "S");
$get['model_no'] = $model_no;
}
if (!empty($qrcode_no)) {
$map[] = array(" like ", "dp.qrcode_number", "%" . $qrcode_no . "%", "S");
$get['qrcode_no'] = $qrcode_no;
}
if (!empty($time_begin)) {
$map[] = array(">=", "ud.created_at", strtotime($time_begin), "I"); #I : int , S:string
$get['time_begin'] = $time_begin;
}
if (!empty($time_end)) {
$map[] = array("<", "ud.created_at", strtotime($time_end) + SysSettingModel::ONE_DAY_SECONDS, "I"); #I : int , S:string
$get['time_end'] = $time_end;
}
if (!empty($owner)) {
$map[] = array("like", "o.name", "%" . $owner . "%", "S"); #I : int , S:string
$get['owner'] = $owner;
}
/**
* 分页处理
*/
//$userDeviceModel = new UserDeviceModel();
$pageSize = $req->get("pageSize") ? (int)$req->get("pageSize") : 20;
$pages = new Pagination(['totalCount' => UserDeviceRepository::getUserDeviceListCount($map), 'pageSize' => $pageSize]);
$userDeviceList = UserDeviceRepository::getUserDeviceList($pages->offset, $pages->limit, $map);
return [
'userDeviceList' => $userDeviceList,
'pages' => $pages,
'gets' => $get,
];
}
/**
* 获取申请列表
*/
protected function getApplyDataList($withPage = 0)
{
$req = Yii::$app->request;
$brandId = $req->get("brand");
$cat_pid = $req->get('deviceParentCat');//设备一级分类
$cat_id = $req->get('deviceChildCat');//设备二级分类
#$brand = $req->get('brand'); 暂不支持基于品牌查询
$engineer = $req->get('engineer');
$time_begin = $req->get('time_begin');
$time_end = $req->get('time_end');
$pass_time_begin = $req->get('pass_time_begin');
$pass_time_end = $req->get('pass_time_end');
$owner = $req->get('owner');
$serial_no = $req->get('serial_no');//序列号
$model_no = $req->get('model_no'); //型号
$qrcode_no = $req->get('qrcode_no'); //二维码编号
$status = intval($req->get('status'));
$apply_by_type = intval($req->get('apply_by_type'));
$role = isset($_GET['role'])?$_GET['role']:-1;
$auth_user = $req->get('auth_user');// 审核人员姓名
$get = array();
$get['cat_pid'] = empty($cat_pid) ? 0 : $cat_pid;
$get['cat_id'] = empty($cat_id) ? 0 : $cat_id;
$get['role'] = $role;
#可以构建一个解析器。。
$map = [];
if (isset($_GET['status']) && $_GET['status'] !== '') {
$map[] = array("=", "dp.status", $status);
} else {
$status = '';
}
$get['status'] = $status;
if (isset($_GET['apply_by_type']) && $_GET['apply_by_type'] !== '') {
$map[] = array("=", "dp.apply_by_type", $apply_by_type);
$apply_by_type = $_GET['apply_by_type'];
} else {
$apply_by_type = '';
}
$get['apply_by_type'] = $apply_by_type;
if (!empty($brandId)) {
$map[] = array("=", "m.brand_id", intval($brandId));
$get['brandId'] = intval($brandId);
} else {
$get['brandId'] = 0;
}
#可以构建一个解析器。。
// 过滤设备分类
if ($cat_pid > 0) {
if ($cat_id > 0) { // 选择了二级分类则精准查找
$map[] = array("=", "d.device_cat_id", $cat_id);
} else { // 仅选择了一级分类, 则查询一级分类下所有子分类的数据
$deviceCatIdArray = array();
$childrens = DeviceCatRepository::getSubCatsByParentId($cat_pid,'sort_order ASC', false);//find()->where(['parent_id' => $cat_pid])->orderBy('sort_order ASC')->all();
foreach ($childrens as $child) {
$deviceCatIdArray[] = $child->id;
}
//$deviceCatIds = '(' . implode(",", $deviceCatIdArray) . ')';
$map[] = array("in", "d.device_cat_id", $deviceCatIdArray); // 转换为sql: id IN (1,2,3)
}
}
if (!empty($engineer)) {
//$map[] = array("=","concat(p.firstname,p.lastname)",$engineer); #I : int , S:string
$map[] = array("or", array("like", "concat(p.firstname,p.lastname)", $engineer), array("like", "p.nickname", $engineer));
$get['engineer'] = $engineer;
}
if (!empty($time_begin)) {
$map[] = array(">=", "dp.apply_at", strtotime($time_begin)); #I : int , S:string
$get['time_begin'] = $time_begin;
}
if (!empty($time_end)) {
$map[] = array("<", "dp.apply_at", strtotime($time_end) + SysSettingModel::ONE_DAY_SECONDS); #I : int , S:string
$get['time_end'] = $time_end;
}
if (!empty($pass_time_begin)) {
$map[] = array(">=", "dp.pass_at", strtotime($pass_time_begin)); #I : int , S:string
$get['pass_time_begin'] = $pass_time_begin;
}
if (!empty($pass_time_end)) {
$map[] = array("<", "dp.pass_at", strtotime($pass_time_end) + SysSettingModel::ONE_DAY_SECONDS); #I : int , S:string
$get['pass_time_end'] = $pass_time_end;
}
if (!empty($owner)) {
$map[] = array("like", "o.name", $owner); #I : int , S:string
$get['owner'] = $owner;
}
if (!empty($auth_user)) {
$get['auth_user'] = $auth_user;
}
if (!empty($serial_no)) {
$map[] = array("like", "ud.serial_no", $serial_no);
$get['serial_no'] = $serial_no;
}
if (!empty($model_no)) {
$map[] = array("like", "m.model", $model_no);
$get['model_no'] = $model_no;
}
if (!empty($qrcode_no)) {
$map[] = array("like", "dp.qrcode_number", $qrcode_no);
$get['qrcode_no'] = $qrcode_no;
}
if (-1 != $role) {
$map[] = array("=", "e.role", $role);
}
if (count($map) == 1) {
$map = $map[0];
} else {
array_unshift($map, 'and');
}
$bindDeviceApply = new BindDeviceApplyModel();
if ($withPage == 0) {
/**
* 排序 分页处理
*/
$sortHelper = Yii::createObject(Sort::className());
$sortHelper->setAttributes($bindDeviceApply);
$sort['apply_at'] = $sortHelper->link('apply_at');
$getSort = $req->get("sort");
$orderBy = !empty($getSort) ? $sortHelper->getOrders() : 'apply_at DESC';
$pageSize = $req->get("pageSize") ? (int)$req->get("pageSize") : 20;
$pages = new Pagination(['totalCount' => BindDeviceApplyModel::getApplyBindUserDeviceListCount($map, $status, $auth_user), 'pageSize' => $pageSize]);
$userDeviceList = BindDeviceApplyModel::getApplyBindUserDeviceList($pages->offset, $pages->limit, $map, $orderBy, $auth_user);
return [
'userDeviceList' => $userDeviceList,
'pages' => $pages,
'gets' => $get,
'status' => $status,
'apply_by_type' => $apply_by_type,
'sort' => $sort
];
} else {
$userDeviceList = $bindDeviceApply->getApplyApproveBindUserDeviceList($map);
return [
'userDeviceList' => $userDeviceList,
];
}
}
// 更新拥有者
public function actionUpdateOwner()
{
$req = Yii::$app->request;
$id = $req->post('id');
$value = $req->post('value');
$column = $req->post('column');
$e = new stdClass();
$e->success = false;
if (empty($value) || empty($id) || empty($column)) {
$e->error = 'error update';
return $this->renderJson($e);
}
// 判断更新序列号
if ($column == "serial_no") {
$userDeviceSerialCheck = UserDeviceModel::find()->where(["serial_no" => $value])->asArray()->one();
if (sizeof($userDeviceSerialCheck) > 0) {
// 进行设备号二次确认
if ($id == $userDeviceSerialCheck["bind_device_apply_id"]) {
$e->error = '提交修改序列号和原来序列号一样!';
return $this->renderJson($e);
} else {
$e->error = '此设备序列号已经存在,无法重复绑定设备';
return $this->renderJson($e);
}
}
}
$bindDeviceApplyModel = BindDeviceApplyModel::find();
$bindDeviceApplyModel->leftJoin("owner", "bind_device_apply.owner_id = owner.id");
$bindDeviceApplyModel->leftJoin("address", "bind_device_apply.address_id = address.id");
$bindDeviceApplyModel->where(['bind_device_apply.id' => $id]);
$bindDeviceApplyModel->select(['owner.id as ownerId', "owner.name as ownerName", "owner.type as type", "address.id as addressId", "address.address as address"]);
$bindDeviceApplyModel->asArray();
$record = $bindDeviceApplyModel->one();
if (empty($record)) {
$e->error = '找不到记录';
return $this->renderJson($e);
} else {
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $id]);
if ($column == 'company_name') {
$ownerModel = OwnerModel::findOne($record['ownerId']);
$oldOwner = $ownerModel->name;
$ownerModel->name = $value;
// 记录用户设备日志
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "编辑设备拥有者({$oldOwner} => {$value})",
'type' => UserDeviceLogModel::TYPE_EDIT_OWNER]);
}
if ($ownerModel->save()) {
$e->success = true;
$e->error = '成功更新';
} else {
$e->error = '更新失败';
}
} elseif ($column == 'address') {
$addressModel = AddressModel::findOne($record['addressId']);
$oldAddress = $addressModel->address;
$addressModel->address = $value;
$addressModel->type = AddressModel::TYPE_DEVICE;
// 记录用户设备日志
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "编辑设备地址({$oldAddress} => {$value})",
'type' => UserDeviceLogModel::TYPE_EDIT_ADDRESS]);
}
if ($addressModel->save()) {
$e->success = true;
$e->error = '成功更新';
} else {
$e->error = '更新失败';
}
} elseif ($column == 'address_detail') {
$addressModel = AddressModel::findOne($record['addressId']);
$oldAddress = $addressModel->detail;
$addressModel->detail = $value;
// 记录用户设备日志
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "编辑详细门牌号({$oldAddress} => {$value})",
'type' => UserDeviceLogModel::TYPE_EDIT_ADDRESS]);
}
if ($addressModel->save()) {
$e->success = true;
$e->error = '成功更新';
} else {
$e->error = '更新失败';
}
} elseif ($column == 'serial_no') {
$userdeviceModel = UserDeviceModel::findOne(array("bind_device_apply_id" => $id));
$oldSerialNo = $userdeviceModel->serial_no;
$userdeviceModel->serial_no = strtoupper($value);
// 记录用户设备日志
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "编辑设备序列号({$oldSerialNo} => " . strtoupper($value) . ")",
'type' => UserDeviceLogModel::TYPE_EDIT_SERIAL_NO]);
}
if ($userdeviceModel->save()) {
$e->success = true;
$e->error = '成功更新';
} else {
$e->error = '更新失败';
}
}
return $this->renderJson($e);
}
}
public function actionDeviceQMap()
{
return $this->render('deviceQMap');
}
/**
* 设备地图
* @return string
*/
public function actionDeviceMap()
{
if (Yii::$app->request->isPost) {
$e = new stdClass();
$e->success = true;
$e->item = [];
$north_east = $this->request->post('north_east');
$south_west = $this->request->post('south_west');
$north_east = Distance::convertBD09ToGCJ02($north_east[0], $north_east[1]);
$south_west = Distance::convertBD09ToGCJ02($south_west[0], $south_west[1]);
$bindDeviceApplyModel = BindDeviceApplyModel::find();
$bindDeviceApplyModel->leftJoin("address", "address.id = bind_device_apply.address_id");
$bindDeviceApplyModel->select(["address.latitude", "address.longitude"]);
$bindDeviceApplyModel->where(['>', 'bind_device_apply.id', 0]);
$bindDeviceApplyModel->andFilterWhere(['<', "address.latitude", $north_east[0]]);
$bindDeviceApplyModel->andFilterWhere(['>', "address.latitude", $south_west[0]]);
$bindDeviceApplyModel->andFilterWhere(['<', "address.longitude", $north_east[1]]);
$bindDeviceApplyModel->andFilterWhere(['>', "address.longitude", $south_west[1]]);
$bindDeviceApplyModel->asArray();
//$userDeviceModel->limit(100);
$oList = $bindDeviceApplyModel->all();
$rItems = [];
foreach ($oList as $k => $v) {
$rItems[] = Distance::convertGCJ02ToBD09($v['longitude'], $v['latitude']);
}
$e->item = $rItems;
unset($oList);
return $this->renderJson($e);
} else {
return $this->render('deviceMap');
}
}
public function getMapDevice($key, $limit = 100)
{
$redis = Yii::$app->redis;
$north_east = $this->request->post('north_east');
$south_west = $this->request->post('south_west');
$ischecked = $this->request->post('ischecked');
$value = $redis->get($key);
$e = new stdClass();
$e->success = true;
$e->item = "";
$table1 = 'address';
$end = time();
$endT = strtotime(date('Y-m-d')) + 1;
$start = $endT - 6 * 24 * 3600;
$bindDeviceApplyModel = BindDeviceApplyModel::find();
$bindDeviceApplyModel->leftJoin("address", "address.id = bind_device_apply.address_id");
$bindDeviceApplyModel->select(["address.id as adid", "address.latitude", "address.longitude", "count(*) as mycount"]);
$bindDeviceApplyModel->where(['bind_device_apply.status' => BindDeviceApplyModel::STATUS_APPLY_PASS]);
//$bindDeviceApplyModel->andFilterWhere(['>','bind_device_apply.apply_at',$start]);
//$bindDeviceApplyModel->andFilterWhere(['<','bind_device_apply.apply_at',$end]);
$bindDeviceApplyModel->andFilterWhere(['<', "address.latitude", $north_east[0]]);
$bindDeviceApplyModel->andFilterWhere(['>', "address.latitude", $south_west[0]]);
$bindDeviceApplyModel->andFilterWhere(['<', "address.longitude", $north_east[1]]);
$bindDeviceApplyModel->andFilterWhere(['>', "address.longitude", $south_west[1]]);
if ($value) {
$bindDeviceApplyModel->andWhere("address.id not in (" . rtrim($value, ",") . ")");
}
$bindDeviceApplyModel->groupBy('adid');
$bindDeviceApplyModel->asArray();
$bindDeviceApplyModel->offset(0)->limit($limit);
$olist = $bindDeviceApplyModel->all();
$item = array('data' => array());
$max = 10;
$adids = $value ? $value : "";
if ($olist) {
foreach ($olist as $k => $v) {
if ($max < $v['mycount']) {
$max = $v['mycount'];
}
$item['data'][] = array('lat' => $v["latitude"], 'lng' => $v["longitude"]);
if ($v["adid"]) {
$adids .= "{$v["adid"]},";
}
}
}
if ($adids) {
if ($value) {
$redis->set($key, $adids);
} else {
$redis->set($key, $adids);
}
}
$item['adids'] = $adids;//$max;
//$item['max'] = 5;//$max;
$e->item = $item;
return $e;
}
public function actionHeatmap()
{
if (Yii::$app->request->isPost) {
$e = new stdClass();
$e->success = true;
$north_east = $this->request->post('north_east');
$south_west = $this->request->post('south_west');
$zoom = $this->request->post('zoom');
/*
$distance = 25000; //25公里内的点
$earthRadius = Distance::EARTH_RADIUS;
*/
$bindDeviceApplyModel = BindDeviceApplyModel::find();
$bindDeviceApplyModel->leftJoin("address", "address.id = bind_device_apply.address_id");
$bindDeviceApplyModel->select(["concat(address.latitude,'_',address.longitude) as latlng", "count(*) as mycount"]);
$bindDeviceApplyModel->where(['bind_device_apply.status' => BindDeviceApplyModel::STATUS_APPLY_PASS]);
//$bindDeviceApplyModel->andFilterWhere(['>','bind_device_apply.apply_at',$start]);
//$bindDeviceApplyModel->andFilterWhere(['<','bind_device_apply.apply_at',$end]);
$bindDeviceApplyModel->andFilterWhere(['<', "address.latitude", $north_east[0]]);
$bindDeviceApplyModel->andFilterWhere(['>', "address.latitude", $south_west[0]]);
$bindDeviceApplyModel->andFilterWhere(['<', "address.longitude", $north_east[1]]);
$bindDeviceApplyModel->andFilterWhere(['>', "address.longitude", $south_west[1]]);
$bindDeviceApplyModel->groupBy('latlng');
$bindDeviceApplyModel->asArray();
$olist = $bindDeviceApplyModel->all();
$item = array('max' => 10, 'data' => array());
$max = 10;
if ($olist) {
foreach ($olist as $k => $v) {
$latlngTemp = explode('_', $v['latlng']);
if ($max < $v['mycount']) {
$max = $v['mycount'];
}
$item['data'][] = array('lat' => $latlngTemp[0], 'lng' => $latlngTemp[1], 'count' => $v['mycount']);
}
}
$item['max'] = 5;//$max;
$e->item = $item;
return $this->renderJson($e);
} else {
$req = Yii::$app->request;
$deviceCatParentList = DeviceCatRepository::getTopParentCats();
$pId = $req->get('deviceParentCat');
if (empty($pId)) {
$pId = intval($deviceCatParentList[0]['id']);
}
//获取子级分类
$deviceCatChildList = DeviceCatRepository::getSubCatsByParentId($pId);
$deviceParentCat = $req->get('deviceParentCat');
$deviceCatId = $req->get('deviceChildCat');
$engineer = $req->get('engineer');
$time_begin = $req->get('time_begin');
$time_end = $req->get('time_end');
$owner = $req->get('owner');
$gets = array();
$gets['cat_pid'] = empty($deviceParentCat) ? 0 : intval($deviceParentCat);
if (!empty($deviceCatId)) {
$gets['cat_id'] = $deviceCatId;
} else {
$gets['cat_id'] = 0;
}
$prams = array('gets' => $gets, 'deviceCatParentList' => $deviceCatParentList, 'deviceCatChildList' => $deviceCatChildList);
return $this->render('heatmap', $prams);
}
}
// 搜索关键字
public function actionSearchItem()
{
$e = new stdClass();
$e->success = false;
$e->list = [];
$req = Yii::$app->request;
$keyword = $req->post('query');
$userDeviceModel = DeviceModel::find();
$userDeviceModel->leftJoin('device_cat', "device_cat.id = device.device_cat_id");
$userDeviceModel->leftJoin('model', "model.id = device.model_id");
$userDeviceModel->leftJoin('device_img', "device_img.device_id = device.id");
$userDeviceModel->select(['device.id as id', 'device.device_name as device_name', 'device.device_cat_id as cat_id',
'device_cat.name as cat_name', 'device_cat.parent_id as cat_parent_id',
'model.id as mode_id', 'model.model as model_no', 'model.brand_id as brand_id', 'device_img.path as img_path']);
$userDeviceModel->where(['or', ['like', 'device.device_name', $keyword], ['like', 'device_cat.name', $keyword]]);
$userDeviceModel->andWhere(['not like', 'device.device_name', 'JIWORK']);
$userDeviceModel->asArray();
$userDeviceModel->limit(15);
$deviceList = $userDeviceModel->all();
if ($deviceList) {
foreach ($deviceList as $k => $v) {
$deviceList[$k]['img_path'] = ImageManager::getUrl($v['img_path'], ImageManager::$STYLE_280);
}
$e->list = $deviceList;
}
return $this->renderJson($e);
}
// 搜设备分类关键字
public function actionSearchCat()
{
$e = new stdClass();
$e->success = false;
$e->list = [];
$req = Yii::$app->request;
$keyword = $req->post('query');
$userDeviceModel = DeviceModel::find();
$userDeviceModel->leftJoin('device_cat', "device_cat.id = device.device_cat_id");
$userDeviceModel->leftJoin('model', "model.id = device.model_id");
$userDeviceModel->leftJoin('device_img', "device_img.device_id = device.id");
$userDeviceModel->select(['device.id as id', 'device.device_name as device_name', 'device.device_cat_id as cat_id',
//'device_cat.name as cat_name','device_cat.parent_id as cat_parent_id',
'model.id as mode_id', 'model.model as model_no', 'model.brand_id as brand_id']);
$userDeviceModel->where(['or', ['like', 'device.device_name', $keyword], ['like', 'device_cat.name', $keyword]]);
$userDeviceModel->andWhere(['not like', 'device.device_name', 'JIWORK']);
$userDeviceModel->asArray();
$userDeviceModel->limit(15);
$deviceList = $userDeviceModel->all();
if ($deviceList) {
$e->list = $deviceList;
}
return $this->renderJson($e);
}
// 保存修改分类
public function actionSaveChange()
{
$e = new stdClass();
$e->success = false;
$req = Yii::$app->request;
$device_id = $req->post('device_id');
$bindApplyId = $req->post('id');
if (empty($device_id) || empty($bindApplyId)) {
$e->msg = 'ID 或设备为空!';
$this->renderJson($e);
}
$bindApplyModel = BindDeviceApplyModel::findOne($bindApplyId);
if (empty($bindApplyModel)) {
$e->msg = '没有该审核记录!';
$this->renderJson($e);
}
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $bindApplyId]);
if (empty($userDeviceModel)) {
$e->msg = '没有该用户设备!';
$this->renderJson($e);
}
if (0 == $userDeviceModel->device_id) {
$e->msg = '该用户设备未绑定过,无法更改类型!';
$this->renderJson($e);
}
$userDeviceModel->device_id = $device_id;
if ($userDeviceModel->save()) {
$e->success = true;
} else {
$e->msg = '更新分类失败!';
}
$this->renderJson($e);
}
// 请求复核原因
private function getReviewReasons()
{
$reasonArray = [
['id' => 1, 'content' => '新型号待录入'],
['id' => 2, 'content' => '图片二维码与手动录入二维码不相符'],
['id' => 3, 'content' => '覆盖时序列号相同但机型等其它信息均不同'],
['id' => 4, 'content' => '国外机型'],
['id' => 5, 'content' => '再生机'],
['id' => 'others', 'content' => '其他']
];
return $reasonArray;
}
/**
* 请求复核
* @return string
*/
public function actionDoReview()
{
$req = Yii::$app->request;
$redis = Yii::$app->redis;
$bindDeviceApplyId = $req->post('id');
$status = ($req->post('status') === '' || empty($req->post('status'))) ? 0 : $req->post('status'); // 获取申请列表跳转过来的状态
$reason = $req->post("reviewReason");
if (empty($bindDeviceApplyId) || empty($reason)) {
return $this->renderJson(array("status" => 0, "msg" => "缺少申请ID或理由"));
}
$bindDeviceApplyModel = BindDeviceApplyModel::findOne($bindDeviceApplyId);
if ($bindDeviceApplyModel == false) {
return $this->renderJson(array("status" => 0, "msg" => "请求复核失败, 无效贴码申请ID"));
}
if ($bindDeviceApplyModel->status != BindDeviceApplyModel::STATUS_APPLYING) {
return $this->renderJson(array("status" => 0, "msg" => "请求复核失败, 只有待审核状态的申请可以复核"));
}
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $bindDeviceApplyId]);
if (isset($userDeviceModel->id)) {
if (!BindDeviceApplyModel::canOperateDevice($userDeviceModel->id)) {
return $this->renderJson(array("status" => 0, "msg" => "该设备还有订单在处理,不能请求复核"));
}
}
$bindDeviceApplyModel->review_reason = $reason;
$bindDeviceApplyModel->status = BindDeviceApplyModel::STATUS_REVIEW;
$bindDeviceApplyModel->save();
// 记录用户设备日志
$userDeviceModel = UserDeviceModel::findOne(['bind_device_apply_id' => $bindDeviceApplyId]);
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog([
'user_device_id' => $userDeviceId,
'operator_id' => Yii::$app->user->id,
'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0,
'content' => "请求复核(原因=>{$reason})",
'type' => UserDeviceLogModel::TYPE_REVIEW]);
}
// 写入日志
$bindDeviceApplyLog = new BindDeviceApplyLogModel();
$bindDeviceApplyLog->bind_device_apply_id = $bindDeviceApplyId;
$bindDeviceApplyLog->operator_id = Yii::$app->user->id;
$bindDeviceApplyLog->operator_type = 2;
$bindDeviceApplyLog->type = BindDeviceApplyLogModel::TYPE_REVIEW;
$bindDeviceApplyLog->content = '请求复核';
$bindDeviceApplyLog->log_at = time();
$bindDeviceApplyLog->insert();
// 删除正在处理的订单
if ($redis->hexists(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $bindDeviceApplyId)) {
$redis->hdel(RedisConfig::PROCESSING_BIND_DEVICE_APPLYS, $bindDeviceApplyId);
}
// 获取下一个
$nextid = $this->getNextData($status, $bindDeviceApplyId);
return $this->renderJson(array("status" => 1, "msg" => "请求复核成功", "nextid" => $nextid));
}
/**
* 撤销重审
* @return string
*/
public function actionDoRevoke()
{
$req = Yii::$app->request;
$bindDeviceApplyId = $req->post('id');
$redis = Yii::$app->redis;
$bindDeviceApplyModel = BindDeviceApplyModel::findOne($bindDeviceApplyId);
if (false == $bindDeviceApplyModel) {
return $this->renderJson(array("status" => 0, "msg" => "撤销重审失败, 无效贴码申请ID"));
}
$userDeviceModel = UserDeviceModel::findOne(["bind_device_apply_id" => $bindDeviceApplyId]);
/* $bindRepairOrderModel = RepairOrder::find();
$user_device_id = isset($userDeviceModel->id)?$userDeviceModel->id:0;
$bindRepairOrderModel->where(['repair_device_type'=>RepairOrder::REPAIR_DEVICE_USER_TYPE,'repair_device_id'=>$user_device_id]);
$repairOrder = $bindRepairOrderModel->all();
if ($repairOrder){
return $this->renderJson(array("status"=>0, "msg"=>"撤销重审失败, 当前该贴码对应的设备已经有订单"));
}*/
if ($bindDeviceApplyModel->status != BindDeviceApplyModel::STATUS_APPLY_PASS
&& $bindDeviceApplyModel->status != BindDeviceApplyModel::STATUS_APPLY_FAIL) {
return $this->renderJson(array("status" => 0, "msg" => "撤销重审失败, 只有审核成功或失败状态的申请可以撤销重审"));
}
// 审核通过的记录要回退相关处理
if (BindDeviceApplyModel::STATUS_APPLY_PASS == $bindDeviceApplyModel->status) {
// 删除用户设备记录
if ($userDeviceModel) {
// 更新user_device 里面的 device id 为 0 ,撤销重审核 被拒绝这个id要改为0
$userDeviceModel->device_id = 0;
$userDeviceModel->serial_no = '';
$userDeviceModel->save();
}
// 二维码状态更新
$qrcodeModel = QrcodeModel::findOne(["number" => $bindDeviceApplyModel->qrcode_number]);
if ($qrcodeModel) {
$qrcodeModel->status = QrcodeModel::STATUS_UNBIND;
$qrcodeModel->save();
}
}
$bindDeviceApplyModel->status = BindDeviceApplyModel::STATUS_APPLYING;
$bindDeviceApplyModel->save();
// 记录用户设备日志
if ($userDeviceModel) {
$userDeviceId = $userDeviceModel->id;
UserDeviceLogModel::addLog(['user_device_id' => $userDeviceId, 'operator_id' => Yii::$app->user->id, 'operator_type' => UserDeviceLogModel::OPERATOR_TYPE_ADMIN,
'old_user_device_id' => 0, 'content' => "撤销重审", 'type' => UserDeviceLogModel::TYPE_REVIEW_CHECK]);
}
// 写入日志
$bindDeviceApplyLog = new BindDeviceApplyLogModel();
$bindDeviceApplyLog->bind_device_apply_id = $bindDeviceApplyId;
$bindDeviceApplyLog->operator_id = Yii::$app->user->id;
$bindDeviceApplyLog->operator_type = 2;
$bindDeviceApplyLog->content = '撤销重审';
$bindDeviceApplyLog->type = BindDeviceApplyLogModel::TYPE_RE_AUTH;
$bindDeviceApplyLog->log_at = time();
$bindDeviceApplyLog->insert();
$redis->lrem(RedisConfig::TODO_BIND_DEVICE_APPLYS, self::REDIS_LIST_DEL_COUNT, $bindDeviceApplyId); //删除redis中对应值的list中的值
$redis->lpush(RedisConfig::TODO_BIND_DEVICE_APPLYS, $bindDeviceApplyId);
return $this->renderJson(array("status" => 1, "msg" => "撤销重审成功"));
}
// 搜索序列号
public function actionSearchSn()
{
$e = new stdClass();
$e->success = false;
$e->list = [];
$req = Yii::$app->request;
$keyword = $req->post('query');
$userDeviceModel = UserDeviceModel::find();
$userDeviceModel->select(['id', 'device_id', 'serial_no']);
$userDeviceModel->where(['like', 'serial_no', $keyword]);
$userDeviceModel->asArray();
$userDeviceModel->limit(10);
$deviceList = $userDeviceModel->all();
if ($deviceList) {
$e->list = $deviceList;
}
return $this->renderJson($e);
}
/**
* 更新设备序列号
*/
public function actionSerialNoUpdate()
{
$post = Yii::$app->request->post();
$findone = UserDeviceModel::findOne(["serial_no" => $post['value']]);
if ($findone) {
if ($findone["id"] == $post['pk']) {
return $this->renderJson(array("status" => -1, "msg" => "序列号未做修改,修改失败"));
exit;
} else {
return $this->renderJson(array("status" => -1, "msg" => "该序列号已经存在,修改失败"));
exit;
}
}
$order = UserDeviceModel::findOne($post['pk']);
$order->serial_no = $post['value'];
$result = $order->save();
if ($result) {
return $this->renderJson(array("status" => 1, "msg" => "修改成功"));
exit;
} else {
return $this->renderJson(array("status" => -1, "msg" => "修改失败"));
exit;
}
}
// 审核图片列表
public function actionApplyImgGallery()
{
$req = Yii::$app->request;
$qRcode = $req->get('qrcode');
$bindDeviceApplyModel = BindDeviceApplyModel::find();
$status = array(BindDeviceApplyModel::STATUS_APPLY_FAIL, BindDeviceApplyModel::STATUS_APPLY_PASS, BindDeviceApplyModel::STATUS_REVIEW);
$bindDeviceApplyModel->select(['bind_device_apply.*']);
$bindDeviceApplyModel->where(['status' => $status]);
if (!empty($qRcode)) {
$bindDeviceApplyModel->andWhere(['like', 'qrcode_number', $qRcode]);
}
$bindDeviceApplyModel->asArray();
$pageSize = $req->get("pageSize") ? (int)$req->get("pageSize") : 48;
$pages = new Pagination(['totalCount' => $bindDeviceApplyModel->count(), 'pageSize' => $pageSize]);
$userDeviceList = $bindDeviceApplyModel->offset($pages->offset)->limit($pages->limit)->orderBy("apply_at desc")->all();
$bindDeviceApplyIds = [];
foreach ($userDeviceList as $k => $v) {
$bindDeviceApplyIds[] = $v['id'];
}
$bindDeviceImgModel = BindDeviceApplyImgModel::find();
$bindDeviceImgModel->where(['bind_device_apply_id' => $bindDeviceApplyIds]);
$bindDeviceImgModel->asArray();
$imagesT = $bindDeviceImgModel->all();
$images = array();
foreach ($imagesT as $kk => $vv) {
$images[$vv['bind_device_apply_id']][] = $vv;
}
foreach ($userDeviceList as $k => $v) {
$newV = $v;
$newV['images'] = isset($images[$v['id']]) ? $images[$v['id']] : [];
$userDeviceList[$k] = $newV;
}
$params = array('applyList' => $userDeviceList, 'pages' => $pages, 'qrcode' => $qRcode, 'flag' => 'apply');
return $this->render("imgGallery", $params);
}
// 审核图片列表
public function actionCoverImgGallery()
{
$req = Yii::$app->request;
$qRcode = $req->get('qrcode');
$query = new Query();
$query->select('id,qrcode_number')->from('bind_device_apply_tmp');
if (!empty($qRcode)) {
$query->where(['like', 'qrcode_number', $qRcode]);
}
$pageSize = $req->get("pageSize") ? (int)$req->get("pageSize") : 48;
$pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => $pageSize]);
$userDeviceList = $query->offset($pages->offset)->limit($pages->limit)->orderBy("apply_at desc")->all();
$bindDeviceApplyIds = [];
foreach ($userDeviceList as $k => $v) {
$bindDeviceApplyIds[] = $v['id'];
}
$bindDeviceImgModel = BindDeviceApplyImgModel::find();
$bindDeviceImgModel->where(['bind_device_apply_id' => $bindDeviceApplyIds]);
$bindDeviceImgModel->asArray();
$imagesT = $bindDeviceImgModel->all();
$images = array();
foreach ($imagesT as $kk => $vv) {
$images[$vv['bind_device_apply_id']][] = $vv;
}
foreach ($userDeviceList as $k => $v) {
$newV = $v;
$newV['images'] = isset($images[$v['id']]) ? $images[$v['id']] : [];
$userDeviceList[$k] = $newV;
}
$params = array('applyList' => $userDeviceList, 'pages' => $pages, 'qrcode' => $qRcode, 'flag' => 'cover');
return $this->render("imgGallery", $params);
}
public function actionGetOldImg()
{
$ids = array(13745, 13751, 13752);
$query = new Query();
$query->select('bind_device_apply_img.bind_device_apply_id as bind_device_apply_id, bind_device_apply_img.path as path, bind_device_apply_tmp.old_user_device_id as old_user_device_id')
->from('bind_device_apply_img')
->leftJoin("bind_device_apply_tmp", "bind_device_apply_tmp.id = bind_device_apply_img.bind_device_apply_id")
->where(array('bind_device_apply_img.bind_device_apply_id' => $ids));
$rows = $query->all();
foreach ($rows as $item) {
$img_path = ImageManager::getUrl($item['path']);
echo '<div>' . $item['old_user_device_id'] . '<img src="' . $img_path . '" width="200" /></div>';
}
}
/**
* 搜索型号
* @return string
*/
public function actionSearchModel()
{
$req = Yii::$app->request;
$keyword = $req->post('query');
$e = new stdClass();
$e->success = false;
$e->list = [];
$models = ModelModel::find()->where(["like", "model", $keyword])->limit(self::SEARCH_MODEL_COUNT)->asArray()->all();
$e->list = $models;
return $this->renderJson($e);
}
}