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

package com.yami.shop.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.enums.EnterpriseApplyType;
import com.yami.shop.bean.enums.ShopWalletChangeReason;
import com.yami.shop.bean.model.EnterprisePay;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopWallet;
import com.yami.shop.bean.model.ShopWithdrawCash;
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.common.util.PageParam;
import com.yami.shop.dao.EnterprisePayMapper;
import com.yami.shop.dao.ShopWithdrawCashMapper;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopWalletLogService;
import com.yami.shop.service.ShopWalletService;
import com.yami.shop.service.ShopWithdrawCashService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 商家提现申请信息
 *
 * @author
 * @date 2019-09-19 14:22:08
 */
@Service
@AllArgsConstructor
public class ShopWithdrawCashServiceImpl extends ServiceImpl<ShopWithdrawCashMapper, ShopWithdrawCash> implements ShopWithdrawCashService {

    private final ShopWithdrawCashMapper shopWithdrawCashMapper;

    private final ShopWalletLogService shopWalletLogService;

    private final ShopWalletService shopWalletService;

    private final MapperFacade mapperFacade;

    private final Snowflake snowflake;

    private final ShopDetailService shopDetailService;

    private final EnterprisePayMapper enterprisePayMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withdrawCash(ShopWithdrawCash shopWithdrawCash) {

        Long shopId = shopWithdrawCash.getShopId();
        ShopWallet dbShopWallet = shopWalletService.getShopWalletByShopId(shopId);

        if (shopWithdrawCash.getAmount() > dbShopWallet.getSettledAmount()) {
            throw new GlobalException("可提现金额不足");
        }

        // 插入提现申请记录
        shopWithdrawCashMapper.insert(shopWithdrawCash);

        ShopWallet newShopWallet = mapperFacade.map(dbShopWallet, ShopWallet.class);
        newShopWallet.setSettledAmount(Arith.sub(dbShopWallet.getSettledAmount(), shopWithdrawCash.getAmount()));
        newShopWallet.setFreezeAmount(Arith.add(dbShopWallet.getFreezeAmount(), shopWithdrawCash.getAmount()));

        // 乐观锁更新钱包信息
        if (!shopWalletService.updateById(newShopWallet)) {
            // 失败则抛异常
            throw new GlobalException("服务器繁忙");
        }
        // 提现记录
        shopWalletLogService.saveShopWalletLog(dbShopWallet, newShopWallet, ShopWalletChangeReason.APPLY_CASH, null);
    }

    @Override
    public IPage<ShopWithdrawCash> pageShopWithdrawCash(PageParam<ShopWithdrawCash> page, ShopWithdrawCash shopWithdrawCash) {
        return shopWithdrawCashMapper.pageShopWithdrawCash(page, shopWithdrawCash);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditWithdrawCash(Long withdrawCashId, ShopWithdrawCash shopWithdrawCash, Long userId, String openId) {
        ShopWithdrawCash dbShopWithdrawCash = shopWithdrawCashMapper.selectById(withdrawCashId);
        if (!dbShopWithdrawCash.getStatus().equals(AuditStatus.WAITAUDIT.value())) {
            throw new GlobalException("该申请已被审核");
        }
        Date now = new Date();
        dbShopWithdrawCash.setAuditingTime(now);
        dbShopWithdrawCash.setAuditorId(userId);
        dbShopWithdrawCash.setStatus(shopWithdrawCash.getStatus());
        dbShopWithdrawCash.setRemarks(shopWithdrawCash.getRemarks());
        dbShopWithdrawCash.setPayingTime(shopWithdrawCash.getPayingTime());
        dbShopWithdrawCash.setPayingCardNo(shopWithdrawCash.getPayingCardNo());
        dbShopWithdrawCash.setPayingAccount(shopWithdrawCash.getPayingAccount());

        ShopWallet dbShopWallet = shopWalletService.getShopWalletByShopId(dbShopWithdrawCash.getShopId());
        ShopWallet newShopWallet = mapperFacade.map(dbShopWallet, ShopWallet.class);
        // 审核通过
        if (Objects.equals(shopWithdrawCash.getStatus(), 1)) {
            //减去冻结的金额
            newShopWallet.setFreezeAmount(Arith.sub(dbShopWallet.getFreezeAmount(), dbShopWithdrawCash.getAmount()));
            shopWalletLogService.saveShopWalletLog(dbShopWallet, newShopWallet, ShopWalletChangeReason.PASS_CASH, null);
        } else if (Objects.equals(shopWithdrawCash.getStatus(), -1)) {
            // 将冻结的金额返回到已结算金额
            newShopWallet.setSettledAmount(Arith.add(dbShopWallet.getSettledAmount(), dbShopWithdrawCash.getAmount()));
            newShopWallet.setFreezeAmount(Arith.sub(dbShopWallet.getFreezeAmount(), dbShopWithdrawCash.getAmount()));
            shopWalletLogService.saveShopWalletLog(dbShopWallet, newShopWallet, ShopWalletChangeReason.REFUSE_CASH, null);
        } else {
            throw new GlobalException("审核状态有误");
        }

        // 乐观锁更新钱包信息
        if (!shopWalletService.updateById(newShopWallet)) {
            // 失败则抛异常
            throw new GlobalException("服务器繁忙");
        }
        shopWithdrawCashMapper.updateById(dbShopWithdrawCash);
    }

    @Override
    public ShopWithdrawCash getShopWithdrawCashByShopId(Long shopId) {
        return shopWithdrawCashMapper.getShopWithdrawCashByShopId(shopId);
    }
}
