package com.bizzan.bitrade.controller;

import static com.bizzan.bitrade.constant.SysConstant.SESSION_MEMBER;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.bizzan.bitrade.entity.FundingRateHistory;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.bizzan.bitrade.service.FundingRateHistoryService;
import com.bizzan.bitrade.util.MessageResult;
import com.bizzan.bitrade.vo.FundingRateForm;
import com.bizzan.bitrade.vo.FundingRateVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(value = "FundingRateController" , tags = "资金费率接口")
@RestController
@RequestMapping("/funding-rate")
public class FundingRateController {

	@Autowired
	private FundingRateHistoryService service;
	
	@ApiOperation(value="获取资金费率历史", notes="获取资金费率历史")
	@GetMapping("history")
	public Page<FundingRateHistory> history( 
			FundingRateForm form,
            int pageNo,
            int pageSize) {
		return service.queryPageByContractId(form.getContractCoinId(), form.getStartDate(), form.getEndDate(), pageNo, pageSize);
	}
	
	@ApiOperation(value="获取当前资金费率", notes="获取当前资金费率")
	@GetMapping("current")
	public MessageResult<FundingRateVo> current(Long contractCoinId) {
		Map<Integer, BigDecimal> currentFundingRateMap = service.getCurrentFundingRateMap();
		BigDecimal rate = currentFundingRateMap.get(contractCoinId.intValue());
		FundingRateVo vo = new FundingRateVo();
		vo.setFundingRate(rate);
		long interval = 8 * 3600000;
		Long refreshTime = (new Date().getTime() / interval) * interval + interval;
		vo.setRefreshTime(refreshTime);
		
		MessageResult<FundingRateVo> result = MessageResult.success();
        result.setData(vo);
        return result;
	}
}
