/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.util.Arith;
import com.yami.shop.distribution.comment.dao.DistributionUserWalletBillMapper;
import com.yami.shop.distribution.comment.dao.DistributionUserWalletMapper;
import com.yami.shop.distribution.comment.dto.DistributionUserWalletDto;
import com.yami.shop.distribution.comment.model.DistributionUserWallet;
import com.yami.shop.distribution.comment.model.DistributionUserWalletBill;
import com.yami.shop.distribution.comment.service.DistributionUserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author lgh on 2019/04/01.
 */
@Service
public class DistributionUserWalletServiceImpl extends ServiceImpl<DistributionUserWalletMapper, DistributionUserWallet> implements DistributionUserWalletService {

    @Autowired
    private DistributionUserWalletMapper distributionUserWalletMapper;

    @Autowired
    private DistributionUserWalletBillMapper distributionUserWalletBillMapper;

    @Override
    public Double getSettledAmountByDistributionUserId(Long distributionUserId) {
        return distributionUserWalletMapper.getSettledAmountByDistributionUserId(distributionUserId);
    }

    @Override
    public Long getWalletIdByDistributionUserId(Long distributionUserId) {
        return distributionUserWalletMapper.getWalletIdByDistributionUserId(distributionUserId);
    }

    @Override
    public Page<DistributionUserWallet> getDistributionUserWalletAndDistributionUserVOPage(Page page, String userMobile) {
        return distributionUserWalletMapper.getDistributionUserWalletAndDistributionUserVOPage(page,userMobile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDistributionUserWallet(DistributionUserWallet distributionUserWallet, Long modifier) {
        //获取到变动前的钱包
        DistributionUserWallet oldDistributionUserWallet=distributionUserWalletMapper.selectById(distributionUserWallet.getWalletId());
        //变动数额
        Double unsettledAmount = Arith.sub(distributionUserWallet.getUnsettledAmount(), oldDistributionUserWallet.getUnsettledAmount()) ;
        Double settledAmount = Arith.sub(distributionUserWallet.getSettledAmount(), oldDistributionUserWallet.getSettledAmount());
        Double invalidAmount =Arith.sub(distributionUserWallet.getInvalidAmount(), oldDistributionUserWallet.getInvalidAmount());
        Double addupAmount = Arith.sub(distributionUserWallet.getAddupAmount(), oldDistributionUserWallet.getAddupAmount());
        //修改钱包
        distributionUserWalletMapper.updateById(distributionUserWallet);
        //增加钱包流水记录
        DistributionUserWalletBill distributionUserWalletBill = new DistributionUserWalletBill(distributionUserWallet,"人工修改",unsettledAmount,settledAmount,invalidAmount,addupAmount,1);
        distributionUserWalletBill.setModifier(modifier);
        distributionUserWalletBillMapper.insert(distributionUserWalletBill);
    }

    @Override
    public DistributionUserWalletDto getDistributionUserWalletDtoByDistributionUserId(Long distributionUserId) {
        return distributionUserWalletMapper.getDistributionUserWalletDtoByDistributionUserId(distributionUserId);
    }


}
