package com.ruoyi.web.controller.school.member;

import com.ruoyi.common.page.RespPage;
import com.ruoyi.core.api.APIResponse;
import com.ruoyi.school.member.domain.req.CreateMemberCardDTO;
import com.ruoyi.school.member.domain.resp.MemberCardDTO;
import com.ruoyi.school.member.domain.resp.MemberCardVO;
import com.ruoyi.school.member.service.IScMemberCardService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员卡类型信息 Controller
 * </p>
 *
 */
@RestController
@RequestMapping("/api/sc/memberCard")
public class ScMemberCardController {

    @Autowired
    private IScMemberCardService memberCardService;


    @PostMapping("/list")
    @ApiOperation("分页查询会员卡列表")
    public APIResponse getCardList(
            @Valid @RequestBody MemberCardDTO queryDTO) {
        RespPage<MemberCardVO> result = memberCardService.getCardList(queryDTO);
        return APIResponse.toAPIResponse(result);
    }


    @PostMapping("/create")
    @ApiOperation("创建会员卡")
    public APIResponse createMemberCard(@Valid @RequestBody CreateMemberCardDTO dto) {
        MemberCardDTO result = memberCardService.createMemberCard(dto);
        return APIResponse.toAPIResponse(result);
    }

    @PostMapping("/checkIn")
    @ApiOperation("批量签到")
    public APIResponse checkIn( @Valid @RequestBody Map<String , List<Long> > map
                                  ) {
        List<Long> bookIds=map.get("bookIds");
        return memberCardService.checkInByIds(bookIds);
    }


    @GetMapping("/{cardNo}")
    @ApiOperation("根据卡号查询会员卡")
    public APIResponse getByCardNo(@PathVariable String cardNo) {
        MemberCardVO result = memberCardService.getByCardNo(cardNo);
        return APIResponse.toAPIResponse(result);
    }






    @PostMapping("/{cardId}/activate")
    @ApiOperation("激活会员卡")
    public APIResponse activateCard(@PathVariable Long cardId) {
        boolean result = memberCardService.activateCard(cardId);
        return APIResponse.toAPIResponse(result);
    }

    @GetMapping("/member/{memberId}")
    @ApiOperation("查询会员的会员卡列表")
    public APIResponse getMemberCards(@PathVariable Long memberId) {
        List<MemberCardVO> result = memberCardService.getMemberCards(memberId);
        return APIResponse.toAPIResponse(result);
    }

    @PostMapping("/member/{cardId}")
    @ApiOperation("暂停会员卡")
    public APIResponse suspendCard(
            @ApiParam(value = "会员卡ID", required = true)
            @PathVariable Long cardId) {
        boolean result = memberCardService.suspendCard(cardId);
        return APIResponse.toAPIResponse(result ? "暂停成功" : "暂停失败");
    }

//    @PostMapping("/renew")
//    @ApiOperation("续费会员卡")
//    public APIResponse renewCard(
//            @Valid @RequestBody RenewCardDTO dto) {
//        boolean result = memberCardService.renewCard(dto.getCardId(), dto.getDays());
//        return APIResponse.toAPIResponse(result ? "续费成功" : "续费失败");
//    }
    @PostMapping("/add-count")
    @ApiOperation("增加次数")
    public APIResponse addCardCount(
            @ApiParam(value = "会员卡ID", required = true)
            @RequestParam Long cardId,
            @ApiParam(value = "增加次数", required = true)
            @RequestParam Integer count) {
        boolean result = memberCardService.addCardCount(cardId, count);
        return APIResponse.toAPIResponse(result ? "增加成功" : "增加失败");
    }

    @PostMapping("/deduct-count")
    @ApiOperation("扣减次数")
    public APIResponse deductCardCount(
            @ApiParam(value = "会员卡ID", required = true)
            @RequestParam Long cardId,
            @ApiParam(value = "扣减次数", required = true)
            @RequestParam Integer count) {
        boolean result = memberCardService.deductCardCount(cardId, count);
        return APIResponse.toAPIResponse(result ? "扣减成功" : "扣减失败");
    }

    @GetMapping("/valid/{memberId}")
    @ApiOperation("查询会员有效的会员卡")
    public APIResponse getValidMemberCards(
            @ApiParam(value = "会员ID", required = true)
            @PathVariable Long memberId) {
        List<MemberCardVO> result = memberCardService.getValidMemberCards(memberId);
        return APIResponse.toAPIResponse(result);
    }

    @PostMapping("/check-status/{cardId}")
    @ApiOperation("检查会员卡状态")
    public APIResponse checkCardStatus(
            @ApiParam(value = "会员卡ID", required = true)
            @PathVariable Long cardId) {
        boolean result = memberCardService.checkCardStatus(cardId);
        return APIResponse.toAPIResponse(result ? "卡状态正常" : "卡状态异常");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除会员卡（逻辑删除）")
    public APIResponse deleteCard(
            @ApiParam(value = "会员卡ID", required = true)
            @PathVariable Long id) {
        boolean result = memberCardService.removeById(id);
        return APIResponse.toAPIResponse(result ? "删除成功" : "删除失败");
    }

    @GetMapping("/todayCardCount")
    @ApiOperation("查询今日办卡数量")
    public APIResponse getTodayCardCount() {
        Integer result = memberCardService.getTodayCardCount();
        return APIResponse.toAPIResponse(result);
    }
}
