package com.sptg.ad.service;

import com.sptg.ad.api.entity.DeviceAdvertStatistics;
import com.sptg.ad.biz.AdApplication;
import com.sptg.ad.biz.service.DeviceAdvertStatisticsService;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.constant.HttpConstant;
import com.sptg.common.core.interfaces.VoidInterface;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *            
 *               
 * description:
 */
@Slf4j
@RunWith(SpringRunner.class)
@SuppressWarnings(value = "all")
@SpringBootTest(classes = AdApplication.class)
public class DeviceAdvertStatisticsTest {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DeviceAdvertStatisticsService deviceAdvertStatisticsService;

    @Test
    public void run(){
        List<Integer> taskTypeList = Arrays.asList(new Integer[]{ 1,2 });
        this.pagingQuery(device -> {
            List<DeviceAdvertStatistics> list = new ArrayList<>();
            taskTypeList.forEach(taskType -> {
                DeviceAdvertStatistics statistics = this.deviceAdvertStatisticsService.selectCurrentMonthAdvertStatistics(device.getId(), taskType);
                statistics.setDeviceInstallPath(device.getInstallPath());
                statistics.setDeviceSerialNumber(device.getSerialNumber());
                statistics.setDeviceName(device.getName());
                statistics.setRegionProvince(device.getRegionProvince());
                statistics.setRegionCity(device.getRegionCity());
                statistics.setRegionArea(device.getRegionArea());
                statistics.setXqId(device.getXqId());
                statistics.setOrgPosition(device.getOrgPosition());
                list.add(statistics);
            });
            this.deviceAdvertStatisticsService.updateBatchById(list);
        });
    }

    private void pagingQuery(VoidInterface<Device> voidInterface){
        //  分页查询上个月的设备统计记录
        DeviceDto dto = new DeviceDto();
        dto.setOrgPosition("");
        PageBean<Device, DeviceDto> pageBean = new PageBean<>(dto);
        pageBean.setSize(100);
        int pageNo = 0;
        while (true){
            ++ pageNo;
            pageBean.setCurrent(pageNo);

            String url = "http://127.0.0.1:8081/device/pagingQuery";
            HttpHeaders headers = new HttpHeaders();
            headers.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
            headers.add("Content-Type",HttpConstant.APPLICATION_JSON);
            HttpEntity<PageBean<Device, DeviceDto>> requestEntity = new HttpEntity<>(pageBean, headers);
            Map map = this.restTemplate.postForObject(url, requestEntity, Map.class);

            if (null != map && map.get("code").equals("200")){
                Map data = (Map) map.get("data");
                List<Device> list = JsonUtils.jsonToList(JsonUtils.objectToJson(data.get("records")), Device.class);
                if (null == list || 0 == list.size()){
                    break;
                }
                list.stream()
                        .filter(device -> null != device.getXqId())
                        .forEach(voidInterface::run);
            }else {
                break;
            }
        }
    }

}
