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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.SysChargeCategoryPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.SysChargeCategoryDO;
import cn.iocoder.yudao.module.system.service.biz.SysChargeCategoryService;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收费分类")
@RestController
@RequestMapping("/chargeCategory")
public class SysChargeCategoryController {

    @Resource
    private SysChargeCategoryService sysChargeCategoryService;

    @PostMapping("/add")
    @Operation(summary = "创建收费分类")
    @TenantIgnore
    public CommonResult<Boolean> addChargeCategory(@RequestBody SysChargeCategoryDO sysChargeCategoryDO) {
        sysChargeCategoryDO.setTenantId(sysChargeCategoryDO.getTenantId() == null ? TenantContextHolder.getTenantId() : sysChargeCategoryDO.getTenantId());
        sysChargeCategoryService.add(sysChargeCategoryDO);
        return success(true);
    }


    @PutMapping("/edit")
    @Operation(summary = "编辑收费分类")
    public CommonResult<Boolean> updateChargeCategory(@RequestBody SysChargeCategoryDO sysChargeCategoryDO) {
        sysChargeCategoryService.edit(sysChargeCategoryDO);
        return success(true);
    }

    @GetMapping("/findPage")
    @Operation(summary = "收费分类分页")
    @TenantIgnore
    public CommonResult<PageResult<SysChargeCategoryDO>> findPage(@Valid SysChargeCategoryPageReqVO pageVO) {
        pageVO.setTenantIds(Objects.isNull(pageVO.getTenantIds()) ? (Long[]) Arrays.asList(TenantContextHolder.getTenantId()).toArray() : pageVO.getTenantIds());
        PageResult<SysChargeCategoryDO> pageResult = sysChargeCategoryService.findPage(pageVO);
        return success(BeanUtils.toBean(pageResult, SysChargeCategoryDO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收费分类")
    @Parameter(name = "id", description = "收费分类ID", required = true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        sysChargeCategoryService.delete(id);
        return success(true);
    }


    @GetMapping("/getInfoById")
    @Operation(summary = "获取收费分类详情")
    @Parameter(name = "id", description = "收费分类ID", required = true, example = "1024")
    public CommonResult<SysChargeCategoryDO> getInfoById(@RequestParam("id") Long id) {
        SysChargeCategoryDO sysChargeCategoryDO = sysChargeCategoryService.getInfoById(id);
        return success(BeanUtils.toBean(sysChargeCategoryDO, SysChargeCategoryDO.class));
    }


    @GetMapping("/getTreeById")
    @Operation(summary = "根据收费分类ID获取树形结构")
    @Parameter(name = "id", description = "收费分类ID", required = true, example = "1024")
    public CommonResult<SysChargeCategoryDO> getTreeById(@RequestParam("id") Long id) {
        SysChargeCategoryDO sysChargeCategoryDO = sysChargeCategoryService.getTreeById(id);
        return success(sysChargeCategoryDO);
    }


    @GetMapping("/getTree")
    @Operation(summary = "获取树形结构")
    @TenantIgnore
    public CommonResult<List<SysChargeCategoryDO>> getTree(@RequestParam(value = "tenantIds", required = false) Long[] tenantIds,
                                                           @RequestParam(value = "orgType", required = false) Integer orgType,
                                                           @RequestParam(value = "type", required = false) Integer type) {
        tenantIds = Objects.isNull(tenantIds) ? Arrays.asList(TenantContextHolder.getTenantId()).toArray(new Long[0]) : tenantIds;
        List<SysChargeCategoryDO> tree = sysChargeCategoryService.getTree(tenantIds,orgType,type);
        return success(tree);
    }
}
