package com.dgtis.dorm.manager.db.service;

import com.github.pagehelper.PageHelper;
import com.dgtis.dorm.manager.db.dao.AdminMapper;
import com.dgtis.dorm.manager.db.domain.Admin;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    public List<Admin> findAdmin(String username) {
        Admin example = new Admin();
        example.setUsername(username);
        example.setDeleted(false);
        //example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);

        return adminMapper.selectByExample(example);
    }


    /**
     * @descption: 根据工号查询用户信息
     * @param: [eno]
     * @return: java.util.List<com.dgtis.dorm.manager.db.domain.Admin>
     * @auther: qxm
     * @date: 2019/6/5 11:55
     */
    public List<Admin> findByEno(String eno) {
        Admin example = new Admin();
        example.setEmployeNo(eno);
        example.setDeleted(false);

        return adminMapper.selectByExample(example);
    }

    private final Admin.Column[] result = new Admin.Column[]{Admin.Column.id, Admin.Column.username, Admin.Column.nickname, Admin.Column.avatar, Admin.Column.phone};
    public List<Admin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
        Admin example = new Admin();
        example.setUsername(username);
        example.setDeleted(false);

//        LitemallAdminExample.Criteria criteria = example.createCriteria();
//        if(!StringUtils.isEmpty(username)){
//            criteria.andUsernameLike("%" + username + "%");
//        }
//        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(page, limit);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public int countSelective(String username, Integer page, Integer size, String sort, String order) {
        Admin example = new Admin();
        example.setUsername(username);
        example.setDeleted(false);

//        LitemallAdminExample.Criteria criteria = example.createCriteria();
//        if(!StringUtils.isEmpty(username)){
//            criteria.andUsernameLike("%" + username + "%");
//        }
//        criteria.andDeletedEqualTo(false);

        return (int)adminMapper.countByExample(example);
    }

    public void updateById(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    public void deleteById(Integer id) {
        Admin admin = adminMapper.selectByPrimaryKey(id);
        if(admin == null){
            return;
        }
        admin.setDeleted(true);
        adminMapper.updateByPrimaryKey(admin);
    }

    public void add(Admin admin) {
        adminMapper.insertSelective(admin);
    }

    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKeySelective(id, result);
    }
    public Admin selectByPrimaryKey(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

	public List<Admin> findByIdList(List ids) {
		// TODO Auto-generated method stub
		return adminMapper.findByIdList(ids,result);
	}

    public Admin selectByUserId(Integer userId){
        return adminMapper.selectByUserId(userId);
    }

    /**
     * @descption: 根据用户id查询Oneportal中的用户信息
     * @param: [userId]
     * @return: deptUnid isAdmin
     * @auther: qxp
     * @date:19-8-6 11:37
     */
    public Map<String, Object> getUserInfoByUserId(String userId) {
        return  adminMapper.getUserInfoByUserId(userId);
    }
}
