package com.zcxk.controller;


import com.zcxk.dto.DeviceTypeDto;
import com.zcxk.dto.TreeDataDto;
import com.zcxk.entity.DeviceAttributeEntity;
import com.zcxk.entity.DeviceAttributeSpecsEntity;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.DeviceAttributeService;
import com.zcxk.service.DeviceAttributeSpecsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.zcxk.entity.DeviceTypeEntity;
import com.zcxk.service.DeviceTypeService;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Value;

/**
 * 设备型号页面控制器
 * @author WYY
 * @date 2020-11-18 08:21
 */
@RestController
@RequestMapping("/devicetype")
@Api(tags = "设备类型管理")
public class DeviceTypeController {

    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceAttributeService deviceAttributeService;
    @Autowired
    private DeviceAttributeSpecsService deviceAttributeSpecsService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${http_pool.requesturl}")
    private  String requestUrl;

    @ApiOperation(value = "查询设备类型")
    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    @ResponseBody
    public List<TreeDataDto> getTreeData(String name) {
        List<TreeDataDto> treeDataDtos = deviceTypeService.getTreeData(name);
        return treeDataDtos;
    }

    @RequestMapping(value = "batchAdd", method = RequestMethod.POST)
    @ApiOperation(value = "批量同步设备类型")
    public AjaxMessage<Integer> batchAdd() {
        int result = 0;
        DeviceTypeDto deviceTypeDto = restTemplate.getForObject(requestUrl, DeviceTypeDto.class);
        //判断是否存在，不存在则进行添加
        for (DeviceTypeEntity item : deviceTypeDto.getData()) {
            if (deviceTypeService.findListByName(item).size() == 0) {
                item.setCreateBy("admin");
                item.setDateCreate(new Date());
                item.setStatus(1);
                item.setUpdateBy("admin");
                item.setDateUpdate(new Date());
                deviceTypeService.save(item);
                for (DeviceAttributeEntity item2 : item.getProperties()) {
                    item2.setDeviceTypeId(item.getId());
                    item2.setCreateBy("admin");
                    item2.setDateCreate(new Date());
                    item2.setStatus(1);
                    item2.setUpdateBy("admin");
                    item2.setDateUpdate(new Date());
                    deviceAttributeService.save(item2);
                    for (DeviceAttributeSpecsEntity item3 : item2.getSpecs()) {
                        item3.setAttributeId(item2.getId());
                        item3.setCreateBy("admin");
                        item3.setDateCreate(new Date());
                        item3.setStatus(1);
                        item3.setUpdateBy("admin");
                        item3.setDateUpdate(new Date());
                        deviceAttributeSpecsService.save(item3);
                    }
                }
            }
        }

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

}
