package org.dromara.zhishu.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.pdd.pop.sdk.common.util.JsonUtil;
import org.apache.ibatis.annotations.Param;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.zhishu.domain.vo.RunningTaskNumVo;
import org.dromara.zhishu.domain.vo.ShopVo;
import org.dromara.zhishu.service.IShopService;
import org.dromara.zhishu.util.InterfaceUtils;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.RunningTaskBo;
import org.dromara.zhishu.domain.vo.RunningTaskVo;
import org.dromara.zhishu.domain.RunningTask;
import org.dromara.zhishu.mapper.RunningTaskMapper;
import org.dromara.zhishu.service.IRunningTaskService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 执行的任务Service业务层处理
 *
 * @author yxy
 * @date 2025-07-23
 */
@RequiredArgsConstructor
@Service
public class RunningTaskServiceImpl implements IRunningTaskService {

    private final RunningTaskMapper baseMapper;

    private final IShopService shopService;


    @DS("slave")
    @Override
    public List<Map> selectGetShopGoodsList(Long shopId,Long pageNum,Long pageSize){
        return baseMapper.selectGetShopGoodsList(shopId,pageNum,pageSize);
    }


    @DS("slave")
    @Override
    public List<Map<String, Object>> selectLogByTaskId(Long taskId){
        return baseMapper.selectLogByTaskId(taskId);
    }

    @DS("slave")
    @Override
    public Long selectGetShopGoodsNum(Long shopId){
        return baseMapper.selectGetShopGoodsNum(shopId);
    }

    @DS("slave")
    @Override
    public  Map selectTaskStatus(){
        return baseMapper.selectTaskStatus();
    }

    @DS("slave")
    @Override
    public List<RunningTaskNumVo> selectTaskAllNum(Long taskId){
        return baseMapper.selectTaskAllNum(taskId);
    }

    @DS("slave")
    @Override
    public Long selectTaskIsRunning(Long taskId){
        return baseMapper.selectTaskIsRunning(taskId);
    }

    @DS("slave")
    @Override
    public Long selectShopTaskByGetShopGoods(Long shopId){
        return baseMapper.selectShopTaskByGetShopGoods(shopId);
    }

    @DS("slave")
    @Override
    public Long selectTaskStatusByTaskType(Long shopId,String taskType){
        return baseMapper.selectTaskStatusByTaskType(shopId,taskType);
    }

    @DS("slave")
    @Override
    public Long selectTaskStatusByTaskTypeAndIsbn(Long shopId, String taskType, String isbn) {
        return baseMapper.selectTaskStatusByTaskTypeAndIsbn(shopId,taskType,isbn);

    }

    @DS("slave")
    @Override
    public Long selectTaskStatusRunningCount(){
        return baseMapper.selectTaskStatusRunningCount();
    }

    @DS("slave")
    @Override
    public List<Map> selectTaskByGroupShopMd5(){
        return baseMapper.selectTaskByGroupShopMd5();
    }

    @DS("slave")
    @Override
    public List<Map> selectTaskGroupShop(String taskType){
        return baseMapper.selectTaskGroupShop(taskType);
    }

    @DS("slave")
    @Override
    public List<RunningTaskNumVo> selectTaskShopNum(Long taskId,Long shopId){
        return baseMapper.selectTaskShopNum(taskId,shopId);
    }

    /**
     * 新增执行的任务
     *
     * @param bo 执行的任务
     * @return 是否新增成功
     */
    @DS("slave")
    @Override
    public Boolean insert(RunningTask runningTask) {
        return baseMapper.insert(runningTask) > 0;
    }

    @DS("slave")
    @Override
    public Boolean batchInsert(List<RunningTask> list){
        return baseMapper.batchInsert(list) > 0;
    }

    /**
     * 修改执行的任务  暂停/恢复/中止
     * @param runningTask
     * @return
     */
    @DS("slave")
    @Override
    public Boolean update(RunningTask runningTask) {
        return baseMapper.update(runningTask) > 0;
    }


    @Override
    public Map getJavaTaskPool(String ip,String host,String url){

        Map javaTaskPoolStatusMap = new HashMap();

        try{
            String javaTaskPool = InterfaceUtils.getInterfaceGetShort("http://"+ip+":"+host,url);
            Map map = JsonUtil.transferToObj(javaTaskPool, Map.class);
            Map data = (Map) map.get("data");

            //活跃线程数
            int active = (int) data.get("active");
            //提交任务数
            int submitted = (int) data.get("submitted");
            //完成任务数
            int completed = (int) data.get("completed");
            String shopMd5 = data.get("shopMd5").toString();
            javaTaskPoolStatusMap.put("ip",ip);
            javaTaskPoolStatusMap.put("host",host);
            javaTaskPoolStatusMap.put("active",active);
            javaTaskPoolStatusMap.put("submitted",submitted);
            javaTaskPoolStatusMap.put("completed",completed);
            javaTaskPoolStatusMap.put("shopMd5",shopMd5);
        }catch (Exception e){
            javaTaskPoolStatusMap.put("ip",ip);
            javaTaskPoolStatusMap.put("host",host);
            javaTaskPoolStatusMap.put("active",-1);
            javaTaskPoolStatusMap.put("submitted",-1);
            javaTaskPoolStatusMap.put("completed",-1);
            javaTaskPoolStatusMap.put("shopMd5","连接中断");
        }
        return javaTaskPoolStatusMap;
    }

    /**
     * 修改拉取的任务  2变3
     * @param shopId
     * @return
     */
    @DS("slave")
    @Override
    public Boolean updataStatus(Long shopId,Long taskId) {
        return baseMapper.updataStatus("t_running_task_"+shopId,taskId);
    }

}
