package com.ruoyi.interfaces.service.impl;

import com.ruoyi.interfaces.domain.PtAppUser;
import com.ruoyi.interfaces.mapper.PtAppUserDao;
import com.ruoyi.interfaces.service.PtAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author weking
 * @date 2018-11-6
 */
@Service
@Transactional
public class PtAppUserServiceImpl implements PtAppUserService {

    @Autowired
    private PtAppUserDao ptAppUserDao;

    @Override
    public int save(PtAppUser ptAppUser) {
        Optional.ofNullable(ptAppUser).map(PtAppUser::getNickName).ifPresent(ptAppUser::setUserName);

        PtAppUser user = ptAppUserDao.get(ptAppUser.getUserId());
        if (user == null) {
            return insert(ptAppUser);
        }
        return updateByPrimaryKey(ptAppUser);
    }

    @Override
    public int insert(PtAppUser ptAppUser) {
        ptAppUser.setUserRegdate(new Date());
        // 默认可以登录系统
        ptAppUser.setUserStatus("1");
        return ptAppUserDao.insert(ptAppUser);
    }

    @Override
    public PtAppUser get(String userId) {
        return ptAppUserDao.get(userId);
    }

    @Override
    public List<PtAppUser> selectByPrimaryKey(String id) {
        return ptAppUserDao.selectByPrimaryKey(id);
    }

    @Override
    public List<PtAppUser> selectByName(String userName) {
        return ptAppUserDao.selectByName(userName);
    }

    @Override
    public int updateByPrimaryKey(PtAppUser ptAppUser) {
        return ptAppUserDao.updateByPrimaryKey(ptAppUser);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return ptAppUserDao.deleteByPrimaryKey(id);
    }

}
