package com.fs.hisStore.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.fs.hisStore.param.FsStoreOrderStatisticsParam;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.hisStore.vo.StoreOrderStatisticsVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;


@RestController
@RequestMapping("/store/store/storeOrder/statistics")
public class FsStoreOrderStatisticsScrmController {

    @Autowired
    private IFsStoreOrderScrmService storeOrderService;

    @ApiOperation("订单统计")
    @GetMapping("/getStoreOrderStatistics")
    public R getStoreOrderStatistics(FsStoreOrderStatisticsParam param)
    {
        Integer orderCount = storeOrderService.selectFsStoreOrderCount(param);
        param.setPaid(1);
        //实际支付金额
        BigDecimal totalAmount = storeOrderService.selectFsStoreOrderByPayPriceCount(param);
        //已支付的总订单数
        Integer orderPayCount = storeOrderService.selectFsStoreOrderCount(param);
        //已完成订单
        param.setStatus(5);
        Integer orderCompleteCount = storeOrderService.selectFsStoreOrderCount(param);
        //退货成功订单
        param.setStatus(-2);
        Integer orderReturnCount = storeOrderService.selectFsStoreOrderCount(param);
        //成交率
        int successRate = 0;
        //退货率
        int returnRate = 0;
        if (orderPayCount != null && orderPayCount != 0) {
            successRate = (int) Math.round((orderCompleteCount != null ? orderCompleteCount : 0) * 100.0 / orderPayCount);
            returnRate = (int) Math.round((orderReturnCount != null ? orderReturnCount : 0) * 100.0 / orderPayCount);
        }
        StoreOrderStatisticsVo storeOrderStatisticsVo = new StoreOrderStatisticsVo();
        storeOrderStatisticsVo.setOrderCount(orderCount);
        storeOrderStatisticsVo.setTotalAmount(totalAmount);
        storeOrderStatisticsVo.setSuccessRate(successRate);
        storeOrderStatisticsVo.setReturnRate(returnRate);
        return R.ok(storeOrderStatisticsVo);
    }
}
