package com.zehong.web.controller.supervise;

import java.util.List;

import com.zehong.system.domain.Listaw;
import com.zehong.system.domain.TDeviceInfoS;
import com.zehong.system.service.ITEmployedPeopleInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.system.domain.TDeviceInfo;
import com.zehong.system.service.ITDeviceInfoService;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;

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

    @Autowired
    private ITEmployedPeopleInfoService tEmployedPeopleInfoService;

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

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

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

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('device:device:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@RequestBody Listaw listaw)
    {
        //添加设备信息表数据 并且返回新增id
        tDeviceInfoService.insertTDeviceInfo(listaw.gettDeviceInfo());
        Long deviceId = listaw.gettDeviceInfo().getDeviceId();
        if (listaw.gettDeviceInfoS().size()!=0){
            //添加 关联设备表 设备id添加
            for (int i=0;i<listaw.gettDeviceInfoS().size();i++){
                listaw.gettDeviceInfoS().get(i).setRelationDeviceId(Math.toIntExact(deviceId));
            }
            //添加关联设备信息表数据
            tDeviceInfoService.insertDeviceDetailInfo(listaw.gettDeviceInfoS());
        }
//        TDeviceInfo tDeviceInfo = new TDeviceInfo();
//        //根据企业id查询企业名称
//        String EnterpriseName = tEmployedPeopleInfoService.selectEnterpriseName(tDeviceInfo.getBeyondEnterpriseId());
//        tDeviceInfo.setBeyondEnterpriseName(EnterpriseName);
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('device:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDeviceInfo tDeviceInfo)
    {
        //根据企业id查询企业名称
        String EnterpriseName = tEmployedPeopleInfoService.selectEnterpriseName(tDeviceInfo.getBeyondEnterpriseId());
        tDeviceInfo.setBeyondEnterpriseName(EnterpriseName);
        return toAjax(tDeviceInfoService.updateTDeviceInfo(tDeviceInfo));
    }

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


    /**
     * 获取设备详细信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:deviceDetailInfo')")
    @GetMapping(value = "/{getdeviceDetailInfo}")
    public AjaxResult getdeviceDetailInfo(@PathVariable("deviceId") Long deviceId)
    {
        List<TDeviceInfoS> tDeviceInfoS = tDeviceInfoService.getdeviceDetailInfo(deviceId);
        System.out.println("111111111");
        System.out.println(tDeviceInfoS+"111111111");
        return AjaxResult.success(tDeviceInfoS);
    }
}
