package com.zsc.edu.gateway.service.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.edu.gateway.domain.system.RoleBuilder;
import com.zsc.edu.gateway.domain.system.UserBuilder;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.modules.system.dto.RoleDto;
import com.zsc.edu.gateway.modules.system.dto.UserCreateDto;
import com.zsc.edu.gateway.modules.system.dto.UserUpdateDto;
import com.zsc.edu.gateway.modules.system.entity.Authority;
import com.zsc.edu.gateway.modules.system.entity.Role;
import com.zsc.edu.gateway.modules.system.entity.User;
import com.zsc.edu.gateway.modules.system.repo.AuthorityRepository;
import com.zsc.edu.gateway.modules.system.repo.RoleAuthoritiesRepository;
import com.zsc.edu.gateway.modules.system.repo.UserRepository;
import com.zsc.edu.gateway.modules.system.service.UserService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author ftz
 * 创建时间:29/12/2023 上午11:21
 */
@SpringBootTest
public class UserServiceTest {
    @Resource
    private RoleAuthoritiesRepository roleAuthoritiesRepository;
    @Resource
    private UserService service;
    @Resource
    private UserRepository repo;
    @Resource
    private AuthorityRepository authorityRepository;

    @Resource
    private PasswordEncoder passwordEncoder;

    private User user1;
    private User user2;
    private User user3;

    @BeforeEach
    void setUp() {
        user1 = UserBuilder.anUser().username("A张三").password("123456").enable(true).build();
        repo.insert(user1);
        user2 = UserBuilder.anUser().username("A李四").password("123456").enable(true).build();
        repo.insert(user2);
        user3 = UserBuilder.anUser().username("A王五").password("123456").enable(true).build();
        repo.insert(user3);
    }

    @AfterEach
    void tearDown() {
        repo.delete(new LambdaQueryWrapper<User>().in(User::getUsername, "A张三", "A李四", "A王五", "A赵六", "A陈七", "A刘八"));
    }

    @Test
    void list() {
        assertEquals(3, service.list(new LambdaQueryWrapper<User>().like(User::getUsername, "A")).size());
        assertEquals(1, service.list(new LambdaQueryWrapper<User>().eq(User::getUsername, user1.getUsername())).size());
//        assertEquals(1, service.list().size());
    }

    @Test
    void create() {
        UserCreateDto dto = new UserCreateDto();
        dto.setUsername("A赵六");
        dto.setRemark("remark...");
        dto.setPassword("654321");
        dto.setDeptId(1L);
        dto.setEnable(true);
        dto.setRoleId(1L);
        dto.setEmail("@123.com");
        dto.setPhone("14315367689");
        assertTrue(service.create(dto));
        assertEquals(4, service.list(new QueryWrapper<User>().like("username", "A")).size());
    }

    @Test
    void update() {
        UserUpdateDto dto = new UserUpdateDto();
        dto.setEnable(false);
        dto.setRemark("remark...");
        dto.setPhone("16786899221");
        dto.setEmail("@abc.com");
        assertTrue(service.update(dto, user2.id));
        UserUpdateDto dto2 = new UserUpdateDto("16786899221", "@141.com", true, 1L,
                "admin", "admin", "admin", 1L, "remark...");
        UserUpdateDto dto3 = new UserUpdateDto("16783399221", "@abc.com", true, 1L,
                "admin", "admin", "admin", 1L, "remark...");
        // 不能创建其他已存在的电话和邮箱
        assertThrows(ConstraintException.class, () -> service.update(dto2, user2.getId()));
        assertThrows(ConstraintException.class, () -> service.update(dto3, user2.getId()));
    }

    @Test
    void updatePassword() {
        assertTrue(service.updatePassword("777777", user3.id));
    }


}
