package com.docus.server.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.sys.common.UserProperties;
import com.docus.server.sys.mapper.PowerUserMapper;
import com.docus.server.sys.common.pojo.dto.UserDTO;
import com.docus.server.sys.common.pojo.dto.UserModifyParam;
import com.docus.server.sys.common.pojo.entity.PowerUser;
import com.docus.server.sys.service.IPowerUserService;
import org.springframework.stereotype.Service;

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

/**
 * 用户服务实现
 *
 * @author wyb
 */
@Service
public class PowerUserServiceImpl extends ServiceImpl<PowerUserMapper, PowerUser> implements IPowerUserService {
    @Resource
    private IdService idService;
    @Resource
    private UserProperties userProperties;

    @Override
    public void batchSaveOrUpdatePowerUser(List<UserDTO> userDTOList) {
        userDTOList.forEach(this::saveOrUpdatePowerUser);
    }

    @Override
    public boolean saveOrUpdatePowerUser(UserDTO userDTO) {
        PowerUser powerUser = baseMapper.getUserByUserName(userDTO.getUserName());
        UserModifyParam userModifyParam = userDTO.transUserAddParam();
        if (Func.isEmpty(powerUser)) {
            long userId = idService.getDateSeq();
            userModifyParam.setUserId(userId);
            userModifyParam.setUserPwd(userProperties.getPassword());
            baseMapper.addUser(userModifyParam);
            return true;
        }
        userModifyParam.setUserId(powerUser.getUserId());
        baseMapper.updateUser(userModifyParam);
        return true;
    }

    @Override
    public boolean delUserByUserName(String userName) {
        PowerUser powerUser = baseMapper.getUserByUserName(userName);
        if (Func.isEmpty(powerUser)) {
            return true;
        }
        baseMapper.delUserByUserName(userName);
        return true;
    }


}
