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.AppAiAssistantDao;
import com.pjilisense.flxai.dto.AppAiAssistantDTO;
import com.pjilisense.flxai.entity.AppAiAssistantEntity;
import com.pjilisense.flxai.entity.AppToolsSetEntity;
import com.pjilisense.flxai.service.AppAiAssistantService;
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-13
 */
@Service
public class AppAiAssistantServiceImpl extends CrudServiceImpl<AppAiAssistantDao, AppAiAssistantEntity, AppAiAssistantDTO> implements AppAiAssistantService {

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

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

        return wrapper;
    }

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

        QueryWrapper<AppAiAssistantEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(userid), "userid", userid);
        wrapper.orderByDesc("create_at","created_at");
        return wrapper;
    }
    @Override
    public List<Map<String, Object>> getAllAiTools(String userid) {
        Map<String, Object> params =new HashMap<>();
        params.put("userid",userid);
        List<AppAiAssistantEntity> entityList = baseDao.selectList(getWrapperAll(params));
        params.clear();
        List<Map<String, Object>> retList = new ArrayList<>();
        if(entityList!=null && entityList.size()>0) {
            for (AppAiAssistantEntity 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<AppAiAssistantEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(identifier), "identifier", identifier);
        wrapper.eq(StrUtil.isNotBlank(userid), "userid", userid);
        return baseDao.delete(wrapper);
    }
}