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.InstallResult;
import org.springframework.stereotype.Repository;

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

/**
 * 装机历史DAO实现
 *
 * @author 007
 */
@Repository("installResultRepository")
public class InstallResultRepository extends BasicDao {
	public static final String NODE_ID = "nodeId";

	public List<InstallResult> 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.node_id id,a.node_id,a.name,a.status,a.status task_status,  ");
		sb.append("                a.rackhd_task_id,a.task_id,a.result,a.gmt_start,a.gmt_finish, ");
		sb.append("                a.task_type task_type, b.user_id,   ");
		sb.append("                c.name user_name   ");
		sb.append("           from install_result a  ");
		sb.append("           left join install_task b on a.task_id = b.id  ");
		sb.append("           left join user c on b.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, InstallResult.class);
		List<InstallResult> results = new ArrayList<>();
		for (Object obj : list) {
			InstallResult result = (InstallResult) obj;
			results.add(result);
		}
		return results;
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.id,t.name,t.task_type,t.status  ");
		sb.append("  from(	select a.node_id id,a.node_id,a.name,a.status,a.status task_status,  ");
		sb.append("                a.rackhd_task_id,a.task_id,a.result,a.gmt_start,a.gmt_finish, ");
		sb.append("                c.name user_name,a.task_type task_type,b.user_id   ");
		sb.append("           from install_result a  ");
		sb.append("           left join install_task b on a.task_id = b.id  ");
		sb.append("           left join user c on b.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, InstallResult.class);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();

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

	public int count(List<Param> params) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.id,t.name,t.task_type,t.status  ");
		sb.append("  from(	select a.node_id id,a.node_id,a.name,a.status,a.status task_status,  ");
		sb.append("                a.rackhd_task_id,a.task_id,a.result,a.gmt_start,a.gmt_finish, ");
		sb.append("                c.name user_name,a.task_type task_type,b.user_id   ");
		sb.append("           from install_result a  ");
		sb.append("           left join install_task b on a.task_id = b.id  ");
		sb.append("           left join user c on b.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, null), "t");
		List<Object> entities = this.queryForList(sql, InstallResult.class);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();

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

	public InstallResult save(InstallResult installResult) throws Exception {
		return (InstallResult) this.saveEntity(installResult);
	}

	public boolean update(InstallResult installResult) throws Exception {
		return this.updateEntity(installResult);
	}

	public boolean delete(InstallResult installResult) throws Exception {
		return this.baseDelete(installResult);
	}

	public boolean delete(Long nodeId) throws Exception {
		String sql = "delete from install_result where node_id = :nodeId";
		Map<String, Object> params = MapTools.simpleMap(NODE_ID, nodeId);
		return this.update(sql, params) >= 0;
	}

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

	public List<InstallResult> queryByTaskId(Long taskId) throws Exception {
		String sql = "select * from install_result where task_id = :taskId";
		Map<String, Object> params = MapTools.simpleMap("taskId", taskId);
		List<Object> list = this.queryForList(sql, params, InstallResult.class);
		List<InstallResult> results = new ArrayList<>();
		for (Object obj : list) {
			results.add((InstallResult) obj);
		}
		return results;
	}

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

	public List<InstallResult> queryOn(Long nodeId) throws Exception {
		String sql = "select * from install_result where node_id = :nodeId and status=\'装机中\'";
		Map<String, Object> params = MapTools.simpleMap(NODE_ID, nodeId);
		List<Object> list = this.queryForList(sql, params, InstallResult.class);
		List<InstallResult> results = new ArrayList<>();
		if (ListTool.isEmpty(list)) {
			return results;
		}
		for (Object obj : list) {
			results.add((InstallResult) obj);
		}
		return results;
	}

	public boolean taskFinished(Long taskId, List<Long> nodes) throws Exception {
		boolean finished = true;
		for (Long nodeId : nodes) {
			String sql = "select * from install_result where task_id = :taskId and node_id = :nodeId";
			Map<String, Object> params = MapTools.simpleMap(NODE_ID, nodeId);
			params.put("taskId", taskId);
			List<Object> list = this.queryForList(sql, params, InstallResult.class);
			if (ListTool.isEmpty(list)) {
				finished = false;
			} else {
				for (Object object : list) {
					InstallResult result = (InstallResult) object;
					if (TaskType.OSINSTALL.name().equals(result.getTaskType())
							&& InstallStatus.INSTALLING.getText().equals(result.getStatus())) {
						finished = false;
					} else if (TaskType.RAID.name().equals(result.getTaskType())
							&& InstallStatus.INSTALLING.getText().equals(result.getStatus())) {
						finished = false;
					}
				}
			}
		}
		return finished;
	}
}
