package io.renren.modules.admin.service.impl;

import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.admin.dao.PaperDao;
import io.renren.modules.admin.entity.PaperEntity;
import io.renren.modules.admin.service.PaperService;

@Service("paperService")
public class PaperServiceImpl extends ServiceImpl<PaperDao, PaperEntity> implements PaperService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String meetingId = (String) params.get("meetingId");
        String title = (String)params.get("title");
        IPage<PaperEntity> page = this.page(
                new Query<PaperEntity>().getPage(params),
                new QueryWrapper<PaperEntity>().eq(StringUtils.isNotBlank(meetingId),"meeting_id", meetingId)
                .like(StringUtils.isNotBlank(title),"title",title)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils myQueryPage(Map<String, Object> params) {
        Long meetingId = Long.valueOf((String) params.get("meetingId"));
        String key = params.containsKey("page") ? (String) params.get("key") : "";
        int page = params.containsKey("page") ? Integer.valueOf(params.get("page").toString()) -1: 0;
        int limit =  params.containsKey("limit") ? Integer.valueOf(params.get("limit").toString()) : 10;
        if (page < 0) {
            page = 0;
        }
        int startLimt = page * limit;
        int endLimt = startLimt + limit;
        List<Map<String, Object>> pList=baseMapper.getPageList(meetingId, startLimt, endLimt, key, key);
        int mc = baseMapper.getCounts(meetingId, key, key);
    

        return new  PageUtils(pList, mc, limit, page+1);
    }
}