package com.jy.business.capital.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.mapper.CapitalAccountMapper;
import com.jy.business.capital.model.dto.CapitalAccountSelectDto;
import com.jy.business.capital.model.entity.CapitalAccount;
import com.jy.business.capital.model.table.CapitalAccountTable;
import com.jy.business.capital.model.vo.CapitalAccountVo;
import com.jy.business.corporation.model.table.CorporationTable;
import com.jy.framework.model.base.BaseDao;
import com.jy.system.service.AuthService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class CapitalAccountDao extends BaseDao<CapitalAccountMapper, CapitalAccount> {

    @Resource
    private AuthService authService;

    /**
     * 资金账户分页
     */
    public Page<CapitalAccountVo> getPage(CapitalAccountSelectDto dto) {
        CapitalAccountTable ca = CapitalAccountTable.ca;
        CorporationTable c = CorporationTable.c;

        return sql(CapitalAccountVo.class)
                .select(
                        ca.all,
                        c.name.as(CapitalAccountVo::getCorporationName)
                )
                .from(ca)
                .leftJoin(c).on(ca.corporationId.eq(c.id))
                .where(
                        ca.createUser.in(authService.getUserPermissionSet()),
                        ca.corporationId.eq(dto.getCorporationId()),
                        ca.accountAlias.like(dto.getAccountAlias()),
                        ca.depositBank.like(dto.getDepositBank()),
                        ca.accountName.like(dto.getAccountName()),
                        ca.account.like(dto.getAccount()),
                        ca.correspondentNumber.like(dto.getCorrespondentNumber())
                )
                .orderBy(
                        ca.id.desc()
                )
                .page(dto.getPage());
    }

    /**
     * 资金账户明细
     */
    public CapitalAccountVo getDetail(Long id) {
        CapitalAccountTable ca = CapitalAccountTable.ca;

        return sql(CapitalAccountVo.class)
                .select(
                        ca.all
                )
                .from(ca)
                .where(
                        ca.id.eq(id)
                )
                .one();
    }

    /**
     * 公司是否存在资金账户
     */
    public boolean corporationExists(Long id) {
        return lambdaQuery().eq(CapitalAccount::getCorporationId, id).exists();
    }

    /**
     * 资金账户余额变更
     *
     * @param capitalAccountId 资金账号id
     * @param type             类型 1收入 0支出
     * @param amount           变更金额
     * @return true/false
     */
    public boolean updateAmount(Long capitalAccountId, Integer type, BigDecimal amount) {
        return lambdaUpdate()
                .eq(CapitalAccount::getId, capitalAccountId)
                .ge(type == 0, CapitalAccount::getAmount, amount)
                .setIncrBy(type == 1, CapitalAccount::getAmount, amount)
                .setDecrBy(type == 0, CapitalAccount::getAmount, amount)
                .update();
    }

}
