/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.utils.DateUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.estate.entity.AccessStatistics;
import org.springblade.estate.entity.EnergyStatistics;
import org.springblade.estate.vo.EnergyStatisticsVO;
import org.springblade.estate.mapper.EnergyStatisticsMapper;
import org.springblade.estate.service.IEnergyStatisticsService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-12-29
 */
@Service
public class EnergyStatisticsServiceImpl extends BaseServiceImpl<EnergyStatisticsMapper, EnergyStatistics> implements IEnergyStatisticsService {

	@Override
	public IPage<EnergyStatisticsVO> selectEnergyStatisticsPage(IPage<EnergyStatisticsVO> page, EnergyStatisticsVO energyStatistics) {
		return page.setRecords(baseMapper.selectEnergyStatisticsPage(page, energyStatistics));
	}

	@Override
	public List<EnergyStatistics> statByDate(String beginDate, String endDate) {
		String tenantId = AuthUtil.getTenantId();
		LambdaQueryWrapper<EnergyStatistics> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		if(StringUtil.isBlank(beginDate) || StringUtil.isBlank(beginDate)){
			//默认获取本周的日期段
			//默认获取本周的日期段。
			String[] weekTimeInterval = DateUtil.getWeekTimeInterval(new Date());
			lambdaQueryWrapper.ge(EnergyStatistics::getDate, weekTimeInterval[0]);
			lambdaQueryWrapper.le(EnergyStatistics::getDate, weekTimeInterval[1]);
			return this.list((!tenantId.equals(BladeConstant.ADMIN_TENANT_ID)) ? lambdaQueryWrapper.eq(EnergyStatistics::getTenantId, tenantId) : lambdaQueryWrapper);
		}else{
			lambdaQueryWrapper.ge(EnergyStatistics::getDate, beginDate);
			lambdaQueryWrapper.le(EnergyStatistics::getDate, endDate);
			return this.list((!tenantId.equals(BladeConstant.ADMIN_TENANT_ID)) ? lambdaQueryWrapper.eq(EnergyStatistics::getTenantId, tenantId) : lambdaQueryWrapper);
		}
	}

}
