ProjectLogic.php 17.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 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
<?php

namespace App\Http\Logic\Aside\Project;

use App\Models\Com\NoticeLog;
use App\Models\Devops\ServerConfig;
use App\Models\Project\ProjectRenew;
use App\Models\User\ProjectMenu;
use App\Models\User\ProjectRole;
use Illuminate\Support\Arr as SupArr;
use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\FormGlobalsoApi;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Channel\Channel;
use App\Models\Channel\User;
use App\Models\Channel\Zone;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquirySet;
use App\Models\Manage\Manage;
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\Task\Task;
use App\Services\ProjectServer;
use Hashids\Hashids;
use App\Models\User\User as UserModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * Class ProjectLogic
 * @package App\Http\Logic\Aside\Project
 * @author zbj
 * @date 2023/4/26
 */
class ProjectLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new Project();
    }

    /**
     * @remark :获取当前数据详情
     * @name   :getProjectInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/28 17:11
     */
    public function getProjectInfo($id){
        $info = $this->model->with('payment')->with('deploy_build')->with('deploy_optimize')->with('online_check')
            ->with('project_after')->where(['id'=>$id])->first()->toArray();
        $info['online_check']['name'] = (new Manage())->getName(!empty($info['online_check']['created_manage_id']) ?? '');
        $info['deploy_optimize']['minor_keywords'] = !empty($info['deploy_optimize']['minor_keywords']) ? json_decode($info['deploy_optimize']['minor_keywords']) : [];
        $info['init_domain'] = $this->getInitDomain($info['serve_id'])['domain'];
        if($info['extend_type'] != 0){
            $info['type'] = $info['extend_type'];
        }
        return $this->success($info);
    }

    /**
     * @remark :获取初始域名
     * @name   :getInitDomain
     * @author :lyh
     * @method :post
     * @time   :2023/9/16 9:40
     */
    public function getInitDomain($serve_id = ''){
        $domain = '';
        if(!empty($serve_id)){
            $serveModel = new ServerConfig();
            $info = $serveModel->read(['id'=>$serve_id]);
            if($info !== false){
                $domain = $info['init_domain'];
            }
        }
        return $this->success(['domain'=>$domain]);
    }

    /**
     * @remark :保存项目数据
     * @name   :projectSave
     * @author :lyh
     * @method :post
     * @time   :2023/8/30 11:57
     */
    public function projectSave(){
        DB::beginTransaction();
        try {
            if($this->param['type'] == Project::TYPE_SEVEN){
                //错误单直接返回,单独处理
                $this->setTypeSevenEdit($this->param);
            }else{
                //初始化项目
                $this->createProjectData($this->param);
                //保存项目信息
                $this->saveProject($this->param);
                //保存建站部署信息
                $this->saveProjectDeployBuild($this->param['deploy_build']);
                //保存付费信息
                $this->saveProjectPayment($this->param['payment']);
                //保存优化信息
                $this->saveProjectDeployOptimize($this->param['deploy_optimize']);
                //保存售后信息
                $this->saveProjectAfter($this->param['project_after']);
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @remark :保存项目
     * @name   :setExtendType
     * @author :lyh
     * @method :post
     * @time   :2023/8/30 12:14
     */
    public function saveProject($param){
        if($param['type'] == Project::TYPE_FIVE){
            $param['extend_type'] = Project::TYPE_FIVE;
            unset($param['type']);
        }
        if(isset($param['level']) && !empty($param['level'])){
            $param['level'] = Arr::arrToSet($param['level']);
        }
        if(isset($param['channel']) && !empty($param['channel'])){
            $param['channel'] = Arr::a2s($param['channel']);
        }
        if(isset($param['notice_file']) && !empty($param['notice_file'])){
            foreach ($param['notice_file'] as &$v1) {
                $v1['url'] = str_replace_url($v1['url']);
            }
            $param['notice_file'] = Arr::a2s($param['notice_file']);
        }
        if(isset($param['confirm_file']) && !empty($param['confirm_file'])){
            foreach ($param['confirm_file'] as &$v2) {
                $v2['url'] = str_replace_url($v2['url']);
            }
            $param['confirm_file'] = Arr::a2s($param['confirm_file']);
        }
        unset($param['payment'],$param['deploy_build'],$param['deploy_optimize'],$param['online_check'],$param['project_after']);

        //文件上传默认值
        if($param['is_upload_manage']){
            $param['upload_config'] = [
                'upload_max_num' => $param['upload_config']['upload_max_num'] ?? 100,
                'allow_file_type' => $param['upload_config']['allow_file_type'] ?? 'doc,docx,xls,xlsx,pdf,txt,csv,png,jpg,jpeg',
                'upload_max_size' => $param['upload_config']['upload_max_size'] ?? 5,
            ];
        }


        $this->model->edit($param,['id'=>$param['id']]);
        Common::del_user_cache($this->model->getTable(),$param['id']);
        return $this->success();
    }

    /**
     * @remark :保存付款续费
     * @name   :savePayment
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 16:19
     */
    protected function saveProjectPayment($payment){
        $paymentModel = new Payment();
        if(isset($payment['contract']) && !empty($payment['contract'])){
            $payment['contract'] = Arr::a2s($payment['contract']);
        }
        if(isset($payment['bill']) && !empty($payment['bill'])){
            $payment['bill'] = Arr::a2s($payment['bill']);
        }
        if(isset($payment['renewal_record']) && !empty($payment['renewal_record'])){
            $payment['renewal_record'] = Arr::a2s($payment['renewal_record']);
        }
        $paymentModel->edit($payment,['id'=>$payment['id']]);
        return $this->success();
    }

    /**
     * @remark :保存建站部署
     * @name   :saveDeployBuild
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 16:19
     */
    protected function saveProjectDeployBuild($deploy_build){
        $deployBuildModel = new DeployBuild();
        if(isset($deploy_build['configuration']['build_status']) && ($deploy_build['configuration']['build_status'] == 0)){

        }
        $deploy_build['configuration'] = Arr::a2s(!empty($deploy_build['configuration']) ? $deploy_build['configuration'] : []);
        $deployBuildModel->edit($deploy_build,['id'=>$deploy_build['id']]);
        return $this->success();
    }

    /**
     * @remark :保存优化部署
     * @name   :saveDeployOptimize
     * @author :lyh
     * @method :post
     * @time   :2023/8/30 13:45
     */
    protected function saveProjectDeployOptimize($deploy_optimize){
        $deployOptimizeModel = new DeployOptimize();
        if(isset($deploy_optimize['domain']) && !empty($deploy_optimize['domain'])){
            //更改域名
            $this->editDomainStatus($deploy_optimize['domain'],$deploy_optimize['project_id']);
        }
        $deploy_optimize['minor_languages'] = Arr::a2s(!empty($deploy_optimize['minor_languages']) ? $deploy_optimize['minor_languages'] : []);
        $deploy_optimize['minor_keywords'] = Arr::a2s(!empty($deploy_optimize['minor_keywords']) ? $deploy_optimize['minor_keywords'] : []);
        $deploy_optimize['special'] = !empty($deploy_optimize['special']) ? ','.trim($deploy_optimize['special'],',').',' : '';
        //是否更新了api_no
        $api_no = DeployOptimize::where('id', $deploy_optimize['id'])->value('api_no');
        if($api_no != $deploy_optimize['api_no']){
            NoticeLog::createLog(NoticeLog::TYPE_RANK_DATA, ['api_no' => $deploy_optimize['api_no']]);
        }
        $deployOptimizeModel->edit($deploy_optimize,['id'=>$deploy_optimize['id']]);
        return $this->success();
    }

    /**
     * @remark :保存为售后部署
     * @name   :saveProjectAfter
     * @author :lyh
     * @method :post
     * @time   :2023/8/30 13:57
     */
    protected function saveProjectAfter($project_after){
        //查询数据是否存在
        $afterModel = new After();
        $afterModel->edit($project_after,['id'=>$project_after['id']]);
        return $this->success();
    }

    /**
     * @remark :创建初始数据
     * @name   :createProjectData
     * @author :lyh
     * @method :post
     * @time   :2023/8/30 14:30
     */
    public function createProjectData($param){
        //查看当前项目状态是否为初始项目
        $info = $this->model->read(['id'=>$param['id']]);
        //项目为初始项目时,只能选择建站中
        if(($info['type'] == Project::TYPE_ZERO) && ($info['type'] != Project::TYPE_FIVE)){
            $param['type'] = Project::TYPE_ONE;
        }
        //创建默认数据库
        if($param['type'] == Project::TYPE_ONE){
            //改为异步
            NoticeLog::createLog(NoticeLog::TYPE_INIT_PROJECT, ['project_id' => $param['id']]);
//            //初始化数据库
//            if(isset($param['mysql_id']) && !empty($param['mysql_id'])){
//                $this->initializationMysql($param['id']);
//            }
//            //初始账号
//            if(isset($param['mobile']) && !empty($param['mobile'])){
//                $this->createUser($param['mobile'],$param['id'],$param['lead_name']);
//            }
//            //更改服务器状态
//            if(isset($param['serve_id']) && !empty($param['serve_id'])){
//                $this->updateServe($param['serve_id']);
//            }
        }
        return $this->success();
    }

    /**
     * @remark :根据类型状态设置
     * @name   :setTypeStatusEdit
     * @author :lyh
     * @method :post
     * @time   :2023/9/12 11:20
     */
    public function setTypeSevenEdit($param){
        $info = $this->model->read(['id'=>$param['id']]);
        if($info['delete_status'] == 0){
            //删除原始项目
            $this->edit(['delete_status' => 1], ['id' => $param['id']]);
            //添加到续费单
            $data = [
                'title' => '【续费单】' . $param['title'],
                'company' => $param['company'],
                'lead_name' => $param['lead_name'],
                'mobile' => $param['mobile'],
                'qq' => $param['qq'],
                'channel' => json_encode($param['channel']),
                'requirement' => $param['requirement'],
                'cooperate_date' => $param['cooperate_date'],
                'service_duration' => $param['deploy_build']['service_duration'],
                'plan' => $param['deploy_build']['plan'],
                'amount' => $param['payment']['amount'],
                'contract' => json_encode($param['payment']['contract']),
                'bill' => json_encode($param['payment']['bill']),
            ];
            $renewModel = new ProjectRenew();
            $renewModel->add($data);
        }
        return $this->success();
    }

    /**
     * @remark :初始化数据库
     * @name   :initializationMysql
     * @author :lyh
     * @method :post
     * @time   :2023/8/4 15:08
     */
    public function initializationMysql($project_id){
        //切换数据库配置
        $project = ProjectServer::useProject($project_id);
        //创建数据库
        ProjectServer::createDatabase($project);
        //创建表
        ProjectServer::initTable();
        //初始数据
        ProjectServer::saveInitParam($project_id);
        return $this->success();
    }


    /**
     * @remark :更新服务器为已使用
     * @name   :updateServe
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 10:50
     */
    public function updateServe($serve_id){
        $serverModel = new ServerConfig();
        $serverModel->edit(['status'=>$serverModel::STATUS_ONE],['id'=>$serve_id]);
        return $this->success();
    }



    /**
     * @remark :创建用户
     * @name   :createUser
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 18:03
     */
    public function createUser($mobile,$project_id,$lead_name){
        $userModel = new UserModel();
        //查看当前用户是否存在
        $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,
                'operator_id'=>$this->manager['id'] ?? 0,
                'create_id'=>$this->manager['id'] ?? 0,
            ];
            $userModel->add($data);
        }
        return $this->success();
    }

    /**
     * @remark :创建角色
     * @name   :createdRole
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 11:16
     */
    public function createdRole($project_id){
        $roleModel = new ProjectRole();
        //查看当前用户是否存在
        $info = $roleModel->read(['project_id'=>$project_id]);
        if($info === false){
            $menuModel = new ProjectMenu();
            $ids = $menuModel->where(['status'=>0])->pluck('id')->toArray();
            $data = [
                'name'=>'超级管理员',
                'role_menu'=>implode(',',$ids),
                'project_id'=>$project_id,
                'type'=>1,
                'operator_id'=>$this->manager['id'],
                'create_id'=>$this->manager['id'],
            ];
            $roleModel->add($data);
        }
        return $this->success();
    }

    public function clearCache($id)
    {
        parent::clearCache($id);
        parent::setWith(['payment', 'deploy_build', 'deploy_optimize', 'online_check']);
        parent::clearCache($id);
    }

    /**
     * 保存询盘通知设置
     * @author zbj
     * @date 2023/5/17
     */
    public function saveInquirySet($param)
    {
        $project = $this->getCacheInfo($param['project_id']);
        //同步到接口
        $domain = parse_url((!empty($project['deploy_optimize']['domain']) ? ((new DomainInfo())->getDomain($project['deploy_optimize']['domain'])) :  ''))['host'];
        $emails = Arr::arrToSet($param['emails']??'', 'trim');
        $phones = Arr::arrToSet($param['phones']??'', 'trim');
        $form_global_api  = new FormGlobalsoApi();
        $res = $form_global_api->setInquiry($domain, $emails, $phones);
        if (!$res) {
            $this->fail('保存失败');
        }
        if ($res['status'] != 200) {
            $this->fail($res['message'] ?? '保存失败');
        }
        //保存
        $set = InquirySet::where('project_id', $param['project_id'])->first();
        if (!$set) {
            $set = new InquirySet();
        }
        $set->project_id = $param['project_id'];
        $set->emails = $emails;
        $set->phones = $phones;
        $set->save();
        return $this->success();
    }

    public function dataSource(){
        $data = [];
        $data['level'] = $this->model::levelMap();
        $data['type'] = $this->model::typeMap();
        $data['special'] = $this->model::specialMap();
        $data['plan'] = $this->model::planMap();
        return $this->success($data);
    }


    public function channelSource($param){
        switch ($param['type']){
            case Project::TYPE_ONE:
                return Zone::pluck('title', 'id')->toArray();
            case Project::TYPE_TWO:
                return Channel::where('zone_id', $param['zone_id']??0)->pluck('alias', 'id')->toArray();
            case Project::TYPE_THREE:
                return User::where('channel_id', $param['channel_id']??0)->pluck('name', 'id')->toArray();
        }
        return $this->success();
    }


    /**
     * @remark :修改域名时,同时更改其状态
     * @name   :domainStatus
     * @author :lyh
     * @method :post
     * @time   :2023/9/4 14:29
     */
    public function editDomainStatus($domain,$project_id){
        $domainModel = new DomainInfo();
        //先清空上一次所绑定的域名
        $info = $domainModel->read(['project_id'=>$project_id]);
        if($info !== false){
            $domainModel->edit(['project_id'=>0,'status'=>DomainInfo::STATUS_ZERO],['id'=>$info['id']]);
        }
        //重新设置域名
        $domainModel->edit(['status'=>DomainInfo::STATUS_ONE,'project_id'=>$project_id],['id'=>$domain]);
        return $this->success();
    }

    /**
     * @remark :删除
     * @name   :projectDel
     * @author :lyh
     * @method :post
     * @time   :2023/9/8 15:23
     */
    public function projectDel(){
        $rs = $this->edit(['delete_status'=>1],['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }

}