Production.php
2.39 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
<?php
namespace domain\production;
use Yii;
use domain\production\models\Production as ProductionModel;
use Exception;
/**
* 生产日期
* Class Production
* @package domain\production
*/
class Production
{
/**
* 创建生产日期
* @param $item
*/
static function create($item)
{
try {
$findProductionModel = ProductionModel::findOne(['name' => $item["name"]]);
if (!empty($findProductionModel)) {
return -1;
}
$productionModel = Yii::createObject(ProductionModel::className());
$productionModel->production_no = self::getProductionNo();
$productionModel->name = $item["name"]; // 生产日期
$saveResult = $productionModel->save();
return $saveResult;
} catch (Exception $e) {
return false;
}
}
/**
* 更新生产日期
* @param $id
* @param $item
* @return null|static
*/
static function update($id, $item)
{
$productionModel = ProductionModel::findOne($id);
if (empty($productionModel)) {
return false;
}
if (isset($item['name']) && $productionModel->name != $item['name']) {
$findProductionModel = ProductionModel::findOne(['name' => $item["name"]]);
if (!empty($findProductionModel)) {
return -1;
}
}
if (isset($item['name']) && !empty($item['name'])) {
$productionModel->name = $item['name'];
}
$resultSave = $productionModel->save();
return $resultSave;
}
/**
* 删除生产日期
* @param $id
* @param $item
* @return null|static
*/
public static function delete($id)
{
$productionModel = ProductionModel::findOne($id);
if (empty($productionModel)) {
return false;
}
return ProductionModel::updateAll(["is_delete" => 1], ["id" => $id]);
}
/**
* 获取十六进制生产日期编号
*/
private static function getProductionNo()
{
$findProduction = ProductionModel::find()->orderBy("id desc")->asArray()->one();
if (empty($findProduction)) {
return "0001";
}
$dataNo = hexdec($findProduction['production_no']) + 1;
$dataNo = sprintf('%04X', $dataNo);
return strtoupper($dataNo);
}
}