src/main/java/com/ruoyi/account/service/impl/AccountServiceImpl.java
@@ -1,36 +1,124 @@
package com.ruoyi.account.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.dto.AccountDto3;
import com.ruoyi.account.mapper.AccountIncomeMapper;
import com.ruoyi.account.dto.AccountTransferDto;
import com.ruoyi.account.mapper.AccountMapper;
import com.ruoyi.account.pojo.Account;
import com.ruoyi.account.pojo.AccountIncome;
import com.ruoyi.account.service.AccountIncomeService;
import com.ruoyi.account.service.AccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dto.DateQueryDto;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysDictDataMapper;
import com.ruoyi.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
@AllArgsConstructor
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    private AccountMapper accountMapper;
    @Override
    public IPage<Account> accountListPage(Page page, Account account) {
        return accountMapper.accountListPage(page, account);
    }
    /**
     * 流水类型 -> 收支方向
     * 0收入 +1, 1支出 -1, 3存入 +1, 4取出 -1, 2转账由 transfer() 处理
     */
    private Integer resolveDirection(String flowType) {
        if (flowType == null) {
            return null;
        }
        switch (flowType) {
            case "0":
            case "3":
                return 1;
            case "1":
            case "4":
                return -1;
            default:
                return null;
        }
    }
    @Override
    public boolean saveFlow(Account account) {
        if (account.getAccountId() == null) {
            throw new ServiceException("资金账户不能为空");
        }
        if (account.getAmount() == null || account.getAmount().signum() <= 0) {
            throw new ServiceException("流水金额必须大于0");
        }
        if ("2".equals(account.getFlowType())) {
            throw new ServiceException("转账请使用转账接口,以确保生成成对流水");
        }
        if (account.getDirection() == null) {
            account.setDirection(resolveDirection(account.getFlowType()));
        }
        if (account.getDirection() == null) {
            throw new ServiceException("请选择有效的流水类型");
        }
        if (account.getAccountDate() == null) {
            account.setAccountDate(new Date());
        }
        return this.save(account);
    }
    @Override
    public boolean updateFlow(Account account) {
        if (account.getId() == null) throw new ServiceException("流水ID不能为空");
        Account existing = this.getById(account.getId());
        if (existing == null) throw new ServiceException("资金流水不存在");
        if ("2".equals(existing.getFlowType()) || "2".equals(account.getFlowType())) {
            throw new ServiceException("转账成对流水不可单独编辑");
        }
        if (account.getAccountId() == null) throw new ServiceException("资金账户不能为空");
        if (account.getAmount() == null || account.getAmount().signum() <= 0) throw new ServiceException("流水金额必须大于0");
        Integer direction = resolveDirection(account.getFlowType());
        if (direction == null) throw new ServiceException("请选择有效的流水类型");
        account.setDirection(direction);
        return this.updateById(account);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean transfer(AccountTransferDto dto) {
        if (dto.getFromAccountId() == null || dto.getToAccountId() == null) {
            throw new ServiceException("转出/转入账户不能为空");
        }
        if (dto.getFromAccountId().equals(dto.getToAccountId())) {
            throw new ServiceException("转出与转入账户不能相同");
        }
        if (dto.getAmount() == null || dto.getAmount().signum() <= 0) {
            throw new ServiceException("转账金额必须大于0");
        }
        Date date = dto.getTransferDate() == null ? new Date() : dto.getTransferDate();
        Account out = new Account();
        out.setAccountDate(date);
        out.setAccountId(dto.getFromAccountId());
        out.setFlowType("2");
        out.setDirection(-1);
        out.setCounterAccountId(dto.getToAccountId());
        out.setAmount(dto.getAmount());
        out.setRemark(dto.getRemark());
        Account in = new Account();
        in.setAccountDate(date);
        in.setAccountId(dto.getToAccountId());
        in.setFlowType("2");
        in.setDirection(1);
        in.setCounterAccountId(dto.getFromAccountId());
        in.setAmount(dto.getAmount());
        in.setRemark(dto.getRemark());
        this.save(out);
        this.save(in);
        return true;
    }
}