package com.pannk.sys.user.service.impl;

import com.pannk.sys.user.entity.SysUserEntity;
import com.pannk.sys.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wolf on 20-11-30.
 */
@Service
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    private List<SysUserEntity> list;

    @Override
    public List<SysUserEntity> list(String filter) {
        List<SysUserEntity> result = new ArrayList<>();
        list.forEach(u -> {
            if (u.getCode().equalsIgnoreCase(filter) || u.getName().equalsIgnoreCase(filter)) {
                result.add(u);
            }
        });
        return result;
    }

    @Override
    public SysUserEntity detail(Long id) {
        for (SysUserEntity sysUserEntity : list) {
            if (sysUserEntity.getId() == id) {
                return sysUserEntity;
            }
        }
        return null;
    }

    @Override
    public void delete(Long id) {
        for (SysUserEntity sysUserEntity : list) {
            if (sysUserEntity.getId() == id) {
                list.remove(sysUserEntity);
            }
        }
    }

    @Override
    public void batchDelete(Long[] ids) {
        for (SysUserEntity sysUserEntity : list) {
            if (Arrays.asList(ids).contains(sysUserEntity.getId())) {
                list.remove(sysUserEntity);
            }
        }
    }

    @Override
    public SysUserEntity save(SysUserEntity sysUserEntity) {
        list.add(sysUserEntity);
        return sysUserEntity;
    }

    @Override
    public SysUserEntity update(SysUserEntity sysUserEntity) {
        for (int i = 0; i < list.size(); i++) {
            SysUserEntity u = list.get(i);
            if (u.getId() == sysUserEntity.getId()) {
                list.remove(u);
                list.add(i, sysUserEntity);
            }
        }
        return sysUserEntity;
    }
}
