package com.tuoheng.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.entity.Channel;
import com.tuoheng.admin.entity.Leg;
import com.tuoheng.admin.mapper.ChannelMapper;
import com.tuoheng.admin.mapper.LegMapper;
import com.tuoheng.admin.request.dept.AddDeptRequest;
import com.tuoheng.admin.dto.ChannelLegDto;
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 java.util.ArrayList;
import java.util.List;
import java.util.Locale;

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

    @Autowired
    private IDeptService deptService;

    @Autowired
    private QueryListTreeService queryListTreeService;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private LegMapper legMapper;

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

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

    @Test
    public void testGetOneById() {
        String url = String.format(Locale.ENGLISH, "%s/api/web/serviceInst/%s/application", dspDomainUrl, dspServiceInstId);

        System.out.println("url = " + 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<ChannelLegDto> channelLegDtoList = build();
        addDeptRequest.setChannelLegDtoList(channelLegDtoList);

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

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

    private List<ChannelLegDto> build() {
        List<ChannelLegDto> channelLegDtoList = new ArrayList<>();
        List<Channel> channelList = channelMapper.selectList(null);
        List<Leg> legList = legMapper.selectList(null);
        ChannelLegDto channelLegDto = null;
        List<Leg> legListTmp;
        for (Channel channel : channelList) {
            channelLegDto = new ChannelLegDto();
            legListTmp = new ArrayList<>();
            for (Leg leg : legList) {
                if (leg.getChannelId().equals(channel.getId())) {
                    legListTmp.add(leg);
                }
            }
            channelLegDto.setChannel(channel);
            channelLegDto.setLegList(legListTmp);
            channelLegDtoList.add(channelLegDto);
        }
        return channelLegDtoList;
    }

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

        ChannelLegDto channelLegDto = null;

        Channel channel;
        Leg leg;
        List<Leg> legList;
        for (int i = 1; i < 4; i++) {
            channelLegDto = new ChannelLegDto();

            channel = new Channel();
            channel.setId(i + 1 + "");
            channel.setCode("G2" + 1);

            legList = new ArrayList<>();
            for (int j = 1; j < 4; j++) {
                leg = new Leg();
                leg.setId(i + j + "");
                leg.setChannelId(j + "");
                leg.setLegRange(channel.getCode() + "-" + j);
                legList.add(leg);
            }
            channelLegDto.setChannel(channel);
            channelLegDto.setLegList(legList);

            list.add(channelLegDto);
        }
        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<ChannelLegDto> channelLegDtoList = new ArrayList<>();

        ChannelLegDto channelLegDto1 = this.buildChannelLegDto1();
        ChannelLegDto channelLegDto2 = this.buildChannelLegDto2();

        channelLegDtoList.add(channelLegDto1);
        channelLegDtoList.add(channelLegDto2);

        oldEeditDeptRequest.setChannelLegDtoList(channelLegDtoList);

        return oldEeditDeptRequest;
    }

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

        List<ChannelLegDto> channelLegDtoList = new ArrayList<>();

        ChannelLegDto channelLegDto1 = this.buildChannelLegDto1();
        ChannelLegDto channelLegDto3 = this.buildChannelLegDto3();

        channelLegDtoList.add(channelLegDto1);
        channelLegDtoList.add(channelLegDto3);

        newEditDeptRequest.setChannelLegDtoList(channelLegDtoList);

        return newEditDeptRequest;
    }

    private ChannelLegDto buildChannelLegDto1() {
        ChannelLegDto channelLegDto = new ChannelLegDto();
        Channel channel = new Channel();
        channel.setId("1");
        List<Leg> legList = new ArrayList<>();
        Leg leg1 = new Leg();
        leg1.setId("1");
        Leg leg2 = new Leg();
        leg2.setId("2");
        Leg leg3 = new Leg();
        leg3.setId("3");

        legList.add(leg1);
        legList.add(leg2);
        legList.add(leg3);

        channelLegDto.setChannel(channel);
        channelLegDto.setLegList(legList);

        return channelLegDto;
    }

    private ChannelLegDto buildChannelLegDto2() {
        ChannelLegDto channelLegDto = new ChannelLegDto();
        Channel channel = new Channel();
        channel.setId("2");
        List<Leg> legList = new ArrayList<>();
        Leg leg1 = new Leg();
        leg1.setId("4");
        Leg leg2 = new Leg();
        leg2.setId("5");
        Leg leg3 = new Leg();
        leg3.setId("6");

        legList.add(leg1);
        legList.add(leg2);
        legList.add(leg3);

        channelLegDto.setChannel(channel);
        channelLegDto.setLegList(legList);

        return channelLegDto;
    }

    private ChannelLegDto buildChannelLegDto3() {
        ChannelLegDto channelLegDto = new ChannelLegDto();
        Channel channel = new Channel();
        channel.setId("4");
        List<Leg> legList = new ArrayList<>();
        Leg leg = new Leg();
        leg.setId("10");
        legList.add(leg);

        channelLegDto.setChannel(channel);
        channelLegDto.setLegList(legList);

        return channelLegDto;
    }

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

    }


}
