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.KwpLedgerLogisticsTrackMapper;
import com.sckw.payment.model.KwpLedgerLogisticsTrack;
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 KwpLedgerLogisticsTrackService {
    private final KwpLedgerLogisticsTrackMapper logisticsTrackMapper;

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

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

    /**
     * 删除物流对账记录表
     * @param lLedgerId 物流对账单id
     */
    public void remove(Long lLedgerId) {
        LambdaQueryWrapper<KwpLedgerLogisticsTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerLogisticsTrack::getLLedgerId, lLedgerId);
        wrapper.eq(KwpLedgerLogisticsTrack::getDelFlag, Global.UN_DELETED);
        KwpLedgerLogisticsTrack logisticsTrack = new KwpLedgerLogisticsTrack();
        logisticsTrack.setUpdateTime(LocalDateTime.now());
        logisticsTrack.setDelFlag(Global.DELETED);
        logisticsTrackMapper.update(logisticsTrack, wrapper);
    }

    public KwpLedgerLogisticsTrack queryLedgered(Long ledgerId) {
        LambdaQueryWrapper<KwpLedgerLogisticsTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerLogisticsTrack::getLLedgerId, ledgerId);
        wrapper.eq(KwpLedgerLogisticsTrack::getStatus, LedgerEnum.LEDGERED);
        wrapper.eq(KwpLedgerLogisticsTrack::getDelFlag, NumberConstant.ZERO);
        wrapper.last("limit 1");
        return logisticsTrackMapper.selectOne(wrapper);
    }
}
