package cn.iocoder.yudao.module.member.controller.admin.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardChangeRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberCardIssuanceRecordDO;
import cn.iocoder.yudao.module.member.service.user.MemberCardService;
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.validation.Valid;

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

import java.util.List;
import java.util.Map;
import java.util.Objects;

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


@Tag(name = "管理后台 - 会员卡")
@RestController
@RequestMapping("/member/card")
@Validated
public class MemberCardController {

    @Resource
    private MemberCardService memberCardService;


    @GetMapping("/getMemberCardByMemberUserId")
    @Operation(summary = "获取正常的会员卡列表")
    public CommonResult<List<MemberCardDO>> getMemberCardByMemberUserId(@RequestParam("memberUserId") Long memberUserId) {
        return success(memberCardService.getMemberCardByMemberUserId(memberUserId));
    }

    @GetMapping("/getAllMemberCardByMemberUserId")
    @Operation(summary = "获取所有的会员卡列表")
    public CommonResult<List<MemberCardDO>> getAllMemberCardByMemberUserId(@RequestParam("memberUserId") Long memberUserId) {
        return success(memberCardService.getAllMemberCardByMemberUserId(memberUserId));
    }


    @PutMapping("/update")
    @Operation(summary = "修改会员卡")
    public CommonResult<Boolean> update(@RequestBody MemberCardDO memberCardDO) {
        memberCardService.update(memberCardDO);
        return success(true);
    }

    @PostMapping("/recharge")
    @Operation(summary = "会员卡充值")
    public CommonResult<Boolean> recharge(@RequestBody MemberCardRechargeSaveVO saveVO) {
        memberCardService.recharge(saveVO);
        return success(true);
    }


    @PostMapping("/issuanceCard")
    @Operation(summary = "发卡")
    public CommonResult<Boolean> issuanceCard(@RequestBody MemberCardDO memberCardDO) {
        memberCardService.issuanceCard(memberCardDO);
        return success(true);
    }


    @PostMapping("/reissueCard")
    @Operation(summary = "补卡")
    public CommonResult<Boolean> reissueCard(@RequestBody MemberCardIssuanceRecordDO reissueRecordDO) {
        memberCardService.reissueCard(reissueRecordDO);
        return success(true);
    }

    @PostMapping("/freezeOrThawCard")
    @Operation(summary = "冻结或者解冻会员卡")
    public CommonResult<Boolean> freezeOrThawCard(@RequestBody MemberCardDO updateVo) {
        memberCardService.freezeOrThawCard(updateVo);
        return success(true);
    }

    @PostMapping("/refund")
    @Operation(summary = "退款")
    public CommonResult<Boolean> refund(@RequestBody MemberCardChangeRecordDO memberCardChangeRecordDO) {
        memberCardService.refund(memberCardChangeRecordDO);
        return success(true);
    }


    @PostMapping("/getMemberCardConsumptionRecordPage")
    @Operation(summary = "会员账单记录分页")
    public CommonResult<PageResult<MemberCardChangeRecordDO>> getMemberCardConsumptionRecordPage(@RequestBody MemberCardChangePageReqVO pageReqVO) {
        return success(memberCardService.getMemberCardConsumptionRecordPage(pageReqVO));
    }


    @PostMapping("/getMemberCardChangeRecordPage")
    @Operation(summary = "会员卡变更记录分页")
    public CommonResult<PageResult<MemberCardIssuanceRecordDO>> getMemberCardChangeRecordPage(@RequestBody MemberCardIssuancePageReqVO pageReqVO) {
        return success(memberCardService.getMemberCardChangeRecordPage(pageReqVO));
    }


    @GetMapping("/getEmployeeMemberCardList")
    @Operation(summary = "获取员工类型的会员卡列表")
    public CommonResult<List<MemberCardDO>> getEmployeeMemberCardList() {
        return success(memberCardService.getEmployeeMemberCardList());
    }


    @PostMapping("/batchRecharge")
    @Operation(summary = "批量充值")
    public CommonResult<Boolean> batchRecharge(@RequestBody MemberCardBatchRechargeSaveVO batchRechargeSaveVO) {
        memberCardService.batchRecharge(batchRechargeSaveVO);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得会员卡详情")
    @Parameter(name = "id", description = "会员卡ID", required = true, example = "1024")
    public CommonResult<MemberCardRespVO> getCardDetail(@RequestParam("id") Long memberCardId) {
        return success(memberCardService.getCardDetail(memberCardId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员卡分页")
    @TenantIgnore
    public CommonResult<PageResult<MemberCardPageRespVO>> getCardPage(@Valid MemberCardPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<MemberCardPageRespVO> pageResult = memberCardService.getCardPage(pageReqVO);
        return success(pageResult);
    }


    @PostMapping("/statisticsMemberCardIssuanceRecordPage")
    @Operation(summary = "发卡明细和补卡明细统计分页查询")
    @TenantIgnore
    public CommonResult<PageResult<MemberCardIssuanceRecordDO>> statisticsMemberCardIssuanceRecordPage(@RequestBody MemberCardIssuancePageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        return success(memberCardService.statisticsMemberCardIssuanceRecordPage(pageReqVO));
    }


    @PostMapping("/statisticsMemberCardConsumptionRecordPage")
    @Operation(summary = "充值和消费明细统计分页查询")
    @TenantIgnore
    public CommonResult<PageResult<MemberCardChangeRecordDO>> statisticsMemberCardConsumptionRecordPage(@RequestBody MemberCardChangePageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        return success(memberCardService.statisticsMemberCardConsumptionRecordPage(pageReqVO));
    }


    @PostMapping("/statisticsRechargeAmount")
    @Operation(summary = "统计充值金额")
    @TenantIgnore
    public CommonResult<Map<String, Object>> statisticsRechargeAmount(@RequestBody MemberCardChangePageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        return success(memberCardService.statisticsRechargeAmount(pageReqVO));
    }


    @PostMapping("/memberCardStatisticalReportPage")
    @Operation(summary = "会员卡报表")
    public CommonResult<PageResult<MemberCardStatisticReportVO>> getMemberCardStatisticalReportPage(@RequestBody MemberCardPageReqVO pageReqVO) {
        return success(memberCardService.getMemberCardStatisticalReportPage(pageReqVO));
    }


//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出会员卡 Excel")
//    @PreAuthorize("@ss.hasPermission('member:card:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportCardExcel(@Valid CardPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<CardDO> list = cardService.getCardPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "会员卡.xls", "数据", CardRespVO.class,
//                        BeanUtils.toBean(list, CardRespVO.class));
//    }


    @GetMapping("/getMemberCardListByIdCard")
    @Operation(summary = "根据身份证号查询会员卡列表")
    public CommonResult<List<MemberCardDO>> getMemberCardListByIdCard(@RequestParam("idCard") String idCard) {
        return success(memberCardService.getMemberCardListByIdCard(idCard));
    }
}