package com.ruoyi.traceability.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.traceability.domain.BaseDeviceInfo;
import com.ruoyi.traceability.service.IBaseDeviceInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 设备信息Controller
 * 
 * @author wenjy
 * @date 2022-11-23
 */
@Controller
@RequestMapping("/traceability/deviceinfo")
public class BaseDeviceInfoController extends BaseController
{
    private String prefix = "traceability/deviceinfo";

    @Autowired
    private IBaseDeviceInfoService baseDeviceInfoService;

    @RequiresPermissions("traceability:deviceinfo:view")
    @GetMapping()
    public String deviceinfo()
    {
        return prefix + "/deviceinfo";
    }

    /**
     * 查询设备信息树列表
     */
    @RequiresPermissions("traceability:deviceinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BaseDeviceInfo> list(BaseDeviceInfo baseDeviceInfo)
    {
        List<BaseDeviceInfo> list = baseDeviceInfoService.selectBaseDeviceInfoList(baseDeviceInfo);
        return list;
    }

    /**
     * 导出设备信息列表
     */
    @RequiresPermissions("traceability:deviceinfo:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseDeviceInfo baseDeviceInfo)
    {
        List<BaseDeviceInfo> list = baseDeviceInfoService.selectBaseDeviceInfoList(baseDeviceInfo);
        ExcelUtil<BaseDeviceInfo> util = new ExcelUtil<BaseDeviceInfo>(BaseDeviceInfo.class);
        return util.exportExcel(list, "设备信息数据");
    }

    /**
     * 新增设备信息
     */
    @GetMapping(value = { "/add/{deviceId}", "/add/" })
    public String add(@PathVariable(value = "deviceId", required = false) Long deviceId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(deviceId))
        {
            mmap.put("baseDeviceInfo", baseDeviceInfoService.selectBaseDeviceInfoByDeviceId(deviceId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存设备信息
     */
    @RequiresPermissions("traceability:deviceinfo:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseDeviceInfo baseDeviceInfo)
    {
        return toAjax(baseDeviceInfoService.insertBaseDeviceInfo(baseDeviceInfo));
    }

    /**
     * 修改设备信息
     */
    @RequiresPermissions("traceability:deviceinfo:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        BaseDeviceInfo baseDeviceInfo = baseDeviceInfoService.selectBaseDeviceInfoByDeviceId(deviceId);
        mmap.put("baseDeviceInfo", baseDeviceInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备信息
     */
    @RequiresPermissions("traceability:deviceinfo:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseDeviceInfo baseDeviceInfo)
    {
        return toAjax(baseDeviceInfoService.updateBaseDeviceInfo(baseDeviceInfo));
    }

    /**
     * 删除
     */
    @RequiresPermissions("traceability:deviceinfo:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{deviceId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deviceId") Long deviceId)
    {
        return toAjax(baseDeviceInfoService.deleteBaseDeviceInfoByDeviceId(deviceId));
    }

    /**
     * 选择设备信息树
     */
    @GetMapping(value = { "/selectDeviceinfoTree/{deviceId}", "/selectDeviceinfoTree/" })
    public String selectDeviceinfoTree(@PathVariable(value = "deviceId", required = false) Long deviceId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(deviceId))
        {
            mmap.put("baseDeviceInfo", baseDeviceInfoService.selectBaseDeviceInfoByDeviceId(deviceId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载设备信息树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = baseDeviceInfoService.selectBaseDeviceInfoTree();
        return ztrees;
    }
}
