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

namespace App\Http\Logic\Aside\Project;


use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\FormGlobalsoApi;
use App\Http\Logic\Aside\BaseLogic;
use App\Http\Logic\Aside\Manage\ManageLogic;
use App\Models\ASide\APublicModel;
use App\Models\Blog\Blog;
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;

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

    public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20)
    {

        parent::setWith(['payment', 'deploy_build', 'deploy_optimize','online_check']);
        $list =  parent::getList($map, $sort, ['id', 'title', 'mysql_id' ,'channel','cooperate_date' ,'type', 'created_at'], $limit);
        $managerLogic = new ManageLogic();
        foreach ($list['list'] as $k => $item){
            var_dump(APublicModel::getProductNumByProjectId(1));
            $item = [
                'id' => $item['id'],
                'title' => $item['title'],
                'channel' => Channel::getChannelText($item['channel']['user_id'] ?? 0),
                'key' => $item['deploy_build']['keyword_num'] ?? 0,
                'day' => $item['deploy_build']['service_duration'] ?? 0,
                'amount' => $item['payment']['amount'] ?? 0,
                'build_leader' => $managerLogic->getCacheName($item['deploy_build']['leader_mid'] ?? 0), //组长
                'build_manager' => $managerLogic->getCacheName($item['deploy_build']['manager_mid'] ?? 0), //项目经理
                'build_designer' => $managerLogic->getCacheName($item['deploy_build']['designer_mid'] ?? 0), //设计师
                'build_tech' => $managerLogic->getCacheName($item['deploy_build']['tech_mid'] ?? 0), //技术助理
                'optimize_manager' => $managerLogic->getCacheName($item['deploy_optimize']['manager_mid'] ?? 0), //优化服务经理
                'optimize_optimist' => $managerLogic->getCacheName($item['deploy_optimize']['optimist_mid'] ?? 0), //优化师
                'optimize_assist' => $managerLogic->getCacheName($item['deploy_optimize']['assist_mid'] ?? 0), //优化助理
                'optimize_tech' => $managerLogic->getCacheName($item['deploy_optimize']['tech_mid'] ?? 0), //售后技术
                'type' => $item['type'],
                'test_domain' => $item['deploy_build']['test_domain'] ?? 0,
                'plan' =>Project::planMap()[$item['deploy_build']['plan']],
                'domain' => $item['deploy_optimize']['domain'] ?? 0,
                'created_at' => date('Y年m月d日', strtotime($item['created_at'])),
                'product_num' => ($item['mysql_id'] != 0) ? APublicModel::getProductNumByProjectId($item['id']) : 0,
                'keyword_num' => $item['deploy_build']['keyword_num'] ?? 0,
                'article_num' => ($item['mysql_id'] != 0) ? APublicModel::getBlogNumByProjectId($item['id']) + APublicModel::getNewsNumByProjectId($item['id']) : 0,
                'task_finish_num' => Task::getNumByProjectId($item['id'], Task::STATUS_DOWN),
                'task_pending_num' => Task::getNumByProjectId($item['id'], [Task::STATUS_DONGING, Task::STATUS_WAIT]),
                'optimist_status'=>$item['online_check']['optimist_status'] ?? 0,
                'qa_status'=>$item['online_check']['qa_status'] ?? 0,
                'service_day'=>$item['deploy_build']['service_duration'] - $this->compliance_day($item['id']),
            ];
            $list['list'][$k] = $item;
        }
        return $list;
    }

    /**
     * @param $yesterday
     * @name   :(服务达标天数)compliance_day
     * @author :lyh
     * @method :post
     * @time   :2023/6/14 15:48
     */
    public function compliance_day($project_id){
        //服务达标天数
        $rank_info = DB::table('gl_rank_data')->where(['project_id'=>$project_id,'lang'=>''])->select(['compliance_day'])->first();
        if(empty($rank_info)){
            $compliance_day = 0;
        }else{
            $compliance_day = $rank_info->compliance_day;
        }
        return $compliance_day;
    }

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

    /**
     * @remark :保存项目配置数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 14:19
     */
    public function save($param){
        DB::beginTransaction();
        try {
            if($param['type'] == Project::TYPE_FIVE){
                $param['extend_type'] == Project::TYPE_FIVE;
                unset($param['type']);
            }
            $res = parent::save($param);
            $param['id'] = $res['id'];
            Common::del_user_cache($this->model->getTable(),$param['id']);
            $this->savePayment($param);
            $this->saveDeployBuild($param);
            $this->saveDeployOptimize($param);
            $this->saveAfter($param);
            //创建默认数据库
            if($param['type'] == Project::TYPE_ONE){
                if(isset($param['mysql_id']) && !empty($param['mysql_id'])){
                    $this->initializationMysql($res['id']);
                }
                if(isset($param['mobile']) && !empty($param['mobile'])){
                    $this->createUser($param['mobile'],$res['id'],$param['lead_name']);
                }
                //更改服务器状态
                if(isset($param['serve_id']) && !empty($param['serve_id'])){
                    $this->updateServe($param['serve_id']);
                }
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('保存失败');
        }
        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();
        return $this->success();
    }

    /**
     * @remark :更新服务器为已使用
     * @name   :updateServe
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 10:50
     */
    public function updateServe($serve_id){
        $domainModel = new DomainInfo();
        $domainModel->edit(['status'=>$domainModel::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'],
                'create_id'=>$this->manager['id'],
            ];
            $userModel->add($data);
        }
        return $this->success();
    }

    /**
     * @remark :保存付款续费
     * @name   :savePayment
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 16:19
     */
    protected function savePayment($param){
        if(empty($param['payment'])){
            return true;
        }
        $data = $param['payment'];
        $data['project_id'] = $param['id'];
        $data['id'] = Payment::where('project_id', $param['id'])->value('id');
        return (new PaymentLogic)->save($data);
    }

    /**
     * @remark :保存建站部署
     * @name   :saveDeployBuild
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 16:19
     */
    protected function saveDeployBuild($param){
        if(empty($param['deploy_build'])){
            return true;
        }
        $data = $param['deploy_build'];
        $data['project_id'] = $param['id'];
        $data['id'] = DeployBuild::where('project_id', $param['id'])->value('id');
        if(empty($data['id'])){
            $hashids = new Hashids('test_domain', 5, 'abcdefghjkmnpqrstuvwxyz1234567890');
            $code = $hashids->encode($data['project_id']);
            $data['test_domain'] = 'https://v6-' . $code . '.globalso.site/';
        }
        return (new DeployBuildLogic)->save($data);
    }

    /**
     * 保存优化部署
     * @author zbj
     * @date 2023/4/26
     */
    protected function saveDeployOptimize($param){
        if(empty($param['deploy_optimize'])){
            return true;
        }
        $data = $param['deploy_optimize'];
        $data['project_id'] = $param['id'];
        if(isset($data['domain']) && !empty($data['domain'])){
            if (!preg_match('/http/', $data['domain'])) {
                $data['domain'] = 'https://'.trim($data['domain'],'/').'/';
            }
        }
        $data['id'] = DeployOptimize::where('project_id', $param['id'])->value('id');
        return (new DeployOptimizeLogic)->save($data);
    }
    /**
     * 保存优化部署
     * @author zbj
     * @date 2023/4/26
     */
    protected function saveAfter($param){
        $data = $param['project_after'];
        $data['project_id'] = $param['id'];
        //查询数据是否存在
        $afterModel = new After();
        $info = $afterModel->read(['project_id'=>$data['project_id']]);
        if($info === false){
            $rs = $afterModel->add($data);
        }else{
            $rs = $afterModel->edit($data,['id'=>$info['id']]);
        }
        return $rs;
    }
    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($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   :sync
     * @author :lyh
     * @method :post
     * @time   :2023/8/9 15:04
     */
    public function sync($param){
        $title = date('Ymd') . '-' . $param['company_name'];
        $data = [
            'title' => $title,
            'api_no'=> $param['id'],
            'company' => $param['company_name'],
            'lead_name' => $param['principal_name'],
            'mobile' => $param['principal_mobile'],
            'mysql_id'=>Project::MYSQL_ID,
            'qq' => $param['customer_qq'],
            'channel' => Channel::getProjectChannel($param['company_id'], $param['username_sales']),
            'requirement' => $param['remark'],
            'cooperate_date' => date('Y-m-d', $param['create_time']),
            'deploy_build' => [
                'service_duration' => $param['years'],
                'plan' => $this->versionData($param['plan_marketing']),
            ],
            'deploy_optimize' => [
                'api_no' => $param['id']
            ],
            'project_after' => [],
            'payment' => [
                'amount' => $param['plan_price'],
                'contract' => $param['files'],
                'bill' => $param['images'],
            ],
        ];
        DB::beginTransaction();
        try {
            $res = parent::save($data);
            $data['id'] = $res['id'];
            $this->setPostId($data['deploy_build']['plan'],$res['id']);
            $this->savePayment($data);
            $this->saveDeployBuild($data);
            $this->saveDeployOptimize($data);
            $this->saveAfter($data);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            errorLog('项目同步失败', $data, $e);
            $this->fail('同步失败');
        }
    }

    /**
     * @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 :设置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;
    }
}