package com.ruoyi.task;

import com.ruoyi.biemo.business.domain.Category;
import com.ruoyi.biemo.business.domain.DocInfo;
import com.ruoyi.biemo.business.domain.Report;
import com.ruoyi.biemo.business.service.CategoryService;
import com.ruoyi.biemo.business.service.DocInfoService;
import com.ruoyi.biemo.business.service.ReportService;
import com.ruoyi.biemo.mongodb.bean.Page;
import com.ruoyi.biemo.mongodb.utils.CriteriaAndWrapper;
import com.ruoyi.biemo.mongodb.utils.MongoHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component("syncData")
public class SyncData {
    @Autowired
    MongoHelper mongoHelper;
    @Autowired
    DocInfoService docInfoService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ReportService reportService;
    public void  mongoToEs(){
        Page<DocInfo> page = new Page();
        Page<DocInfo> pageInfo =  mongoHelper.findPage(new CriteriaAndWrapper().eq(DocInfo::getIsSync,0),page, DocInfo.class);
        List<DocInfo> docInfos = pageInfo.getList();
        if(docInfos!=null&& docInfos.size()>0){
            docInfos.forEach(docInfo -> {
                docInfo.setCreatedBy(1L);
                docInfo.setUpdatedBy(1L);
                docInfo.setCreateTime(System.currentTimeMillis());
                docInfo.setUpdateTime(System.currentTimeMillis());
                docInfo.setIsSync(1);
                docInfoService.insertOrUpdateDocInfo(docInfo);
            });
        }
    }

    public void categoryToEs(){
        Page<Category> page = new Page();
        Page<Category> pageInfo =  mongoHelper.findPage(new CriteriaAndWrapper().eq(Category::getIsSync,0),page, Category.class);
        List<Category> categories = pageInfo.getList();
        if(categories!=null&& categories.size()>0){
            categories.forEach(category -> {
                category.setCreatedBy(1L);
                category.setUpdatedBy(1L);
                category.setIsSync(1);
                categoryService.insertOrUpdateCategory(category);
            });
        }
    }

    public void reportToEs(){
        Page<Report> page = new Page();
        Page<Report> pageInfo =  mongoHelper.findPage(new CriteriaAndWrapper().eq(Report::getIsSync,0),page, Report.class);
        List<Report> reports = pageInfo.getList();
        if(reports!=null&& reports.size()>0){
            reports.forEach(report -> {
                report.setCreatedBy(1L);
                report.setUpdatedBy(1L);
                report.setIsSync(1);
                reportService.insertOrUpdateReport(report);
            });
        }
    }

}
