package com.willalp.web.controller.system;

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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
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.system.domain.DeviceRelation;
import com.willalp.system.service.IDeviceRelationService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

/**
 * 设备绑定1Controller
 *
 * @author songyu
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/deviceRelation/relation")
public class DeviceRelationController extends BaseController
{
    @Autowired
    private IDeviceRelationService deviceRelationService;

    /**
     * 查询设备绑定1列表
     */
    @PreAuthorize("@ss.hasPermi('deviceRelation:relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceRelation deviceRelation)
    {
        startPage();
        List<DeviceRelation> list = deviceRelationService.selectDeviceRelationList(deviceRelation);
        return getDataTable(list);
    }

    /**
     * 导出设备绑定1列表
     */
    @PreAuthorize("@ss.hasPermi('deviceRelation:relation:export')")
    @Log(title = "设备绑定1", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DeviceRelation deviceRelation)
    {
        List<DeviceRelation> list = deviceRelationService.selectDeviceRelationList(deviceRelation);
        ExcelUtil<DeviceRelation> util = new ExcelUtil<DeviceRelation>(DeviceRelation.class);
        return util.exportExcel(list, "设备绑定1数据");
    }

    /**
     * 获取设备绑定1详细信息
     */
    @PreAuthorize("@ss.hasPermi('deviceRelation:relation:query')")
    @GetMapping(value = "/{relationId}")
    public AjaxResult getInfo(@PathVariable("relationId") String relationId)
    {
        return AjaxResult.success(deviceRelationService.getById(relationId));
    }

    /**
     * 新增设备绑定1
     */
    @PreAuthorize("@ss.hasPermi('deviceRelation:relation:add')")
    @Log(title = "设备绑定1", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceRelation deviceRelation)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            DeviceRelation byId = deviceRelationService.getById(Long.valueOf(idStr));
            if (null == byId) {
                deviceRelation.setRelationId(idStr);
                break;
            }
        }
        return toAjax(deviceRelationService.save(deviceRelation));
    }

    /**
     * 修改设备绑定1
     */
    @PreAuthorize("@ss.hasPermi('deviceRelation:relation:edit')")
    @Log(title = "设备绑定1", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceRelation deviceRelation)
    {
        return toAjax(deviceRelationService.updateById(deviceRelation));
    }

    /**
     * 删除设备绑定1
     */
    @PreAuthorize("@ss.hasPermi('deviceRelation:relation:remove')")
    @Log(title = "设备绑定1", businessType = BusinessType.DELETE)
	@DeleteMapping("/{relationIds}")
    public AjaxResult remove(@PathVariable String[] relationIds)
    {
        return toAjax(deviceRelationService.removeByIds(Arrays.asList(relationIds)));
    }
}
