package com.izouma.wenlvju.repo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.izouma.wenlvju.ApplicationTests;
import com.izouma.wenlvju.domain.*;
import com.izouma.wenlvju.enums.RateStatus;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.service.storage.StorageService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class RepoTest extends ApplicationTests {
    @Autowired
    private CollaborateRepo  collaborateRepo;
    @Autowired
    private StorageService   storageService;
    @Autowired
    private OrganizationRepo organizationRepo;
    @Autowired
    private RateRepo         rateRepo;
    @Autowired
    private RateAuditRepo    rateAuditRepo;
    @Autowired
    private SettingRepo      settingRepo;

    @Test
    public void test() {

        List<Collaborate> users = collaborateRepo.findAll();
        users.forEach(user -> {
            List<String> strs = new ArrayList<>();
            user.getConsent().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs.add(url);
            });
            user.setConsent(strs);
            collaborateRepo.save(user);
        });

        List<Organization> users1 = organizationRepo.findAll();
        users1.forEach(user -> {
            String con = user.getBusinessLicense();
            if (StrUtil.isNotBlank(con)) {


                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);

                user.setBusinessLicense(url);
                organizationRepo.save(user);
            }
        });


        List<Rate> users2 = rateRepo.findAll();
        users2.forEach(user -> {
            List<String> strs = new ArrayList<>();
            List<String> strs1 = new ArrayList<>();
            List<String> strs2 = new ArrayList<>();
            List<String> strs3 = new ArrayList<>();
            List<String> strs4 = new ArrayList<>();
            List<String> strs5 = new ArrayList<>();
            List<String> strs6 = new ArrayList<>();
            List<String> strs7 = new ArrayList<>();
            user.getBusiness().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs.add(url);
            });
            user.setBusiness(strs);

            user.getCredits().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs1.add(url);
            });
            user.setCredits(strs1);

            user.getFire().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs2.add(url);
            });
            user.setFire(strs2);

            user.getHygiene().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs3.add(url);
            });
            user.setHygiene(strs3);

            user.getPrivacyPolicy().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs4.add(url);
            });
            user.setPrivacyPolicy(strs4);

            user.getProperty().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs5.add(url);
            });
            user.setProperty(strs5);

            user.getFinance().forEach(con -> {
                String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                String url = storageService.uploadFromUrl(con, path);
                strs6.add(url);
            });
            user.setFinance(strs6);

            if (CollUtil.isNotEmpty(user.getIntroductionAnnex())) {
                user.getIntroductionAnnex().forEach(con -> {
                    String path = con.substring(con.lastIndexOf("/", con.lastIndexOf("/") - 1) + 1);
                    String url = storageService.uploadFromUrl(con, path);
                    strs7.add(url);
                });
                user.setIntroductionAnnex(strs7);
            }

            rateRepo.save(user);
        });
    }

    @Test
    public void test1() {
        List<RateAudit> rateAudits = rateAuditRepo.findAllByStatus(RateStatus.REVIEW_DENY);
        Map<Long, LocalDateTime> collect = rateAudits.stream()
                .collect(Collectors.toMap(RateAudit::getRateId, RateAudit::getCreatedAt, (old, newV) -> old.compareTo(newV) > 0 ? old : newV));
        Set<Long> ids = rateAudits.stream().map(RateAudit::getRateId).collect(Collectors.toSet());
        List<Rate> rates = rateRepo.findAllById(ids);
        rates.forEach(rate -> {
//            rate.setRejected(true);
            rate.setRejectedAt(collect.get(rate.getId()));
            rateRepo.save(rate);
        });

    }

    @Test
    public void test2() {
        List<Rate> all = rateRepo.findAll();
        all.forEach(rate -> {
            rate.setSort(rate.getStatus().getSort());
            rateRepo.save(rate);
        });
    }

    @Test
    public void test3() {
//        settingRepo.findAllByFlag(5);
//        System.out.println(settingRepo.findAllByNameLikeAndFlag("%一枝梅%", 5));
        Map<String, Long> settingMap = settingRepo.findAllByFlag(5)
                .stream()
                .collect(Collectors.toMap(Setting::getName, Setting::getId));

        List<Organization> all = organizationRepo.findAll();
        all.forEach(organization -> organization.setAllianceId(settingMap.get(organization.getName())));
        organizationRepo.saveAll(all);
    }

    @Test
    public void test4(){
        Path path = Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "templates", "Programme.xlsx");
        System.out.println(this.getClass().getResource("/templates/Programme.xlsx").getFile());
        System.out.println(this.getClass().getResource("Programme.xlsx").getPath());
    }
}
