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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private UserModelContentService userModelContentService;

    /**
     * 保存、更新模型
     *
     * @param list
     * @param menuType
     * @return 返回一个map 只有两个key addIdModels是添加的id集合 updateIdModels是更新的id集合
     */
    @Override
    public HashMap<String, List<String>> saveOrUpdateModel(List<ModelContentDTO> list, String menuType) {
        if (CollectionUtil.isEmpty(list))
            return null;

        //获取需要保存的id,然后返回给中间表
        List<String> addIdModels = new ArrayList<>();
        List<String> updateIdModels = new ArrayList<>();
        for (ModelContentDTO modelContentDTO : list) {
            ModelContentDO modelContentDO = ModelContentDTO.dtoConvertDO(modelContentDTO);
            disposeDO(modelContentDO, updateIdModels);

            //这种情况可能是直接复用其他组件
            if (StrUtil.isNotEmpty(modelContentDO.getId())) {
                QueryWrapper queryWrapper = QueryWrapper.create()
                    .select()
                    .from(UserModelContentDO.class)
                    .where(UserModelContentDO.MODEL_CONTENT_ID, modelContentDO.getId())
                    .eq(UserModelContentDO.MENU_TYPE, menuType)
                    //true:ASC   false:DESC
                    .orderBy(UserModelContentDO.ID, false)
                    .limit(1);

                List<UserModelContentDO> doList = userModelContentService.list(queryWrapper);

//                List<UserModelContentDO> doList = userModelContentService.list(new QueryWrapper<UserModelContentDO>()
//                    .eq(UserModelContentDO.MODEL_CONTENT_ID, modelContentDO.getId())
//                    .eq(UserModelContentDO.MENU_TYPE, menuType)
//                    .orderByDesc(UserModelContentDO.ID)
//                    .last("limit 1")
//                );

                if (CollectionUtil.isEmpty(doList))
                    modelContentDO.setId(null);
                else
                    updateIdModels.add(modelContentDO.getId());
            }


            saveOrUpdate(modelContentDO);
            if (!updateIdModels.contains(modelContentDO.getId()))
                addIdModels.add(modelContentDO.getId());
        }
        HashMap<String, List<String>> map = new HashMap<>();
        map.put("addIdModels", addIdModels);
        map.put("updateIdModels", updateIdModels);
        return map;
    }

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

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

    }
}
