package com.jy.business.capital.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.mapper.CapitalTransactionsMapper;
import com.jy.business.capital.model.dto.CapitalTransactionsSelectDto;
import com.jy.business.capital.model.entity.CapitalTransactions;
import com.jy.business.capital.model.table.CapitalAccountTable;
import com.jy.business.capital.model.table.CapitalTransactionsTable;
import com.jy.business.capital.model.vo.CapitalTransactionsVo;
import com.jy.business.corporation.model.table.CorporationShopTable;
import com.jy.business.corporation.model.table.CorporationTable;
import com.jy.framework.model.base.BaseDao;
import com.jy.system.model.table.SysUserTable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CapitalTransactionsDao extends BaseDao<CapitalTransactionsMapper, CapitalTransactions> {

    /**
     * 资金流水分页
     */
    public Page<CapitalTransactionsVo> getPage(CapitalTransactionsSelectDto dto) {
        CapitalTransactionsTable ct = CapitalTransactionsTable.ct;
        CapitalAccountTable ca = CapitalAccountTable.ca;
        CorporationTable c = CorporationTable.c;
        CorporationShopTable cs = CorporationShopTable.cs;

        return sql(CapitalTransactionsVo.class)
                .select(
                        ct.all,
                        ca.accountAlias.as(CapitalTransactionsVo::getAccountAlias),
                        ca.amount.as(CapitalTransactionsVo::getBalance),
                        ca.initialAmount.as(CapitalTransactionsVo::getInitialAmount),
                        c.name.as(CapitalTransactionsVo::getCorporationName),
                        cs.name.as(CapitalTransactionsVo::getCorporationShopName)
                )
                .from(ct)
                .leftJoin(ca).on(ct.capitalAccountId.eq(ca.id))
                .leftJoin(c).on(ca.corporationId.eq(c.id))
                .leftJoin(cs).on(cs.id.eq(ct.corporationShopId))
                .where(
                        ca.corporationId.eq(dto.getCorporationId()),
                        ct.capitalAccountId.eq(dto.getCapitalAccountId()),
                        ct.tradingTime.between(dto.getTradingTime()),
                        ct.type.eq(dto.getType()),
                        ct.targetType.eq(dto.getTargetType()),
                        ct.corporationShopId.eq(dto.getCorporationShopId())
                )
                .orderBy(
                        ct.tradingTime.desc(),
                        ct.id.desc()
                )
                .page(dto.getPage());
    }

    /**
     * 资金流水明细
     */
    public CapitalTransactionsVo getDetail(Long id) {
        CapitalTransactionsTable ct = CapitalTransactionsTable.ct;
        CapitalAccountTable ca = CapitalAccountTable.ca;
        CorporationTable c = CorporationTable.c;
        CorporationShopTable cs = CorporationShopTable.cs;

        return sql(CapitalTransactionsVo.class)
                .select(
                        ct.all,
                        ca.accountAlias.as(CapitalTransactionsVo::getAccountAlias),
                        ca.amount.as(CapitalTransactionsVo::getBalance),
                        ca.initialAmount.as(CapitalTransactionsVo::getInitialAmount),
                        c.name.as(CapitalTransactionsVo::getCorporationName),
                        cs.name.as(CapitalTransactionsVo::getCorporationShopName)
                )
                .from(ct)
                .leftJoin(ca).on(ct.capitalAccountId.eq(ca.id))
                .leftJoin(c).on(ca.corporationId.eq(c.id))
                .leftJoin(cs).on(cs.id.eq(ct.corporationShopId))
                .where(
                        ct.id.eq(id)
                )
                .one();
    }

    public boolean capitalAccountExists(Long id) {
        return lambdaQuery().eq(CapitalTransactions::getCapitalAccountId, id).exists();
    }

    public List<CapitalTransactionsVo> getByContract(List<Long> contractIdList) {
        CapitalTransactionsTable ct = CapitalTransactionsTable.ct;
        SysUserTable su = SysUserTable.su;

        return sql(CapitalTransactionsVo.class)
                .select(
                        ct.all,
                        su.nickname
                )
                .from(ct)
                .leftJoin(su).on(ct.createUser.eq(su.id))
                .where(
                        ct.targetType.eq(70),
                        ct.contractId.in(contractIdList)
                )
                .orderBy(
                        ct.tradingTime.desc(),
                        ct.id.desc()
                )
                .list();
    }
}
