package com.ningdatech.carapi.driver;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.driver.constant.*;
import com.ningdatech.carapi.driver.entity.*;
import com.ningdatech.carapi.driver.model.TrainingMaterialInfo;
import com.ningdatech.carapi.driver.model.TrainingMaterialOverviewInfo;
import com.ningdatech.carapi.driver.service.*;
import com.ningdatech.carapi.sys.entity.UserRole;
import com.ningdatech.carapi.sys.service.UserRoleService;
import com.ningdatech.carapi.user.entity.NdUserAuth;
import com.ningdatech.carapi.user.entity.NdUserInfo;
import com.ningdatech.carapi.user.security.auth.constant.AuthTypeEnum;
import com.ningdatech.carapi.user.service.INdUserAuthService;
import com.ningdatech.carapi.user.service.INdUserInfoService;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @Classname AddDriverAccount
 * @Description
 * @Date 2022/11/28 10:16
 * @Created by PoffyZhang
 */
public class AddDriverAccountTest extends AppTests {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IDriverInfoService driverInfoService;

    @Autowired
    private INdUserInfoService userInfoService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private INdUserAuthService userAuthService;

    @Autowired
    private ICertificateService certificateService;

    @Autowired
    private IDriverScoreInfoService driverScoreInfoService;

    @Autowired
    private ITrainingOrganizationService iTrainingOrganizationService;

    @Autowired
    private ITrainingOrganizationMaterialService iTrainingOrganizationMaterialService;

    @Autowired
    private ITrainingOrganizationTrainerService iTrainingOrganizationTrainerService;

    @Autowired
    private IExamRecordService iExamRecordService;

    @Autowired
    private IStarManageService iStarManageService;

    @Test
    public void addDriver(){
        String driverName = "吴素兵";
        String mobile = "15958142337";
        Long driverId = 65709L;
        //Long driverId = null;

        //恒力的公司ID
        Long companyId = 16681L;
        //余杭区 区域
        Long regionId = 11L;
        String regionName = "浙江省杭州市余杭区";
        //恒力的公司名
        String companyName = "杭州余杭恒力混凝土有限公司";

        //1.添加账号 用户信息
        NdUserInfo user = new NdUserInfo();
        user.setCompanyId(companyId);
        user.setCreateBy(2L);
        user.setUpdateBy(2L);
        user.setMobile(mobile);
        user.setUsername(driverName);
        user.setNickName(driverName);
        userInfoService.save(user);

        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(11L);
        userRoleService.save(userRole);

        NdUserAuth userAuth = new NdUserAuth();
        userAuth.setUserId(user.getId());
        userAuth.setIdentifier(mobile);
        userAuth.setCredential(passwordEncoder.encode(mobile));
        userAuth.setAuthType(AuthTypeEnum.ACCOUNT_PASSWORD.getKey());
        userAuthService.save(userAuth);

        //2.如果驾驶员ID为null就去添加 如果已经存在修改驾驶员公司和信息
        DriverInfo driverInfo = new DriverInfo();

        Assert.assertNotNull(driverId);

        if(Objects.isNull(driverId)){
            driverInfo.setDriverName(driverName);
            driverInfo.setGender(1);
            driverInfo.setIdCard(mobile);
            driverInfo.setNativePlaceRegionCode("330000,330100");
            driverInfo.setHealth("健康");
            driverInfo.setAddress(regionName);
            driverInfo.setDriverLicenseType("B2");
            driverInfo.setDriverStatus("induction");
            driverInfo.setCompanyId(companyId);
            driverInfo.setCompanyName(companyName);
            driverInfo.setRegionId(regionId);
            driverInfo.setPhoneNo(mobile);
            driverInfo.setUserId(user.getId());
            driverInfo.setDriverLicenseNo(mobile);
            driverInfo.setCreateOn(LocalDateTime.now());
            driverInfo.setDriverRegistrationStatus("available");
            driverInfo.setNativePlace("浙江省");
            driverInfo.setUpdateOn(LocalDateTime.now());
            driverInfoService.save(driverInfo);
            driverId = driverInfo.getId();
        }else{
            driverInfo = driverInfoService.getOne(Wrappers.lambdaQuery(DriverInfo.class).eq(DriverInfo::getId,driverId));
            driverInfo.setCompanyId(companyId);
            driverInfo.setCompanyName(companyName);
            driverInfo.setRegionId(regionId);
            driverInfo.setPhoneNo(mobile);
            driverInfo.setUserId(user.getId());
            driverInfoService.updateById(driverInfo);
        }

        //3.驾驶员合格证
        Certificate certificate = new Certificate();
        certificate.setRegionName(regionName);
        certificate.setDriverId(driverId);
        certificate.setCompanyId(companyId);
        certificate.setCompanyName(companyName);
        certificate.setRegionId(regionId);
        certificate.setCertificateNo(mobile);
        certificate.setAddress(driverInfo.getAddress());
        certificate.setBirth(driverInfo.getBirth());
        certificate.setDriverLicenseNo(driverInfo.getDriverLicenseNo());
        certificate.setCreateBy(2L);
        certificate.setDriverLicenseType(driverInfo.getDriverLicenseType());
        certificate.setGender(driverInfo.getGender());
        certificate.setHealth("健康");
        certificate.setIssueDate(LocalDateTime.now());
        certificate.setNativePlace("浙江省");
        certificate.setPhoneNo(mobile);
        certificate.setExpiryDate(LocalDateTime.now().plusYears(30L));
        certificate.setCreateOn(LocalDateTime.now());
        certificate.setUpdateOn(LocalDateTime.now());
        certificate.setDriverName(driverName);
        certificate.setUserId(user.getId());
        certificateService.save(certificate);

        //4.添加上车分数信息
        DriverScoreInfo scoreInfo = new DriverScoreInfo();
        scoreInfo.setDriverScore(60);
        scoreInfo.setDriverId(driverId);
        scoreInfo.setUserId(user.getId());
        scoreInfo.setCreateBy(2L);
        scoreInfo.setCreateOn(LocalDateTime.now());
        scoreInfo.setUpdateOn(LocalDateTime.now());
        driverScoreInfoService.save(scoreInfo);
    }

