package cn.feast.coding.schedule;

import cn.feast.coding.model.generator.Project;
import cn.feast.coding.service.generator.ColunmService;
import cn.feast.coding.service.generator.ModuleProjectService;
import cn.feast.coding.service.generator.ProjectService;
import cn.feast.coding.service.generator.TableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public class ProjectSchedule {
    private static final Logger logger = LoggerFactory.getLogger(ProjectSchedule.class);
    @Autowired
    private ProjectService projectService;
    @Autowired
    private TableService tableService;
    @Autowired
    private ColunmService colunmService;
    @Autowired
    private ModuleProjectService moduleProjectService;

    /**
     * 清理项目数据
     */
    public void clearData() {
        logger.info("定时任务：清理项目数据开始....");
        try {
            List<Project> projectList = projectService.queryAll();
            for(Project project : projectList){
                //删除项目关联的表和字段数据
                colunmService.deleteProjectRelatedData(project.getId());
                tableService.deleteProjectRelatedData(project.getId());
                //删除项目关联的功能模块数据
                moduleProjectService.deleteProjectRelatedData(project.getId());
                logger.info("删除项目《"+project.getProjectName()+"》数据完成....");
            }
            logger.info("定时任务：清理项目数据完成....");
        }catch (Exception e){
            logger.error("定时任务：清理项目数据错误 -》"+e.getMessage(), e);
        }
    }
}
