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.CanvasCfgMapper;
import com.sz.admin.monitor.pojo.dto.dataModel.CanvasCfgDTO;
import com.sz.admin.monitor.pojo.po.CanvasCfgDO;
import com.sz.admin.monitor.service.CanvasCfgService;
import org.springframework.stereotype.Service;

/**
 * @author xq
 * @description: 画板
 * @date 2026/1/29 11:34
 */
@Service
public class CanvasCfgServiceImpl extends ServiceImpl<CanvasCfgMapper, CanvasCfgDO> implements CanvasCfgService {
    @Override
    public void saveOrUpdateCanvas(CanvasCfgDTO canvasCfgDTO) {
        if(ObjectUtil.isEmpty(canvasCfgDTO))
            return;
        CanvasCfgDO canvasCfgDO = CanvasCfgDTO.dtoConvertDO(canvasCfgDTO);
        if(StrUtil.isEmpty(canvasCfgDO.getUserId())) {
            saveOrUpdate(canvasCfgDO);
            return;
        }

        QueryWrapper queryWrapper = QueryWrapper
            .create()
            .from(CanvasCfgDO.class)
            .where(CanvasCfgDO.USER_ID, canvasCfgDTO.getUserId())
            .limit(1);
        CanvasCfgDO one = getOne(queryWrapper);

//        CanvasCfgDO one = getOne(new QueryWrapper<CanvasCfgDO>()
//            .eq(CanvasCfgDO.USER_ID, canvasCfgDTO.getUserId())
//            .last("limit 1")
//        );

        if(ObjectUtil.isEmpty(one)) {
            saveOrUpdate(canvasCfgDO);
            return;
        }
        canvasCfgDO.setId(one.getId());
        saveOrUpdate(canvasCfgDO);
    }
}
