package cn.feast.coding.service.generator;

import cn.feast.coding.mapper.generator.TableMapper;
import cn.feast.coding.model.generator.Page;
import cn.feast.coding.model.generator.Project;
import cn.feast.coding.model.generator.Table;
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 TableService {

    @Autowired
    private TableMapper mapper;

    public Integer save(Table table) {
        return mapper.save(table);
    }

    public void update(Table table) {
        mapper.update(table);
    }

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

    public int queryByCount(Table table) {
        return mapper.queryByCount(table);
    }

    public List<Table> queryByList(Page page, Table table) {
        String pageQueryCondition = " limit " + page.getStart() + " , " + page.getMaxRows();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("tableName", table.getTableName());
        paramsMap.put("className", table.getClassName());
        paramsMap.put("showStyle", table.getShowStyle());
        paramsMap.put("projectId", table.getProjectId());
        paramsMap.put("pageQueryCondition", pageQueryCondition);
        return mapper.queryByList(paramsMap);
    }

    public List<Table> queryByTable(Table table) {
        return mapper.queryByTable(table);
    }

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

    public void deleteProjectRelatedData(Integer projectId) {
        mapper.deleteProjectRelatedData(projectId);
    }

    public List<Table> queryByProject(Integer projectId) {
        return mapper.queryByProject(projectId);
    }

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

    public int saveByBatch(List<Table> tableList) {
        return mapper.saveByBatch(tableList);
    }
}
