package com.fs.store.controller;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.core.domain.R;
import com.fs.common.utils.TimeUtils;
import com.fs.store.service.IFsStoreOrderService;
import com.fs.store.service.IFsStorePaymentService;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.service.IFsUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/store/index")
public class FsIndexController
{
    private static final Logger log = LoggerFactory.getLogger(FsIndexController.class);

    @Autowired
    private IFsUserService userService;

    @Autowired
    private IFsStoreOrderService storeOrderService;

    @Autowired
    private IFsStorePaymentService paymentService;

    @Autowired
    private IFsStoreProductService productService;

    @GetMapping("/getIndexData")
    public R getIndexData()
    {
        Map<String,Object> data=new HashMap<>();
        Long userCount=userService.selectFsUserCount(0,null);
        Long dayUserCount=userService.selectFsUserCount(1,null);
        data.put("userCount",userCount);
        data.put("dayUserCount",dayUserCount);
        Long storeOrderCount=storeOrderService.selectFsStoreOrderTotalCount(0,null);
        Long dayStoreOrderCount=storeOrderService.selectFsStoreOrderTotalCount(1,null);
        data.put("storeOrderCount",storeOrderCount);
        data.put("dayStoreOrderCount",dayStoreOrderCount);
        Long paymentCount=paymentService.selectFsStorePaymentCount(0,null);
        Long dayPaymentCount=paymentService.selectFsStorePaymentCount(1,null);
        data.put("paymentCount",paymentCount);
        data.put("dayPaymentCount",dayPaymentCount);
        Long productCount=productService.selectFsStoreProductCount(0);
        Long dayProductCount=productService.selectFsStoreProductCount(1);
        data.put("productCount",productCount);
        data.put("dayProductCount",dayProductCount);

        return R.ok().put("data",data);
    }


    @GetMapping("/getStoreOrderData")
    public R getStoreData()
    {
        Long[] users=new Long[0];
        TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime("5",null,null);
        timeEntity.setUserIds(users);
        Integer cycleNum = timeEntity.getCycleNum();
        Integer beginTime = timeEntity.getBeginTime();
        List<Integer> timeList = new ArrayList<>();
        for (int i = 1; i <= cycleNum; i++) {
            timeList.add(beginTime);
            beginTime = TimeUtils.formatTime(beginTime);
        }
        List<JSONObject> jsonObjectList = storeOrderService.selectFsStoreOrderCounts(timeEntity.toMap());
        List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
        List<Integer> orderCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("orderCount")).collect(Collectors.toList());
        List<Integer> payPrice = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("payPrice")).collect(Collectors.toList());
        return R.ok().put("dates",dates).put("orderCount",orderCount).put("payPrice",payPrice);
    }


    @GetMapping("/getStorePaymentData")
    public R getStorePaymentData()
    {
        Long[] users=new Long[0];
        TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime("5",null,null);
        timeEntity.setUserIds(users);
        Integer cycleNum = timeEntity.getCycleNum();
        Integer beginTime = timeEntity.getBeginTime();
        List<Integer> timeList = new ArrayList<>();
        for (int i = 1; i <= cycleNum; i++) {
            timeList.add(beginTime);
            beginTime = TimeUtils.formatTime(beginTime);
        }
        List<JSONObject> jsonObjectList = paymentService.selectFsStorePaymentCounts(timeEntity.toMap());
        List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
        List<Integer> orderCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("orderCount")).collect(Collectors.toList());
        List<Integer> payMoney = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("payMoney")).collect(Collectors.toList());
        return R.ok().put("dates",dates).put("orderCount",orderCount).put("payMoney",payMoney);
    }

}
