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

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.fusionsphere.FusionSphereHostPort;
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;

/**
 * @ClassName: FusionSphereHostPortRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/11/2 14:02
 */
@Repository
public class FusionSphereHostPortRepository extends BasicGenericDao<FusionSphereHostPort, Long> {
    public FusionSphereHostPortRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<FusionSphereHostPort> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select a.* ,v.name host_name"
                + " from fusion_sphere_host_port a "
                + " left join server v on a.host_id = v.id "
                + " where a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(FusionSphereHostPort.class, sql);
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "select count(1) "
                + " from fusion_sphere_host_port a "
                + " left join server v on a.host_id = v.id "
                + " 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 FusionSphereHostPort query(long id) throws BoCloudDBException {
        String sql = "select a.* ,v.name host_name "
                + " from fusion_sphere_host_port a "
                + " left join server v on a.host_id = v.id "
                + " where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<FusionSphereHostPort> list = this.list(FusionSphereHostPort.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update fusion_sphere_host_port set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }


    public boolean removeByHost(Long hostId, Long userId) {
        String sql = "update fusion_sphere_host_port set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and host_id = :hostId";
        Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public List<FusionSphereHostPort> listByHost(Long hostId) {
        String sql = "select a.* from fusion_sphere_host_port  a where a.is_deleted = 0 and a.host_id = :hostId";
        Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
        return this.list(FusionSphereHostPort.class, sql, params);
    }

    public List<FusionSphereHostPort> listByVendorId(Long vendorId) {
        String sql = "select a.* from fusion_sphere_host_port  a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return this.list(FusionSphereHostPort.class, sql, params);
    }
    public boolean removeByVendorId(Long vendorId, Long userId) {
        String sql = "update fusion_sphere_host_port set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }
}