/*
 *      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.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.estate.dto.AccessStatisticsDTO;
import org.springblade.estate.entity.AccessStatistics;
import org.springblade.estate.vo.AccessStatisticsVO;
import org.springblade.estate.mapper.AccessStatisticsMapper;
import org.springblade.estate.service.IAccessStatisticsService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.system.user.entity.User;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-12-29
 */
@Service
public class AccessStatisticsServiceImpl extends BaseServiceImpl<AccessStatisticsMapper, AccessStatistics> implements IAccessStatisticsService {

	@Override
	public IPage<AccessStatisticsVO> selectAccessStatisticsPage(IPage<AccessStatisticsVO> page, AccessStatisticsVO accessStatistics) {
		return page.setRecords(baseMapper.selectAccessStatisticsPage(page, accessStatistics));
	}

	@Override
	public R statCount(AccessStatistics accessStatistics) {
		LambdaQueryWrapper<AccessStatistics> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(AccessStatistics::getDate, accessStatistics.getDate());
		lambdaQueryWrapper.eq(AccessStatistics::getTenantId, accessStatistics.getTenantId());
		return R.status(this.saveOrUpdate(accessStatistics, lambdaQueryWrapper));
	}

	@Override
	public AccessStatistics getByDate(String date, String tenantId) {
		LambdaQueryWrapper<AccessStatistics> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(AccessStatistics::getDate, date);
		lambdaQueryWrapper.eq(AccessStatistics::getTenantId, tenantId);
		return this.getOne(lambdaQueryWrapper);
	}

	@Override
	public IPage<AccessStatistics> listByDate(AccessStatisticsDTO accessStatisticsDTO, Query query) {
		String tenantId = AuthUtil.getTenantId();
		if(StringUtil.isBlank(accessStatisticsDTO.getBeginDate()) || StringUtil.isBlank(accessStatisticsDTO.getEndDate())){
			QueryWrapper<AccessStatistics> queryWrapper = Condition.getQueryWrapper(accessStatisticsDTO);
			return this.page(Condition.getPage(query), (!tenantId.equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(AccessStatistics::getTenantId, tenantId) : queryWrapper);
		}else{
			LambdaQueryWrapper<AccessStatistics> lambdaQueryWrapper = new LambdaQueryWrapper();
			lambdaQueryWrapper.gt(AccessStatistics::getDate, accessStatisticsDTO.getBeginDate());
			lambdaQueryWrapper.lt(AccessStatistics::getDate, accessStatisticsDTO.getEndDate());
			return this.page(Condition.getPage(query), (!tenantId.equals(BladeConstant.ADMIN_TENANT_ID)) ? lambdaQueryWrapper.eq(AccessStatistics::getTenantId, tenantId) : lambdaQueryWrapper);
		}
	}

}
