package com.fjhx.account.service.transaction.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.account.entity.account.po.AccountManagement;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.entity.transaction.dto.TransactionDto;
import com.fjhx.account.entity.transaction.dto.TransactionSelectDto;
import com.fjhx.account.entity.transaction.po.Transaction;
import com.fjhx.account.entity.transaction.vo.TransactionVo;
import com.fjhx.account.mapper.transaction.TransactionMapper;
import com.fjhx.account.service.account.AccountManagementService;
import com.fjhx.account.service.account.AccountRunningWaterService;
import com.fjhx.account.service.transaction.TransactionDepartmentService;
import com.fjhx.account.service.transaction.TransactionService;
import com.fjhx.common.service.corporation.CorporationService;
import com.fjhx.common.utils.ExchangeRateUtil;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 往来管理 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-26
 */
@Service
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements TransactionService {

    @Autowired
    private AccountManagementService accountManagementService;
    @Autowired
    private TransactionDepartmentService transactionDepartmentService;
    @Autowired
    private AccountRunningWaterService accountRunningWaterService;
    @Autowired
    private CorporationService corporationService;

    @Override
    public Page<TransactionVo> getPage(TransactionSelectDto dto) {
        IWrapper<Transaction> wrapper = getWrapper();
        //条件筛选
        wrapper.eq(Transaction::getIsFlowingWater, dto.getIsFlowingWater());
        wrapper.eq(Transaction::getDepartmentId, dto.getDepartmentId());
        wrapper.eq(Transaction::getType, dto.getType());
        //关键字搜索
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            List<Long> accountIds = accountManagementService.listObject(AccountManagement::getId,
                    q -> q.like(AccountManagement::getName, dto.getKeyword()));
            wrapper.and(q -> q
                    .like(Transaction::getAmount, dto.getKeyword())
                    .or().like(Transaction::getRemark, dto.getKeyword())
                    .or().in(Transaction::getAccountId, accountIds)
            );
        }
        //排序
        wrapper.orderByDesc("t", Transaction::getId);
        Page<TransactionVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        //赋值往来单位名称
        List<TransactionVo> records = page.getRecords();
        transactionDepartmentService.attributeAssign(records, TransactionVo::getDepartmentId, (item, transactionDepartment) -> {
            item.setDepartmentName(transactionDepartment.getName());
        });
        //赋值往来账户名称
        accountManagementService.attributeAssign(records, TransactionVo::getAccountId, (item, account) -> {
            item.setAccountName(account.getName());
        });
        //赋值归属公司名称
        corporationService.attributeAssign(records, TransactionVo::getCorporationId, (item, corporation) -> {
            item.setCorporationName(corporation.getName());
        });
        return page;
    }

    @Override
    @DSTransactional
    public void add(TransactionDto transactionDto) {
        //如果是流水就创建流水数据
        if (1 == transactionDto.getIsFlowingWater()) {
            AccountRunningWater accountRunningWater = new AccountRunningWater();
            accountRunningWater.setAccountManagementId(transactionDto.getAccountId());
            accountRunningWater.setStatus(transactionDto.getType() == 0 ? "10" : "20");
            accountRunningWater.setCurrency(transactionDto.getCurrency());
            accountRunningWater.setAmount(transactionDto.getAmount());
            accountRunningWater.setRemarks(transactionDto.getRemark());
            accountRunningWater.setIsTransaction(1);
            accountRunningWater.setTransactionDeptId(transactionDto.getDepartmentId());

            accountRunningWater.setClaimAmount(BigDecimal.ZERO);
            accountRunningWater.setReceived("20");
            accountRunningWater.setIsClaim(0);

            accountRunningWater.setRate(ExchangeRateUtil.getCnyToCodeRate(accountRunningWater.getCurrency()));

            accountRunningWaterService.save(accountRunningWater);

            //赋值流水id
            transactionDto.setAccountRunningWaterId(accountRunningWater.getId());

            //操作余额
            accountRunningWaterService.changeRemainder(accountRunningWater);
        }

        this.save(transactionDto);
    }

}
