package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayController;
import com.crm.pay.entity.qr.QrCodeAddEntity;
import com.crm.pay.service.FinanceTrainService;
import com.crm.pay.service.QrCodePayService;
import com.crm.rely.backend.core.constant.RequestSinglePointLimiter;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.rely.backend.model.entity.finance.train.FinanceTrainAddEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/qr")
public class QrCodePayController extends BasePayController {
    @Autowired
    private QrCodePayService qrCodePayService;

    @Autowired
    private FinanceTrainService financeTrainService;

    @RequestMapping("/pay")
    @RequestSinglePointLimiter
    public BaseResultDto pay(@RequestBody @Validated QrCodeAddEntity entity, CustomInfoEntity infoEntity) throws Exception {

        FinanceDepositAddEntity balanceSaveEntity = getFinanceDepositSaveEntity(
                entity.getLogin(),
                entity.getAmount(),
                PayConstants.QR_CODE_KEY,
                entity.getVoucherUrl(),
                infoEntity);
        qrCodePayService.pay(balanceSaveEntity);
        return BaseResultDto.success();
    }

    @RequestMapping("/train/pay")
    public BaseResultDto trainPay(@RequestBody @Validated FinanceTrainAddEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCustomId(infoEntity.getId());
        financeTrainService.add(entity);
        return BaseResultDto.success();
    }

}
