/*
 * 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.yami.shop.bean.enums.ShopWalletAmountType;
import com.yami.shop.bean.enums.ShopWalletChangeReason;
import com.yami.shop.bean.model.ShopWallet;
import com.yami.shop.bean.model.ShopWalletLog;
import com.yami.shop.common.util.Arith;
import com.yami.shop.dao.ShopWalletLogMapper;
import com.yami.shop.service.ShopWalletLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 商家钱包记录
 *
 * @author Dwl
 * @date 2019-09-19 14:02:57
 */
@Service
@AllArgsConstructor
public class ShopWalletLogServiceImpl extends ServiceImpl<ShopWalletLogMapper, ShopWalletLog> implements ShopWalletLogService {

    private final ShopWalletLogMapper shopWalletLogMapper;


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void saveShopWalletLog(ShopWallet oldShopWallet,ShopWallet newShopWallet , ShopWalletChangeReason shopWalletChangeReason, String orderNumber) {

        ShopWalletLog shopWalletLog = new ShopWalletLog();

        shopWalletLog.setCreateTime(new Date());

        shopWalletLog.setOrderNumber(orderNumber);
        shopWalletLog.setReason(shopWalletChangeReason.value());
        shopWalletLog.setShopId(oldShopWallet.getShopId());

        // 插入未结算金额修改记录
        if (!Objects.equals(oldShopWallet.getUnsettledAmount(), newShopWallet.getUnsettledAmount())) {
            double changeAmount = Arith.sub(oldShopWallet.getUnsettledAmount(), newShopWallet.getUnsettledAmount());
            shopWalletLog.setChangeAomunt(oldShopWallet.getUnsettledAmount() > newShopWallet.getUnsettledAmount() ? changeAmount: -changeAmount);
            shopWalletLog.setBeforeAmount(oldShopWallet.getUnsettledAmount());
            shopWalletLog.setAfterAmount(newShopWallet.getUnsettledAmount());
            shopWalletLog.setIoType(oldShopWallet.getUnsettledAmount() > newShopWallet.getUnsettledAmount() ? 0 : 1);
            shopWalletLog.setAmountType(ShopWalletAmountType.UNSETTLED_AMOUNT.value());
            shopWalletLogMapper.insert(shopWalletLog);
        }

        // 插入结算金额修改记录
        if (!Objects.equals(oldShopWallet.getSettledAmount(), newShopWallet.getSettledAmount())) {
            double changeAmount = Arith.sub(oldShopWallet.getSettledAmount(), newShopWallet.getSettledAmount());
            shopWalletLog.setChangeAomunt(oldShopWallet.getSettledAmount() > newShopWallet.getSettledAmount() ? changeAmount: -changeAmount);
            shopWalletLog.setBeforeAmount(oldShopWallet.getSettledAmount());
            shopWalletLog.setAfterAmount(newShopWallet.getSettledAmount());
            shopWalletLog.setIoType(oldShopWallet.getSettledAmount() > newShopWallet.getSettledAmount() ? 0 : 1);
            shopWalletLog.setAmountType(ShopWalletAmountType.SETTLED_AMOUNT.value());
            shopWalletLogMapper.insert(shopWalletLog);
        }

        // 插入冻结金额修改记录
        if (!Objects.equals(oldShopWallet.getFreezeAmount(), newShopWallet.getFreezeAmount())) {
            double changeAmount = Arith.sub(oldShopWallet.getFreezeAmount(), newShopWallet.getFreezeAmount());
            shopWalletLog.setChangeAomunt(oldShopWallet.getFreezeAmount() > newShopWallet.getFreezeAmount() ? changeAmount: -changeAmount);
            shopWalletLog.setBeforeAmount(oldShopWallet.getFreezeAmount());
            shopWalletLog.setAfterAmount(newShopWallet.getFreezeAmount());
            shopWalletLog.setIoType(oldShopWallet.getFreezeAmount() > newShopWallet.getFreezeAmount() ? 0 : 1);
            shopWalletLog.setAmountType(ShopWalletAmountType.FREEZE_AMOUNT.value());
            shopWalletLogMapper.insert(shopWalletLog);
        }

        // 插入总结算金额记录
        if (!Objects.equals(oldShopWallet.getTotalSettledAmount(), newShopWallet.getTotalSettledAmount())) {
            double changeAmount = Arith.sub(oldShopWallet.getTotalSettledAmount(), newShopWallet.getTotalSettledAmount());
            shopWalletLog.setChangeAomunt(oldShopWallet.getTotalSettledAmount() > newShopWallet.getTotalSettledAmount() ? changeAmount: -changeAmount);
            shopWalletLog.setBeforeAmount(oldShopWallet.getTotalSettledAmount());
            shopWalletLog.setAfterAmount(newShopWallet.getTotalSettledAmount());
            shopWalletLog.setIoType(oldShopWallet.getTotalSettledAmount() > newShopWallet.getTotalSettledAmount() ? 0 : 1);
            shopWalletLog.setAmountType(ShopWalletAmountType.TOTAL_SETTLED_AMOUNT.value());
            shopWalletLogMapper.insert(shopWalletLog);
        }
    }
}
