package com.bocloud.ims.repository.database;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.middleware.RdsPublicCloudInstanceParameterGroup;
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;

/**
 * @author lyy
 * @version 1.0
 * @date 2021/5/14 19:34
 */
@Repository("rdsPublicCloudInstanceParameterGroupRepository")
public class RdsPublicCloudInstanceParameterGroupRepository extends BasicGenericDao<RdsPublicCloudInstanceParameterGroup, Long> {

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

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

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

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

    public List<RdsPublicCloudInstanceParameterGroup> list(Long vendorId) throws Exception {
        String sql = "select * from rds_pc_instance_parameter_group where vendor_id = :vendorId and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return this.list(RdsPublicCloudInstanceParameterGroup.class, sql, params);
    }

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

    public boolean delete(Long id, Long userId) throws Exception {
        String sql = "update rds_pc_instance_parameter_group 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 List<RdsPublicCloudInstanceParameterGroup> listByVid(Long vendor) throws Exception {
        String sql = "select a.* from rds_pc_instance_parameter_group a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendor);
        return this.list(RdsPublicCloudInstanceParameterGroup.class, sql, params);
    }

    public boolean removeByVid(Long vendor, Long userId) throws Exception {
        String sql = "update rds_pc_instance_parameter_group 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;
    }
}
