package com.bocloud.ims.repository.manageone;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
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.manageone.ManageOneVdc;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * manage one vdc数据访问
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 */
@Repository("manageOneVdcRepository")
public class ManageOneVdcRepository extends BasicGenericDao<ManageOneVdc, Long> {

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

    public List<ManageOneVdc> list(Long parentId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        if (null == parentId || parentId == 0) {
            String sql = "select a.* from manage_one_vdc a where (a.parent_id = 0 or a.parent_id is null) and a.is_deleted = 0";
            return this.list(ManageOneVdc.class, sql, params);
        } else {
            String sql = "select a.* from manage_one_vdc a where a.parent_id = :parentId and a.is_deleted = 0";
            params.put("parentId", parentId);
            return this.list(ManageOneVdc.class, sql, params);
        }
    }

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

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name from manage_one_vdc a where 1 = 1 ";
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<ManageOneVdc> groups = this.list(ManageOneVdc.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<>();
        for (ManageOneVdc group : groups) {
            beans.add(new SimpleBean(group.getId(), group.getName()));
        }
        return beans;
    }

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

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

    public ManageOneVdc queryByTenantId(Long tenantId) throws Exception {
        String sql = "select a.* from manage_one_vdc a where a.tenant_id = :tenantId";
        Map<String, Object> params = MapTools.simpleMap(Common.TENANTID, tenantId);
        List<ManageOneVdc> list = this.list(ManageOneVdc.class, sql, params);
        return ListTool.isEmpty(list) ? null : list.get(0);
    }

    public List<ManageOneVdc> listByVendorId(Long vendorId) {
        String sql = "select a.* from manage_one_vdc a where a.vendor_id = :vendorId";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(ManageOneVdc.class, sql, paramMap);
    }

    public boolean removeByVendorId(Long vendorId) {
        String sql = "delete from manage_one_vdc where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.execute(sql, params) > 0;
    }

    public boolean removeByParentId(Long parentId) {
        String sql = "delete from manage_one_vdc where parent_id = :parentId";
        Map<String, Object> params = MapTools.simpleMap(Common.PARENTID, parentId);
        return this.execute(sql, params) > 0;
    }

    public boolean removeByTenantId(Long tenantId) {
        String sql = "delete from manage_one_vdc where tenant_id = :tenantId";
        Map<String, Object> params = MapTools.simpleMap(Common.TENANTID, tenantId);
        return this.execute(sql, params) > 0;
    }

    public boolean remove(Long id) {
        String sql = "delete from manage_one_vdc where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.execute(sql, params) > 0;
    }

}
