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

import org.springframework.stereotype.Service;

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.OrganizationDao;
import io.renren.modules.admin.entity.OrganizationEntity;
import io.renren.modules.admin.service.OrganizationService;


@Service("organizationService")
public class OrganizationServiceImpl extends ServiceImpl<OrganizationDao, OrganizationEntity> implements OrganizationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        int page = params.containsKey("page") ? Integer.valueOf((String) params.get("page")) -1: 0;
        int limit =  params.containsKey("limit") ? Integer.valueOf((String) params.get("limit")) : 10;
        if (page < 0) {
            page = 0;
        }
        int offset = page * limit;

        List<Map<String, Object>> list = baseMapper.getPageList(key, offset, limit);
        int counts = baseMapper.getCounts(key);

        return new PageUtils(list, counts, limit, page+1);
    }

    @Override
    public List<OrganizationEntity> previousList(String organization) {
        List<OrganizationEntity> list = this.list(new QueryWrapper<OrganizationEntity>().eq("organization", organization));
        return list;
    }

    
    @Override
    public PageUtils attendList(Map<String, Object> params) {
        String organization = (String) params.get("organization");
        int page = params.containsKey("page") ? Integer.valueOf((String) params.get("page")) -1: 0;
        int limit =  params.containsKey("limit") ? Integer.valueOf((String) params.get("limit")) : 10;
        if (page < 0) {
            page = 0;
        }
        int offset = page * limit;

        List<Map<String, Object>> list = baseMapper.getPageAttendList(organization, offset, limit);
        int counts = baseMapper.getAttendCounts(organization);

        return new PageUtils(list, counts, limit, page+1);
    }
}