package com.bocloud.ims.repository.database;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.middleware.RdsDb;
import com.bocloud.ims.entity.middleware.RdsPublicCloudFlavor;

import org.apache.commons.lang3.StringUtils;
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.List;
import java.util.Map;

/**
 * liuyuanyuan
 */
@Repository("rdsPublicCLoudFlavorRepository")
public class RdsPublicCLoudFlavorRepository extends BasicGenericDao<RdsPublicCloudFlavor, Long> {


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

	public List<RdsPublicCloudFlavor> list(String category, Long vendorId, String regionId, String zone, String engine,
			String version, String volType, String payType) throws Exception {
		String sql = "select a.* from rds_pc_flavor a where a.is_deleted = 0 and a.vendor_id = :vendorId and region_id = :regionId  and charge_type = :payType ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("regionId", regionId);
		params.put("payType", payType);
		if (null != zone) {
			sql += " and zone like '%" + zone + "%' ";
		}
		if (null != category) {
			sql += " and category like '%" + category + "%' ";
		}
		if (null != engine) {
			sql += " and engine like '%" + engine + "%' ";
		}
		if (null != version) {
			sql += " and version like '%" + version + "%' ";
		}
		if (null != volType) {
			sql += " and storage_type like '%" + volType + "%' ";
		}
		return this.list(RdsPublicCloudFlavor.class, sql, params);
	}


    public boolean removeByVid(Long vendor, Long userId) throws Exception {
        String sql = "update rds_pc_flavor set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendor);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        return this.execute(sql, params) > 0;
    }
    
	public boolean removeByVid(Long vendor, Long userId, String regionId) throws Exception {
		String sql = "update rds_pc_flavor set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendor);
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and region_id = :regionId ";
			params.put("regionId", regionId);
		}
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

    public boolean delete(Long vendor) throws Exception {
        String sql = "delete from rds_pc_flavor where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendor);
        return this.execute(sql, params) > 0;
    }


    public List<RdsPublicCloudFlavor> listByVid(Long vendor, String regionId) throws Exception {
        String sql = "select a.* from rds_pc_flavor a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendor);
        params.put("regionId", regionId);
        return this.list(RdsPublicCloudFlavor.class, sql, params);
    }


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


    public RdsPublicCloudFlavor queryBySpecCode(String specCode) throws Exception {
        String sql = "select * from rds_pc_flavor where spec_code = :specCode and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("specCode", specCode);
        List<RdsPublicCloudFlavor> list = this.list(RdsPublicCloudFlavor.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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