package com.pjilisense.flxai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pjilisense.flxai.base.service.impl.CrudServiceImpl;
import com.pjilisense.flxai.dao.AppAiPluginDao;
import com.pjilisense.flxai.dto.AppAiPluginDTO;
import com.pjilisense.flxai.entity.AppAiPluginEntity;
import com.pjilisense.flxai.service.AppAiPluginService;
import cn.hutool.core.util.StrUtil;
import com.pjilisense.flxai.utils.MapUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * AI插件表
 *
 * @author liushujing liushujing@philisense.com
 * @since 1.0.0 2024-12-20
 */
@Service
public class AppAiPluginServiceImpl extends CrudServiceImpl<AppAiPluginDao, AppAiPluginEntity, AppAiPluginDTO> implements AppAiPluginService {

    @Override
    public QueryWrapper<AppAiPluginEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AppAiPluginEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    public QueryWrapper<AppAiPluginEntity> getWrapperAll(Map<String, Object> params){
        String userid = (String)params.get("userid");

        QueryWrapper<AppAiPluginEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(userid), "userid", userid);
        wrapper.orderByDesc("created_at");
        return wrapper;
    }
    @Override
    public List<Map<String, Object>> getAllAiPlugin(String userid) {
        Map<String, Object> params =new HashMap<>();
        params.put("userid",userid);
        List<AppAiPluginEntity> entityList = baseDao.selectList(getWrapperAll(params));
        params.clear();
        List<Map<String, Object>> retList = new ArrayList<>();
        if(entityList!=null && entityList.size()>0) {
            for (AppAiPluginEntity entity : entityList) {
                Map<String, Object> mapx = MapUtils.objectToMap(entity);
                retList.add(mapx);
                HashMap<String, Object> meta = new HashMap<>();
                mapx.put("meta", meta);
                meta.put("avatar", mapx.get("avatar"));
                meta.put("description", mapx.get("description"));
                meta.put("title", mapx.get("title"));
                meta.put("category", mapx.get("category"));
                String tags = (String) mapx.get("tags");
                if (tags!=null && tags.length() > 0) {
                    meta.put("tags", Arrays.asList(tags.split("\\,")));
                } else {
                    meta.put("tags", new ArrayList<>());
                }
                mapx.remove("avatar");
                mapx.remove("description");
                mapx.remove("title");
                mapx.remove("category");
                mapx.remove("tags");
            }
        }
        return retList;
    }

    @Override
    public int deleteByIdentifier(String userid, String identifier) {
        QueryWrapper<AppAiPluginEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(identifier), "identifier", identifier);
        wrapper.eq(StrUtil.isNotBlank(userid), "userid", userid);
        return baseDao.delete(wrapper);
    }
}