package com.nokia.service;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nokia.dao.UserDao;
import com.nokia.dao.UserRoleCityDao;
import com.nokia.pojo.User;
import com.nokia.pojo.UserRoleCity;

@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleCityDao userRoleCityDao;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void testRedisTemplate() {
        Object value = redisTemplate.opsForValue().get("test_token_hengshui");
        System.out.println(value);
        // Boolean delete = redisTemplate.delete("test_token_hengshui");
        // System.out.println(delete);
        // value = redisTemplate.opsForValue().get("test_token_hengshui");
        // System.out.println(value);
    }

    @Test
    void testGetByLoginName() {
        User user = userService.getByLoginName("test_abc");
        System.out.println(user);
    }

    @Test
    void testAddUserRoleCity() throws FileNotFoundException, IOException {
        try (BufferedReader reader = new BufferedReader(
                new FileReader("D:\\code\\acl_tousu_all\\acl_tousu\\ignored\\user_role_city.txt"))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] strs = line.split("\t");
                UserRoleCity userRoleCity = new UserRoleCity();
                userRoleCity.setUserId(Integer.parseInt(strs[0]));
                userRoleCity.setCityId(Integer.parseInt(strs[1]));
                userRoleCity.setRoleId(Integer.parseInt(strs[2]));
                UserRoleCity selectOne = userRoleCityDao.selectOne(Wrappers.query(userRoleCity));
                if (selectOne != null) {
                    System.out.println("=======================================" + selectOne);
                    // return;
                } else {
                    userService.addUserRoleCity(userRoleCity);
                }
                // userService.addUserRoleCity(userRoleCity);
            }
        }
    }

    @Test
    void testAddUser() throws FileNotFoundException, IOException {
        try (BufferedReader reader = new BufferedReader(
                new FileReader("D:\\code\\acl_tousu_all\\acl_tousu\\ignored\\user.txt"))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                String[] strs = line.split(",");
                System.out.println(Arrays.toString(strs));
                User user = new User();
                user.setLoginName(strs[0]);
                user.setUserName(strs[1]);
                user.setPhone(strs[2]);
                user.setEmail(strs[3]);
                user.setCityId(Integer.parseInt(strs[4]));
                Integer areaId = null;
                try {
                    areaId = Integer.parseInt(strs[5]);
                } catch (Exception e) {

                }
                user.setAreaId(areaId);
                user.setProvinceId(Integer.parseInt(strs[6]));
                user.setOrg(strs[7]);
                System.out.println(user);
                userDao.insert(user);
            }
        }
    }

}
