package com.jy.business.contract.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.model.table.CapitalAccountTable;
import com.jy.business.contract.mapper.ContractFundReceivedMapper;
import com.jy.business.contract.model.dto.ContractFundReceivedSelectDto;
import com.jy.business.contract.model.entity.ContractFundReceived;
import com.jy.business.contract.model.table.ContractFundReceivedTable;
import com.jy.business.contract.model.table.ContractInfoTable;
import com.jy.business.contract.model.vo.ContractFundReceivedVo;
import com.jy.framework.model.base.BaseDao;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContractFundReceivedDao extends BaseDao<ContractFundReceivedMapper, ContractFundReceived> {

    public List<ContractFundReceivedVo> getList(ContractFundReceivedSelectDto dto) {
        ContractFundReceivedTable cfr = ContractFundReceivedTable.cfr;
        ContractInfoTable ci = ContractInfoTable.ci;
        CapitalAccountTable ca = CapitalAccountTable.ca;

        return sql(ContractFundReceivedVo.class)
                .select(
                        cfr.all,
                        ci.contractNo,
                        ca.accountAlias
                )
                .from(cfr)
                .leftJoin(ci).on(ci.id.eq(cfr.contractId))
                .leftJoin(ca).on(ca.id.eq(cfr.capitalAccountId))
                .where(
                        cfr.hasReceived.eq(dto.getHasReceived()),
                        ci.contractNo.like(dto.getContractNo()),
                        ci.id.eq(dto.getContractId())
                )
                .orderBy(
                        cfr.id.desc()
                )
                .list();
    }

    /**
     * 合同到账分页
     */
    public Page<ContractFundReceivedVo> getPage(ContractFundReceivedSelectDto dto) {
        ContractFundReceivedTable cfr = ContractFundReceivedTable.cfr;
        ContractInfoTable ci = ContractInfoTable.ci;
        CapitalAccountTable ca = CapitalAccountTable.ca;

        return sql(ContractFundReceivedVo.class)
                .select(
                        cfr.all,
                        ci.contractNo,
                        ca.accountAlias
                )
                .from(cfr)
                .leftJoin(ci).on(ci.id.eq(cfr.contractId))
                .leftJoin(ca).on(ca.id.eq(cfr.capitalAccountId))
                .where(
                        cfr.hasReceived.eq(dto.getHasReceived()),
                        ci.contractNo.like(dto.getContractNo())
                )
                .orderBy(
                        cfr.id.desc()
                )
                .page(dto.getPage());
    }

    /**
     * 合同到账明细
     */
    public ContractFundReceivedVo getDetail(Long id) {
        ContractFundReceivedTable cfr = ContractFundReceivedTable.cfr;
        ContractInfoTable ci = ContractInfoTable.ci;

        return sql(ContractFundReceivedVo.class)
                .select(
                        cfr.all,
                        ci.contractNo,
                        ci.sellBankAccount
                )
                .from(cfr)
                .leftJoin(ci).on(ci.id.eq(cfr.contractId))
                .where(
                        cfr.id.eq(id)
                )
                .one();
    }

}
