package com.zswl.cloud.springBtach.server.core.service;

import com.github.microservice.auth.client.content.ResultContent;
import com.github.microservice.auth.client.content.ResultState;
import com.github.microservice.auth.security.helper.AuthHelper;
import com.zhongshu.payment.client.model.order.AdminSeachOrderModel;
import com.zhongshu.payment.client.model.order.v2.ApplicantRefundParam2Model;
import com.zhongshu.payment.client.model.order.v2.RefundParam2Model;
import com.zhongshu.payment.client.model.order.v2.WithdrawRequestModel;
import com.zhongshu.payment.client.model.payment.QueryResponseModel;
import com.zhongshu.payment.client.model.payment.zswl.CreatePayment2Model;
import com.zhongshu.payment.client.model.payment.zswl.CreatePaymentModel;
import com.zhongshu.payment.client.model.payment.zswl.SyncInformModel;
import com.zhongshu.payment.client.service.v2.PaymentService2;
import com.zswl.cloud.shop.client.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentService {


    @Autowired
    PaymentService2 paymentService2;

    @Autowired
    AuthHelper authHelper;

    @Autowired
    GoodsService goodsService;


    public ResultContent creatPayOrder(CreatePaymentModel createPaymentModel) {
        CreatePayment2Model createPayment2Model = new CreatePayment2Model();
        BeanUtils.copyProperties(createPaymentModel, createPayment2Model);
        createPayment2Model.setAuthUserId(authHelper.getCurrentUser().getUserId());

        return paymentService2.creatPayOrder(createPayment2Model);
    }

    public ResultContent query(String orderNo) {

        String userId = authHelper.getCurrentUser().getUserId();
        ResultContent<QueryResponseModel> query = paymentService2.query(userId, orderNo);
        QueryResponseModel content = query.getContent();

        if (query.getState().equals(ResultState.Success) && content.getStatus().equals("TRADE_SUCCESS")) {
            //todo 处理发货
            return ResultContent.buildContent(content);
        }
        return query;
    }

    public ResultContent applicantRefund(ApplicantRefundParam2Model applicantRefundParam2Model) {

        String userId = authHelper.getCurrentUser().getUserId();
        applicantRefundParam2Model.setUserId(userId);
        return paymentService2.applicantRefund(applicantRefundParam2Model);
    }


    public ResultContent unRefund(ApplicantRefundParam2Model applicantRefundParam2Model) {
        String userId = authHelper.getCurrentUser().getUserId();
        applicantRefundParam2Model.setUserId(userId);
        return paymentService2.unRefund(applicantRefundParam2Model);
    }

    public ResultContent auditRefund(RefundParam2Model refundParam2Model) {
        return paymentService2.auditRefund(refundParam2Model);
    }

    public ResultContent close(String orderNo) {
        String userId = authHelper.getCurrentUser().getUserId();
        return paymentService2.close(orderNo, userId);
    }

    public ResultContent transfer(String id) {
        return paymentService2.transfer(id);
    }

    public ResultContent transferList(AdminSeachOrderModel seachOrderModel) {
        String enterPriseId = authHelper.getEnterPriseId();
        seachOrderModel.setShopId(enterPriseId);
        return paymentService2.transferList(seachOrderModel);
    }

    public ResultContent getDetail() {
        return paymentService2.getDetail(authHelper.getEnterPriseId());
    }

    public ResultContent platformDetail() {
        return paymentService2.getDetail("PLATFORM");
    }

    public ResultContent withdrawals() {
        return paymentService2.applicationWithdrawals(authHelper.getEnterPriseId());
    }

    public ResultContent withdrawalsPlatform() {
        return paymentService2.applicationWithdrawals("PLATFORM");
    }

    public ResultContent withdrawalsList(WithdrawRequestModel withdrawRequestModel) {
        String enterPriseId = authHelper.getEnterPriseId();
        withdrawRequestModel.setShopId(enterPriseId);
        return paymentService2.withdrawalsList(withdrawRequestModel);
    }

    public ResultContent withdrawalsPlatformList(WithdrawRequestModel withdrawRequestModel) {
        withdrawRequestModel.setShopId("PLATFORM");

        return paymentService2.withdrawalsList(withdrawRequestModel);
    }

    public ResultContent withdrawalsDetailList(WithdrawRequestModel withdrawRequestModel) {
        String enterPriseId = authHelper.getEnterPriseId();
        withdrawRequestModel.setShopId(enterPriseId);
        return paymentService2.withdrawalsDetailList(withdrawRequestModel);
    }

    public ResultContent subOrdersConfirm(String goodsId, String useShopId) {
        return paymentService2.subOrdersConfirm(goodsId, useShopId);
    }

    public String sync(SyncInformModel syncInformModel) {
        paymentService2.sync(syncInformModel);
        return "SUCCESS";
    }


}
