package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.exception.BasicInfoExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.clock.domain.bo.BasicInfoBo;
import com.ruoyi.clock.domain.vo.BasicInfoVo;
import com.ruoyi.clock.domain.BasicInfo;
import com.ruoyi.clock.mapper.BasicInfoMapper;
import com.ruoyi.clock.service.IBasicInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 基础设置Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class BasicInfoServiceImpl implements IBasicInfoService {

    private final BasicInfoMapper baseMapper;

    /**
     * 查询基础设置*
     * @return 基础设置
     */
    @Override
    public BasicInfo queryBasicInfo(){
        BasicInfo basicInfo = this.baseMapper.selectOne(new LambdaQueryWrapper<BasicInfo>()
            .last(" limit 1 ")
        );
        return basicInfo;
    }

    @Override
    public Boolean saveBasicInfo(BasicInfoBo bo) {
        BasicInfo basicInfo = this.queryBasicInfo();
        if(ObjectUtil.isEmpty(basicInfo)){
            basicInfo = BeanCopyUtils.copy(bo, BasicInfo.class);
            this.baseMapper.insert(basicInfo);
        } else {
            basicInfo.setAboutUs(bo.getAboutUs());
            basicInfo.setProtocol(bo.getProtocol());
            this.baseMapper.updateById(basicInfo);
        }
        return true;
    }
}
