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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
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.VolunteerTeamPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerTeamRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerTeamSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.VolunteerTeamDO;
import cn.iocoder.yudao.module.system.service.biz.VolunteerTeamService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/7  11:16
 * @Description:
 * @Version: 4.0
 */


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

    @Resource
    private VolunteerTeamService teamService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得志愿者团队")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<VolunteerTeamRespVO> getTeam(@RequestParam("id") Long id) {
        VolunteerTeamDO team = teamService.getTeam(id);
        return success(BeanUtils.toBean(team, VolunteerTeamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得志愿者团队分页")
    public CommonResult<PageResult<VolunteerTeamRespVO>> getTeamPage(@Valid VolunteerTeamPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<VolunteerTeamDO> pageResult = teamService.getTeamPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VolunteerTeamRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出志愿者团队 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeamExcel(@Valid VolunteerTeamPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VolunteerTeamDO> list = teamService.getTeamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "志愿者团队.xls", "数据", VolunteerTeamRespVO.class,
                BeanUtils.toBean(list, VolunteerTeamRespVO.class));
    }

}
