package com.xinda.data.controller;

import com.xinda.common.core.controller.BaseController;
import com.xinda.common.core.domain.AjaxResult;
import com.xinda.common.core.domain.R;
import com.xinda.common.core.page.TableDataInfo;
import com.xinda.common.utils.poi.ExcelUtil;
import com.xinda.iot.domain.DeviceModel;
import com.xinda.iot.service.IDeviceModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 产品型号Controller
 *
 * @author kerwincui
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/iot/deviceModel")
@Api(tags = "产品型号")
public class DeviceModelController extends BaseController {
    @Autowired
    private IDeviceModelService deviceModelService;


    /**
     * 查询全部产品下拉框
     * @return
     */
    @GetMapping("/getAllModel")
    public R getAllModel()
    {
        return R.ok(deviceModelService.selectDeviceModelList(new DeviceModel()));
    }



    /**
     * 查询产品型号列表
     */
    @PreAuthorize("@ss.hasPermi('iot:model:list')")
    @GetMapping("/list")
    @ApiOperation("查询产品型号列表")
    public TableDataInfo list(DeviceModel deviceModel) {
        startPage();
        List<DeviceModel> list = deviceModelService.selectDeviceModelList(deviceModel);
        return getDataTable(list);
    }

    /**
     * 导出产品型号列表
     */
    @ApiOperation("导出产品型号列表")
    @PreAuthorize("@ss.hasPermi('iot:model:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceModel deviceModel) {
        List<DeviceModel> list = deviceModelService.selectDeviceModelList(deviceModel);
        ExcelUtil<DeviceModel> util = new ExcelUtil<DeviceModel>(DeviceModel.class);
        util.exportExcel(response, list, "产品型号数据");
    }

    /**
     * 获取产品型号详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:model:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取产品型号详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(deviceModelService.selectDeviceModelById(id));
    }

    /**
     * 新增产品型号
     */
    @PreAuthorize("@ss.hasPermi('iot:model:add')")
    @PostMapping
    @ApiOperation("新增产品型号")
    public AjaxResult add(@RequestBody DeviceModel deviceModel) {
        deviceModel.setCreateTime(new Date());
        return toAjax(deviceModelService.insertDeviceModel(deviceModel));
    }

    /**
     * 修改产品型号
     */
    @PreAuthorize("@ss.hasPermi('iot:model:edit')")
    @PutMapping
    @ApiOperation("修改产品型号")
    public AjaxResult edit(@RequestBody DeviceModel deviceModel) {
        return toAjax(deviceModelService.updateDeviceModel(deviceModel));
    }

    /**
     * 删除产品型号
     */
    @PreAuthorize("@ss.hasPermi('iot:model:remove')")
    @DeleteMapping("/{ids}")
    @ApiOperation("删除产品型号")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deviceModelService.deleteDeviceModelByIds(ids));
    }
}
