package com.ningdatech.carapi.safe.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.datascope.contants.LambdaDataScopeTypeEnum;
import com.ningdatech.carapi.safe.entity.NdVehicleViolation;
import com.ningdatech.carapi.safe.mapper.NdVehicleViolationMapper;
import com.ningdatech.carapi.safe.service.INdVehicleViolationService;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-12
 */
@Service
@RequiredArgsConstructor
public class NdVehicleViolationServiceImpl extends ServiceImpl<NdVehicleViolationMapper, NdVehicleViolation>
        implements INdVehicleViolationService {

    private final NdVehicleViolationMapper mapper;

    @Override
    public Long countStatistics(List<Long> regionIds, LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<NdVehicleViolation> wrapper = Wrappers.lambdaQuery(NdVehicleViolation.class);
        NdVehicleViolationServiceImpl context = (NdVehicleViolationServiceImpl)AopContext.currentProxy();
        context.dataScopeCount(wrapper,regionIds,start,end);
        return count(wrapper);
    }

    @LambdaDataScope
    public void dataScopeCount(LambdaQueryWrapper<NdVehicleViolation> wrapper, List<Long> regionIds, LocalDateTime start, LocalDateTime end) {
        wrapper.in(CollUtil.isNotEmpty(regionIds),NdVehicleViolation::getRegionId, regionIds)
                .ge(NdVehicleViolation::getViolationDate, start)
                .le(NdVehicleViolation::getViolationDate, end);
    }

    @Override
    @LambdaDataScope(wrapperIndex = 1,type = LambdaDataScopeTypeEnum.ALL)
    public List<NdVehicleViolation> pageDataScope(Page<NdVehicleViolation> page, LambdaQueryWrapper<NdVehicleViolation> wrapper) {
        return mapper.selectList(wrapper);
    }
}
