package com.bocloud.ims.repository.database;

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

/**
 * 关系型数据库Repository接口实现
 *
 * @author weiwei
 */
@Repository("rdsDbRepository")
public class RdsDbRepository extends BasicGenericDao<RdsDb, Long> {

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

    public boolean removeByVidAndRdsId(Long vendor, Long rdsId, Long userId) throws Exception {
        String sql =
                "update rds_db set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId and rds_id = :rdsId ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendor);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        params.put("rdsId", rdsId);
        return this.execute(sql, params) > 0;
    }

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

    public List<RdsDb> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.* from rds_db 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(RdsDb.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select b.id,b.name from rds_db b where b.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "b");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<RdsDb> users = this.list(RdsDb.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (RdsDb db : users) {
            beans.add(new SimpleBean(db.getId(), db.getName()));
        }
        return beans;
    }

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

    public boolean remove(Long id, Long userId) throws Exception {
        String sql = "update rds_db 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 RdsDb query(Long id) throws Exception {
        String sql = "select a.* from rds_db a where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> paramMap = MapTools.simpleMap("id", id);
        List<RdsDb> list = this.list(RdsDb.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

    public boolean deleteByRds(Long rdsId) throws Exception {
        String sql = "delete from rds_db where rds_id = :rdsId";
        Map<String, Object> params = MapTools.simpleMap("rdsId", rdsId);
        return this.execute(sql, params) > 0;
    }

}
