package com.ningdatech.carapi.driver;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.driver.entity.DriverInfo;
import com.ningdatech.carapi.driver.entity.StarManage;
import com.ningdatech.carapi.driver.service.IDriverInfoService;
import com.ningdatech.carapi.driver.service.IStarManageService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @author liuxinxin
 * @date 2022/11/16 下午2:56
 */

class DriverStarDataImportTest extends AppTests {

    @Autowired
    private IDriverInfoService iDriverInfoService;

    @Autowired
    private IStarManageService iStarManageService;


    @Test
    void importData() throws Exception {
//        long count = iDriverInfoService.count(Wrappers.lambdaQuery(DriverInfo.class)
//                .isNotNull(DriverInfo::getCompanyId));
        int pageNumber = 1;
        int pageSize = 500;

        while (true) {
            Page<DriverInfo> page = iDriverInfoService.page(new Page<>(pageNumber, pageSize), Wrappers.lambdaQuery(DriverInfo.class)
                    .isNotNull(DriverInfo::getCompanyId).orderByAsc(DriverInfo::getId));
            if (page.getTotal() == 0) {
                return;
            }
            pageNumber++;
            List<DriverInfo> records = page.getRecords();
            List<StarManage> collect = records.stream().map(r -> {
                StarManage starManage = new StarManage();
                starManage.setCreateOn(LocalDateTime.now());
                starManage.setUpdateOn(LocalDateTime.now());
                starManage.setDriverId(r.getId());
                starManage.setDriverName(r.getDriverName());
                starManage.setCompanyId(r.getCompanyId());
                starManage.setCompanyName(r.getCompanyName());
                starManage.setRegionId(r.getRegionId());
                starManage.setAccidentsCnt(0);
                starManage.setInjuredCnt(0);
                starManage.setInsuranceCnt(0);
                starManage.setInjuredCnt(0);
                starManage.setCurrentStar(5D);
                starManage.setDeductStar(0D);
                starManage.setAccidentsCnt(0);
                return starManage;
            }).collect(Collectors.toList());
            Assert.assertTrue(iStarManageService.saveBatch(collect));
        }
    }


}