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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.excel.DepositExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.DepositRecordPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DepositDO;
import cn.iocoder.yudao.module.system.service.biz.DepositService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

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

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

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

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


@Tag(name = "管理后台 - 长者押金")
@RestController
@RequestMapping("/elderly/deposit")
@Validated
public class DepositController {

    @Resource
    private DepositService depositService;


    @PostMapping("/pay")
    @Operation(summary = "缴纳长者押金")
    public CommonResult<Boolean> pay(@Valid @RequestBody DepositDO createReqVO) {
        depositService.pay(createReqVO);
        return success(true);
    }

    @PutMapping("/refund")
    @Operation(summary = "退款长者押金")
    public CommonResult<Boolean> refund(@Valid @RequestBody DepositDO updateReqVO) {
        depositService.refund(updateReqVO);
        return success(true);
    }

    @PutMapping("/consumption")
    @Operation(summary = "消费长者押金")
    public CommonResult<Boolean> consumption(@Valid @RequestBody DepositDO updateReqVO) {
        depositService.consumption(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者押金详情")
    @Parameter(name = "id", description = "押金记录ID", required = true, example = "1024")
    public CommonResult<DepositDO> getDeposit(@RequestParam("id") Long id) {
        return success(depositService.getDepositDetails(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者押金分页")
    @TenantIgnore
    public CommonResult<PageResult<DepositDO>> getDepositPage(@Valid DepositRecordPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} :pageReqVO.getTenantIds());
        PageResult<DepositDO> pageResult = depositService.getDepositPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/exportDepositExcel")
    @Operation(summary = "导出长者押金Excel")
    public void exportDepositExcel(@Valid DepositRecordPageReqVO pageReqVO , HttpServletResponse response) throws IOException {
        List<DepositExcelVO> list = depositService.exportDepositExcel(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "长者押金.xls", "数据", DepositExcelVO.class,
                list);
    }

}