package com.jy.business.capital.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.dao.CapitalAccountDao;
import com.jy.business.capital.dao.CapitalTransactionsDao;
import com.jy.business.capital.model.dto.CapitalTransactionsDto;
import com.jy.business.capital.model.dto.CapitalTransactionsSelectDto;
import com.jy.business.capital.model.entity.CapitalTransactions;
import com.jy.business.capital.model.vo.CapitalTransactionsVo;
import com.jy.business.capital.service.CapitalTransactionsService;
import com.jy.framework.utils.AssertUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 资金流水 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-16
 */
@Service
public class CapitalTransactionsServiceImpl implements CapitalTransactionsService {

    @Resource
    private CapitalTransactionsDao capitalTransactionsDao;

    @Resource
    private CapitalAccountDao capitalAccountDao;

    @Override
    public Page<CapitalTransactionsVo> getPage(CapitalTransactionsSelectDto dto) {
        return capitalTransactionsDao.getPage(dto);
    }

    @Override
    public CapitalTransactionsVo getDetail(Long id) {
        CapitalTransactionsVo vo = capitalTransactionsDao.getDetail(id);
        AssertUtil.notNull(vo, "未知数据");
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(CapitalTransactionsDto dto) {
        boolean updateAmount = capitalAccountDao.updateAmount(dto.getCapitalAccountId(), dto.getType(), dto.getAmount());
        AssertUtil.eqTrue(updateAmount, "账户余额不足");

        capitalTransactionsDao.save(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(CapitalTransactionsDto dto) {
        CapitalTransactions oldCapitalTransactions = capitalTransactionsDao.getById(dto.getId());
        AssertUtil.notNull(oldCapitalTransactions, "未知资金流水");

        boolean updateOldAmount = capitalAccountDao.updateAmount(
                oldCapitalTransactions.getCapitalAccountId(),
                oldCapitalTransactions.getType() == 1 ? 0 : 1,
                oldCapitalTransactions.getAmount());
        AssertUtil.eqTrue(updateOldAmount, "账户余额不足");

        boolean updateAmount = capitalAccountDao.updateAmount(dto.getCapitalAccountId(), dto.getType(), dto.getAmount());
        AssertUtil.eqTrue(updateAmount, "账户余额不足");

        capitalTransactionsDao.updateById(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Long> idList) {
        capitalTransactionsDao.listByIds(idList).forEach(capitalTransactions -> {
            boolean updateAmount = capitalAccountDao.updateAmount(
                    capitalTransactions.getCapitalAccountId(),
                    capitalTransactions.getType() == 0 ? 1 : 0,
                    capitalTransactions.getAmount());

            AssertUtil.eqTrue(updateAmount, "账户余额不足");
        });

        capitalTransactionsDao.removeBatchByIds(idList);
    }

}
