package com.cool.store.service.wallet;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.wallet.*;
import com.cool.store.http.WalletHttpClientRest;
import com.cool.store.request.wallet.*;
import com.cool.store.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author suzhuhong
 * @Date 2025/11/13 10:51
 * @Version 1.0
 */
@Service
public class WalletApiService {

    @Resource
    WalletHttpClientRest walletHttpClientRest;

    @Value("${wallet.url}")
    private String walletBaseUrl;

    /**
     * 在平安扫呗系统中,创建签约人账户
     * @param request
     * @return
     */
    public StoreAccountDTO createStoreAndAccount(CreateStoreAndAccountRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/createStoreAndAccount", request, StoreAccountDTO.class);
    }

    /**
     * 开通失败 开通
     * @param request
     * @return
     */
    public StoreAccountDTO updateStoreAccount(UpdateStoreAccountRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/updateStoreAccount", request, StoreAccountDTO.class);
    }

    /**
     * 创建门店接口
     * @param request
     * @return
     */
    public AccountNoDTO createStore(CreateStoreRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/createStore", request, AccountNoDTO.class);
    }

    /**
     * 银行发送短信接口
     * @param request
     * @return
     */
    public AccountAuthenticationDTO authentication(OutStoreIdRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/authentication", request, AccountAuthenticationDTO.class);
    }

    /**
     * 门店开通平安银行接口
     * @param request
     * @return
     */
    public AccountVerifyDTO openAccount(AccountVerifyRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/openAccount", request, AccountVerifyDTO.class);
    }

    /**
     * 签约人账户打标升级
     * @param request
     * @return
     */
    public AddTagDTO addTag(AccountAddTagRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/addTag", request, AddTagDTO.class);
    }

    /**
     * 获取账户信息
     * @param request
     * @return
     */
    public List<AccountInfoDTO> getAccountInfo(OutStoreIdRequest request){
        String accountInfoStr = walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/queryAccountInfo", request, String.class);
        if (StringUtil.isNotEmpty(accountInfoStr)){
            return JSONObject.parseArray(accountInfoStr, AccountInfoDTO.class);
        }
        return null;
    }

    /**
     * 大额预支付接口
     * @param request
     * @return
     */
    public LargePaymentDTO  largePayment(LargePaymentRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/largePayment", request, LargePaymentDTO.class);
    }

    /**
     * 大额预支付查询接口
     * @param request
     * @return
     */
    public PaymentDTO largePaymentQuery(PaymentDetailRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/largePaymentQuery", request, PaymentDTO.class);
    }

    /**
     * 门店账户向公司分账转账接口
     * @param request
     * @return
     */
    public TransferDTO transfer(TransferRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/transfer", request, TransferDTO.class);
    }


    /**
     * 门店账户向公司分账转账接口（批量转账接口）
     * @param request
     * @return
     */
    public BatchTransferDTO batchTransfer(BatchTransferRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/batchTransfer", request, BatchTransferDTO.class);
    }



    /**
     * 门店签约账户,退款提现至提现卡
     * @param request
     * @return
     */
    public WithDrawerDTO withdraw(WithDrawerRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/withdrawer", request, WithDrawerDTO.class);
    }

    /**
     * 账单详情
     * @param request
     * @return
     */
    public TradeRecordDTO getBillDetail(BillDetailRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/billDetail", request, TradeRecordDTO.class);
    }

    /**
     * 获取门店账户流水
     * @param request
     * @return
     */
    public BillPageDTO getBillPage(BillPageRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/billPage", request, BillPageDTO.class);
    }

    /**
     * 获取门店账户信息
     * @param request
     * @return
     */
    public CompanyListDTO getCompanyInfo(FindPageCompanyRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/base/v1/findPageCompany", request, CompanyListDTO.class);
    }

    /**
     * 获取支行信息
     * @param request
     * @return
     */
    public BankListDTO getBankList(GetBankRequest request) {
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/base/v1/findPageBank", request, BankListDTO.class);
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    public String upholdPwd(UpdatePasswordRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/upholdPwd", request, String.class);
    }

    /**
     * 获取账户列表信息
     * @param request
     * @return
     */
    public AccountPageDTO getAccountList(AccountBatchQueryRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/findAccountPage", request, AccountPageDTO.class);
    }

    /**
     * 获取账户激活链接
     * @param request
     * @return
     */
    public AccountActiveUrlDTO pushAccountActiveUrl(OutStoreIdRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/ws/v1/create", request, AccountActiveUrlDTO.class);
    }

    /**
     * 老店创建门店网商账户
     * @param request
     * @return
     */
    public StoreIdDTO oldStoreOpenAccount(OldStoreAccountCreateRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/ws/v1/create", request, StoreIdDTO.class);
    }


    /**
     * 老店创建门店网商账户V2
     * @param request
     * @return
     */
    public StoreIdDTO oldStoreOpenAccountV2(OldStoreAccountCreateV2Request request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/ws/v2/create", request, StoreIdDTO.class);
    }

    /**
     * 新店创建门店网商账户
     */
    public StoreIdDTO newStoreOpenAccount(NewStoreAccountCreateRequest request) {
        return walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/account/ws/v1/createIndividualAccount", request, StoreIdDTO.class);
    }

    /**
     * 获取交易记录列表
     * @param request
     * @return
     */
    public TradeRecordListDTO getTradeRecordList(TradeRecodePageRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/trans/v1/accRecordPage", request, TradeRecordListDTO.class);
    }

    /**
     * 网商银行重新激活发送短链接
     * @param request 网商账户重新发送激活短信Request
     * @return 网商激活短链DTO
     */
    public TextMsgSendDTO textMsgSend(TextMsgSendRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/ws/v1/textMsgSend", request, TextMsgSendDTO.class);
    }


    /**
     * 查询密码是否设置
     * @param request
     * @return
     */
    public PasswordDTO passwordIsExist(OutStoreIdRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/account/v1/existPwd", request, PasswordDTO.class);
    }

    /**
     * 查询所有的银行名称
     * @return
     */
    public List<BankInfoDTO> findBankHeadName(){
        String bankHeadName = walletHttpClientRest.postWithSign(walletBaseUrl+"/open/crm/base/v1/findBankHeadName", null, String.class);
        if (StringUtil.isNotEmpty(bankHeadName)){
            return JSONObject.parseArray(bankHeadName, BankInfoDTO.class);
        }
        return null;
    }

    /**
     * 网商充值账户查询
     */
    public WsPayAccountDTO wsPayAccountQuery(OutStoreIdRequest request) {
        return walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/account/ws/v1/queryCollectionAccount", request, WsPayAccountDTO.class);
    }

    /**
     * 批量转账分账查询
     */
    public BatchTransferDTO queryBatchTransfer(BatchTransferQueryRequest request) {
        return walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/trans/v1/queryBatchTransfer", request, BatchTransferDTO.class);
    }

    public AccountTransferDTO accountTransfer(AccountTransferRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/trans/v1/accountTransfer", request, AccountTransferDTO.class);
    }

    public OrgAccountDTO findOrgAccount(AccountPageRequest request){
        return  walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/base/v1/findOrgAccount", request, OrgAccountDTO.class);
    }

    public AccountWithdrawerDTO accountWithdrawer(AccountWithdrawerRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/trans/v1/accountWithdrawer", request, AccountWithdrawerDTO.class);
    }

    public AccountInfoDTO getAccountByAccNo(AccountDetailRequest request){
        return walletHttpClientRest.postWithSign(walletBaseUrl + "/open/crm/trans/v1/getAccountByAccNo", request, AccountInfoDTO.class);
    }




}
