package com.uas.platform.b2b.publicapi.controller;

import com.uas.platform.b2b.publicapi.model.TradeCount;
import com.uas.platform.b2b.publicapi.service.TradeCountService;
import com.uas.platform.core.model.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 查询销售额接口
 *
 * Created by hejq on 2018-05-30.
 */
@RequestMapping("/public/tradecount")
@RestController
public class TradeCountController {

    @Autowired
    private TradeCountService tradeCountService;

    /**
     * 查询销售额统计
     *
     * @param tradeType 类型
     * @param currency 币别
     * @param fromDate 开始时间
     * @param endDate 截止时间
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public BigDecimal findByParameters(String tradeType, String currency, String fromDate, String endDate) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNumber(1);
        pageInfo.setPageSize(20);
        pageInfo.setSort(new Sort(Sort.Direction.DESC, "id"));
        List<TradeCount> counts = tradeCountService.findByParameters(tradeType, currency, fromDate, endDate, pageInfo);
        return !CollectionUtils.isEmpty(counts) ? counts.get(0).getAmount() : new BigDecimal(0);
    }
}
