package com.yolo.keyboard.controller.admin.productitems;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.productitems.vo.*;
import com.yolo.keyboard.dal.dataobject.productitems.KeyboardProductItemsDO;
import com.yolo.keyboard.service.productitems.KeyboardProductItemsService;

@Tag(name = "管理后台 - 内购商品")
@RestController
@RequestMapping("/keyboard/product-items")
@Validated
public class KeyboardProductItemsController {

    @Resource
    private KeyboardProductItemsService productItemsService;

    @PostMapping("/create")
    @Operation(summary = "创建内购商品")
    @PreAuthorize("@ss.hasPermission('keyboard:product-items:create')")
    public CommonResult<Long> createProductItems(@Valid @RequestBody KeyboardProductItemsSaveReqVO createReqVO) {
        return success(productItemsService.createProductItems(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新内购商品")
    @PreAuthorize("@ss.hasPermission('keyboard:product-items:update')")
    public CommonResult<Boolean> updateProductItems(@Valid @RequestBody KeyboardProductItemsSaveReqVO updateReqVO) {
        productItemsService.updateProductItems(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除内购商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:product-items:delete')")
    public CommonResult<Boolean> deleteProductItems(@RequestParam("id") Long id) {
        productItemsService.deleteProductItems(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除内购商品")
                @PreAuthorize("@ss.hasPermission('keyboard:product-items:delete')")
    public CommonResult<Boolean> deleteProductItemsList(@RequestParam("ids") List<Long> ids) {
        productItemsService.deleteProductItemsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得内购商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:product-items:query')")
    public CommonResult<KeyboardProductItemsRespVO> getProductItems(@RequestParam("id") Long id) {
        KeyboardProductItemsDO productItems = productItemsService.getProductItems(id);
        return success(BeanUtils.toBean(productItems, KeyboardProductItemsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得内购商品分页")
    @PreAuthorize("@ss.hasPermission('keyboard:product-items:query')")
    public CommonResult<PageResult<KeyboardProductItemsRespVO>> getProductItemsPage(@Valid KeyboardProductItemsPageReqVO pageReqVO) {
        PageResult<KeyboardProductItemsDO> pageResult = productItemsService.getProductItemsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardProductItemsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出内购商品 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:product-items:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductItemsExcel(@Valid KeyboardProductItemsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardProductItemsDO> list = productItemsService.getProductItemsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "内购商品.xls", "数据", KeyboardProductItemsRespVO.class,
                        BeanUtils.toBean(list, KeyboardProductItemsRespVO.class));
    }

}