package cn.feast.coding.service.generator;

import cn.feast.coding.mapper.generator.ColunmMapper;
import cn.feast.coding.model.generator.Colunm;
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 ColunmService {

    @Autowired
    private ColunmMapper mapper;

    public Integer save(Colunm colunm) {
        return mapper.save(colunm);
    }

    public Integer saveByBatch(List<Colunm> list){
        return mapper.saveByBatch(list);
    }

    public void update(Colunm colunm) {
        mapper.update(colunm);
    }

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

    public int queryByCount(Colunm colunm) {
        return mapper.queryByCount(colunm);
    }

    public List<Colunm> queryByList(Page page, Colunm colunm) {
        String pageQueryCondition = " limit " + page.getStart() + " , " + page.getMaxRows();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("colunmName", colunm.getColunmName());
        paramsMap.put("filedName", colunm.getFiledName());
        paramsMap.put("colunmType", colunm.getColunmType());
        paramsMap.put("showName", colunm.getShowName());
        paramsMap.put("editStyle", colunm.getEditStyle());
        paramsMap.put("isPrimary", colunm.getIsPrimary());
        paramsMap.put("isShowed", colunm.getIsShowed());
        paramsMap.put("tableId", colunm.getTableId());
        paramsMap.put("pageQueryCondition", pageQueryCondition);
        return mapper.queryByList(paramsMap);
    }

    public List<Colunm> queryByColunm(Colunm colunm) {
        return mapper.queryByColunm(colunm);
    }

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

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

    public List<Colunm> queryByTable(Integer tableId) {
        return mapper.queryByTable(tableId);
    }

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