/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.yami.shop.bean.enums.EnterpriseApplyType;
import com.yami.shop.bean.event.EnterprisePayFailEvent;
import com.yami.shop.bean.event.EnterprisePaySuccessEvent;
import com.yami.shop.bean.model.EnterprisePay;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.Arith;
import com.yami.shop.config.WxConfig;
import com.yami.shop.dao.EnterprisePayMapper;
import com.yami.shop.service.EnterprisePayService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * @author
 * @date 2019-10-15 16:44:50
 */
@Service
@AllArgsConstructor
public class EnterprisePayServiceImpl extends ServiceImpl<EnterprisePayMapper, EnterprisePay> implements EnterprisePayService {

    private final EnterprisePayMapper enterprisePayMapper;

    private final ApplicationContext applicationContext;

    private static final String CLIENT_IP = "127.0.0.1";

    private final WxConfig wxConfig;


    @Override
    public List<EnterprisePay> listApplyEnterprisePay() {
        return enterprisePayMapper.listApplyEnterprisePay();
    }

    @Override
    public void sendEnterprisePay(List<EnterprisePay> enterprisePayList) {
        if (CollectionUtils.isEmpty(enterprisePayList)) {
            return;
        }
        for (EnterprisePay enterprisePay : enterprisePayList) {
            try {
                EntPayRequest entPayRequest = new EntPayRequest();
                entPayRequest.setPartnerTradeNo(String.valueOf(enterprisePay.getEntPayOrderNo()));
                entPayRequest.setOpenid(enterprisePay.getOpenId());
                entPayRequest.setAmount((int) Arith.mul(enterprisePay.getAmount(), 100.0));
                entPayRequest.setCheckName("NO_CHECK");
                if (Objects.equals(enterprisePay.getType(), EnterpriseApplyType.SHOP_WITHDRAW.value())) {
                    entPayRequest.setDescription("商家提现发放");
                } else {
                    entPayRequest.setDescription("提现发放");
                }
                entPayRequest.setSpbillCreateIp(CLIENT_IP);
                wxConfig.getEntPayService().entPay(entPayRequest);
            } catch (WxPayException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySuccess(EnterprisePay enterprisePay) {
        //更新企业支付信息
        if (enterprisePayMapper.paySuccess(enterprisePay.getEntPayOrderNo(), enterprisePay.getVersion()) != 1) {
            throw new GlobalException("更新余额记录失败，请稍后再试");
        }
        // 推送支付成功事件
        applicationContext.publishEvent(new EnterprisePaySuccessEvent(enterprisePay));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payFailed(EnterprisePay enterprisePay) {
        //更新企业支付信息
        if (enterprisePayMapper.payFailed(enterprisePay.getEntPayOrderNo(), enterprisePay.getVersion()) != 1) {
            throw new GlobalException("更新余额记录失败，请稍后再试");
        }
        // 推送支付失败事件
        applicationContext.publishEvent(new EnterprisePayFailEvent(enterprisePay));

    }
}
