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

import cn.hutool.core.util.ObjectUtil;
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.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.transaction.TransactionDepartmentService;
import com.fjhx.account.service.transaction.TransactionService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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;

    @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);


        //权限过滤：往来管理
        wrapper.in("t", Transaction::getCompanyId, SecurityUtils.getCompanyIds());
        wrapper.eq("t", Transaction::getCompanyId, dto.getCompanyId());


        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());
        });
        return page;
    }

    @Override
    public void add(TransactionDto transactionDto) {
        //赋值归属公司
        transactionDto.setCompanyId(SecurityUtils.getCompanyId());
        this.save(transactionDto);
    }

}
