package com.fdage.service.impl;

import com.fdage.dao.base.TbUserMapper;
import com.fdage.dao.cust.TbUserMapperCust;
import com.fdage.pojo.TbRole;
import com.fdage.pojo.TbUser;
import com.fdage.request.RequestUser;
import com.fdage.respon.ResponUser;
import com.fdage.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by Hb_zzZ on 2019/9/11.
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private TbUserMapper mapper;

    @Autowired
    private TbUserMapperCust mapperCust;

    @Override
    public TbUser findByUserName(String userName) {
        return mapperCust.findByUserName(userName);
    }

    @Override
    public int insert(TbUser user) {
        return mapper.insertSelective(user);
    }

    @Override
    public int update(TbUser user) {
        return mapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbUser findById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ResponUser> findUserList(RequestUser bo) {
        int pageNum = bo.getPageNum() == 0? 1: bo.getPageNum();
        int pageSize = bo.getPageSize() == 0? 10: bo.getPageSize();
        PageHelper.startPage(pageNum, pageSize);

        TbUser user = new TbUser();
        user.setState(bo.getState());
        if(StringUtils.isNotEmpty(bo.getSearchKey())){
            user.setUserName(bo.getSearchKey());
        }
        return mapperCust.findUserList(user);
    }

    @Override
    public List<TbRole> roleList() {
        return mapperCust.roleList();
    }

    @Override
    public Set<Long> findUserRoleByUserId(Long userId) {
        return mapperCust.findUserRoleByUserId(userId);
    }

    @Override
    public List<String> findRoleByUserId(Long id) {
        return mapperCust.findRoleByUserId(id);
    }
}
