package org.dromara.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.convert.SysTenantAccountConvert;
import org.dromara.system.domain.CommissionSection;
import org.dromara.system.domain.SysTenantAccount;
import org.dromara.system.domain.bo.CommissionSectionBo;
import org.dromara.system.domain.bo.SysTenantAccountBo;
import org.dromara.system.domain.vo.CommissionSectionVo;
import org.dromara.system.domain.vo.SysTenantAccountVo;
import org.dromara.system.service.ISysTenantAccountService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "租户账户管理接口")
@RestController
@RequestMapping("/system/tenant/account")
@RequiredArgsConstructor
public class SysTenantAccountController {

    private final ISysTenantAccountService service;
    private final SysTenantAccountConvert convert;

    @Operation(summary = "查询租户账户列表")
    @PostMapping("/list")
    public R<IPage<SysTenantAccountVo>> fansList(@RequestBody SysTenantAccountBo bo, Page<SysTenantAccount> page) {
        IPage<SysTenantAccountVo> result = service.selectTenantAccountPage(page, bo);
        return R.ok(result);
    }

    @Operation(summary = "导出租户账户列表")
    @Log(title = "租户账户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R<String> export(SysTenantAccountBo query) {
        List<SysTenantAccount> list = service.list(query);
        ExcelUtil<SysTenantAccountVo> util = new ExcelUtil<>(SysTenantAccountVo.class);
        return R.ok(util.writeExcel(convert.toVO(list), "租户账户数据"));
    }

    @Operation(summary = "获取租户账户详细信息")
    @GetMapping(value = "/{id}")
    public R<SysTenantAccountVo> getInfo(@Parameter(description = "租户账户ID") @PathVariable("id") Long id) {
        return R.ok(convert.toVO(service.getById(id)));
    }

    @Operation(summary = "新增租户账户")
    @Log(title = "租户账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Object> add(@RequestBody SysTenantAccountBo tenantAccount) {
        return R.ok(service.save(convert.toPo(tenantAccount)));
    }

    @Operation(summary = "修改租户账户")
    @Log(title = "租户账户", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Object> edit(@RequestBody SysTenantAccountBo tenantAccount) {
        return R.ok(service.updateById(convert.toPo(tenantAccount)));
    }

    @Operation(summary = "删除租户账户")
    @Log(title = "租户账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Object> remove(@Parameter(description = "租户账户ID") @PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
