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

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

import java.util.Arrays;
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.EmployeeDao;
import io.renren.modules.admin.entity.EmployeeEntity;
import io.renren.modules.admin.service.EmployeeService;


@Service("employeeService")
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, EmployeeEntity> implements EmployeeService {

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

        return new PageUtils(page);
    }

    @Override
    public List<EmployeeEntity> findAllByisDel(int isDel) {
        return  baseMapper.findAllByisDel(isDel);
    }

    @Override
    public List<EmployeeEntity> findAllByTrueName(String truename) {
        return baseMapper.findAllByTrueName(truename);
    }

    @Override
    public boolean updatePasswordByid(Long id) {
        return baseMapper.updatePasswordByid(id);
    }

    @Override
    public void delete(Long[]  ids) {
       baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public PageUtils queryList(int page, int limit, String key) {
        System.out.println(page);
        System.out.println(limit);
        System.out.println(key);
        List<Map<String, Object>> mList = baseMapper.queryListBykey(key, key,page, limit);
        Integer total = baseMapper.queryTotalCount(key, key);


        return new PageUtils(mList,total,limit,page);
    }

    @Override
    public List<Map<String, Object>> selectNameList(String truename) {
        return baseMapper.selectNameList(truename);
    }

    @Override
    public Map<Long, String> selectNameListByCompanyId(Long companyId) {
        List<Map<String, Object>> list= baseMapper.selectNameListByCompanyId(companyId);
        Map<Long, String> result = new HashMap<Long, String>();
        for (Map<String,Object> map : list) {
            Long id =(Long) map.get("id");
            String name = (String) map.get("truename");
            result.put(id, name);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getContact(String ids) {
        return baseMapper.getContact(ids);
    }
}