package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
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.SangforIface;
import com.bocloud.ims.entity.resource.Storage;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

@Repository
public class SangforIfaceRepository extends BasicGenericDao<SangforIface, Long> {
    public SangforIfaceRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<SangforIface> listByVid(Long vendorId) {
        String sql =
                "select a.*  from sangfor_iface a where a.is_deleted = 0 and a.vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(SangforIface.class, sql, params);
    }
    public void removeByVendor(long vendorId) {
        String sql = "update sangfor_iface a set is_deleted = 1 where a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        execute(sql, params);
    }

    public int stCount(List<Param> params, Long vendorId){
        String sql = "select count(1) from sangfor_iface a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.build(sql,params,null,"a");
        Map<String,Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql,param).intValue();
    }

    public List<SangforIface> stList(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) throws BoCloudDBException {
        String sql = "select * from sangfor_iface a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(SangforIface.class, sql);
    }

    public Boolean remove(Long id, Long userId) throws Exception {
        String sql = "update sangfor_iface set is_deleted = true where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.execute(sql, params) > 0;
    }


    public int counts(List<Param> params) {

        String sql = " select count(1) from sangfor_iface a where is_deleted = 0 ";
        Map<String, Object> param = QueryBuilder.getParam(params);
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        return this.countQuery(sql,param).intValue();
    }

    public List<SangforIface> list(List<Param> params, Map<String, String> sorter) {
        String sql = " select a.* from sangfor_iface a where is_deleted = 0 ";
        Map<String, Object> param = QueryBuilder.getParam(params);
        sql = QueryBuilder.buildRaw(sql, params,sorter, "a");
        return this.list(SangforIface.class,sql,param);
    }
}
