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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DeviceProductDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DeviceWarnUserDO;
import cn.iocoder.yudao.module.system.service.biz.DeviceProductService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2024/10/28  10:42
 * @Description:
 * @Version: 4.0
 */

@Tag(name = "管理后台 - 设备产品")
@RestController
@RequestMapping("/device/product")
@Validated
public class DeviceProductController {

    @Resource
    private DeviceProductService productService;

    @PostMapping("/create")
    @Operation(summary = "创建设备产品")
    @PreAuthorize("@ss.hasPermission('device:product:create')")
    public CommonResult<Long> createProduct(@Valid @RequestBody DeviceProductSaveReqVO createReqVO) {
        return success(productService.createProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备产品")
    @PreAuthorize("@ss.hasPermission('device:product:update')")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody DeviceProductSaveReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/getDetail")
    @Operation(summary = "获得设备产品详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:product:query')")
    public CommonResult<DeviceProductDetailVO> getDetail(@RequestParam("id") Long id) {
        return CommonResult.success(productService.getDetail(id));
    }


    @GetMapping("/getWarnUserList")
    @Operation(summary = "获取设备报警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:product:getWarnUserList')")
    public CommonResult<List<DeviceWarnUserDO>> getWarnUserList(@RequestParam("id") Long id) {
        return CommonResult.success(productService.getWarnUserList(id));
    }

    @PostMapping("/updateWarnUser")
    @Operation(summary = "修改报警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:product:updateWarnUser')")
    public CommonResult<Boolean> updateWarnUser(@Valid @RequestBody DeviceWarnUserUpdateReqVO deviceWarnUserUpdateReqVO) {
        return CommonResult.success(productService.updateWarnUser(deviceWarnUserUpdateReqVO));
    }

    @DeleteMapping("/delectWarnUserById")
    @Operation(summary = "修改报警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:product:delectWarnUserById')")
    public CommonResult<Integer> delectWarnUserById(@RequestParam("id") Long id) {
        return CommonResult.success(productService.delectWarnUserById(id));
    }

    @GetMapping("/allDeviceProductPage")
    @Operation(summary = "获取全部设备分页")
    @PreAuthorize("@ss.hasPermission('device:product:allDeviceProductPage')")
    public CommonResult<PageResult<DeviceProductRespVO>> allDeviceProductPage(@Valid DeviceProductPageReqVO pageReqVO) {
        PageResult<DeviceProductRespVO> pageResult = productService.getAllDeviceProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备产品分页")
//    @PreAuthorize("@ss.hasPermission('device:product:query')")
    public CommonResult<PageResult<DeviceProductRespVO>> getProductPage(@Valid DeviceProductPageReqVO pageReqVO) {
        PageResult<DeviceProductRespVO> pageResult = productService.getProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备产品 Excel")
    @PreAuthorize("@ss.hasPermission('device:product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductExcel(@Valid DeviceProductPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceProductRespVO> list = productService.getProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备产品.xls", "数据", DeviceProductRespVO.class,
                BeanUtils.toBean(list, DeviceProductRespVO.class));
    }


    @PutMapping("/bindDevice")
    @Operation(summary = "绑定设备")
    @PreAuthorize("@ss.hasPermission('device:product:bindingDevice')")
    public CommonResult<Boolean> bindDevice(@Valid @RequestBody BindingDeviceReqVO updateReqVO) {
        productService.bindDevice(updateReqVO);
        return success(true);
    }


    @PutMapping("/unbindDevice")
    @Operation(summary = "解绑设备")
    @PreAuthorize("@ss.hasPermission('device:product:unbindDevice')")
    public CommonResult<Boolean> unbindDevice(@RequestParam Long id) {
        productService.unbindDevice(id);
        return success(true);
    }

    @PutMapping("/groupBatchBind")
    @Operation(summary = "全部设备-绑定设备")
    @PreAuthorize("@ss.hasPermission('device:product:groupBatchBind')")
    public CommonResult<Boolean> groupBatchBind(@Valid @RequestBody BindingAllDeviceReqVO updateReqVO) {
        return success(productService.groupBatchBind(updateReqVO));
    }

    @PutMapping("/groupBatchUnbind")
    @Operation(summary = "全部设备-批量解绑")
    @PreAuthorize("@ss.hasPermission('device:product:groupBatchUnbind')")
    public CommonResult<Boolean> groupBatchUnbind(@Valid @RequestBody BindingAllDeviceReqVO updateReqVO) {
        return success(productService.groupBatchUnbind(updateReqVO));
    }

    @PutMapping("/groupBatchDelete")
    @Operation(summary = "全部设备-批量删除")
    @PreAuthorize("@ss.hasPermission('device:product:groupBatchDelete')")
    public CommonResult<Boolean> groupBatchDelete(@Valid @RequestBody BindingAllDeviceReqVO updateReqVO) {
        return success(productService.groupBatchDelete(updateReqVO));
    }

}
