package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.VApp;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * vapp数据访问
 * 
 * @author HP
 *
 */
@Repository("vAppRepository")
public class VAppRepository extends BasicGenericDao<VApp, Long> {

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

	public List<VApp> list(Long parentId, Long poolId, Long vendorId) throws Exception {
		Map<String, Object> params = new HashMap<>(8);
		if (null == parentId || parentId == 0L) {
			String sql = "select * from vapp where (parent_id = 0 or parent_id is null) and is_deleted = 0 and vendor_id = :vendorId";
			params.put(Common.VENDORID, vendorId);
			if (null != poolId && 0L != poolId) {
				sql += " and pool_id = :poolId";
				params.put("poolId", poolId);
			}
			return this.list(VApp.class, sql, params);
		} else {
			String sql = "select * from vapp where parent_id = :parentId and is_deleted = 0 and vendor_id = :vendorId";
			params.put(Common.VENDORID, vendorId);
			params.put("parentId", parentId);
			if (null != poolId && 0L != poolId) {
				sql += " and pool_id = :poolId";
				params.put("poolId", poolId);
			}
			return this.list(VApp.class, sql, params);
		}
	}

	public boolean remove(Long id, Long menderId) throws Exception {
		String sql = "update vapp set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = new HashMap<>(8);
		params.put("gmtModify", new Date());
		params.put("menderId", menderId);
		params.put("id", id);
		return this.execute(sql, params) > 0;
	}

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

	public boolean exists(String name, Long parentId) throws Exception {
		String sql = "select * from vapp where is_deleted = 0 and name = :name and parent_id = :parentId";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("parentId", parentId);
		List<VApp> list = this.list(VApp.class, sql, params);
		if (list.isEmpty()) {
			return false;
		}
		return true;
	}

	public List<VApp> listByVdc(Long vdcId) throws Exception {
		String sql = "select * from vapp where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		return this.list(VApp.class, sql, params);
	}

	public boolean removeByVdc(Long vdcId, Long userId) throws Exception {
		String sql = "update vapp set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public VApp queryByVal(String val, Long vdcId) throws Exception {
		String sql = "select * from vapp where is_deleted = 0 and vapp_val = :val and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("val", val);
		params.put("vdcId", vdcId);
		List<VApp> list = this.list(VApp.class, sql, params);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public boolean removeByVendorId(Long vendorId, Long userId) throws Exception {
		String sql = "update vapp set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = new HashMap<>(8);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public List<VApp> listByParentId(Long parentId) throws Exception {
		String sql = "select * from vapp where parent_id = :parentId and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("parentId", parentId);
		return this.list(VApp.class, sql, params);
	}

	public boolean removeByTarget(Long targetId, String targetCategory, Long userId) throws Exception {
		String sql = "update vapp set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where target_id = :targetId and target_category = :targetCategory";
		Map<String, Object> params = new HashMap<>(8);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("targetId", targetId);
		params.put("targetCategory", targetCategory);
		return this.execute(sql, params) > 0;
	}

}
