package com.bocloud.ims.repository.resource.f5;

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.resource.f5.BiOutstandingIssues;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by liuyuanyuan on 2019/5/8 17:04
 */
@Repository("biOutstandingIssuesRepository")
public class F5OutstandingIssuesRepository extends BasicGenericDao<BiOutstandingIssues, Long> {

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

    public List<BiOutstandingIssues> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*,b.name AS apply_user_name from bi_outstanding_issues a left join users b ON a.apply_user = b.id where b.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(BiOutstandingIssues.class, sql, paramMap);
    }

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

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