package com.ningdatech.carapi.driver.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionWrapper;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.driver.entity.DriverBlackList;
import com.ningdatech.carapi.driver.model.po.ReqDriverBlackListSearchPO;
import com.ningdatech.carapi.driver.model.vo.ResDriverBlackListVO;
import com.ningdatech.carapi.driver.service.IDriverBlackListService;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/10/9 下午1:57
 */
@Component
@RequiredArgsConstructor
public class DriverBlackListManage {

    private final IDriverBlackListService iDriverBlackListService;

    public PageVo<ResDriverBlackListVO> driverBlackListSearch(ReqDriverBlackListSearchPO po) {
        PageVo<ResDriverBlackListVO> pageVo = new PageVo<>();
        LambdaQueryWrapper<DriverBlackList> wrapper = Wrappers.lambdaQuery(DriverBlackList.class);
        DriverBlackListManage context = (DriverBlackListManage) AopContext.currentProxy();
        context.buildDriverBlackListQuery(wrapper,po);
        Page<DriverBlackList> page = iDriverBlackListService.page(po.page(), wrapper);
        long total = page.getTotal();
        pageVo.setTotal(total);
        if (total == 0){
            return pageVo;
        }
        pageVo.setRecords(page.getRecords().stream().map(r -> {
            ResDriverBlackListVO resVo = BeanUtil.copyProperties(r,ResDriverBlackListVO.class);
            CodeUtil.searchCompanyNameAndRegionName(resVo);
            return resVo;
        }).collect(Collectors.toList()));
        return pageVo;
    }

