package cn.feast.coding.service.generator;

import cn.feast.coding.mapper.generator.ProjectMapper;
import cn.feast.coding.model.generator.Colunm;
import cn.feast.coding.model.generator.Page;
import cn.feast.coding.model.generator.Project;
import cn.feast.coding.tools.SysContants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By FeastCoding.
 */
@Service
public class ProjectService {

    @Autowired
    private ProjectMapper mapper;

    public Integer save(Project project) {
        return mapper.save(project);
    }

    public void update(Project project) {
        mapper.update(project);
    }

    public void deleteById(Integer id) {
        mapper.deleteById(id);
    }

    public int queryByCount(Project project) {
        return mapper.queryByCount(project);
    }

    public List<Project> queryByList(Page page, Project project) {
        String pageQueryCondition = " limit " + page.getStart() + " , " + page.getMaxRows();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("projectName", project.getProjectName());
        paramsMap.put("projectDesc", project.getProjectDesc());
        paramsMap.put("dataSourse", project.getDataSourse());
        paramsMap.put("packName", project.getPackName());
        paramsMap.put("pagePath", project.getPagePath());
        paramsMap.put("pageStyle", project.getPageStyle());
        paramsMap.put("pageQueryCondition", pageQueryCondition);
        return mapper.queryByList(paramsMap);
    }

    public List<Project> queryByProject(Project project) {
        return mapper.queryByProject(project);
    }

    public Project queryById(Integer id) {
        return mapper.queryById(id);
    }

    public List<Project> queryByDataSource(Integer dataSourceId) {
        return mapper.queryByDataSource(dataSourceId);
    }

    public List<Project> queryByPageTemplate(Integer pageTemplateId) {
        return mapper.queryByPageTemplate(pageTemplateId);
    }

    public PageInfo<Project> queryPage(Project project, int pageNum, int pageSize) {
        if (pageSize < 1)
            pageSize = SysContants.MAX_PAGE_ROWS;
        PageHelper.startPage(pageNum, pageSize);
        List<Project> list = mapper.queryByProject(project);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public List<Project> queryAll() {
        return mapper.queryAll();
    }
}
