package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.SchedulerPolicy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 资源调度数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("schedulerPolicyRepository")
public class SchedulerPolicyRepository extends BasicGenericDao<SchedulerPolicy, Long> {

	public SchedulerPolicyRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
		super(jdbcTemplate, npJdbcTemplate);
	}

	/**
	 * 分页查询
	 *
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @return
	 * @throws Exception
	 */

	public List<SchedulerPolicy> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.* from scheduler_policy a where a.is_deleted = 0";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramsMap = QueryBuilder.getParam(params);
		return this.list(SchedulerPolicy.class, sql, paramsMap);
	}

	/**
	 * 简易查询
	 *
	 * @param params
	 * @param sorter
	 * @return
	 * @throws Exception
	 */

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from scheduler_policy a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramsMap = QueryBuilder.getParam(params);
		List<SchedulerPolicy> policyList = list(SchedulerPolicy.class, sql, paramsMap);
		List<SimpleBean> listBean = new ArrayList<>();
		for (SchedulerPolicy SchedulerPolicy : policyList) {
			listBean.add(new SimpleBean(SchedulerPolicy.getId(), SchedulerPolicy.getName()));
		}
		return listBean;
	}

	/**
	 * 查询
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */

	public SchedulerPolicy query(Long id) throws Exception {
		String sql = "select a.* from scheduler_policy a where a.id = :id and a.is_deleted = 0 ";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<SchedulerPolicy> SchedulerPolicyList = this.list(SchedulerPolicy.class, sql, params);
		if (SchedulerPolicyList.isEmpty()) {
			return null;
		}
		return SchedulerPolicyList.get(0);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @param userId
	 * @return
	 * @throws Exception
	 */

	public Boolean remove(Long id, Long userId) throws Exception {
		String sql = "update scheduler_policy set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	/**
	 * 查询数量
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from scheduler_policy where is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, params, null, "");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	/**
	 * 查询名称是否重复
	 *
	 * @param name
	 * @return
	 * @throws Exception
	 */

	public boolean existName(String name) throws Exception {
		String sql = "select a.* from scheduler_policy a where a.is_deleted = 0 and a.name = :name";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		List<SchedulerPolicy> SchedulerPolicyList = this.list(SchedulerPolicy.class, sql, paramMap);
		if (SchedulerPolicyList.isEmpty()) {
			return false;
		}
		return true;
	}

	/**
	 * 查询策略是否重复
	 *
	 * @param policy
	 * @param policyCategory
	 * @return
	 * @throws Exception
	 */

	public boolean exist(String policy, String policyCategory) throws Exception {
		String sql = "select a.* from scheduler_policy a where a.is_deleted = 0 and a.policy = :policy and a.policy_category = :policyCategory";
		Map<String, Object> paramMap = MapTools.simpleMap("policy", policy);
		paramMap.put("policyCategory", policyCategory);
		List<SchedulerPolicy> SchedulerPolicyList = this.list(SchedulerPolicy.class, sql, paramMap);
		if (SchedulerPolicyList.isEmpty()) {
			return false;
		}
		return true;
	}
}
