package com.bocloud.ims.service.resource.azure.prices;

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.azure.price.BillSyncRecord;
import com.bocloud.ims.interfaces.resource.azure.prices.BillSyncRecordService;
import com.bocloud.ims.repository.resource.azure.price.BillSyncRecordRepository;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: BillSyncRecordServiceImpl
 * @Description:账单同步记录
 * @author: zhenpingLi/李振平
 * @date: 2021/3/29 14:52
 */
@Service
@Slf4j
public class BillSyncRecordServiceImpl implements BillSyncRecordService {

    @Autowired
    private BillSyncRecordRepository billSyncRecordRepository;


    public BocloudResult<GridBean<BillSyncRecord>> list(Pager pager, RequestContext context) {
        List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
        Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
        sorter.put("startTime", Common.ONE);
        Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
        try {
            GridBean<BillSyncRecord> gridBean;
            int total = billSyncRecordRepository.count(params);
            List<BillSyncRecord> list = this.billSyncRecordRepository.list(page, rows, params, sorter);
            gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            log.error("list billSyncRecord error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }

    }
}