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

import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
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.ReportDao;
import io.renren.modules.admin.entity.ReportEntity;
import io.renren.modules.admin.service.ReportService;

@Service("reportService")
public class ReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity> implements ReportService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long meetingId = Long.valueOf((String) params.get("meetingId"));
        String key = params.containsKey("key") ? (String) params.get("key") : null;
        Long bid = params.containsKey("bid") ? Long.valueOf((String) params.get("bid")) : 0;
        IPage<ReportEntity> page = this.page(new Query<ReportEntity>().getPage(params),
                new QueryWrapper<ReportEntity>().eq("meeting_id", meetingId).eq(bid!=0L, "branch_id", bid)
                        .like(StringUtils.isNotBlank(key), "title", key).or()
                        .like(StringUtils.isNotBlank(key), "truename", key).or()
                        .like(StringUtils.isNotBlank(key), "organization", key));

        return new PageUtils(page);
    }

    @Override
    public List<ReportEntity> listByBranch(Long mid, Long bid, String order) {
        return this.list(new QueryWrapper<ReportEntity>().eq(bid == 0, "meeting_id", mid).eq("branch_id", bid).orderByAsc(StringUtils.isNotBlank(order), order));
    }

    @Override
    public List<ReportEntity> listByBranch(Long mid, String order) {
        return this.list(new QueryWrapper<ReportEntity>().eq("meeting_id", mid).orderByAsc(StringUtils.isNotBlank(order), order));
    }

}