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.common.util.TreeUtil;
import com.zsc.edu.gateway.domain.system.DeptBuilder;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.modules.system.dto.DeptDto;
import com.zsc.edu.gateway.modules.system.entity.Dept;
import com.zsc.edu.gateway.modules.system.repo.DeptRepository;
import com.zsc.edu.gateway.modules.system.service.DeptService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DuplicateKeyException;

import jakarta.annotation.Resource;

import java.util.HashSet;
import java.util.List;

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

/**
 * @author pengzheng
 */
//@ActiveProfiles("test")
@SpringBootTest
class DeptServiceTest {

    @Autowired
    private DeptService service;

    @Resource
    private DeptRepository repo;

    private Dept dept1;
    private Dept dept2;

    private Dept dept3;
    private Dept dept4;

    @BeforeEach
    void setUp() {
        dept1 = DeptBuilder.aDept().name("A测试部门1").build();
        dept1.setId(121L);
        repo.insert(dept1);
        dept2 = DeptBuilder.aDept().name("A测试部门2").pid(dept1.id).build();
        dept2.setId(122L);
        repo.insert(dept2);
        dept3 = DeptBuilder.aDept().name("A测试部门3").pid(dept1.id).build();
        dept3.setId(123L);
        repo.insert(dept3);
        dept4 = DeptBuilder.aDept().name("A测试部门4").pid(dept3.id).build();
        dept4.setId(124L);
        repo.insert(dept4);
    }

    @AfterEach
    void tearDown() {
        repo.delete(new LambdaQueryWrapper<Dept>().in(Dept::getName, "A测试部门1", "A测试部门2", "A测试部门3", "A测试部门4", "A东菱经销商3", "A东菱经销商5"));
    }

    @Test
    void list() {
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(4, service.list(queryWrapper.like(Dept::getName, "A测试部门")).size());
        assertEquals(1, service.list(queryWrapper.eq(Dept::getName, dept1.getName())).size());
//        assertEquals(4, service.list().size());
    }


    @Test
    void createAdmin() {
        DeptDto dto = new DeptDto();
        dto.setName("A东菱经销商3");
        dto.setRemark("remark...");
        dto.setPid(dept1.id);
        service.create(dto);
        assertEquals(5, service.list(new LambdaQueryWrapper<Dept>().like(Dept::getName, "A")).size());
        // 不能创建其他已存在的同名同代码部门
        assertThrows(DuplicateKeyException.class, () -> service.save(dept1));
    }


    @Test
    void updateAdmin() {
        DeptDto dto = new DeptDto();
        dto.setName("A东菱经销商5");
        dto.setRemark("remark...");
        assertTrue(service.edit(dto, dept2.id));
        Dept tmp = service.getOne(new LambdaQueryWrapper<Dept>().eq(Dept::getName, dto.getName()));
        assertEquals(tmp.getName(), dto.getName());
        assertEquals(tmp.getId(), dept2.id);
        // 不能改为其他已存在的同名同代码部门
        assertThrows(ConstraintException.class,
                () -> service.edit(new DeptDto(dept3.getName(), "remark",null), dept2.id));
    }


    @Test
    void tree() {
        List<Dept> results = service.listTree(dept3.id);
        System.out.println(results);
        assertEquals(1, results.size());
    }

    @Test
    void findTreeChild() {
        List<Dept> childNode = service.listTree(dept3.id);
        Dept dept = childNode.get(0);
        System.out.println(dept.id);
        assertEquals(dept3.id, dept.id);
    }

}
