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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.code.DaySelectEnum;
import com.mortals.xhx.common.code.TimeUnitEnum;
import com.mortals.xhx.module.device.dao.DeviceStatDao;
import com.mortals.xhx.module.device.model.DeviceStatEntity;
import com.mortals.xhx.module.device.model.DeviceStatQuery;
import com.mortals.xhx.module.device.service.DeviceStatService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * DeviceStatService
 * 设备统计 service实现
 *
 * @author zxfei
 * @date 2022-07-05
 */
@Service("deviceStatService")
public class DeviceStatServiceImpl extends AbstractCRUDServiceImpl<DeviceStatDao, DeviceStatEntity, Long> implements DeviceStatService {


    @Override
    public List<DeviceStatEntity> find(DeviceStatEntity params, Context context) throws AppException {
        return super.find(params, context);
    }

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

    @Override
    public List<DeviceStatEntity> getBillInfos(Long siteId, Integer datePattern, PageInfo pageInfo, Context context) {
        List<DeviceStatEntity> statList = new ArrayList<>();
        DeviceStatQuery query = new DeviceStatQuery();
        query.setSiteId(siteId);
        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;
    }

}