package com.bocloud.ims.repository.resource.azure.price;

import com.bocloud.common.exception.BoCloudDBException;
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.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.azure.price.BillSyncRecord;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: BillSyncRecordRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/3/29 14:34
 */
@Repository
public class BillSyncRecordRepository extends BasicDao {

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select * from bill_sync_record where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<BillSyncRecord> list = this.queryForList(sql, paramMap, BillSyncRecord.class).stream().map(o -> (BillSyncRecord) o).collect(Collectors.toList());
        List<SimpleBean> beans = Lists.newArrayList();
        for (BillSyncRecord entity : list) {
            beans.add(new SimpleBean(entity.getId(), entity.getStatus()));
        }
        return beans;
    }

    public List<BillSyncRecord> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "SELECT a.* " +
                "FROM bill_sync_record a " +
                "WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.queryForList(sql, paramMap, BillSyncRecord.class).stream().map(o -> (BillSyncRecord) o).collect(Collectors.toList());
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "SELECT COUNT(1) " +
                "FROM bill_sync_record 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 BillSyncRecord query(Long id) throws BoCloudDBException {
        String sql = "select a.* from bill_sync_record a where  a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<BillSyncRecord> list = this.queryForList(sql, params, BillSyncRecord.class).stream().map(o -> (BillSyncRecord) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public BillSyncRecord save(BillSyncRecord ip) throws BoCloudDBException {
        return (BillSyncRecord) this.saveEntity(ip);
    }

    public Boolean update(BillSyncRecord ip) throws BoCloudDBException {
        return this.updateEntity(ip);
    }

    public Boolean delete(Long id) throws BoCloudDBException {
        String sql = "delete from bill_sync_record where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    public boolean hasSync(Long vendorId) {
        String sql = "select count(1) from bill_sync_record a where a.status = 'SYNC' and a.vendor_id = :vendorId";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.countQuery(sql, paramMap) > 0;
    }

}