package com.sckw.payment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.core.common.enums.NumberConstant;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.IdWorker;
import com.sckw.payment.dao.KwpLedgerTradeTrackMapper;
import com.sckw.payment.model.KwpLedgerTradeTrack;
import com.sckw.payment.model.constant.LedgerEnum;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */

@Service
@AllArgsConstructor
public class KwpLedgerTradeTrackService {
    private final KwpLedgerTradeTrackMapper tradeTrackMapper;

    /**
     * 保存操作日志
     *
     * @param t_ledger_id 对账单id
     * @param remark      备注
     * @param status      状态值
     */
    public void saveTrack(Long t_ledger_id, String remark, LedgerEnum status) {
        KwpLedgerTradeTrack tradeTrack = new KwpLedgerTradeTrack();
        tradeTrack.setId(new IdWorker(NumberConstant.ONE).nextId());
        tradeTrack.setTLedgerId(t_ledger_id);
        tradeTrack.setRemark(remark);
        tradeTrack.setStatus(status.getStatus());
        tradeTrackMapper.insert(tradeTrack);
    }

    /**
     * 查询订单记录
     *
     * @param id     物流对账单id notnull
     * @param status 流程状态
     * @return
     */
    public KwpLedgerTradeTrack selectOne(@NotNull Long id, LedgerEnum status) {
        LambdaQueryWrapper<KwpLedgerTradeTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerTradeTrack::getTLedgerId, id);
        wrapper.eq(Objects.nonNull(status), KwpLedgerTradeTrack::getStatus, status.getStatus());
        wrapper.eq(KwpLedgerTradeTrack::getDelFlag, NumberConstant.ZERO);
        wrapper.orderByDesc(KwpLedgerTradeTrack::getCreateTime);
        wrapper.last("limit 1");
        return tradeTrackMapper.selectOne(wrapper);
    }

    /**
     * 根据对账单id删除队长记录
     *
     * @param tLedgerId
     */
    public void remove(Long tLedgerId) {
        LambdaQueryWrapper<KwpLedgerTradeTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerTradeTrack::getTLedgerId, tLedgerId);
        wrapper.eq(KwpLedgerTradeTrack::getDelFlag, NumberConstant.ZERO);
        KwpLedgerTradeTrack tradeTrack = new KwpLedgerTradeTrack();
        tradeTrack.setUpdateTime(LocalDateTime.now());
        tradeTrack.setDelFlag(Global.DELETED);
        tradeTrackMapper.update(tradeTrack, wrapper);
    }

    /**
     * 查询已对账的操作记录
     *
     * @param id
     * @return
     */
    public KwpLedgerTradeTrack queryLedgered(Long id) {
        LambdaQueryWrapper<KwpLedgerTradeTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerTradeTrack::getTLedgerId, id);
        wrapper.eq(KwpLedgerTradeTrack::getStatus, LedgerEnum.LEDGERED);
        wrapper.eq(KwpLedgerTradeTrack::getDelFlag, NumberConstant.ZERO);
        wrapper.last("limit 1");
        return tradeTrackMapper.selectOne(wrapper);
    }
}
