package com.bocloud.ims.repository.powervc;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.powervc.ValidateResult;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

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

/**
 * powervc validate result dao impl
 *
 * @author lkx007
 */
@Repository
public class ValidateResultRepository extends EnhancedBaseRepository<ValidateResult, Long> {
    public List<ValidateResult> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        if (sorter == null) {
            sorter = new HashMap<>(8);
        }
        if (sorter.size() == 0) {
            sorter.put("status_value", "0");
        }
        String sql = " select * from (" +
                " select *, case when status = 'pass' then 3 when status = 'warning' then 2 when status = 'fail' then 1 end as status_value from validate_result " +
                ") a where 1 = 1 ";
        sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        for (Param param : params) {
            if (param.getSubParams() != null) {
                paramMap.putAll(QueryBuilder.getParam(param.getSubParams()));
            }
        }
        return this.list(ValidateResult.class, sql, paramMap);
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from validate_result a where 1 = 1  ";
        sql = QueryBuilder.build(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        for (Param param : params) {
            if (param.getSubParams() != null) {
                paramMap.putAll(QueryBuilder.getParam(param.getSubParams()));
            }
        }
        return this.countQuery(sql, paramMap).intValue();
    }

    public boolean deleteByVendorId(Long vendorId) {
        String sql = "delete from validate_result where vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return update(sql, params) >= 0;
    }
}
