package cn.iocoder.yudao.module.llm.service.basemodel;

import cn.iocoder.yudao.module.llm.controller.admin.basemodel.vo.BaseModelSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.basemodel.BaseModelDO;
import cn.iocoder.yudao.module.llm.service.basemodel.vo.ModelListRes;
import cn.iocoder.yudao.module.llm.service.http.TrainHttpService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class BaseModelTaskService {

    @Resource
    TrainHttpService trainHttpService;

    @Resource
    BaseModelService baseModelService;


//    @Scheduled(cron ="0 0/1 * * * ?")
    public void updateBaseModel() {
        Log.info("定时任务启动");
        String resStr = trainHttpService.modelsList("");
        Log.info("获取aicg模型列表返回数据内容{}",resStr);
        JSONObject jsonObject = JSONObject.parseObject(resStr);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        String s = list.toJSONString();
        List<ModelListRes> modelListRes = JSONObject.parseArray(s, ModelListRes.class);
        for (ModelListRes modelListRe : modelListRes) {
            Long id = modelListRe.getId();
            BaseModelDO byAigcId = baseModelService.getByAigcId(id);
            if(byAigcId == null){
                BaseModelSaveReqVO baseModelSaveReqVO = new BaseModelSaveReqVO();
                baseModelSaveReqVO.setAigcId(id);
                baseModelSaveReqVO.setModelName(modelListRe.getModelName());
                baseModelSaveReqVO.setModelType(modelListRe.getModelType());
                baseModelSaveReqVO.setMaxContextLength(modelListRe.getMaxTokens());
                baseModelSaveReqVO.setIsActive(modelListRe.getEnabled());
                baseModelSaveReqVO.setIsFinetuned(modelListRe.getIsFineTuning());
                baseModelSaveReqVO.setNotes(modelListRe.getRemark());
                baseModelSaveReqVO.setParameterCount(1);
                baseModelService.createBaseModel(baseModelSaveReqVO);
            }else{
                    BaseModelSaveReqVO  baseModelSaveReqVO = new BaseModelSaveReqVO();
                    baseModelSaveReqVO.setId(byAigcId.getId());
                    baseModelSaveReqVO.setParameterCount(1);
                    baseModelSaveReqVO.setModelName(modelListRe.getModelName());
                    baseModelSaveReqVO.setModelType(modelListRe.getModelType());
                    baseModelSaveReqVO.setMaxContextLength(modelListRe.getMaxTokens());
                    baseModelSaveReqVO.setIsActive(modelListRe.getEnabled());
                    baseModelSaveReqVO.setIsFinetuned(modelListRe.getIsFineTuning());
                    baseModelSaveReqVO.setNotes(modelListRe.getRemark());
                    baseModelService.updateBaseModel(baseModelSaveReqVO);
            }
        }

        List<BaseModelDO> baseModelList = baseModelService.getBaseModelList();

        List<Long> ids =  modelListRes.stream().map(ModelListRes::getId).collect(Collectors.toList());

        for (BaseModelDO baseModelDO : baseModelList) {
            if(baseModelDO.getAigcId() == null){
                continue;
            }
            if(!ids.contains(baseModelDO.getAigcId())){
                baseModelService.deleteBaseModel(baseModelDO.getId());
            }

        }
    }
}
