package com.bocloud.ims.repository.installation;

import com.bocloud.cmp.entity.enums.installation.InstallStatus;
import com.bocloud.cmp.entity.enums.installation.TaskType;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.installation.InstallTask;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 装机历史DAO实现
 *
 * @author 007
 */
@Repository("installTaskDaoRepository")
public class InstallTaskRepository extends BasicDao {
	/**
	 * 列表
	 *
	 * @author sxp
	 * @since Nov 16, 2017
	 */
	public List<InstallTask> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.*  ");
		sb.append("  from(select a.id, a.task_name name, a.user_id, a.status, a.gmt_start, a.gmt_finish, ");
		sb.append("              a.status task_status, a.nodes, a.image_id, ");
		sb.append("              c.name user_name     ");
		sb.append("         from install_task a  ");
		sb.append("         left join user c on a.user_id = c.id         ");
		sb.append("       ) t   ");
		sb.append(" where 1=1   ");

		String sql = sb.toString();
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "t");
		List<Object> list = this.queryForList(sql, InstallTask.class);
		List<InstallTask> results = new ArrayList<>();
		for (Object obj : list) {
			results.add((InstallTask) obj);
		}
		return results;
	}

	/**
	 * 简易列表
	 *
	 * @author sxp
	 * @since Nov 16, 2017
	 */
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.id,t.name  ");
		sb.append("  from(select a.id, a.task_name name, a.user_id, a.status, a.gmt_start, a.gmt_finish, ");
		sb.append("              a.status task_status, a.nodes, a.image_id, ");
		sb.append("              c.name user_name     ");
		sb.append("         from install_task a  ");
		sb.append("         left join user c on a.user_id = c.id         ");
		sb.append("       ) t   ");
		sb.append(" where 1=1   ");

		String sql = sb.toString();
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "t");
		List<Object> entities = this.queryForList(sql, InstallTask.class);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();

		InstallTask entity;
		for (Object obj : entities) {
			entity = (InstallTask) obj;
			beans.add(new SimpleBean(entity.getId(), entity.getTaskName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(" select count('x')   ");
		sb.append("  from(select a.id, a.task_name name, a.user_id, a.status, a.gmt_start, a.gmt_finish, ");
		sb.append("              a.status task_status, a.nodes, ");
		sb.append("              c.name user_name     ");
		sb.append("         from install_task a  ");
		sb.append("         left join user c on a.user_id = c.id         ");
		sb.append("       ) t   ");
		sb.append(" where 1=1   ");

		String sql = sb.toString();

		sql = QueryBuilder.build(sql, params, null, "t");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public InstallTask save(InstallTask installTask) throws Exception {
		return (InstallTask) this.saveEntity(installTask);
	}

	public boolean update(InstallTask installTask) throws Exception {
		return this.updateEntity(installTask);
	}

	public boolean delete(InstallTask installTask) throws Exception {
		return this.baseDelete(installTask);
	}

	public InstallTask query(Long id) throws Exception {
		String sql = "select * from install_task where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, InstallTask.class);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return (InstallTask) list.get(0);
	}

	/**
	 * 查询物理主机节点最后一次成功进行系统安装的任务信息
	 *
	 * @param nodeId 物理主机节点的唯一标识
	 * @return 装机任务信息
	 * @author sxp
	 * @since Mar 9, 2018
	 */
	public InstallTask queryLastOsInstallTask(Long nodeId) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(" select a.*         ");
		sb.append("   from install_task a  ");
		sb.append("  inner join install_result b on b.task_id = a.id   ");
		sb.append("  where b.node_id = :nodeId   ");
		sb.append("    and b.task_type = :taskType   ");
		sb.append("    and b.status = :status ");
		sb.append("  order by b.gmt_finish desc   ");
		String sql = sb.toString();

		Map<String, Object> params = MapTools.simpleMap("nodeId", nodeId);
		params.put("taskType", TaskType.OSINSTALL.name());
		params.put("status", InstallStatus.INSTALL_SUCCEED.getText());

		List<Object> list = this.queryForList(sql, params, InstallTask.class);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return (InstallTask) list.get(0);
	}

}
