package com.sz.admin.monitor.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.monitor.mapper.GridCfgMapper;
import com.sz.admin.monitor.pojo.dto.dataModel.GridCfgDTO;
import com.sz.admin.monitor.pojo.po.GridCfgDO;
import com.sz.admin.monitor.service.GridCfgService;
import org.springframework.stereotype.Service;

/**
 * @author xq
 * @description:网格
 * @date 2026/1/29 11:45
 */
@Service
public class GridCfgServiceImpl extends ServiceImpl<GridCfgMapper, GridCfgDO> implements GridCfgService {
    @Override
    public void saveOrUpdateGrid(GridCfgDTO dto) {
        if (ObjectUtil.isEmpty(dto))
            return;
        GridCfgDO gridCfgDO = GridCfgDTO.dtoConvertDO(dto);
        if (StrUtil.isEmpty(gridCfgDO.getUserId())) {
            saveOrUpdate(gridCfgDO);
            return;
        }

        QueryWrapper queryWrapper = QueryWrapper.create()
            .from(GridCfgDO.class)
            .where(GridCfgDO.USER_ID, gridCfgDO.getUserId())
            .limit(1);

        GridCfgDO one = getOne(queryWrapper);

//        GridCfgDO one = getOne(new QueryWrapper<GridCfgDO>()
//            .eq(GridCfgDO.USER_ID, gridCfgDO.getUserId())
//            .last("limit 1")
//        );
        if (ObjectUtil.isEmpty(one)) {
            saveOrUpdate(gridCfgDO);
            return;
        }
        gridCfgDO.setId(one.getId());
        saveOrUpdate(gridCfgDO);
    }
}
