package cn.iocoder.yudao.module.pms.controller.admin.yanfan.product;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.product.vo.ChangeProductStatusModel;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.product.vo.IotYfProductPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.product.vo.IotYfProductRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.product.vo.IotYfProductSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.product.IotYfProductDO;
import cn.iocoder.yudao.module.pms.service.yanfan.product.IotYfProductService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 产品")
@RestController
@RequestMapping("/rq/iot-product")
@Validated
public class IotYfProductController {

    @Resource
    private IotYfProductService iotYfProductService;

    @PostMapping
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('rq:iot-product:create')")
    public CommonResult<Long> createIotProduct(@Valid @RequestBody IotYfProductSaveReqVO createReqVO) {
        return success(iotYfProductService.createIotProduct(createReqVO));
    }

    @PutMapping
    @Operation(summary = "更新产品")
    @PreAuthorize("@ss.hasPermission('rq:iot-product:update')")
    public CommonResult<Boolean> updateIotProduct(@Valid @RequestBody IotYfProductSaveReqVO updateReqVO) {
        iotYfProductService.updateIotProduct(updateReqVO);
        return success(true);
    }


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

    @GetMapping("/get")
    @Operation(summary = "获得产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-product:query')")
    public CommonResult<IotYfProductRespVO> getIotProduct(@RequestParam("id") Long id) {
        IotYfProductRespVO iotProduct = iotYfProductService.getIotProduct(id);
        return success(iotProduct);
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-product:query')")
    public CommonResult<PageResult<IotYfProductRespVO>> getIotProductPage(@Valid IotYfProductPageReqVO pageReqVO) {
        PageResult<IotYfProductDO> pageResult = iotYfProductService.getIotProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotProductExcel(@Valid IotYfProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfProductDO> list = iotYfProductService.getIotProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品.xls", "数据", IotYfProductRespVO.class,
                        BeanUtils.toBean(list, IotYfProductRespVO.class));
    }


    @PutMapping("/status")
    @Operation(summary = "更新产品状态")
    public CommonResult<Boolean> changeProductStatus(@RequestBody ChangeProductStatusModel model) {
        iotYfProductService.changeProductStatus(model);
        return success(true);
    }

    @DeleteMapping("/{productIds}")
    @Operation(summary = "批量删除产品")
    public CommonResult<Boolean> remove(@PathVariable Long[] productIds) {
        iotYfProductService.deleteProductByProductIds(productIds);
        return success(true);
    }
}
