package com.bohuikeji.delivery.backstage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.listener.pay.PayTimoutListener;
import com.bohuikeji.delivery.backstage.mapper.TStoreWallePayMapper;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletDetailService;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletPayService;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.component.MqTool;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * <p>
 * 加盟商门店钱包明细 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-12-27
 */
@Service
@Slf4j
public class TStoreWalletPayServiceImpl extends ServiceImplBase<TStoreWallePayMapper, TStoreWalletPay> implements ITStoreWalletPayService {

    @Autowired
    ITStoreWalletPayService storeWalletPayService;
    @Autowired
    ITStoreWalletService storeWalletService;
    @Autowired
    ITStoreWalletDetailService storeWalletDetailService;
    @Autowired
    MqTool mqTool;

    @Override
    public TStoreWalletPay storeWalletCreatePay(Integer franId,String price,String payWay,String scenario) {
        TStoreWalletPay walletPay=new TStoreWalletPay();
        TStoreWallet storeWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","门店钱包").eq("fran_id",franId));
        if(storeWallet == null){
            TStoreWallet tStoreWallet = new TStoreWallet();
            tStoreWallet.setFranId(franId);
            tStoreWallet.setWalletType("门店钱包");
            tStoreWallet.setWalletState("正常");
            tStoreWallet.setCreateTime(LocalDateTime.now());
            storeWalletService.save(tStoreWallet);
        }
        log.info("过来了码:[{}]",price);
        walletPay.setPayStatus("待付款");
        walletPay.setFranId(franId);
        walletPay.setPayNum(NumberUtils.get("MD"));
        walletPay.setPayExpectPrice(new BigDecimal(price));
        walletPay.setCreateTime(LocalDateTime.now());
        walletPay.setPayScenario(scenario);
        walletPay.setPayWay(payWay);
        storeWalletPayService.save(walletPay);
        mqTool.sendDelayMsg(walletPay.getPayNum(), PayTimoutListener.class, Duration.ofHours(3));
        return walletPay;
    }

    @Override
    public boolean wxWalletPay(JSONObject jsonObject, TStoreWalletPay walletPay) {
        TStoreWallet storeWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","门店钱包").eq("fran_id",walletPay.getFranId()));
        if(storeWallet == null){
            log.error("客户信息不存在，请重新登陆");
            return false;
        }
        if("微信".equals(walletPay.getPayWay())){
            walletPay.setPayFactPrice(new BigDecimal(jsonObject.getJSONObject("amount").getString("total")).divide(new BigDecimal(100)));
            walletPay.setPayChannel("微信");
            walletPay.setPayChannelNum(jsonObject.getString("transaction_id"));
            walletPay.setPayStatus("已支付");
            walletPay.setPayWay("微信");
            walletPay.setPayTime(LocalDateTime.now());
        }
        if(storeWalletPayService.updateById(walletPay)){
            storeWallet.setWalletPrice(storeWallet.getWalletPrice().add(walletPay.getPayFactPrice()));
            storeWalletService.updateById(storeWallet);
            storeWalletDetailService.savewalletDetail(walletPay,storeWallet);
            return true;
        }
        return false;
    }

}
