package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DeviceModelDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ModelFieldDO;
import cn.iocoder.yudao.module.system.service.biz.DeviceModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2024/10/16  9:22
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 设备模型")
@RestController
@RequestMapping("/device/model")
@Validated
public class DeviceModelController {

    @Resource
    private DeviceModelService modelService;

    @PostMapping("/save")
    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('device:model:save')")
    public CommonResult<Long> saveModel(@Valid @RequestBody DeviceModelSaveReqVO createReqVO) {
        return success(modelService.saveModel(createReqVO));
    }

//    @PutMapping("/update")
//    @Operation(summary = "更新设备模型")
//    @PreAuthorize("@ss.hasPermission('device:model:update')")
//    public CommonResult<Boolean> updateModel(@Valid @RequestBody DeviceModelSaveReqVO updateReqVO) {
//        modelService.updateModel(updateReqVO);
//        return success(true);
//    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:model:delete')")
    public CommonResult<Boolean> deleteModel(@RequestParam("id") Long id) {
        modelService.deleteModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:model:query')")
    public CommonResult<DeviceModelRespVO> getModel(@RequestParam("id") Long id) {
        return success(modelService.getModel(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备模型分页")
//    @PreAuthorize("@ss.hasPermission('device:model:query')")
    public CommonResult<PageResult<DeviceModelRespVO>> getModelPage(@Valid DeviceModelPageReqVO pageReqVO) {
        PageResult<DeviceModelDO> pageResult = modelService.getModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceModelRespVO.class));
    }

//    @GetMapping("/getSubList")
//    @Operation(summary = "获取子设备列表")
//    @PreAuthorize("@ss.hasPermission('device:model:query')")
//    public CommonResult<List<DeviceModelDO>> getSubList(@RequestParam("parentId") Long parentId) {
//        return success(modelService.getSubList(parentId));
//    }

//    @GetMapping("/getModelFielList")
//    @Operation(summary = "获取子设备字段")
//    @PreAuthorize("@ss.hasPermission('device:model:query')")
//    public CommonResult<List<ModelFieldDO>> getModelFielList(@RequestParam("id") Long id) {
//        return success(modelService.getModelFielList(id));
//    }

    @GetMapping("/getOriginalModelFielList")
    @Operation(summary = "获取设备初始字段")
    @PreAuthorize("@ss.hasPermission('device:model:query')")
    // 类型，0-健康，1-监控，2-睡眠，3-跌倒，4-安防，5-腕表，6-环境
    public CommonResult<List<ModelTableFieldVO>> getOriginalModelFielList(@RequestParam("type") Integer type) {
        return success(modelService.getOriginalModelFielList(type));
    }

    @GetMapping("/getCount")
    @Operation(summary = "统计数量")
    @PreAuthorize("@ss.hasPermission('device:model:getCount')")
    public CommonResult<DeviceModelCountVO> getCount(@RequestParam("deviceCategoryId") Long deviceCategoryId) {
        return success(modelService.getCount(deviceCategoryId));
    }


    @GetMapping("/getManufacturerModelList")
    @Operation(summary = "厂商模型下拉")
//    @PreAuthorize("@ss.hasPermission('device:model:query')")
    public CommonResult<List<ManufacturerModelRespVO>> getManufacturerModelList() {
        return success(modelService.getManufacturerModelList());
    }

}
