package com.ruoyi.business.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.HwDeviceModeFunction;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
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.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.HwDeviceMode;
import com.ruoyi.business.service.IHwDeviceModeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备模型Controller
 *
 * @author xins
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/deviceMode")
public class HwDeviceModeController extends BaseController {
    @Autowired
    private IHwDeviceModeService hwDeviceModeService;

    /**
     * 查询设备模型列表
     */
    @RequiresPermissions("business:deviceMode:list")
    @GetMapping("/list")
    public TableDataInfo list(HwDeviceMode hwDeviceMode) {
        startPage();
        List<HwDeviceMode> list = hwDeviceModeService.selectHwDeviceModeList(hwDeviceMode);
        return getDataTable(list);
    }

    /**
     * 导出设备模型列表
     */
    @RequiresPermissions("business:deviceMode:export")
    @Log(title = "设备模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HwDeviceMode hwDeviceMode) {
        List<HwDeviceMode> list = hwDeviceModeService.selectHwDeviceModeList(hwDeviceMode);
        ExcelUtil<HwDeviceMode> util = new ExcelUtil<HwDeviceMode>(HwDeviceMode.class);
        util.exportExcel(response, list, "设备模型数据");
    }

    /**
     * 获取设备模型详细信息
     */
    @RequiresPermissions("business:deviceMode:query")
    @GetMapping(value = "/{deviceModeId}")
    public AjaxResult getInfo(@PathVariable("deviceModeId") Long deviceModeId) {
        HwDeviceMode hwDeviceMode = hwDeviceModeService.selectHwDeviceModeByDeviceModeId(deviceModeId);
        List<HwDeviceModeFunction> hwDeviceModeFunctions = hwDeviceMode.getHwDeviceModeFunctionList();
        hwDeviceMode.setHwDeviceModeFunctionList(null);
        Map<String, List<HwDeviceModeFunction>> hwDeviceModeFunctionMap = hwDeviceModeFunctions.stream()
                .collect(Collectors.groupingBy(HwDeviceModeFunction::getFunctionMode));
        Map<String, Object> map = new HashMap<>();
        map.put("deviceMode", hwDeviceMode);
        map.put("deviceModeFunctionMap", hwDeviceModeFunctionMap);
        return success(map);
    }

    /**
     * 新增设备模型
     */
    @RequiresPermissions("business:deviceMode:add")
    @Log(title = "设备模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HwDeviceMode hwDeviceMode) {
        return toAjax(hwDeviceModeService.insertHwDeviceMode(hwDeviceMode));
    }

    /**
     * 修改设备模型
     */
    @RequiresPermissions("business:deviceMode:edit")
    @Log(title = "设备模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HwDeviceMode hwDeviceMode) {
        return toAjax(hwDeviceModeService.updateHwDeviceMode(hwDeviceMode));
    }

    /**
     * 删除设备模型
     */
    @RequiresPermissions("business:deviceMode:remove")
    @Log(title = "设备模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceModeIds}")
    public AjaxResult remove(@PathVariable Long[] deviceModeIds) {
        return toAjax(hwDeviceModeService.deleteHwDeviceModeByDeviceModeIds(deviceModeIds));
    }

}
