package com.willalp.web.controller.device;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.device.domain.NetworkConfig;
import com.willalp.device.service.INetworkConfigService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

/**
 * 网络配置 Controller
 *
 * @author willalp
 * @date 2021-11-23
 */
@RestController
@RequestMapping("/device/networkconfig")
public class NetworkConfigController extends BaseController
{
    @Autowired
    private INetworkConfigService networkConfigService;

    /**
     * 查询网络配置 列表
     */
    @PreAuthorize("@ss.hasPermi('device:networkconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(NetworkConfig networkConfig)
    {
        startPage();
        List<NetworkConfig> list = networkConfigService.selectNetworkConfigList(networkConfig);
        return getDataTable(list);
    }

    /**
     * 导出网络配置 列表
     */
    @PreAuthorize("@ss.hasPermi('device:networkconfig:export')")
    @Log(title = "网络配置 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(NetworkConfig networkConfig)
    {
        List<NetworkConfig> list = networkConfigService.selectNetworkConfigList(networkConfig);
        ExcelUtil<NetworkConfig> util = new ExcelUtil<NetworkConfig>(NetworkConfig.class);
        return util.exportExcel(list, "网络配置 数据");
    }

    /**
     * 获取网络配置 详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:networkconfig:query')")
    @GetMapping(value = "/{networkConfigId}")
    public AjaxResult getInfo(@PathVariable("networkConfigId") String networkConfigId)
    {
        return AjaxResult.success(networkConfigService.getById(networkConfigId));
    }

    /**
     * 新增网络配置
     */
    @PreAuthorize("@ss.hasPermi('device:networkconfig:add')")
    @Log(title = "网络配置 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NetworkConfig networkConfig)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            NetworkConfig byId = networkConfigService.getById(idStr);
            if (null == byId) {
                networkConfig.setNetworkConfigId(idStr);
                break;
            }
        }
        networkConfig.setCreateBy(SecurityUtils.getUsername());
        networkConfig.setCreateTime(DateUtils.getNowDate());
        return toAjax(networkConfigService.save(networkConfig));
    }

    /**
     * 修改网络配置
     */
    @PreAuthorize("@ss.hasPermi('device:networkconfig:edit')")
    @Log(title = "网络配置 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NetworkConfig networkConfig)
    {
        networkConfig.setUpdateBy(SecurityUtils.getUsername());
        networkConfig.setUpdateTime(DateUtils.getNowDate());
        return toAjax(networkConfigService.updateById(networkConfig));
    }

    /**
     * 删除网络配置
     */
    @PreAuthorize("@ss.hasPermi('device:networkconfig:remove')")
    @Log(title = "网络配置 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{networkConfigIds}")
    public AjaxResult remove(@PathVariable String[] networkConfigIds)
    {
        return toAjax(networkConfigService.removeByIds(Arrays.asList(networkConfigIds)));
    }
}
