package cn.feast.coding.service.generator;

import cn.feast.coding.mapper.generator.ModuleMapper;
import cn.feast.coding.mapper.generator.ModuleProjectMapper;
import cn.feast.coding.mapper.generator.TableMapper;
import cn.feast.coding.model.generator.Colunm;
import cn.feast.coding.model.generator.ModuleProject;
import cn.feast.coding.model.generator.Page;
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 ModuleProjectService {

    @Autowired
    private ModuleProjectMapper mapper;
    @Autowired
    private ModuleMapper moduleMapper;
    @Autowired
    private TableMapper tableMapper;

    public Integer save(ModuleProject moduleProject) {
        return mapper.save(moduleProject);
    }

    public void update(ModuleProject moduleProject) {
        mapper.update(moduleProject);
    }

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

    public int queryByCount(ModuleProject moduleProject) {
        return mapper.queryByCount(moduleProject);
    }

    public List<ModuleProject> queryByList(Page page, ModuleProject moduleProject) {
        String pageQueryCondition = " limit " + page.getStart() + " , " + page.getMaxRows();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("moduleId", moduleProject.getModuleId());
        paramsMap.put("projectId", moduleProject.getProjectId());
        paramsMap.put("tableId", moduleProject.getTableId());
        paramsMap.put("moduleParam1", moduleProject.getModuleParam1());
        paramsMap.put("moduleParam1Type", moduleProject.getModuleParam1Type());
        paramsMap.put("moduleParam1Field", moduleProject.getModuleParam1Field());
        paramsMap.put("moduleParam2", moduleProject.getModuleParam2());
        paramsMap.put("moduleParam2Type", moduleProject.getModuleParam2Type());
        paramsMap.put("moduleParam2Field", moduleProject.getModuleParam2Field());
        paramsMap.put("moduleParam3", moduleProject.getModuleParam3());
        paramsMap.put("moduleParam3Type", moduleProject.getModuleParam3Type());
        paramsMap.put("moduleParam3Field", moduleProject.getModuleParam3Field());
        paramsMap.put("pageQueryCondition", pageQueryCondition);
        return mapper.queryByList(paramsMap);
    }

    public List<ModuleProject> queryByModuleProject(ModuleProject moduleProject) {
        return mapper.queryByModuleProject(moduleProject);
    }

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

    public List<ModuleProject> queryByProject(Integer projectId) {
        List<ModuleProject> list = mapper.queryByProject(projectId);
        for (ModuleProject mp : list) {
            mp.setModule(moduleMapper.queryById(mp.getModuleId()));
            mp.setTable(tableMapper.queryById(mp.getTableId()));
        }
        return list;
    }

    public List<ModuleProject> queryByModule(Integer moduleId) {
        return mapper.queryByModule(moduleId);
    }

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

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

    public List<ModuleProject> queryByProjectAndModule(Integer projectId, String moduleName) {
        return mapper.queryByProjectAndModule(projectId, moduleName);
    }
}
