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

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.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.CardPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.CardRespVO;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.CardSaveReqVO;
import cn.iocoder.yudao.module.member.controller.admin.card.vo.RechargeVO;
import cn.iocoder.yudao.module.member.dal.dataobject.card.CardDO;
import cn.iocoder.yudao.module.member.service.card.CardService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

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

@Tag(name = "管理后台 - 余额变动明细")
@RestController
@RequestMapping("/member/card")
@Validated
public class CardController {


    @Resource
    private CardService cardService;

    @PostMapping("/create")
    @Operation(summary = "创建余额变动明细")
    @PreAuthorize("@ss.hasPermission('member:card:create')")
    public CommonResult<Long> createCard(@Valid @RequestBody CardSaveReqVO createReqVO) {
        return success(cardService.createCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新余额变动明细")
    @PreAuthorize("@ss.hasPermission('member:card:update')")
    public CommonResult<Boolean> updateCard(@Valid @RequestBody CardSaveReqVO updateReqVO) {
        cardService.updateCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除余额变动明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:card:delete')")
    public CommonResult<Boolean> deleteCard(@RequestParam("id") Long id) {
        cardService.deleteCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得余额变动明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:card:query')")
    public CommonResult<CardRespVO> getCard(@RequestParam("id") Long id) {
        CardDO card = cardService.getCard(id);
        return success(BeanUtils.toBean(card, CardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得余额变动明细分页")
    @PreAuthorize("@ss.hasPermission('member:card:query')")
    public CommonResult<PageResult<CardRespVO>> getCardPage(@Valid CardPageReqVO pageReqVO) {
        PageResult<CardDO> pageResult = cardService.getCardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出余额变动明细 Excel")
    @PreAuthorize("@ss.hasPermission('member:card:export')")
    @OperateLog(type = 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));
    }

    @PutMapping("/recharge")
    @Operation(summary = "充值")
    //@PreAuthorize("@ss.hasPermission('member:card:update')")
    public CommonResult<Boolean> recharge(BigDecimal money,BigDecimal giftMoney,Long userId) {
        cardService.recharge(money, CardDO.ADD,giftMoney, userId);
        return success(true);
    }

    @GetMapping("/getMoney")
    @Operation(summary = "获取余额")
    public CommonResult<BigDecimal> getMoney(Long carteenId) {
        return success(cardService.getMoney(carteenId));
    }

    @PutMapping("/rechargeByAdmin")
    @Operation(summary = "管理后台充值")
    //@PreAuthorize("@ss.hasPermission('member:card:update')")
    public CommonResult<Boolean> rechargeByAdmin(@RequestBody RechargeVO vo) {
        cardService.rechargeByAdmin(vo);
        return success(true);
    }

    @GetMapping("/getCashMoney")
    @Operation(summary = "获取现金充值")
    public CommonResult<BigDecimal> getCashMoney(Long userId,Long carteenId) {
        return success(cardService.getCashMoney(userId,carteenId));
    }

    @GetMapping("/cashDraw")
    @Operation(summary = "现金提现")
    public CommonResult<Boolean> cashDraw(Long userId,BigDecimal money,Long carteenId) {
        return success(cardService.cashDraw(userId,money,carteenId));
    }


}