RenewLogic.php 4.8 KB
<?php
/**
 * @remark :
 * @name   :RenewLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/8/11 17:29
 */

namespace App\Http\Logic\Aside\Project;

use App\Http\Logic\Aside\BaseLogic;
use App\Models\Project\DeployBuild;
use App\Models\Project\Project;
use App\Models\Project\ProjectRenew;
use App\Models\Project\RenewLog;
use Illuminate\Support\Facades\DB;

class RenewLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new ProjectRenew();
    }

    /**
     * @remark :续费单记录列表
     * @name   :renewLog
     * @author :lyh
     * @method :post
     * @time   :2023/8/11 17:30
     */
    public function renewListsLog($map,$page,$row,$order,$field = ['*']){
        $lists = $this->model->lists($map,$page,$row,$order,$field);
        if(!empty($lists['list'])){
            $projectModel = new Project();
            foreach ($lists['list'] as $k => $v){
                $v['plan'] = Project::planMap()[$v['plan']];
                $v['project_name'] = $projectModel->getProjectName($v['project_id']);
                $lists['list'][$k] = $v;
            }
        }
        return $this->success($lists);
    }

    /**
     * @remark :根据主键获取续费单详情
     * @name   :renewRead
     * @author :lyh
     * @method :post
     * @time   :2023/8/14 9:12
     */
    public function renewRead(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在或者被删除');
        }
        return $this->success($info);
    }

    /**
     * @remark :关联续费单
     * @name   :editProjectRenew
     * @author :lyh
     * @method :post
     * @time   :2023/9/19 10:21
     */
    public function editProjectRenew(){
        //获取续费单详情
        $info = $this->model->read(['id'=>$this->param['renew_id']]);
        if($info === false){
            $this->fail('当前续费单不存在');
        }
        if($info['project_id'] != 0){
            $this->fail('当前续费单已关联项目,请重新选择');
        }
        DB::beginTransaction();
        try {
            $this->model->edit(['project_id'=>$this->param['id'],'operator_id'=>$this->manager['id'],'status'=>1],['id'=>$this->param['renew_id']]);
            $this->saveLog($this->param['renew_id'],$this->param['service_duration'],$this->param['plan'],$info['amount'],$info['api_no'],$this->param['id']);
            $this->updateProject($this->param['id'],$this->param['type']);
            $this->updateProjectBuild($this->param['id'],$this->param['service_duration'],$this->param['plan']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :编辑续费单状态
     * @name   :editStatus
     * @author :lyh
     * @method :post
     * @time   :2023/9/19 15:50
     */
    public function editStatus(){
        $rs = $this->model->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :关联续费单生成一条日志
     * @name   :saveLog
     * @author :lyh
     * @method :post
     * @time   :2023/9/27 16:29
     */
    public function saveLog($renew_id,$service_duration,$plan,$amount,$api_no,$project_id){
        $data = [
            'renew_id'=>$renew_id,
            'service_duration'=>$service_duration,
            'plan'=>$plan,
            'amount'=>$amount,
            'api_no'=>$api_no,
            'project_id'=>$project_id,
            'operator_id'=>$this->manager['id']
        ];
        $renewLogModel = new RenewLog();
        return $renewLogModel->add($data);
    }

    /**
     * @remark :更新项目部署信息
     * @name   :updateProjectBuild
     * @author :lyh
     * @method :post
     * @time   :2023/9/27 16:32
     */
    public function updateProjectBuild($id,$service_duration,$plan){
        $deployBuild = new DeployBuild();
        return $deployBuild->edit(
            [
                'service_duration'=>DB::raw('service_duration + ' . $service_duration),
                'plan'=>$plan
            ],
            [
                'project_id'=>$id
            ]);
    }

    /**
     * @remark :更新项目
     * @name   :updateProject
     * @author :lyh
     * @method :post
     * @time   :2023/9/27 16:35
     */
    public function updateProject($id,$type){
        $project = new Project();
        return $project->edit([
            'extend_type'=>0,
            'type'=>$type
        ],[
            'id'=>$id
        ]);
    }

}