package com.mortals.xhx.module.realtime.service.impl;

import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.common.code.DaySelectEnum;
import com.mortals.xhx.common.code.TimeUnitEnum;
import com.mortals.xhx.module.realtime.model.RealtimeDataflowStatQuery;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.realtime.dao.RealtimeDataflowStatDao;
import com.mortals.xhx.module.realtime.model.RealtimeDataflowStatEntity;
import com.mortals.xhx.module.realtime.service.RealtimeDataflowStatService;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * RealtimeDataflowStatService
 * 人员流量统计 service实现
 *
 * @author zxfei
 * @date 2023-04-19
 */
@Service("realtimeDataflowStatService")
public class RealtimeDataflowStatServiceImpl extends AbstractCRUDServiceImpl<RealtimeDataflowStatDao, RealtimeDataflowStatEntity, Long> implements RealtimeDataflowStatService {


    @Override
    public Result<RealtimeDataflowStatEntity> find(RealtimeDataflowStatEntity query, PageInfo pageInfo, Context context) throws AppException {
        Result<RealtimeDataflowStatEntity> statEntityResult = new Result<>();
        if (!ObjectUtils.isEmpty(query.getSelected())) {
            PageInfo page = new PageInfo();
            List<RealtimeDataflowStatEntity> billInfos;
            //转换日期
            switch (DaySelectEnum.getByValue(query.getSelected())) {
                case 今天:
                    page.setPrePageResult(1);
                    billInfos = this.getBillInfos(query.getSiteId(),query.getCreateTimeStart(), TimeUnitEnum.DAY.getValue(), page, context);
                    statEntityResult.setList(billInfos);
                    statEntityResult.setPageInfo(page);
                    break;
                case 近七天:
                    page.setPrePageResult(7);
                    billInfos = this.getBillInfos(query.getSiteId(),query.getCreateTimeStart(), TimeUnitEnum.DAY.getValue(), page, context);
                    statEntityResult.setList(billInfos);
                    statEntityResult.setPageInfo(page);
                    break;
                case 近三十天:
                    page.setPrePageResult(30);
                    billInfos = this.getBillInfos(query.getSiteId(),query.getCreateTimeStart(), TimeUnitEnum.DAY.getValue(), page, context);
                    statEntityResult.setList(billInfos);
                    statEntityResult.setPageInfo(page);
                    break;
                case 近三月:
                    page.setPrePageResult(3);
                    billInfos = this.getBillInfos(query.getSiteId(),query.getCreateTimeStart(), TimeUnitEnum.MONTH.getValue(), page, context);
                    statEntityResult.setList(billInfos);
                    statEntityResult.setPageInfo(page);
                    break;
                case 今年:
                    page.setPrePageResult(1);
                    billInfos = this.getBillInfos(query.getSiteId(),query.getCreateTimeStart(), TimeUnitEnum.YEAR.getValue(), page, context);
                    statEntityResult.setList(billInfos);
                    statEntityResult.setPageInfo(page);
                    break;
                default:
                    break;
            }
            return statEntityResult;
        } else {
            return super.find(query, pageInfo, context);
        }

    }

    @Override
    public List<RealtimeDataflowStatEntity> getBillInfos(Long siteId,String createTimeStart, Integer datePattern, PageInfo pageInfo, Context context) {
        List<RealtimeDataflowStatEntity> statList;
        RealtimeDataflowStatQuery query = new RealtimeDataflowStatQuery();
        query.setSiteId(siteId);
        query.setCreateTimeStart(createTimeStart);
        if (datePattern == TimeUnitEnum.HOUR.getValue()) {
            query.setGroupList(Arrays.asList("hour"));
            query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("month", OrderCol.DESCENDING), new OrderCol("day", OrderCol.DESCENDING), new OrderCol("hour", OrderCol.DESCENDING)
                    , new OrderCol("createTime")));
            statList = this.getDao().getStatList(query, pageInfo);
        } else if (datePattern == TimeUnitEnum.DAY.getValue()) {
            query.setGroupList(Arrays.asList("day"));
            query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("month", OrderCol.DESCENDING), new OrderCol("day", OrderCol.DESCENDING), new OrderCol("createTime")));
            statList = this.getDao().getStatList(query, pageInfo);
        } else if (datePattern == TimeUnitEnum.MONTH.getValue()) {
            query.setGroupList(Arrays.asList("month"));
            query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("month", OrderCol.DESCENDING), new OrderCol("createTime")));
            statList = this.getDao().getStatList(query, pageInfo);
        } else if (datePattern == TimeUnitEnum.YEAR.getValue()) {
            query.setGroupList(Arrays.asList("year"));
            query.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("createTime")));
            statList = this.getDao().getStatList(query, pageInfo);
        } else {
            throw new AppException("不支持当前日期格式查询统计！");
        }
        return statList;
    }
}