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

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

import java.util.Date;
import java.util.Arrays;
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.MyUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.admin.dao.MemberDao;
import io.renren.modules.admin.entity.MemberEntity;
import io.renren.modules.admin.service.MemberService;

@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = params.containsKey("key") ? (String) params.get("key") : null;
        IPage<MemberEntity> page = this.page(new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>().like(StringUtils.isNotBlank(key), "truename", key)

        );

        return new PageUtils(page);
    }

    @Override
    public int updateRemark(Long id, String remark) {
        return baseMapper.updateRemark(id, remark);
    }

    @Override
    public MemberEntity getBySymbol(String symbol) {
        MemberEntity member = this.getOne(new QueryWrapper<MemberEntity>().eq("symbol", symbol));
        return member;
    }

    @Override
    public boolean saveMember(MemberEntity member) {
        member.setSymbol(MyUtils.md5(member.getTruename() + member.getPhone()));
        member.setCreateTime(new Date());
        return this.save(member);
    }

    @Override
    public boolean updateMember(MemberEntity member) {
        member.setSymbol(MyUtils.md5(member.getTruename() + member.getPhone()));
        member.setModifyTime(new Date());
        return this.updateById(member);
    }

    @Override
    public int updateFlag(Long id, Integer flag) {
        return baseMapper.updateFlag(id, flag);
    }

    @Override
    public PageUtils queryInvitationPage(Map<String, Object> params) {
        Long meetingId = Long.valueOf(params.get("meetingId").toString());
        String key = params.containsKey("key") ? (String) params.get("key") : null;
        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>> mList = baseMapper.getInvitationPageList(meetingId, key, key, offset, limit);
        Integer mc = baseMapper.getInvitationCounts(meetingId, key, key);
        return new PageUtils(mList, mc, limit, page+1);
    }

    @Override
    public MemberEntity saveMember(String truename, String phone, String organization, String position) {
        String symbol = MyUtils.md5(truename + phone);
        MemberEntity member = getBySymbol(symbol);
        if (member == null) {
            member = new MemberEntity();
            member.setTruename(truename);
            member.setOrganization(organization);
            member.setPosition(position);
            member.setPhone(phone);
            member.setSymbol(MyUtils.md5(member.getTruename() + member.getPhone()));
            member.setCreateTime(new Date());
            this.save(member);
        } else {
            member.setOrganization(organization);
            member.setPosition(position);
            member.setModifyTime(new Date());
            this.updateById(member);
        }

        return member;
    }
}