package net.wicp.tams.duckula.ops.pages.devops;

import java.util.List;
import java.util.Set;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.TextStreamResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.app.duckula.controller.bean.models.DevopsDoris;
import net.wicp.tams.app.duckula.controller.config.constant.DevOpsType;
import net.wicp.tams.app.duckula.controller.dao.DevopsDorisMapper;
import net.wicp.tams.common.Result;
import net.wicp.tams.common.apiext.CollectionUtil;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.apiext.json.JSONUtil;
import net.wicp.tams.common.constant.PathType;
import net.wicp.tams.common.exception.ExceptAll;
import net.wicp.tams.common.exception.ProjectExceptionRuntime;
import net.wicp.tams.common.os.SSHAssit;
import net.wicp.tams.common.os.bean.DiskBean;
import net.wicp.tams.common.os.pool.SSHConnection;
import net.wicp.tams.component.tools.TapestryAssist;
import net.wicp.tams.duckula.ops.pages.ParentPageBean;

@Slf4j
public class DorisConfig extends ParentPageBean<DevopsDoris> {

	@Inject
	private DevopsDorisMapper devopsDorisMapper;

	@Override
	public void doSave(DevopsDoris t, boolean isInsert) {
		// 1、登陆
		SSHConnection conn = SSHAssit.getConn(t.getIp(), "root", t.getRootPwd());
		try {
			List<DiskBean> list = conn.fdiskData();
			Set<String> paths = CollectionUtil.getColSetFromObj(list, "path");
			if (!paths.contains(t.getFs())) {
				throw new ProjectExceptionRuntime(ExceptAll.param_error,
						"存储磁盘不正确，只能填写：" + CollectionUtil.arrayJoin(paths.toArray(new String[paths.size()]), ","));
			}
		} finally {
			SSHAssit.close(conn);
		}

		if (isInsert) {
			devopsDorisMapper.insert(t);
		} else {
			devopsDorisMapper.updateByPrimaryKeySelective(t);
		}
	}

	@Override
	public void doDel(String id) {
		this.devopsDorisMapper.deleteById(id);
	}

	@Override
	public BaseMapper<DevopsDoris> getBaseMapper() {
		return this.devopsDorisMapper;
	}

	@Override
	public void packageQuery(DevopsDoris t, QueryWrapper<DevopsDoris> queryWrapper) {
		if (StringUtil.isNotNull(t.getName())) {
			queryWrapper.eq("name", t.getName());
		}
		if (StringUtil.isNotNull(t.getIsInit())) {
			queryWrapper.eq("is_init", t.getIsInit());
		}
	}

	/***
	 * <r:combobox id="groupIsEdit" name="isEdit" textField="name1" valueField=
	 * "name" url="/sys/OptionItemManager:list/YesOrNo" panelHeight="50" value=
	 * "literal:yes"/> 得到centos的磁盘。
	 * 
	 * @return
	 */
//	public TextStreamResponse onListFs() {
//		final DevopsDoris devopsDoris = TapestryAssist.getBeanFromPage(DevopsDoris.class, requestGlobals);
//		
//	}

	/***
	 * 测试用后端调用
	 * 
	 * @return
	 */
	public TextStreamResponse onCheckHost() {
		final DevopsDoris DevopsDoris = TapestryAssist.getBeanFromPage(DevopsDoris.class, requestGlobals);
		// 1、OSS下载
		String localPath = PathType.getPath(DevOpsType.flink.getCheckurl());
		// 1、登陆
		SSHConnection conn = SSHAssit.getConn(DevopsDoris.getIp(), "root", DevopsDoris.getRootPwd());
		try {
			String fileName = localPath.substring(localPath.lastIndexOf("/") + 1);
			conn.scp(localPath, fileName, "~", "0744");
			// 执行
			Result result = conn.executeCommand(String.format("sh ~/%s", fileName));
			log.info(result.getMessage());
			return TapestryAssist.getTextStreamResponse(Result.getError("检查结果：" + result.getMessage()));
		} finally {
			SSHAssit.close(conn);
		}
	}

}
