CreateProject.php
8.2 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
<?php
/**
* @remark :
* @name :CreateProject.php
* @author :lyh
* @method :post
* @time :2025/1/6 10:34
*/
namespace App\Console\Commands\Project;
use App\Helper\Arr;
use App\Models\Channel\Channel;
use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateVisit;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Manage\Mobile;
use App\Models\Project\After;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Payment;
use App\Models\Project\Project;
use App\Models\User\User as UserModel;
use App\Services\ProjectServer;
use Hashids\Hashids;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CreateProject extends Command
{
protected $signature = 'create_project';
protected $description = '创建项目';
public function handle(){
return $this->sync();
}
public function sync($is_update = 0){
$title = date('Ymd') . '-杭州露娜贸易有限公司';
$data = [
'project'=>[
'title' => $title,
'company' => '杭州露娜贸易有限公司',
'lead_name' => '13008931688',
'mobile' => '13008931688',
'mysql_id'=>Project::MYSQL_ID,
'serve_id'=>9,
'qq' => '',
'channel' => '{"user_id": "381", "zone_id": "4", "channel_id": "46"}',
'requirement' => '',
'cooperate_date' => '2019-11-13',
'from_order_id' => '',
'type' => $is_update,
'is_upgrade'=>$is_update,
],
'deploy_build' => [
'service_duration' => 0,
'plan' => $this->versionData('标准版'),
'login_mobile'=>'13008931688'
],
'deploy_optimize' => [
// 'api_no' => 0
],
'project_after' => [],
'payment' => [
'amount' => 0,
'contract'=>[],
'bill'=>[]
],
];
DB::beginTransaction();
try {
$id = $this->saveProject($data['project']);
$this->setPostId($data['deploy_build']['plan'],$id);
$this->savePayment($data['payment'],$id);
$this->saveDeployBuild($data['deploy_build'],$id);
$this->saveDeployOptimize($data['deploy_optimize'],$id);
$this->saveAfter($data['project_after'],$id);
$serversIpModel = new ServersIp();
$serversModel = new Servers();
$serversIpModel->where(['id'=>9])->increment('total',1);
$serversModel->where(['id'=>7])->increment('being_number',1);
if($is_update == 1){
//4.0或5.0更新到6.0
//初始化数据库
$this->initializationMysql($id);
//初始账号
$this->createUser($data['project']['mobile'],$id,$data['project']['lead_name']);
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
throw new \Exception($e->getMessage());
}
}
/**
* @remark :获取版本
* @name :versionData
* @author :lyh
* @method :post
* @time :2023/8/9 14:46
*/
public function versionData($param){
$data = Project::planMap();
$data = array_flip($data);
if(isset($data[$param])){
return $data[$param];
}else{
return 1;
}
}
/**
* @remark :保存项目
* @name :saveProject
* @author :lyh
* @method :post
* @time :2023/8/30 15:53
*/
public function saveProject($param){
$projectModel = new Project();
return $projectModel->addReturnId($param);
}
/**
* @remark :设置post_id
* @name :setPostId
* @author :lyh
* @method :post
* @time :2023/8/9 14:47
*/
public function setPostId($plan,$id){
$length = strlen((string)$id); // 获取变量的位数
$paddingLength = Project::TYPE_FIVE - $length; // 计算填充前面的 0 的位数
$zeros = str_repeat("0", $paddingLength);
$number = Project::TYPE_SIX.$plan.$zeros.$id;
$projectModel = new Project();
$projectModel->edit(['post_id'=>$number],['id'=>$id]);
return true;
}
/**
* 保存优化部署
* @author zbj
* @date 2023/4/26
*/
protected function saveAfter($param,$id){
$param['project_id'] = $id;
//查询数据是否存在
$afterModel = new After();
$info = $afterModel->read(['project_id'=>$id]);
if($info !== false){
$afterModel->edit($param,['id'=>$info['id']]);
}else{
$afterModel->add($param);
}
}
/**
* @remark :保存付款续费
* @name :savePayment
* @author :lyh
* @method :post
* @time :2023/8/29 16:19
*/
protected function savePayment($param,$id){
$param['project_id'] = $id;
$paymentModel= new Payment();
$param['contract'] = Arr::a2s($param['contract']);
$param['bill'] = Arr::a2s($param['bill']);
if(!isset($param['renewal_record'])){
$param['renewal_record'] = Arr::a2s([["amount"=> null, "remark"=> null, "expire_at"=> null]]);
}
$info = $paymentModel->read(['project_id'=>$id]);
if($info !== false){
$paymentModel->edit($param,['id'=>$info['id']]);
}else{
$paymentModel->add($param);
}
}
/**
* @remark :保存建站部署
* @name :saveDeployBuild
* @author :lyh
* @method :post
* @time :2023/8/29 16:19
*/
protected function saveDeployBuild($param,$id){
$param['project_id'] = $id;
$hashids = new Hashids('test_domain', 5, 'abcdefghjkmnpqrstuvwxyz1234567890');
$code = $hashids->encode($id);
$param['test_domain'] = 'https://v6-' . $code . '.globalso.site/';
$deployBuildModel = new DeployBuild();
$info = $deployBuildModel->read(['project_id'=>$id]);
if($info !== false){
$deployBuildModel->edit($param,['id'=>$info['id']]);
}else{
$deployBuildModel->add($param);
}
}
/**
* @remark :保存优化信息
* @name :saveDeployOptimize
* @author :lyh
* @method :post
* @time :2023/8/30 16:11
*/
protected function saveDeployOptimize($param,$id){
$param['project_id'] = $id;
$deployOptimizeModel = new DeployOptimize();
$info = $deployOptimizeModel->read(['project_id'=>$id]);
if($info !== false){
//存在就不处理了
// $deployOptimizeModel->edit($param,['id'=>$info['id']]);
}else{
$deployOptimizeModel->add($param);
}
}
/**
* 初始化数据库
* @param $project_id
* @author Akun
* @date 2023/09/19 15:12
*/
public function initializationMysql($project_id){
//切换数据库配置
$project = ProjectServer::useProject($project_id);
//创建数据库
ProjectServer::createDatabase($project);
//创建表
ProjectServer::initTable();
//初始数据
ProjectServer::saveInitParam($project_id);
}
/**
* 创建用户
* @param $mobile
* @param $project_id
* @param $lead_name
* @author Akun
* @date 2023/09/19 15:12
*/
public function createUser($mobile,$project_id,$lead_name){
$userModel = new UserModel();
//查看当前项目是否存在号码库中
$mobileModel = new Mobile();
$mobileInfo = $mobileModel->read(['mobile'=>$mobile]);
if($mobileInfo === false){
//查看当前用户是否存在
$info = $userModel->read(['mobile'=>$mobile,'project_id'=>$project_id]);
if($info === false){
$data = [
'mobile'=>$mobile,
'password'=>base64_encode(md5('123456')),
'project_id'=>$project_id,
'name'=>$lead_name,
'type'=>UserModel::TYPE_ONE
];
$userModel->add($data);
}
}
return true;
}
}