package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmSysUserDao;
import com.ssm.manage.entity.SysRole;
import com.ssm.manage.entity.SysUser;
import com.ssm.manage.mapper.SysUserMapper;
import com.ssm.manage.query.UserRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Component
public class SsmSysUserDaoImpl extends BaseDaoImpl<SysUser> implements SsmSysUserDao {
    @Autowired
    public SysUserMapper mapper;

    @Override
    public SysUserMapper getMapper() {
        return mapper;
    }

    @Override
    public PageInfo<SysUser> selectByPage(UserRequest userRequest) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(userRequest.getPhone())){
            criteria.andLike("mobilePhone","%" + userRequest.getPhone() + "%");
        }
        if (StringUtils.isNotBlank(userRequest.getUserName())){
            criteria.andLike("userName","%" + userRequest.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(userRequest.getUserNum())){
            criteria.andLike("userNum","%" + userRequest.getUserNum() + "%");
        }
        criteria.andEqualTo("state",userRequest.getState());
//        if (StringUtils.isNotBlank(userRequest.getQq())){
//            criteria.orLike("qq","%" + userRequest.getQq() + "%");
//        }
//        if (StringUtils.isNotBlank(userRequest.getWechat())){
//            criteria.orLike("weChat","%"+ userRequest.getWechat() + "%");
//        }
//        if (StringUtils.isNotBlank(userRequest.getMailbox())){
//            criteria.orLike("mailBox","%" + userRequest.getMailbox() + "%");
//        }
        PageHelper.startPage(userRequest.getPageNum(),userRequest.getPageSize(),userRequest.getOrderAndSort());
        List<SysUser> list = mapper.selectByExample(example);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int deleteSysUser(UserRequest userRequest) {
        return mapper.deleteSysUser(userRequest);
    }
}
