package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.controller.base.BasePayController;
import com.crm.pay.entity.telegraphic.TelegraphicAddEntity;
import com.crm.pay.service.TelegraphicPayService;
import com.crm.rely.backend.core.constant.Constants;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/telegraphic")
public class TelegraphicPayController extends BasePayCloseFunctionController {

    @Autowired
    private TelegraphicPayService service;


    /**
     * 支付
     *
     * @return 错误信息
     * @throws Exception
     */
    @RequestMapping("/pay")
    public BaseResultDto pay(@RequestBody @Validated TelegraphicAddEntity 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(),
                entity.getCode(),
                entity.getCode(),
                entity.getActivityDeposit(),
                entity, request);
        balanceSaveEntity.setVoucherUrl(entity.getVoucherUrl());
        balanceSaveEntity.setBankMsg(entity.getBankMsg());
        service.pay(balanceSaveEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/upload")
    public BaseResultDto upload(@RequestParam(value = "file", required = false) MultipartFile file,
                                CustomInfoEntity infoEntity) throws Exception {

        service.validated(file);

        String path = service.uploadFile(infoEntity.getId(), file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

}
