package com.tuoheng.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.entity.Section;
import com.tuoheng.admin.mapper.RoadInformationMapper;
import com.tuoheng.admin.mapper.SectionMapper;
import com.tuoheng.admin.request.dept.AddDeptRequest;
import com.tuoheng.admin.dto.RoadSectionDto;
import com.tuoheng.admin.request.dept.EditDeptRequest;
import com.tuoheng.admin.service.dept.IDeptService;
import com.tuoheng.admin.service.dept.query.QueryListTreeService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DeptServiceTest {

    @Autowired
    private IDeptService deptService;

    @Autowired
    private QueryListTreeService queryListTreeService;

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Value("${tuoheng.dsp-domain-url:}")
    private String dspDomainUrl;

    @Value("${tuoheng.dsp-service-inst-id:}")
    private String dspServiceInstId;

    @Test
    public void testGetOneById() {
        System.out.println("url = ");
    }

    @Test
    public void testGetListTree() {
        JsonResult result = queryListTreeService.getListTree();

        log.info("-----> 获取部门树形列表：{}", result);
    }

    @Test
    public void testAdd() {
        AddDeptRequest addDeptRequest = new AddDeptRequest();
        addDeptRequest.setPid("774630400fa782df1c0b38c582a9c468");
        addDeptRequest.setName("拓恒-技术部-测试");

        List<RoadSectionDto> roadSectionDtoList = build();
        addDeptRequest.setRoadSectionDtoList(roadSectionDtoList);

        JSONObject json = (JSONObject) JSONObject.toJSON(addDeptRequest);
        log.info("-----> 新增部门：json={}", json);

        JsonResult result = deptService.insert(addDeptRequest);
        log.info("-----> 新增部门：{}", result);
    }

    private List<RoadSectionDto> build() {
        List<RoadSectionDto> roadSectionDtoList = new ArrayList<>();
        List<RoadInformation> roadInformationList = roadInformationMapper.selectList(null);
        List<Section> sectionList = sectionMapper.selectList(null);
        RoadSectionDto roadSectionDto = null;
        List<Section> sectionListTmp;
        for (RoadInformation roadInformation : roadInformationList) {
            roadSectionDto = new RoadSectionDto();
            sectionListTmp = new ArrayList<>();
            for (Section section : sectionList) {
                if (section.getRoadId().equals(roadInformation.getId())) {
                    sectionListTmp.add(section);
                }
            }
            roadSectionDto.setRoad(roadInformation);
            roadSectionDto.setSectionList(sectionListTmp);
            roadSectionDtoList.add(roadSectionDto);
        }
        return roadSectionDtoList;
    }

    private List<RoadSectionDto> build2() {
        List<RoadSectionDto> list = new ArrayList<>();

        RoadSectionDto roadSectionDto = null;

        RoadInformation road;
        Section section;
        List<Section> sectionList;
        for (int i = 1; i < 4; i++) {
            roadSectionDto = new RoadSectionDto();

            road = new RoadInformation();
            road.setId(i + 1 + "");
            road.setCode("G2" + 1);

            sectionList = new ArrayList<>();
            for (int j = 1; j < 4; j++) {
                section = new Section();
                section.setId(i + j + "");
                section.setRoadId(j + "");
                section.setSectionRange(road.getCode() + "-" + j);
                sectionList.add(section);
            }
            roadSectionDto.setRoad(road);
            roadSectionDto.setSectionList(sectionList);

            list.add(roadSectionDto);
        }
        return list;
    }


    @Test
    public void testEdit() {
        EditDeptRequest oldEeditDeptRequest = this.buildOldEditDeptRequest();

        EditDeptRequest newEditDeptRequest = this.buildNewEditDeptRequest();

        JSONObject oldEeditDeptRequestJson = (JSONObject) JSONObject.toJSON(oldEeditDeptRequest);
        log.info("-----> 修改部门：json={}", oldEeditDeptRequestJson);


        JSONObject newEeditDeptRequestJson = (JSONObject) JSONObject.toJSON(oldEeditDeptRequest);
        log.info("-----> 修改部门：json={}", oldEeditDeptRequestJson);

//        JsonResult result = deptService.update(oldEeditDeptRequest, newEditDeptRequest);
//        log.info("-----> 修改部门：{}", result);
    }

    private EditDeptRequest buildOldEditDeptRequest() {
        EditDeptRequest oldEeditDeptRequest = new EditDeptRequest();
        oldEeditDeptRequest.setId("576f0c83faa8415148c5ee24d7da5fd3");
        oldEeditDeptRequest.setPid("9e7f336d4fe1751fda497ba4cf860f37");
        oldEeditDeptRequest.setName("技术部");

        List<RoadSectionDto> roadSectionDtoList = new ArrayList<>();

        RoadSectionDto roadSectionDto1 = this.buildRoadSectionDto1();
        RoadSectionDto roadSectionDto2 = this.buildRoadSectionDto2();

        roadSectionDtoList.add(roadSectionDto1);
        roadSectionDtoList.add(roadSectionDto2);

        oldEeditDeptRequest.setRoadSectionDtoList(roadSectionDtoList);

        return oldEeditDeptRequest;
    }

    private EditDeptRequest buildNewEditDeptRequest() {
        EditDeptRequest newEditDeptRequest = new EditDeptRequest();
        newEditDeptRequest.setId("576f0c83faa8415148c5ee24d7da5fd3");
        newEditDeptRequest.setPid("9e7f336d4fe1751fda497ba4cf860f37");
        newEditDeptRequest.setName("技术部new");

        List<RoadSectionDto> roadSectionDtoList = new ArrayList<>();

        RoadSectionDto roadSectionDto1 = this.buildRoadSectionDto1();
        RoadSectionDto roadSectionDto3 = this.buildRoadSectionDto3();

        roadSectionDtoList.add(roadSectionDto1);
        roadSectionDtoList.add(roadSectionDto3);

        newEditDeptRequest.setRoadSectionDtoList(roadSectionDtoList);

        return newEditDeptRequest;
    }

    private RoadSectionDto buildRoadSectionDto1() {
        RoadSectionDto roadSectionDto = new RoadSectionDto();
        RoadInformation roadInformation = new RoadInformation();
        roadInformation.setId("1");
        List<Section> sectionList = new ArrayList<>();
        Section section1 = new Section();
        section1.setId("1");
        Section section2 = new Section();
        section2.setId("2");
        Section section3 = new Section();
        section3.setId("3");

        sectionList.add(section1);
        sectionList.add(section2);
        sectionList.add(section3);

        roadSectionDto.setRoad(roadInformation);
        roadSectionDto.setSectionList(sectionList);

        return roadSectionDto;
    }

    private RoadSectionDto buildRoadSectionDto2() {
        RoadSectionDto roadSectionDto = new RoadSectionDto();
        RoadInformation roadInformation = new RoadInformation();
        roadInformation.setId("2");
        List<Section> sectionList = new ArrayList<>();
        Section section1 = new Section();
        section1.setId("4");
        Section section2 = new Section();
        section2.setId("5");
        Section section3 = new Section();
        section3.setId("6");

        sectionList.add(section1);
        sectionList.add(section2);
        sectionList.add(section3);

        roadSectionDto.setRoad(roadInformation);
        roadSectionDto.setSectionList(sectionList);

        return roadSectionDto;
    }

    private RoadSectionDto buildRoadSectionDto3() {
        RoadSectionDto roadSectionDto = new RoadSectionDto();
        RoadInformation roadInformation = new RoadInformation();
        roadInformation.setId("4");
        List<Section> sectionList = new ArrayList<>();
        Section section = new Section();
        section.setId("10");
        sectionList.add(section);

        roadSectionDto.setRoad(roadInformation);
        roadSectionDto.setSectionList(sectionList);

        return roadSectionDto;
    }

    @Test
    public void testDelete() {
        String id = "1";
        JsonResult result = deptService.deleteById(id);
        log.info("-----> 删除部门：{}", result);

    }


}
