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

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/10  8:44
 * @Description:
 * @Version: 4.0
 */


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.biz.vo.VolunteerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.VolunteerDO;
import cn.iocoder.yudao.module.system.service.biz.VolunteerService;
import org.springframework.web.bind.annotation.*;
import javax.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 javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
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 static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 志愿者")
@RestController
@RequestMapping("/volunteer/")
@Validated
public class VolunteerController {

    @Resource
    private VolunteerService volunteerService;

    @PostMapping("/create")
    @Operation(summary = "创建志愿者")
    @TenantIgnore
    public CommonResult<Long> create(@Valid @RequestBody VolunteerSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId()  == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(volunteerService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新志愿者")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody VolunteerSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(updateReqVO.getTenantId()  == null ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        volunteerService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除志愿者")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        volunteerService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得志愿者")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<VolunteerRespVO> get(@RequestParam("id") Long id) {
        VolunteerDO volunteerDO  = volunteerService.get(id);
        return success(BeanUtils.toBean(volunteerDO, VolunteerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得志愿者分页")
    @TenantIgnore
    public CommonResult<PageResult<VolunteerRespVO>> getPage(@Valid VolunteerPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<VolunteerDO> pageResult = volunteerService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VolunteerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出志愿者 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid VolunteerPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VolunteerDO> list = volunteerService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "志愿者.xls", "数据", VolunteerRespVO.class,
                BeanUtils.toBean(list, VolunteerRespVO.class));
    }

}
