package com.storlead.centre.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.centre.entity.SystemConfigItemEntity;
import com.storlead.centre.mapper.SystemConfigItemMapper;
import com.storlead.centre.service.SystemConfigItemService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 系统配置项 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2026-01-08
 */
@Service
public class SystemConfigItemServiceImpl extends MyBaseServiceImpl<SystemConfigItemMapper, SystemConfigItemEntity> implements SystemConfigItemService {

    @Override
    public SystemConfigItemEntity getSystemConfigItem(String code) {
        LambdaQueryWrapper<SystemConfigItemEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SystemConfigItemEntity::getItemCode,code);
        queryWrapper.last("limit 1");
        SystemConfigItemEntity item = this.getOne(queryWrapper);
        if (Objects.isNull(item)) {
            return null;
        }
        return item;
    }

    @Override
    public Long getCheckinDataTime() {
        SystemConfigItemEntity item = getSystemConfigItem("GET_CHECKIN_DATA_TIME");
        if (Objects.isNull(item) || StrUtil.isBlank(item.getItemFormatValue())) {
            return null;
        }
        return Long.valueOf(item.getItemFormatValue());
    }

    @Override
    public void setCheckinDataTime(Long checkinTime) {
        LambdaUpdateWrapper<SystemConfigItemEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SystemConfigItemEntity::getItemFormatValue,checkinTime.toString());
        updateWrapper.eq(SystemConfigItemEntity::getItemCode,"GET_CHECKIN_DATA_TIME");
        update(updateWrapper);
    }
}