    @Test
    public void addDriverTrainingOrganization(){
        //恒力的公司ID
        Long companyId = 16681L;
        //余杭区 区域
        Long regionId = 11L;
        String regionName = "浙江省杭州市余杭区";
        //恒力的公司名
        String companyName = "杭州余杭恒力混凝土有限公司";

        List<DriverInfo> driverInfos = driverInfoService.list(Wrappers.lambdaQuery(DriverInfo.class)
                .eq(Objects.nonNull(companyId), DriverInfo::getCompanyId, companyId)
                .eq(Objects.nonNull(regionId), DriverInfo::getRegionId, regionId)
                .eq(Objects.nonNull(companyName), DriverInfo::getCompanyName, companyName));

        Assert.assertNotNull(driverInfos);

        for (DriverInfo driverInfo : driverInfos) {

            //保存培训组织基础信息 考试试卷
            TrainingOrganization saveRecord = new TrainingOrganization();
            saveRecord.setUpdateOn(LocalDateTime.now());
            saveRecord.setTrainingTopic("取证培训");
            saveRecord.setTrainingStartTime(LocalDateTime.of(2022,12,1,9,30));
            saveRecord.setTrainingEndTime(LocalDateTime.of(2022,12,15,18,30));
            saveRecord.setAddress("杭州");
            saveRecord.setTrainingMode(TrainingModeEnum.online.name());

            saveRecord.setTrainingType(TrainingTypeEnum.forensic_registration_training.name());
            saveRecord.setContacts("宁达");
            saveRecord.setContactNumber("18556996666");

            saveRecord.setTrainingStatus(TrainingStatusEnum.enable.name());
            saveRecord.setRemark("备注");
            saveRecord.setExamPaperId(18L);
            saveRecord.setExamPaperName("取证培训试卷");
            saveRecord.setCreateBy(2L);
            saveRecord.setCreateOn(LocalDateTime.now());
            saveRecord.setRegionId(regionId);
            iTrainingOrganizationService.save(saveRecord);

            //保存培训人员
            TrainingOrganizationTrainer trainingOrganizationTrainer = new TrainingOrganizationTrainer();
            trainingOrganizationTrainer.setDriverId(driverInfo.getId());
            trainingOrganizationTrainer.setDriverName(driverInfo.getDriverName());
            trainingOrganizationTrainer.setCompanyId(driverInfo.getCompanyId());
            trainingOrganizationTrainer.setDriverUserId(driverInfo.getUserId());
            trainingOrganizationTrainer.setCompanyName(driverInfo.getCompanyName());
            trainingOrganizationTrainer.setCreateOn(LocalDateTime.now());
            trainingOrganizationTrainer.setUpdateOn(LocalDateTime.now());
            trainingOrganizationTrainer.setExamPaperId(18L);
            trainingOrganizationTrainer.setExaminationStatus(ExaminationStatusEnum.examined.name());
            trainingOrganizationTrainer.setExamPaperName("取证培训试卷");
            trainingOrganizationTrainer.setExamRegistrationNo("abcdefg");
            trainingOrganizationTrainer.setTrainingOrganizationId(saveRecord.getId());
            iTrainingOrganizationTrainerService.save(trainingOrganizationTrainer);

            //保存考试记录
            ExamRecord saveExamRecord = new ExamRecord();
            saveExamRecord.setCreateBy(2L);
            saveExamRecord.setCreateOn(LocalDateTime.now());
            saveExamRecord.setUpdateOn(LocalDateTime.now());
            saveExamRecord.setExamPaperId(18L);
            saveExamRecord.setTrainingOrganizationId(saveRecord.getId());
            saveExamRecord.setExamType(TrainingTypeEnum.forensic_registration_training.name());
            iExamRecordService.save(saveExamRecord);

            //保存考试分数
            ExamRecordDetail examRecordDetail = new ExamRecordDetail();
            examRecordDetail.setDriverId(driverInfo.getId());
            examRecordDetail.setDriverUserId(driverInfo.getUserId());
            examRecordDetail.setDriverName(driverInfo.getDriverName());
            examRecordDetail.setCompanyName(driverInfo.getCompanyName());
            examRecordDetail.setCompanyId(driverInfo.getCompanyId());
            examRecordDetail.setScore(100);
            examRecordDetail.setExamResult(ExamQualifiedEnum.qualified.name());
            examRecordDetail.setExamTime(LocalDateTime.of(2022,12,7,9,30));
            examRecordDetail.setRegionId(driverInfo.getRegionId());
            examRecordDetail.setExamType(TrainingTypeEnum.forensic_registration_training.name());
            examRecordDetail.setExamRecordId(saveExamRecord.getId());
            examRecordDetail.setCreateBy(2L);
            examRecordDetail.setCreateOn(LocalDateTime.now());
            examRecordDetail.setUpdateOn(LocalDateTime.now());
            //初始化驾驶员星级
            StarManage starManage = new StarManage();
            starManage.setCurrentStar(5.0);
            starManage.setDriverId(driverInfo.getId());
            starManage.setDriverName(driverInfo.getDriverName());
            starManage.setRegionId(driverInfo.getRegionId());
            starManage.setCompanyName(driverInfo.getCompanyName());
            starManage.setCompanyId(driverInfo.getCompanyId());
            starManage.setCreateOn(LocalDateTime.now());
            starManage.setUpdateOn(LocalDateTime.now());
            iStarManageService.save(starManage);

            TrainingOrganizationTrainer trainer = iTrainingOrganizationTrainerService.getOne(Wrappers.lambdaQuery(TrainingOrganizationTrainer.class)
                    .eq(Objects.nonNull(driverInfo), TrainingOrganizationTrainer::getDriverId, driverInfo.getId()));

            // 保存培训资料
            TrainingMaterialOverviewInfo trainingMaterialOverviewInfo = new TrainingMaterialOverviewInfo();
            List<TrainingMaterialInfo> ruleSystemMaterialList = new ArrayList<>();
            TrainingMaterialInfo trainingMaterialInfo1 = new TrainingMaterialInfo();
            trainingMaterialInfo1.setTrainingMaterialId(1L);
            trainingMaterialInfo1.setMaterialType(MaterialTypeEnum.rule_system);
            trainingMaterialInfo1.setContent("1");
            trainingMaterialInfo1.setTitle("杭州市水泥车培训资料");
            ruleSystemMaterialList.add(trainingMaterialInfo1);
            trainingMaterialOverviewInfo.setRuleSystemMaterialList(ruleSystemMaterialList);

            List<TrainingMaterialInfo> policyDocumentsMaterialInfoList = new ArrayList<>();
            TrainingMaterialInfo trainingMaterialInfo2 = new TrainingMaterialInfo();
            trainingMaterialInfo2.setTrainingMaterialId(4L);
            trainingMaterialInfo2.setMaterialType(MaterialTypeEnum.policy_documents);
            trainingMaterialInfo2.setContent("1");
            trainingMaterialInfo2.setTitle("散办水泥车政策文件");
            ruleSystemMaterialList.add(trainingMaterialInfo2);
            trainingMaterialOverviewInfo.setPolicyDocumentsMaterialInfoList(policyDocumentsMaterialInfoList);

            List<TrainingMaterialInfo> safetyKnowledgeMaterialInfoList = new ArrayList<>();
            TrainingMaterialInfo trainingMaterialInfo3 = new TrainingMaterialInfo();
            trainingMaterialInfo3.setTrainingMaterialId(3L);
            trainingMaterialInfo3.setMaterialType(MaterialTypeEnum.safety_knowledge);
            trainingMaterialInfo3.setContent("资料2内容");
            trainingMaterialInfo3.setTitle("散办水泥车规则");
            safetyKnowledgeMaterialInfoList.add(trainingMaterialInfo3);
            trainingMaterialOverviewInfo.setSafetyKnowledgeMaterialInfoList(safetyKnowledgeMaterialInfoList);

            List<TrainingMaterialInfo> accidentCaseMaterialInfoList = new ArrayList<>();
            TrainingMaterialInfo trainingMaterialInfo4 = new TrainingMaterialInfo();
            trainingMaterialInfo4.setTrainingMaterialId(8L);
            trainingMaterialInfo4.setMaterialType(MaterialTypeEnum.accident_case);
            trainingMaterialInfo4.setContent("122222222");
            trainingMaterialInfo4.setTitle("浙江省2022年事故案例");
            safetyKnowledgeMaterialInfoList.add(trainingMaterialInfo4);
            trainingMaterialOverviewInfo.setAccidentCaseMaterialInfoList(accidentCaseMaterialInfoList);

            List<TrainingMaterialInfo> trainingVideoMaterialInfoList = new ArrayList<>();
            TrainingMaterialInfo trainingMaterialInfo5 = new TrainingMaterialInfo();
            trainingMaterialInfo5.setTrainingMaterialId(9L);
            trainingMaterialInfo5.setMaterialType(MaterialTypeEnum.training_video);
            trainingMaterialInfo5.setContent("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
            trainingMaterialInfo5.setTitle("省交通局培训视频");
            safetyKnowledgeMaterialInfoList.add(trainingMaterialInfo5);
            trainingMaterialOverviewInfo.setTrainingVideoMaterialInfoList(trainingVideoMaterialInfoList);

            List<TrainingMaterialInfo> otherMaterialInfoList = new ArrayList<>();
            TrainingMaterialInfo trainingMaterialInfo6 = new TrainingMaterialInfo();
            trainingMaterialInfo6.setTrainingMaterialId(10L);
            trainingMaterialInfo6.setMaterialType(MaterialTypeEnum.other);
            trainingMaterialInfo6.setContent("21");
            trainingMaterialInfo6.setTitle("交通厅其他资料");
            safetyKnowledgeMaterialInfoList.add(trainingMaterialInfo6);
            trainingMaterialOverviewInfo.setOtherMaterialInfoList(otherMaterialInfoList);

            List<TrainingMaterialInfo> allList = trainingMaterialOverviewInfo.getAllList();
            List<TrainingOrganizationMaterial> materialSaveRecordList = new ArrayList<>();
            for (TrainingMaterialInfo trainingMaterialInfo : allList) {
                TrainingOrganizationMaterial materialSaveRecord = new TrainingOrganizationMaterial();
                materialSaveRecord.setCreateBy(2L);
                materialSaveRecord.setCreateOn(LocalDateTime.now());
                materialSaveRecord.setUpdateOn(LocalDateTime.now());
                materialSaveRecord.setMaterialType(trainingMaterialInfo.getMaterialType().name());
                materialSaveRecord.setTrainingMaterialId(trainingMaterialInfo.getTrainingMaterialId());
                materialSaveRecord.setTrainingOrganizationId(trainer.getTrainingOrganizationId());
                materialSaveRecordList.add(materialSaveRecord);
            }
            iTrainingOrganizationMaterialService.saveBatch(materialSaveRecordList);
        }
    }
}
