package com.sckw.payment.service;

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

import java.time.LocalDateTime;
import java.util.List;
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 Integer saveTrack(Long t_ledger_id, String remark, LedgerTrackEnum status) {
        KwpLedgerTradeTrack tradeTrack = new KwpLedgerTradeTrack();
        tradeTrack.setId(new IdWorker(1).nextId());
        tradeTrack.setTLedgerId(t_ledger_id);
        tradeTrack.setRemark(remark);
        tradeTrack.setStatus(status.getStatus());
        return tradeTrackMapper.insert(tradeTrack);
    }

    /**
     * 查询订单记录
     *
     * @param id     物流对账单id notnull
     * @param status 流程状态
     * @return
     */
    public List<KwpLedgerTradeTrack> selectList(@NotNull Long id, LedgerTrackEnum status) {
        LambdaQueryWrapper<KwpLedgerTradeTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerTradeTrack::getTLedgerId, id);
        wrapper.eq(Objects.nonNull(status), KwpLedgerTradeTrack::getStatus, status.getStatus());
        wrapper.eq(KwpLedgerTradeTrack::getDelFlag, 0);
        return tradeTrackMapper.selectList(wrapper);
    }

    public void remove(Long tLedgerId) {
        LambdaQueryWrapper<KwpLedgerTradeTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerTradeTrack::getTLedgerId, tLedgerId);
        wrapper.eq(KwpLedgerTradeTrack::getDelFlag, 0);
        KwpLedgerTradeTrack tradeTrack = new KwpLedgerTradeTrack();
        tradeTrack.setUpdateTime(LocalDateTime.now());
        tradeTrack.setDelFlag(1);
        tradeTrackMapper.update(tradeTrack, wrapper);
    }
}
