package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsUpgradeLogsMapper;
import com.njcn.csdevice.pojo.po.CsUpgradeLogs;
import com.njcn.csdevice.service.CsUpgradeLogsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author caozehui
 * @data 2026-04-23
 */
@Service
@RequiredArgsConstructor
public class CsUpgradeLogsServiceImpl extends ServiceImpl<CsUpgradeLogsMapper, CsUpgradeLogs> implements CsUpgradeLogsService {

    @Override
    public boolean updateLogs(String devId, String code, Integer result) {
        CsUpgradeLogs latestLog = this.baseMapper.selectOne(
                new LambdaQueryWrapper<CsUpgradeLogs>()
                        .eq(CsUpgradeLogs::getDevId, devId)
                        .orderByDesc(CsUpgradeLogs::getCreateTime)
                        .last("LIMIT 1")
        );
        if (latestLog != null) {
            LambdaUpdateWrapper<CsUpgradeLogs> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CsUpgradeLogs::getCode, code)
                    .set(CsUpgradeLogs::getResult, result)
                    .eq(CsUpgradeLogs::getId, latestLog.getId());
            return this.update(null, updateWrapper);
        }
        return false;
    }
}
