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

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.tenantcommission.vo.*;
import com.yolo.keyboard.dal.dataobject.tenantcommission.KeyboardTenantCommissionDO;
import com.yolo.keyboard.service.tenantcommission.KeyboardTenantCommissionService;

@Tag(name = "管理后台 - 租户内购分成记录")
@RestController
@RequestMapping("/keyboard/tenant-commission")
@Validated
public class KeyboardTenantCommissionController {

    @Resource
    private KeyboardTenantCommissionService tenantCommissionService;


    @PostMapping("/create")
    @Operation(summary = "创建租户内购分成记录")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:create')")
    public CommonResult<Long> createTenantCommission(@Valid @RequestBody KeyboardTenantCommissionSaveReqVO createReqVO) {
        
        return success(tenantCommissionService.createTenantCommission(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户内购分成记录")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:update')")
    public CommonResult<Boolean> updateTenantCommission(@Valid @RequestBody KeyboardTenantCommissionSaveReqVO updateReqVO) {
        tenantCommissionService.updateTenantCommission(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户内购分成记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:delete')")
    public CommonResult<Boolean> deleteTenantCommission(@RequestParam("id") Long id) {
        tenantCommissionService.deleteTenantCommission(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户内购分成记录")
                @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:delete')")
    public CommonResult<Boolean> deleteTenantCommissionList(@RequestParam("ids") List<Long> ids) {
        tenantCommissionService.deleteTenantCommissionListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户内购分成记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:query')")
    public CommonResult<KeyboardTenantCommissionRespVO> getTenantCommission(@RequestParam("id") Long id) {
        KeyboardTenantCommissionDO tenantCommission = tenantCommissionService.getTenantCommission(id);
        return success(BeanUtils.toBean(tenantCommission, KeyboardTenantCommissionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户内购分成记录分页")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:query')")
    public CommonResult<PageResult<KeyboardTenantCommissionRespVO>> getTenantCommissionPage(@Valid KeyboardTenantCommissionPageReqVO pageReqVO) {
        PageResult<KeyboardTenantCommissionDO> pageResult = tenantCommissionService.getTenantCommissionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardTenantCommissionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户内购分成记录 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-commission:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantCommissionExcel(@Valid KeyboardTenantCommissionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardTenantCommissionDO> list = tenantCommissionService.getTenantCommissionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户内购分成记录.xls", "数据", KeyboardTenantCommissionRespVO.class,
                        BeanUtils.toBean(list, KeyboardTenantCommissionRespVO.class));
    }

}