package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.digital.DigitalAddEntity;
import com.crm.pay.service.DigitalPayService;
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 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;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/digital")
public class DigitalPayController extends BasePayCloseFunctionController {

    @Autowired
    private DigitalPayService digitalPayService;

    /**
     * 支付
     *
     * @return 错误信息
     * @throws Exception
     */
    @RequestMapping("/pay")
    public BaseResultDto pay(@RequestBody @Validated DigitalAddEntity entity, CustomInfoEntity infoEntity,
                             HttpServletRequest request) throws Exception {

        validated(entity.getLogin(), entity.getAmount());

        if (entity.getActivityDeposit() == null) {
            entity.setActivityDeposit(0);
        }
        FinanceDepositAddEntity balanceSaveEntity = getFinanceDepositSaveEntity(
                entity.getLogin(),
                entity.getAmount(),
                PayConstants.DIGITAL_PAY_TYPE_KEY,
                PayConstants.DIGITAL_PAY_TYPE_KEY,
                entity.getActivityDeposit(),
                entity, request);
        balanceSaveEntity.setVoucherUrl(entity.getVoucherUrl());
        digitalPayService.pay(balanceSaveEntity);
        return BaseResultDto.success();
    }

    @RequestMapping("/uk/pay")
    public BaseResultDto ukPay(@RequestBody @Validated DigitalAddEntity entity, CustomInfoEntity infoEntity,
                               HttpServletRequest request) throws Exception {

        validated(entity.getLogin(), entity.getAmount());

        if (entity.getActivityDeposit() == null) {
            entity.setActivityDeposit(0);
        }
        FinanceDepositAddEntity balanceSaveEntity = getFinanceDepositSaveEntity(
                entity.getLogin(),
                entity.getAmount(),
                PayConstants.DIGITAL_PAY_TYPE_KEY_UK,
                PayConstants.DIGITAL_PAY_TYPE_KEY_UK,
                entity.getActivityDeposit(),
                entity, request);
        balanceSaveEntity.setVoucherUrl(entity.getVoucherUrl());
        digitalPayService.pay(balanceSaveEntity);
        return BaseResultDto.success();
    }
}
