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

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

import java.util.ArrayList;
import java.util.HashMap;
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.AttendersDao;
import io.renren.modules.admin.entity.AttendersEntity;
import io.renren.modules.admin.service.AttendersService;

@Service("attendersService")
public class AttendersServiceImpl extends ServiceImpl<AttendersDao, AttendersEntity> implements AttendersService {

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

        return new PageUtils(page);
    }

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

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

    @Override
    public List<Map<String, Object>> getMeetingListByMemberId(Long memberId){
        return baseMapper.getMeetingListByMemberId(memberId);
    }

    @Override
    public Map<String, Object> getNameById(Long aid) {
        return baseMapper.getNameById(aid);
    }

    @Override
    public Map<String, Object> getMapById(Long aid) {
        return baseMapper.getMapById(aid);
    }

    @Override
    public List<Map<String, Object>> getMapList(Long meetingId) {
        return baseMapper.getMapList(meetingId);
    }

    @Override
    public int changeColumnValue(String column, Integer value, Long id) {
        return baseMapper.changeColumnValue(column, value,id);
    }
}