package com.zehong.web.controller.device;

import com.github.pagehelper.PageInfo;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.core.page.TableDataInfo;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.system.domain.EquipmentForm;
import com.zehong.system.domain.TDeviceInfo;
import com.zehong.system.domain.TDeviceInformation;
import com.zehong.system.domain.vo.DeviceInfoVo;
import com.zehong.system.service.ITDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备信息Controller
 *
 * @author zehong
 * @date 2021-07-09
 */
@RestController
@RequestMapping("/device/deviceInfo")
public class TDeviceInfoController extends BaseController {
    @Autowired
    private ITDeviceInfoService tDeviceInfoService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:deviceInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDeviceInfo tDeviceInfo) {
        startPage();
        PageInfo<DeviceInfoVo> page = tDeviceInfoService.selectTDeviceInfoPage(tDeviceInfo);
        return getDataTable(page);
    }

    /**
     * 获取所有设备信息
     *
     * @param tDeviceInfo
     * @return
     */
    @GetMapping("/deviceListInfo")
    public AjaxResult deviceListInfo(TDeviceInfo tDeviceInfo) {
        return AjaxResult.success(tDeviceInfoService.selectTDeviceInfoList(tDeviceInfo));
    }

    /**
     * 获取设备下拉树列表
     */
    @PutMapping("/deviceTree")
    public AjaxResult deviceTree(@RequestBody Map<Object, List> param) {
        return AjaxResult.success(tDeviceInfoService.buildDeviceTreeSelect(param));
    }

    /**
     * 获取设备树
     */
    @GetMapping("/deviceNodeTree")
    public AjaxResult deviceNodeTree() throws Exception {
        List<Map<Object, Object>> list = null;
        try {
            list = tDeviceInfoService.buildDeviceTree();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("获取设备列表失败");
        }

        return AjaxResult.success(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:deviceInfo:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TDeviceInfo tDeviceInfo) {
        List<DeviceInfoVo> list = tDeviceInfoService.selectTDeviceInfoList(tDeviceInfo);
        ExcelUtil<DeviceInfoVo> util = new ExcelUtil<DeviceInfoVo>(DeviceInfoVo.class);
        return util.exportExcel(list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:deviceInfo:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") int deviceId) {
        return AjaxResult.success(tDeviceInfoService.selectTDeviceInfoById(deviceId));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('device:deviceInfo:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDeviceInfo tDeviceInfo) {
        int result = tDeviceInfoService.insertTDeviceInfo(tDeviceInfo);
        if (result <= 0) {
            new Exception("设备信息插入失败");
        }
        return AjaxResult.success(tDeviceInfo.getDeviceId());
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('device:deviceInfo:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDeviceInfo tDeviceInfo) {
        System.out.println(tDeviceInfo.getEquipmentList());
        /**
         * 先执行删除 在执行添加  不能删除任何一个
         */
        /**删除下级设备信息*/
        tDeviceInfoService.deleteEquipmentList(tDeviceInfo.getEquipmentList(),tDeviceInfo.getDeviceId());

        /**新增下级设备信息*/
        tDeviceInfoService.insertEquipmentList(tDeviceInfo.getEquipmentList(),tDeviceInfo.getDeviceId());
        return toAjax(tDeviceInfoService.updateTDeviceInfo(tDeviceInfo));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('device:deviceInfo:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable int[] deviceIds) {
        return toAjax(tDeviceInfoService.deleteTDeviceInfoByIds(deviceIds));
    }

    /**
     * 统计各设备类型的设备总数
     */
    //@PreAuthorize("@ss.hasPermi('device:deviceInfo:countDeviceByType')")
    @GetMapping("/countDeviceByType")
    public AjaxResult countDeviceByType() {
        List<Map<Object, Object>> list = tDeviceInfoService.countDeviceByType();
        return AjaxResult.success(list);
    }

    @PutMapping("/devicefeed")
    public AjaxResult devicefeed(@RequestBody Map<String, Object> param) {
        return AjaxResult.success(tDeviceInfoService.devicefeed(param));
    }

    /**
     * 查询下级设备所有信息
     * @return
     */
    @GetMapping("/selectDevice")
    public AjaxResult selectDevice(Integer id) {
      return   AjaxResult.success(tDeviceInfoService.selectDevice(id));
    }


    /**
     * 根据设备id查询最新的设备信息数据
     * @param deviceId
     * @return
     */
    @GetMapping("/getInformation")
    public AjaxResult getInformation(Integer deviceId){
      return  AjaxResult.success(tDeviceInfoService.getInformations(deviceId));
    }



}