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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.carapi.car.datascreen.model.dto.DataScreenParam;
import com.ningdatech.carapi.car.datascreen.model.po.OverspeedStatisticsPO;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.datascope.aop.XmlDataScope;
import com.ningdatech.carapi.safe.entity.NdInsureOutDanger;
import com.ningdatech.carapi.safe.mapper.NdInsureOutDangerMapper;
import com.ningdatech.carapi.safe.service.INdInsureOutDangerService;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-12
 */
@Service
@RequiredArgsConstructor
public class NdInsureOutDangerServiceImpl extends ServiceImpl<NdInsureOutDangerMapper, NdInsureOutDanger>
        implements INdInsureOutDangerService {

    private final NdInsureOutDangerMapper insureOutDangerMapper;

    @Override
    public Long countStatistics(List<Long> regionIds, LocalDateTime start, LocalDateTime end) {
        NdInsureOutDangerServiceImpl contex = (NdInsureOutDangerServiceImpl)AopContext.currentProxy();
        LambdaQueryWrapper<NdInsureOutDanger> wrapper = Wrappers.lambdaQuery(NdInsureOutDanger.class);
        contex.searchStatisticsCount(wrapper,regionIds,start,end);
        return count();
    }

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

    @Override
    public List<NdInsureOutDanger> statistics(List<Long> regionIds, LocalDateTime start, LocalDateTime end) {
        NdInsureOutDangerServiceImpl contex = (NdInsureOutDangerServiceImpl)AopContext.currentProxy();
        LambdaQueryWrapper<NdInsureOutDanger> wrapper = Wrappers.lambdaQuery(NdInsureOutDanger.class);
        contex.searchStatistics(wrapper,regionIds,start,end);
        return list(wrapper);
    }

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