package com.izouma.zhirongip.service;

import com.izouma.zhirongip.domain.demand.CopyrightDemand;
import com.izouma.zhirongip.domain.demand.PatentDemand;
import com.izouma.zhirongip.domain.demand.TechProductDemand;
import com.izouma.zhirongip.domain.resource.*;
import com.izouma.zhirongip.domain.supply.*;
import com.izouma.zhirongip.dto.SearchDTO;
import com.izouma.zhirongip.enums.CopyrightType;
import com.izouma.zhirongip.repo.demand.CopyrightDemandRepo;
import com.izouma.zhirongip.repo.demand.LogoDemandRepo;
import com.izouma.zhirongip.repo.demand.PatentDemandRepo;
import com.izouma.zhirongip.repo.demand.TechProductDemandRepo;
import com.izouma.zhirongip.repo.resource.*;
import com.izouma.zhirongip.repo.supply.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class AllService {
    private PatentRepo            patentRepo;
    private LogoRepo              logoRepo;
    private CopyrightRepo         copyrightRepo;
    private TechnologyProductRepo technologyProductRepo;
    private TechnicalManagerRepo  technicalManagerRepo;

    private PatentDemandRepo      patentDemandRepo;
    private CopyrightDemandRepo   copyrightDemandRepo;
    private TechProductDemandRepo techProductDemandRepo;
    private LogoDemandRepo        logoDemandRepo;

    private PurchaseRepo    purchaseRepo;
    private SuccessCaseRepo successCaseRepo;

    private PolicyLawRepo            policyLawRepo;
    private IntellectualPropertyRepo intellectualPropertyRepo;
    private DigitalRepo              digitalRepo;
    private ExpertRepo               expertRepo;
    private CompanyRepo              companyRepo;

    public List<SearchDTO> search(String search) {
        String sea = "%" + search + "%";
        // 供给
        List<Patent> patents = patentRepo.findAllByNameLike(sea);
        List<SearchDTO> dtos = patents.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getCaseType().name()))
                .collect(Collectors.toList());

        List<Logo> logos = logoRepo.findAllByNameLike(sea);
        dtos.addAll(logos.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), "LOGO"))
                .collect(Collectors.toList()));

        List<Copyright> copyrights = copyrightRepo.findAllByNameLike(sea);
        dtos.addAll(copyrights.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getType().name()))
                .collect(Collectors.toList()));

        List<TechnologyProduct> tecPros = technologyProductRepo.findAllByNameLike(sea);
        dtos.addAll(tecPros.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getTpType().name()))
                .collect(Collectors.toList()));

        List<TechnicalManager> techMans = technicalManagerRepo.findAllByNameLike(sea);
        dtos.addAll(techMans.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), "TECHNICAL_MANAGER"))
                .collect(Collectors.toList()));

        // 需求
        List<PatentDemand> patentDemands = patentDemandRepo.findAllByNameLike(sea);
        dtos.addAll(patentDemands.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getCaseType().name() + "_DEMAND"))
                .collect(Collectors.toList()));

        List<CopyrightDemand> copyrightDemands = copyrightDemandRepo.findAllByNameLikeAndTypeIsNot(sea, CopyrightType.SERVICE);
        dtos.addAll(copyrightDemands.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getType().name() + "_DEMAND"))
                .collect(Collectors.toList()));

        List<TechProductDemand> techProductDemands = techProductDemandRepo.findAllByNameLike(sea);
        dtos.addAll(techProductDemands.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getTpType().name() + "_DEMAND"))
                .collect(Collectors.toList()));

        // 军采
        List<Purchase> purchases = purchaseRepo.findAllByNameLike(sea);
        dtos.addAll(purchases.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), "PURCHASE"))
                .collect(Collectors.toList()));

        // 成交案例
        List<SuccessCase> cases = successCaseRepo.findAllByNameLike(sea);
        dtos.addAll(cases.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getCategory() + "_CASE"))
                .collect(Collectors.toList()));

        // 政策法律新闻
        List<PolicyLaw> laws = policyLawRepo.findAllByNameLike(sea);
        dtos.addAll(laws.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getCategory().name()))
                .collect(Collectors.toList()));

        List<IntellectualProperty> inteProps = intellectualPropertyRepo.findAllByNameLike(sea);
        dtos.addAll(inteProps.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), "INTELLECTUAL_PROPERTY"))
                .collect(Collectors.toList()));

        List<Digital> digitals = digitalRepo.findAllByNameLike(sea);
        dtos.addAll(digitals.stream()
                .map(temp -> new SearchDTO(temp.getId(), temp.getName(), temp.getType() + "_DIGITAL"))
                .collect(Collectors.toList()));

        List<Expert> experts = expertRepo.findAllByNameLike(sea);
        dtos.addAll(experts.stream()
                .map(expert -> new SearchDTO(expert.getId(), expert.getName(), "EXPERT"))
                .collect(Collectors.toList()));

        List<Company> companies = companyRepo.findAllByNameLike(sea);
        dtos.addAll(companies.stream()
                .map(company -> new SearchDTO(company.getId(), company.getName(), "COMPANY"))
                .collect(Collectors.toList()));

        return dtos;
    }

    //添加浏览量
    public void addView(Long id, String type) {
        switch (type) {
            case "CopyrightDemand":
                copyrightDemandRepo.addView(id);
                break;
            case "LogoDemand":
                logoDemandRepo.addView(id);
                break;
            case "PatentDemand":
                patentDemandRepo.addView(id);
                break;
            case "TechProductDemand":
                techProductDemandRepo.addView(id);
                break;
            case "SuccessCase":
                successCaseRepo.addView(id);
                break;
            case "Copyright":
                copyrightRepo.addView(id);
                break;
            case "Logo":
                logoRepo.addView(id);
                break;
            case "Patent":
                patentRepo.addView(id);
                break;
            case "TechnologyProduct":
                technologyProductRepo.addView(id);
                break;
            case "Purchase":
                purchaseRepo.addView(id);
            default:
                break;
        }

    }
}
