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.CorporationTable;
import com.jy.business.payment.mapper.PaymentRequestsMapper;
import com.jy.business.payment.model.dto.PaymentRequestsSelectDto;
import com.jy.business.payment.model.entity.PaymentRequests;
import com.jy.business.payment.model.table.PaymentRequestsTable;
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;

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

        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)
                )
                .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))
                .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())
                )
                .orderBy(
                        pr.id.desc()
                )
                .page(dto.getPage());
    }

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

        // 部门
        SysDeptTable sd = SysDeptTable.sd;
        // 用户
        SysUserTable su = SysUserTable.su;

        return sql(PaymentRequestsVo.class)
                .select(
                        pr.all,
                        sd.name.as(PaymentRequestsVo::getDeptName),
                        su.nickname.as(PaymentRequestsVo::getUserName)
                )
                .from(pr)
                .leftJoin(sd).on(pr.deptId.eq(sd.id))
                .leftJoin(su).on(pr.createUser.eq(su.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;

        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)
                )
                .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))
                .where(
                        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())
                )
                .orderBy(
                        pr.id.desc()
                )
                .page(dto.getPage());
    }

}
