package com.zcxk.charge.web.controller;


import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.core.entity.DeviceAttributeEntity;
import com.zcxk.charge.core.entity.DeviceAttributeSpecsEntity;
import com.zcxk.charge.core.entity.DeviceTypeEntity;
import com.zcxk.charge.web.service.DeviceAttributeService;
import com.zcxk.charge.web.service.DeviceAttributeSpecsService;
import com.zcxk.charge.web.service.DeviceTypeService;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.common.util.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

/**
 * 设备型号页面控制器
 * @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 (DeviceTypeEntityDto item : deviceTypeDto.getData()) {
            DeviceTypeEntity entity = new DeviceTypeEntity();
            BeanCopyUtils.copyProperties(item, entity, DeviceTypeEntity.class);
            if (deviceTypeService.findListByName(entity).size() == 0) {
                item.setCreateBy("admin");
                item.setDateCreate(new Date());
                item.setStatus(1);
                item.setUpdateBy("admin");
                item.setDateUpdate(new Date());
                deviceTypeService.save(entity);
                for (DeviceAttributeEntityDto item2 : item.getProperties()) {
                    item2.setDeviceTypeId(item.getId());
                    item2.setCreateBy("admin");
                    item2.setDateCreate(new Date());
                    item2.setStatus(1);
                    item2.setUpdateBy("admin");
                    item2.setDateUpdate(new Date());

                    DeviceAttributeEntity buteEntity = new DeviceAttributeEntity();
                    BeanCopyUtils.copyProperties(item2, buteEntity, DeviceAttributeEntity.class);
                    deviceAttributeService.save(buteEntity);
                    for (DeviceAttributeSpecsEntityDto item3 : item2.getSpecs()) {
                        item3.setAttributeId(item2.getId());
                        item3.setCreateBy("admin");
                        item3.setDateCreate(new Date());
                        item3.setStatus(1);
                        item3.setUpdateBy("admin");
                        item3.setDateUpdate(new Date());
                        DeviceAttributeSpecsEntity specsEntity = new DeviceAttributeSpecsEntity();
                        BeanCopyUtils.copyProperties(item3, specsEntity, DeviceAttributeSpecsEntity.class);
                        deviceAttributeSpecsService.save(specsEntity);
                    }
                }
            }
        }

        return AjaxMessage.success(result);
    }

}
