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


import cn.hutool.core.collection.CollectionUtil;
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.ModelContentMapper;
import com.sz.admin.monitor.pojo.dto.dataModel.ModelContentDTO;
import com.sz.admin.monitor.pojo.po.ModelContentDO;
import com.sz.admin.monitor.pojo.po.UserModelContentDO;
import com.sz.admin.monitor.service.ModelContentService;
import com.sz.admin.monitor.service.UserModelContentService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xq
 * @description:存储模型的基本信息和配置
 * @date 2026/1/29 11:30
 */
@Component
public class ModelContentServiceImpl extends ServiceImpl<ModelContentMapper, ModelContentDO> implements ModelContentService {

    @Autowired
    private UserModelContentService userModelContentService;
    @Resource
    private ModelContentMapper modelContentMapper;
    /**
     * 保存、更新模型
     *
     * @param list
     * @param menuType
     * @return 返回一个map 只有两个key addIdModels是添加的id集合 updateIdModels是更新的id集合
     */
    @Override
    public void saveOrUpdateModel(List<ModelContentDTO> list, String menuType) {
        if (CollectionUtil.isEmpty(list))
            return;
        // 获取所有roomIds
        Set<Long> roomIds = list.stream().map(ModelContentDTO::getRoomId).collect(Collectors.toSet());
        // DELETE FROM ry_model_content WHERE room_id in '1'
        if (!roomIds.isEmpty())
        {
            modelContentMapper.deleteByRoomIds(roomIds);
        }
        for (ModelContentDTO modelContentDTO : list) {
            ModelContentDO modelContentDO = ModelContentDTO.dtoConvertDO(modelContentDTO);
            disposeDO(modelContentDO);
            save(modelContentDO);
        }
    }

    /**
     * 处理DO的id
     *
     * @param modelContentDO
     */
    public void disposeDO(ModelContentDO modelContentDO) {
        //这里是空，肯定是add
        if (StrUtil.isEmpty(modelContentDO.getId()))
            return;

        //判断是否是前端生成的id
        if (!modelContentDO.getId().matches("\\d+"))
            modelContentDO.setId(null);

    }
}
