package com.jkcredit.invoice.service.user.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.model.entity.user.Role;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.mapper.user.UserMapper;
import com.jkcredit.invoice.service.user.RoleService;
import com.jkcredit.invoice.service.user.UserService;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: sunzhaoning
 * @create: 2019-05-28 17:53
 * @version: V1.0
 **/
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RoleService roleService;

    @Override
    public IPage getUserWithRolePage(Page page, User user) {
        return baseMapper.getUsersPage(page, user);
    }

    @Override
    public List<User> getAllUser() {
        return baseMapper.getAllUser();
    }

    @Override
    public Boolean deleteUserById(User user) {
        this.removeById(user.getId());
        return Boolean.TRUE;
    }

    @Override
    public RespR<Boolean> updateUserInfo(User user){
       /* if(user.getRoleId()!=null){
            //根据角色ID查询角色信息
            Role role =roleService.getById(user.getRoleId());
            //设置角色名称
            user.setRoleName(role.getRoleName());
        }*/
        return new RespR<>(baseMapper.updateUserInfo(user));
    }

    @Override
    public RespR<Boolean> updateCompanyNumById(User user) {
        return new RespR<>(baseMapper.updateCompanyNumById(user));
    }

    @Override
    public User selectUserById(Integer id) {
        return baseMapper.getUserById(id);
    }

    @Override
    public User selectUserByUserName(String userName) {
        return baseMapper.getUserByUserName(userName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveUser(User user) {
        //根据角色ID查询角色信息
        Role role =roleService.getById(user.getRoleId());
        //设置角色名称
        user.setRoleName(role.getRoleName());
        //设置创建时间
        user.setCreateTime(System.currentTimeMillis());
        this.save(user);
        return Boolean.TRUE;
    }

    @Override
    public RespR<Boolean> restPassword(User user) {
        return new RespR<>(baseMapper.updatePassword(user));
    }

    @Override
    public RespR<Boolean> updateLock(User user) {
        return new RespR<>(baseMapper.updateLock(user));
    }
}