package com.ruoyi.mall.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.mapper.IndexDataMapper;
import com.ruoyi.mall.service.IMallShopShopService;
import com.ruoyi.mall.service.IndexDataService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class IndexDataServiceImpl implements IndexDataService {

    @Resource
    private IndexDataMapper indexDataMapper;
    @Autowired
    private IMallShopShopService shopShopService;

    /**
     * 首页顶部数据
     * @return
     */
    @Override
    public AjaxResult indexTabData(){


        AjaxResult ajaxResult = new AjaxResult();
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long shopId = 0L;
            MallShopShop mallShopShop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
            if (ObjectUtils.isNotEmpty(mallShopShop)){
                shopId = mallShopShop.getShopId();
            }


            // 今日成交额
            BigDecimal todayPrice = indexDataMapper.selectTodayPrice(shopId);
            // 今日总访客
            Integer todayVisitCount = indexDataMapper.selectTodayVisitCount();
            // 今日订单数
            Integer todayOrderCount = indexDataMapper.selectTodayOrderCount(shopId);

            ajaxResult.put("code",200);
            ajaxResult.put("msg","查询成功");
            ajaxResult.put("todayPrice",todayPrice);
            ajaxResult.put("todayVisitCount",todayVisitCount);
            ajaxResult.put("todayOrderCount",todayOrderCount);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",500);
            ajaxResult.put("msg","查询失败");
        }
        return ajaxResult;

    }

    /**
     * 折线图数据
     * @return
     */
    @Override
    public AjaxResult indexLineChartData(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long shopId = 0L;
            MallShopShop mallShopShop = shopShopService.selectMallShopShopBySysUserId(loginUser.getUserId());
            if (ObjectUtils.isNotEmpty(mallShopShop)){
                shopId = mallShopShop.getShopId();
            }


            // 近七天日期
            List<String> oneWeekDateList = indexDataMapper.oneWeekDate();
            if (oneWeekDateList.size() <= 0){
                return AjaxResult.error("查询日期失败");
            }

            // 近七天成交额
            List<BigDecimal> oneWeekPrice = indexDataMapper.oneWeekPrice(shopId);
            // 近七天访客
            List<Integer> oneWeekVisitCount = indexDataMapper.oneWeekVisitCount();
            // 近七天订单数
            List<Integer> oneWeekOrderCount = indexDataMapper.oneWeekOrderCount(shopId);

            ajaxResult.put("code",200);
            ajaxResult.put("msg","查询成功");
            ajaxResult.put("oneWeekDateList",oneWeekDateList);
            ajaxResult.put("oneWeekPrice",oneWeekPrice);
            ajaxResult.put("oneWeekVisitCount",oneWeekVisitCount);
            ajaxResult.put("oneWeekOrderCount",oneWeekOrderCount);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",500);
            ajaxResult.put("msg","查询失败");
        }
        return ajaxResult;
    }

}
