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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.domain.iot.ServeBuilder;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ServeDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.DataType;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Serve;
import com.zsc.edu.gateway.modules.iot.tsl.repo.ParamRepository;
import com.zsc.edu.gateway.modules.iot.tsl.repo.ServeRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.ServeService;
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 java.util.ArrayList;
import java.util.List;

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

@SpringBootTest
public class ServeServiceTest {
    @Autowired
    private ServeService service;
    @Autowired
    private ServeRepository repo;
    @Autowired
    private ParamRepository paramRepo;

    private Serve serve1;
    private Serve serve2;

    @BeforeEach
    void setUp() {
        serve1 = ServeBuilder.aServe().name("SERVE_NAME_1").identifier("测试1").productId(121L).build();
        repo.insert(serve1);
        serve2 = ServeBuilder.aServe().name("SERVE_NAME_2").identifier("测试2").productId(121L).build();
        repo.insert(serve2);
    }

    @AfterEach
    void tearDown() {
        repo.delete(new LambdaQueryWrapper<Serve>()
                .in(Serve::getName, "SERVE_NAME_1", "SERVE_NAME_2", "SERVE_3", "SERVE_UPDATE"));
        paramRepo.delete(new LambdaQueryWrapper<Param>()
                .in(Param::getName, "联合测试1"));
    }

    @Test
    void list() {
        LambdaQueryWrapper<Serve> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(2, service.list(queryWrapper.like(Serve::getName, "SERVE")).size());
        assertEquals(1, service.list(queryWrapper.eq(Serve::getName, serve1.getName())).size());
//        assertEquals(2, service.list().size());
    }

    @Test
    void createProduct() {
        ServeDto dto = new ServeDto(121L, "SERVE_3", "测试3", "备注", createParam(), createParam());
        Serve serve = service.create(dto);
        assertNotNull(serve);
        List<Serve> serves = service.list(new LambdaQueryWrapper<Serve>().like(Serve::getName, "SERVE"));
        assertEquals(3, serves.size());
        // 不能创建其他已存在的同名同代码部门
        ServeDto dto2 = new ServeDto(121L, serve2.getName(), "测试1", "备注", null, null);
        assertThrows(ConstraintException.class, () -> service.create(dto2));
    }

    @Test
    void updateProduct() {
        ServeDto dto = new ServeDto();
        dto.setName("SERVE_UPDATE");
        dto.setIdentifier("测试更新");
        dto.setProductId(121L);
        dto.setInputs(createParam());
        dto.setOutputs(createParam());
        service.update(dto, serve2.getId());
        Serve updatedServe = repo.selectOne(new LambdaQueryWrapper<Serve>().eq(Serve::getName, dto.getName()));
        assertNotNull(updatedServe);
        assertEquals("SERVE_UPDATE", updatedServe.getName());
        assertEquals(serve2.getId(), updatedServe.getId());
    }


    public List<ParamDto> createParam() {
        ParamDto paramDto = new ParamDto();
        paramDto.setName("联合测试1");
        paramDto.setType(Param.Type.OUTPUT);
        paramDto.setDataType(DataType.DATE);
        List<ParamDto> params = new ArrayList<>();
        params.add(paramDto);
        return params;
    }


}
