package com.jy.business.payment.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.model.table.CapitalAccountTable;
import com.jy.business.corporation.model.table.CorporationShopTable;
import com.jy.business.corporation.model.table.CorporationTable;
import com.jy.business.payment.mapper.PaymentRequestsMapper;
import com.jy.business.payment.model.dto.DetailListDto;
import com.jy.business.payment.model.dto.PaymentRequestsSelectDto;
import com.jy.business.payment.model.entity.PaymentRequests;
import com.jy.business.payment.model.table.PaymentRemitTable;
import com.jy.business.payment.model.table.PaymentRequestsDetailTable;
import com.jy.business.payment.model.table.PaymentRequestsTable;
import com.jy.business.payment.model.vo.DetailListVo;
import com.jy.business.payment.model.vo.PaymentRequestsVo;
import com.jy.flow.model.enums.FlowStatusEnum;
import com.jy.framework.model.base.BaseDao;
import com.jy.framework.model.base.BaseIdPo;
import com.jy.framework.satoken.LoginContext;
import com.jy.system.model.table.SysDeptTable;
import com.jy.system.model.table.SysUserTable;
import com.jy.system.service.AuthService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PaymentRequestsDao extends BaseDao<PaymentRequestsMapper, PaymentRequests> {

    @Resource
    private AuthService authService;

    /**
     * 请款分页
     */
    public Page<PaymentRequestsVo> getPage(PaymentRequestsSelectDto dto) {
        PaymentRequestsTable pr = PaymentRequestsTable.pr;

        // 公司信息
        CorporationTable c = CorporationTable.c;
        // 部门
        SysDeptTable sd = SysDeptTable.sd;
        // 用户
        SysUserTable su = SysUserTable.su;
        // 资金账户
        CapitalAccountTable ca = CapitalAccountTable.ca;
        // 店铺信息
        CorporationShopTable cs = CorporationShopTable.cs;

        return sql(PaymentRequestsVo.class)
                .select(
                        pr.all,
                        c.name.as(PaymentRequestsVo::getCorporationName),
                        sd.name.as(PaymentRequestsVo::getDeptName),
                        su.nickname.as(PaymentRequestsVo::getUserName),
                        ca.accountAlias.as(PaymentRequestsVo::getCapitalAccountName),
                        cs.name.as(PaymentRequestsVo::getCorporationShopName)
                )
                .from(pr)
                .leftJoin(c).on(pr.corporationId.eq(c.id))
                .leftJoin(sd).on(pr.deptId.eq(sd.id))
                .leftJoin(su).on(pr.createUser.eq(su.id))
                .leftJoin(ca).on(pr.capitalAccountId.eq(ca.id))
                .leftJoin(cs).on(pr.corporationShopId.eq(cs.id))
                .where(
                        pr.createUser.eq(LoginContext.getUserId()),
                        pr.corporationId.eq(dto.getCorporationId()),
                        pr.deptId.eq(dto.getDeptId()),
                        pr.type.eq(dto.getType()),
                        pr.payType.eq(dto.getPayType()),
                        pr.approvalStatus.eq(dto.getApprovalStatus()),
                        pr.paymentStatus.eq(dto.getPaymentStatus()),
                        pr.corporationShopId.eq(dto.getCorporationShopId())
                )
                .orderBy(
                        pr.id.desc()
                )
                .page(dto.getPage());
    }

    /**
     * 请款明细
     */
    public PaymentRequestsVo getDetail(Long id) {
        PaymentRequestsTable pr = PaymentRequestsTable.pr;

        // 公司信息
        CorporationTable c = CorporationTable.c;
        // 部门
        SysDeptTable sd = SysDeptTable.sd;
        // 用户
        SysUserTable su = SysUserTable.su;
        // 资金账户
        CapitalAccountTable ca = CapitalAccountTable.ca;
        // 店铺信息
        CorporationShopTable cs = CorporationShopTable.cs;

        return sql(PaymentRequestsVo.class)
                .select(
                        pr.all,
                        c.name.as(PaymentRequestsVo::getCorporationName),
                        sd.name.as(PaymentRequestsVo::getDeptName),
                        su.nickname.as(PaymentRequestsVo::getUserName),
                        ca.accountAlias.as(PaymentRequestsVo::getCapitalAccountName),
                        cs.name.as(PaymentRequestsVo::getCorporationShopName)
                )
                .from(pr)
                .leftJoin(c).on(pr.corporationId.eq(c.id))
                .leftJoin(sd).on(pr.deptId.eq(sd.id))
                .leftJoin(su).on(pr.createUser.eq(su.id))
                .leftJoin(ca).on(pr.capitalAccountId.eq(ca.id))
                .leftJoin(cs).on(pr.corporationShopId.eq(cs.id))
                .where(
                        pr.id.eq(id)
                )
                .one();
    }

    /**
     * 更新流程状态
     */
    public void updateFlowStatus(String businessId, String flowStatus) {
        lambdaUpdate().eq(BaseIdPo::getId, businessId).set(PaymentRequests::getApprovalStatus, flowStatus).update();
    }

    /**
     * 打款分页
     */
    public Page<PaymentRequestsVo> getRemitPage(PaymentRequestsSelectDto dto) {
        PaymentRequestsTable pr = PaymentRequestsTable.pr;

        // 公司信息
        CorporationTable c = CorporationTable.c;
        // 部门
        SysDeptTable sd = SysDeptTable.sd;
        // 用户
        SysUserTable su = SysUserTable.su;
        // 资金账户
        CapitalAccountTable ca = CapitalAccountTable.ca;
        // 店铺信息
        CorporationShopTable cs = CorporationShopTable.cs;

        return sql(PaymentRequestsVo.class)
                .select(
                        pr.all,
                        c.name.as(PaymentRequestsVo::getCorporationName),
                        sd.name.as(PaymentRequestsVo::getDeptName),
                        su.nickname.as(PaymentRequestsVo::getUserName),
                        ca.accountAlias.as(PaymentRequestsVo::getCapitalAccountName),
                        cs.name.as(PaymentRequestsVo::getCorporationShopName)
                )
                .from(pr)
                .leftJoin(c).on(pr.corporationId.eq(c.id))
                .leftJoin(sd).on(pr.deptId.eq(sd.id))
                .leftJoin(su).on(pr.createUser.eq(su.id))
                .leftJoin(ca).on(pr.capitalAccountId.eq(ca.id))
                .leftJoin(cs).on(pr.corporationShopId.eq(cs.id))
                .where(
                        pr.totalAmount.like(dto.getKeyword()).or(pr.useRemark.like(dto.getKeyword())),
                        pr.createUser.in(authService.getUserPermissionSet()),
                        pr.approvalStatus.eq(FlowStatusEnum.PASS.getKey()),
                        pr.corporationId.eq(dto.getCorporationId()),
                        pr.deptId.eq(dto.getDeptId()),
                        pr.type.eq(dto.getType()),
                        pr.payType.eq(dto.getPayType()),
                        pr.paymentStatus.eq(dto.getPaymentStatus()),
                        pr.corporationShopId.eq(dto.getCorporationShopId())
                )
                .orderBy(
                        pr.id.desc()
                )
                .page(dto.getPage());
    }

    public List<PaymentRequests> getReceiveCountList(Long userId) {
        PaymentRequestsTable pr = PaymentRequestsTable.pr;

        return sql(PaymentRequests.class)
                .distinct()
                .select(
                        pr.accountName,
                        pr.account,
                        pr.depositBank,
                        pr.correspondentNumber
                )
                .from(pr)
                .where(
                        pr.createUser.eq(userId)
                )
                .orderBy(
                        pr.id.desc()
                )
                .list();
    }

    public List<DetailListVo> getDetailList(DetailListDto dto) {
        PaymentRequestsTable pr = PaymentRequestsTable.pr;
        PaymentRequestsDetailTable prd = PaymentRequestsDetailTable.prd;
        PaymentRemitTable pre = PaymentRemitTable.payment_remit;

        return sql(DetailListVo.class)
                .select(
                        prd.amount,
                        pr.createTime.as(DetailListVo::getRequestsTime),
                        pre.createTime.as(DetailListVo::getRemitTime),
                        prd.expenseType.as(DetailListVo::getType)
                )
                .from(prd)
                .innerJoin(pr).on(prd.paymentRequestsId.eq(pr.id))
                .leftJoin(pre).on(pre.paymentRequestsId.eq(pr.id))
                .where(
                        prd.contractId.eq(dto.getContractId())
                )
                .list();
    }

}