    @LambdaDataScope
    @BuildChildrenRegionWrapper
    public void buildDriverBlackListQuery(LambdaQueryWrapper<DriverBlackList> wrapper, ReqDriverBlackListSearchPO request) {
        wrapper.eq(StringUtils.isNotBlank(request.getYear()),DriverBlackList::getYear,request.getYear())
               .like(StringUtils.isNotBlank(request.getCompanyName()),DriverBlackList::getCompanyName,request.getCompanyName())
               .like(StringUtils.isNotBlank(request.getDriverName()),DriverBlackList::getDriverName,request.getDriverName());
    }
//        String driverName = reqDriverBlackListSearchPO.getDriverName();
//        RegionInfo regionInfo = reqDriverBlackListSearchPO.getRegionInfo();
//        String year = reqDriverBlackListSearchPO.getYear();
//
//        List<Company> companyList = new ArrayList<>();
//        if (StringUtils.isNotBlank(driverName) ||
//                (Objects.nonNull(regionInfo) && StringUtils.isNotBlank(regionInfo.getRegionCode()))) {
//            LambdaQueryWrapper<Company> companyLambdaQueryWrapper = Wrappers.lambdaQuery(Company.class);
//            DriverBlackListManage context = (DriverBlackListManage) AopContext.currentProxy();
//            context.build(companyLambdaQueryWrapper, reqDriverBlackListSearchPO);
//            if (Objects.nonNull(regionInfo)
//                    && StringUtils.isNotBlank(regionInfo.getRegionCode())
//                    && Objects.nonNull(regionInfo.getRegionLevel())) {
//                RegionContainsBO containsRegionBo = regionSearchWrapperAssemblerHelper.getContainsRegionBo(
//                        regionInfo.getRegionLevel(), regionInfo.getRegionCode());
//                regionSearchWrapperAssemblerHelper.expertMetaApplyRegionContainsWrapperAssembler(companyLambdaQueryWrapper, CollUtil.toList(containsRegionBo));
//            }
//            companyList = companyService.list(companyLambdaQueryWrapper);
//            if (CollectionUtils.isEmpty(companyList)) {
//                pageVo.setTotal(0L);
//                pageVo.setRecords(new ArrayList<>());
//                return pageVo;
//            }
//        }
//        List<Long> companyIdList = companyList.stream().map(Company::getId).collect(Collectors.toList());
//
//        // 获取符合条件的driverId List
//        List<Long> driverIdList = new ArrayList<>();
//        if (StringUtils.isNotBlank(driverName) && CollectionUtils.isNotEmpty(companyIdList)) {
//            LambdaQueryWrapper<DriverInfo> driverInfoLambdaQueryWrapper = Wrappers.lambdaQuery(DriverInfo.class);
//            DriverBlackListManage context2 = (DriverBlackListManage) AopContext.currentProxy();
//            context2.build2(driverInfoLambdaQueryWrapper, driverName, companyIdList);
//            List<DriverInfo> driverInfoList = iDriverInfoService.list(driverInfoLambdaQueryWrapper);
//            driverIdList = driverInfoList.stream().map(DriverInfo::getId).collect(Collectors.toList());
//        }
//        LambdaQueryWrapper<DriverBlackList> driverBlackListLambdaQueryWrapper = Wrappers.lambdaQuery(DriverBlackList.class);
//        DriverBlackListManage context3 = (DriverBlackListManage) AopContext.currentProxy();
//        context3.build3(driverBlackListLambdaQueryWrapper, year, driverIdList);
//        Page<DriverBlackList> page = iDriverBlackListService.page(new Page<>(pageNumber, pageSize),driverBlackListLambdaQueryWrapper);
//
//        long total = page.getTotal();
//        pageVo.setTotal(total);
//        if (total == 0) {
//            return pageVo;
//        }
//        List<DriverBlackList> records = page.getRecords();
//
//        // 黑名单驾驶员信息列表
//        List<Long> driverBlackDriverIdList = records.stream().map(DriverBlackList::getDriverId).distinct().collect(Collectors.toList());
//        LambdaQueryWrapper<DriverInfo> driverInfoLambdaQueryWrapper = Wrappers.lambdaQuery(DriverInfo.class);
//        driverInfoLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(driverBlackDriverIdList), DriverInfo::getId, driverBlackDriverIdList);
//        List<DriverInfo> driverInfoList = iDriverInfoService.list(driverInfoLambdaQueryWrapper);
//        Map<Long, String> driverIdNameMap = driverInfoList.stream().collect(Collectors.toMap(DriverInfo::getId, DriverInfo::getDriverName));
//
//        // 黑名单驾驶员公司信息列表
//        List<Long> blackListDriverCompanyIdList = driverInfoList.stream()
//                .filter(r -> Objects.nonNull(r.getCompanyId()))
//                .map(DriverInfo::getCompanyId)
//                .collect(Collectors.toList());
//        LambdaQueryWrapper<Company> companyLambdaQueryWrapper = Wrappers.lambdaQuery(Company.class);
//        companyLambdaQueryWrapper.eq(Company::getId, blackListDriverCompanyIdList);
//        companyList = companyService.list(companyLambdaQueryWrapper);
//        Map<Long, Company> companyIdInfoMap = companyList.stream()
//                .collect(Collectors.toMap(Company::getId, Function.identity()));
//
//        // 装配黑名单列表展示VO
//        List<ResDriverBlackListVO> resDriverBlackListVOList = records.stream().map(r -> {
//            ResDriverBlackListVO resDriverBlackListVO = new ResDriverBlackListVO();
//            Long driverId = r.getDriverId();
//            resDriverBlackListVO.setYear(r.getYear());
//            resDriverBlackListVO.setDriverId(r.getDriverId());
//            // 装配公司信息 驾驶员信息
//            resDriverBlackListVO.setDriverName(driverIdNameMap.get(driverId));
//            Company company = companyIdInfoMap.get(driverId);
//            if (Objects.nonNull(company)) {
//                RegionInfo companyRegionInfo = new RegionInfo();
////                companyRegionInfo.setRegionCode(company.getRegionCode());
////                companyRegionInfo.setRegionLevel(company.getRegionLevel());
//                resDriverBlackListVO.setRegionInfo(companyRegionInfo);
//                resDriverBlackListVO.setCompanyName(company.getName());
//            }
//
//            resDriverBlackListVO.setAccumulatedAccidentsCnt(r.getAccumulatedAccidentsCnt());
//            resDriverBlackListVO.setAccumulatedOverspeedCnt(r.getAccumulatedOverspeedCnt());
//            resDriverBlackListVO.setAccumulatedViolationsCnt(r.getAccumulatedViolationsCnt());
//            return resDriverBlackListVO;
//        }).collect(Collectors.toList());
//
//        pageVo.setRecords(resDriverBlackListVOList);
//        return pageVo;
//    }

    //@LambdaDataScope
    //public void build3(LambdaQueryWrapper<DriverBlackList> driverBlackListLambdaQueryWrapper, String year, List<Long> driverIdList) {
    //    driverBlackListLambdaQueryWrapper.eq(DriverBlackList::getYear, year)
    //            .in(CollUtil.isNotEmpty(driverIdList), DriverBlackList::getDriverId, driverIdList);
    //}
    //
    //@LambdaDataScope
    //public void build2(LambdaQueryWrapper<DriverInfo> driverInfoLambdaQueryWrapper, String driverName, List<Long> companyIdList) {
    //    driverInfoLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(companyIdList), DriverInfo::getCompanyId, companyIdList)
    //            .like(StringUtils.isNotBlank(driverName), DriverInfo::getDriverName, driverName);
    //}
    //
    //@LambdaDataScope
    //public void build(LambdaQueryWrapper<Company> companyLambdaQueryWrapper, ReqDriverBlackListSearchPO reqDriverBlackListSearchPO) {
    //    String companyName = reqDriverBlackListSearchPO.getCompanyName();
    //    companyLambdaQueryWrapper.like(StringUtils.isNotBlank(companyName), Company::getName, companyName);
    //}
}
