<?php
/**
 * 作者: theworld
 * 时间: 2023-03-10
 * 主机module操作      
 */

class serviceIdcsmart {	
	
	/**
	 * 作者: theworld
	 * 时间: 2023-03-10
	 * 开通
	 * @params  string   $hosting_id  主机ID
	 * @return array
	 */
	public function create($params){
		$adminUsername = ''; 
		if(empty($params['hosting_id'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="hosting_id不能为空";
		}
		$hosting = _pdoSelect("tblhosting","`domainstatus`,`dedicatedip`,`assignedips`,`nextduedate`,`username`,`password`","id ={$params['hosting_id']} AND `userid`='{$params['account']['userid']}'");
		if(empty($hosting)){
			$resultMsg['status'] = 400;
			$resultMsg['msg'] = 'hosting_id有误';
		}else{
			$result = localAPI('ModuleCreate', array(
				'serviceid'		=> $params['hosting_id'],
			),$adminUsername);
			if ($result["result"] == "success"){
				$resultMsg['status']=200;
				$resultMsg['msg']="服务开通成功";
			}else{				
				$resultMsg['status']=400;
				$resultMsg['msg']="服务开通失败: ".$result["message"];
			}
		}
		return $resultMsg;
	}
	/**
	 * 作者: theworld
	 * 时间: 2023-03-10
	 * 暂停
	 * @params  string   $hosting_id  主机ID
	 * @params  string   $suspendreason 暂停原因
	 * @return array
	 */
	public function suspend($params){
		$adminUsername = ''; 
		if(empty($params['hosting_id'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="hosting_id不能为空";
		}
		$hosting = _pdoSelect("tblhosting","`domainstatus`,`dedicatedip`,`assignedips`,`nextduedate`,`username`,`password`","id ={$params['hosting_id']} AND `userid`='{$params['account']['userid']}'");
		if(empty($hosting)){
			$resultMsg['status'] = 400;
			$resultMsg['msg'] = 'hosting_id有误';
		}else{
			$result = localAPI('ModuleSuspend', array(
				'serviceid'		=> $params['hosting_id'],
				'suspendreason'	=> $params['suspendreason'],
			),$adminUsername);
			if ($result["result"] == "success"){
				$resultMsg['status']=200;
				$resultMsg['msg']="暂停成功";
			}else{				
				$resultMsg['status']=400;
				$resultMsg['msg']="暂停失败: ".$result["message"];
			}
		}
		return $resultMsg;
	}	
	/**
	 * 作者: theworld
	 * 时间: 2023-03-10
	 * 解除暂停
	 * @params  string   $hosting_id  主机ID
	 * @return array
	 */
	public function unsuspend($params){
		$adminUsername = ''; 
		if(empty($params['hosting_id'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="hosting_id不能为空";
		}
		$hosting = _pdoSelect("tblhosting","`domainstatus`,`dedicatedip`,`assignedips`,`nextduedate`,`username`,`password`","id ={$params['hosting_id']} AND `userid`='{$params['account']['userid']}'");
		if(empty($hosting)){
			$resultMsg['status'] = 400;
			$resultMsg['msg'] = 'hosting_id有误';
		}else{
			$result = localAPI('ModuleUnsuspend', array(
				'serviceid'		=> $params['hosting_id'],
			),$adminUsername);
			if ($result["result"] == "success"){
				$resultMsg['status']=200;
				$resultMsg['msg']="解除暂停成功";
			}else{				
				$resultMsg['status']=400;
				$resultMsg['msg']="解除暂停失败: ".$result["message"];
			}
		}
		return $resultMsg;
	}

	/**
	 * 作者: theworld
	 * 时间: 2023-03-10
	 * 删除主机
	 * @params  string   $hosting_id  主机ID
	 * @return array
	 */
	public function terminate($params){
		$adminUsername = ''; 
		if(empty($params['hosting_id'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="hosting_id不能为空";
		}
		$hosting = _pdoSelect("tblhosting","`domainstatus`,`dedicatedip`,`assignedips`,`nextduedate`,`username`,`password`","id ={$params['hosting_id']} AND `userid`='{$params['account']['userid']}'");
		if(empty($hosting)){
			$resultMsg['status'] = 400;
			$resultMsg['msg'] = 'hosting_id有误';
		}else{
			$result = localAPI('ModuleTerminate', array(
				'serviceid'		=> $params['hosting_id'],
			),$adminUsername);
			if ($result["result"] == "success"){
				$resultMsg['status']=200;
				$resultMsg['msg']="删除主机成功";
			}else{				
				$resultMsg['status']=400;
				$resultMsg['msg']="删除主机失败: ".$result["message"];
			}
		}
		return $resultMsg;
	}	
	/**
	 * 作者: theworld
	 * 时间: 2023-03-10
	 * 修改密码
	 * @params  string   $hosting_id  主机ID
	 * @params  string   $password  密码
	 * @return array
	 */
	public function changePassword($params){
		$adminUsername = ''; 
		if(empty($params['hosting_id'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="hosting_id不能为空";
		}
		if(empty($params['password'])){
			$resultMsg['status']=400;
			$resultMsg['msg']=$resultMsg['msg']."密码不能为空";
		}
		$hosting = _pdoSelect("tblhosting","`domainstatus`,`dedicatedip`,`assignedips`,`nextduedate`,`username`,`password`","id ={$params['hosting_id']} AND `userid`='{$params['account']['userid']}'");
		if(empty($hosting)){
			$resultMsg['status'] = 400;
			$resultMsg['msg'] = 'hosting_id有误';
		}else{
			$result = localAPI('ModuleChangePw', array(
				'serviceid'		=> $params['hosting_id'],
				'servicepassword'		=> $params['password'],
			),$adminUsername);
			if ($result["result"] == "success"){
				$resultMsg['status']=200;
				$resultMsg['msg']="修改密码成功";
			}else{				
				$resultMsg['status']=400;
				$resultMsg['msg']="修改密码失败: ".$result["message"];
			}
		}
		return $resultMsg;
	}

	public function custom($params)
	{
		$adminUsername = ''; 
		if(empty($params['hosting_id'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="hosting_id不能为空";
		}

		$hosting = _pdoSelect("tblhosting","`domainstatus`,`dedicatedip`,`assignedips`,`nextduedate`,`username`,`password`","id ={$params['hosting_id']} AND `userid`='{$params['account']['userid']}'");
		if(empty($hosting)){
			$resultMsg['status'] = 400;
			$resultMsg['msg'] = 'hosting_id有误';
		}else{
			$params['serviceid'] = $params['hosting_id'];
			$params['func_name'] = $params['action'];
			$result = localAPI('ModuleCustom', $params, $adminUsername);
			if ($result["result"] == "success"){
				$resultMsg['status']=200;
				$resultMsg['msg']=$result["message"] ?? "执行成功";
			}else{				
				$resultMsg['status']=400;
				$resultMsg['msg']=$result["message"];
			}
		}
		return $resultMsg;
	}
}
