package org.stm.baosong.service.impl;

import org.apache.ibatis.annotations.Param;
import org.stm.baosong.entity.BaosongTask;
import org.stm.baosong.entity.BaosongTaskAlloc;
import org.stm.baosong.entity.BaosongTpl;
import org.stm.baosong.entity.BaosongTplItem;
import org.stm.baosong.mapper.BaosongTaskAllocMapper;
import org.stm.baosong.service.IBaosongTaskAllocService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

/**
 * @Description: 任务分配
 * @Author: jeecg-boot
 * @Date:   2024-12-17
 * @Version: V1.0
 */
@Service
public class BaosongTaskAllocServiceImpl extends ServiceImpl<BaosongTaskAllocMapper, BaosongTaskAlloc> implements IBaosongTaskAllocService {
    @Override
    public List<Map<String,Object>> queryAllTaskAlloc(BaosongTaskAlloc alloc) {
        return this.baseMapper.queryAllTaskAlloc(alloc);
    }
    @Override
    public List<BaosongTpl> queryAllTplForUser(BaosongTaskAlloc taskAlloc){
        return this.baseMapper.queryAllTplForUser(taskAlloc);
    }

    @Override
    public List<BaosongTplItem> queryAllTplItemForUser(BaosongTaskAlloc alloc){
        return this.baseMapper.queryAllTplItemForUser(alloc);
    }

    @Override
    public List<Map<String,Object>> queryCurTaskAllocUser(BaosongTaskAlloc alloc){
        List<Map<String,Object>> retList = this.baseMapper.queryCurTaskAllocUser(alloc);
        return retList;
    }

    @Override
    public Map<Integer,Integer> statAllocNum(BaosongTaskAlloc alloc){
        List<Map<String,Object>> retList = this.baseMapper.statAllocNum(alloc);
        Map<Integer,Integer> retMap = new HashMap<>();
        for(Map<String,Object> item:retList) {
            Integer taskId = Integer.valueOf(item.get("taskid").toString());
            Integer cnt = Integer.valueOf(item.get("cnt").toString());
            retMap.put(taskId,cnt);
        }
        return retMap;
    }

    @Override
    public Map<Integer,Integer> staTplChildNum(BaosongTaskAlloc alloc){
        List<Map<String,Object>> retList = this.baseMapper.staTplChildNum(alloc);
        Map<Integer,Integer> retMap = new HashMap<>();
        for(Map<String,Object> item:retList) {
            Integer tp = Integer.valueOf(item.get("tp").toString());
            Integer cnt = Integer.valueOf(item.get("cnt").toString());
            retMap.put(tp,cnt);
        }
        return retMap;
    }

    @Override
    public Map<Integer,Boolean> selectTaskStatus(@Param("query") BaosongTaskAlloc alloc){
        List<Map<String,Object>> retList = this.baseMapper.selectTaskStatus(alloc);
        Map<Integer,Boolean> retMap = new HashMap<>();
        for(Map<String,Object> map:retList) {
          Integer taskid = Integer.valueOf(map.get("taskid").toString());
          retMap.put(taskid,  Boolean.valueOf(map.get("sta").toString()));
        }
        return retMap;
    }


    @Override
    public Map<Integer,Integer> selectTaskFillSta(BaosongTaskAlloc alloc){
        List<Map<String,Object>> retList = this.baseMapper.selectTaskFillSta(alloc);
        Map<Integer,Integer> retMap = new HashMap<>();
        for(Map<String,Object> map:retList) {
            Integer taskid = Integer.valueOf(map.get("taskid").toString());
            retMap.put(taskid,  Integer.valueOf(map.get("cnt").toString()));
        }
        return retMap;
    }


    @Override
    public String getFillUsers(Integer taskId,Integer tplId){
        BaosongTaskAlloc alloc =  new BaosongTaskAlloc();
        alloc.setTaskid(taskId);
        alloc.setTplid(tplId);
        List<Map<String,Object>> retList = this.baseMapper.getFillUsers(alloc);
        if(retList.size()>0) {
            return  retList.get(0).get("usernames").toString();
        }
        return "";
    }




}
