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

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.basic.util.CollUtils;
import com.ningdatech.carapi.car.datascreen.model.dto.DataScreenParam;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionEntity;
import com.ningdatech.carapi.common.model.KeyValueDTO;
import com.ningdatech.carapi.common.model.po.MapDataPO;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.datascope.aop.XmlDataScope;
import com.ningdatech.carapi.driver.entity.DriverInfo;
import com.ningdatech.carapi.driver.mapper.DriverInfoMapper;
import com.ningdatech.carapi.driver.service.IDriverInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuXinXin
 * @since 2022-10-09
 */
@Service
@RequiredArgsConstructor
public class DriverInfoServiceImpl extends ServiceImpl<DriverInfoMapper, DriverInfo> implements IDriverInfoService {

    @Override
    public Map<Long, DriverInfo> listDriverInfoByIds(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        List<DriverInfo> driverInfoList = list(Wrappers.lambdaQuery(DriverInfo.class)
                .in(DriverInfo::getId, ids));
        return CollUtils.listToMap(driverInfoList, DriverInfo::getId);
    }

    @Override
    @XmlDataScope(entityIndex = 2)
    @BuildChildrenRegionEntity(entityIndex = 2)
    public Map<Integer, Integer> driverAgeStatistics(String caseSql, Integer undefinedAge, DataScreenParam param) {
        List<KeyValueDTO<Integer, Long>> countList = baseMapper.driverAgeStatistics(caseSql, undefinedAge, param);
        return CollUtils.listToMap(countList, KeyValueDTO::getKey, w -> w.getValue().intValue());
    }

    @Override
    @XmlDataScope(entityIndex = 2)
    @BuildChildrenRegionEntity(entityIndex = 2)
    public Map<Integer, Integer> drivingExperienceStatistics(String caseSql, Integer undefinedAge, DataScreenParam param) {
        List<KeyValueDTO<Integer, Long>> countList = baseMapper.drivingExperienceStatistics(caseSql, undefinedAge, param);
        return CollUtils.listToMap(countList, KeyValueDTO::getKey, w -> w.getValue().intValue());
    }

    @Override
    public Long selectUserIdById(Long driverId) {
        DriverInfo driverInfo = baseMapper.selectById(driverId);
        return driverInfo.getUserId();
    }

    @Override
    public Map<String, DriverInfo> listByIdCard(List<String> idCards) {
        if (CollectionUtils.isEmpty(idCards)) {
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<DriverInfo> wrapper = Wrappers.lambdaQuery(DriverInfo.class);
        wrapper.in(DriverInfo::getIdCard, idCards);
        return CollUtils.listToMap(this.list(wrapper), DriverInfo::getIdCard);
    }

    @Override
    @XmlDataScope
    @BuildChildrenRegionEntity
    public List<MapDataPO> diverCountByRegion(DataScreenParam param) {
        CodeUtil.buildRegionLevel(param);
        return baseMapper.diverCountByRegion(param);
    }

    @Override
    @XmlDataScope
    @BuildChildrenRegionEntity
    public List<Map<String, Object>> nativePlaceStatisticsByRegion(DataScreenParam param) {
        return baseMapper.nativePlaceStatisticsByRegion(param);
    }
}
