package org.stm.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;

import org.stm.metric.entity.MetricMonitorSet;
import org.stm.metric.service.IMetricMonitorSetService;
import org.stm.my.entity.MyTask;
import org.stm.my.mapper.MyTaskMapper;
import org.stm.my.service.IMyTaskService;
import org.springframework.stereotype.Service;

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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 待办任务
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class MyTaskServiceImpl extends ServiceImpl<MyTaskMapper, MyTask> implements IMyTaskService {
    @Autowired
    private IMetricMonitorSetService metricMonitorSetService;
    /*
    public void addMyTaskForFxc(FxcPj fxcPj, List<String> uids, int mtp) {
        List<MyTask> listMyTask = new ArrayList<>();
        for(int i=0;i<uids.size();i++) {
            MyTask myTask = new MyTask();
            myTask.setUid(uids.get(i));
            myTask.setTp(1);
            myTask.setTarget(fxcPj.getNm());
            myTask.setTargetId(fxcPj.getId().toString());
            myTask.setStTime(new Date());
            myTask.setTaskName(mtp==1?"待填报":"待审批");
            myTask.setName(fxcPj.getNm());
            myTask.setSta(0);
            myTask.setPriority("H");
            StringBuffer sbNames = new StringBuffer();
            sbNames.append("非现场报送").append("-").append(fxcPj.getNm());
            myTask.setName(sbNames.toString());
            myTask.setDes("");
            if(mtp==1) {
                if (fxcPj.getTp() == 1) {
                    myTask.setLinkAddr("/fxc/fxcPjList");
                } else if (fxcPj.getTp() == 2) {
                    myTask.setLinkAddr("/fxc/FxcPjQuarterList");
                } else if (fxcPj.getTp() == 3) {
                    myTask.setLinkAddr("/fxc/FxcPjMonthList");
                }
            }  else {
                myTask.setLinkAddr("/fxc/FxcAppList");
            }
            listMyTask.add(myTask);
        }
        this.saveBatch(listMyTask);
    }


     */
    @Override
    public void addMyTaskForMetrc(String[] mtrcNos, String uid) {
        QueryWrapper<MetricMonitorSet> queryWrapper = new QueryWrapper<>();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.in("mtrc_no",mtrcNos);
        List<MetricMonitorSet> mmsetList = metricMonitorSetService.list(queryWrapper);
        List<MyTask> listMyTask = new ArrayList<>();
        for(int i=0;i<mmsetList.size();i++) {
            MetricMonitorSet mmSet = mmsetList.get(i);
            MyTask myTask = new MyTask();
            myTask.setUid(uid);
            myTask.setTp(2);
            myTask.setTarget(mmSet.getMtrcName());
            myTask.setTargetId(mmSet.getId().toString());
            myTask.setStTime(new Date());
            myTask.setTaskName("待填报");

            StringBuffer sbNames = new StringBuffer();
            sbNames.append("风险监测-").append(mmSet.getMtrcName());

            myTask.setName(sbNames.toString());
            myTask.setSta(0);
            myTask.setDes(mmSet.getDes());
            myTask.setPriority("M");
            myTask.setLinkAddr("/metric/metricReportIndex");
            listMyTask.add(myTask);
        }
        this.saveBatch(listMyTask);
    }

    @Override
    public List<String>  getUserIds(Integer fxcPjId) {
//        QueryWrapper<FxcPjItemUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("pj_id",fxcPjId);
//        List<FxcPjItemUser> itemUserList = fxcPjItemUserService.list(queryWrapper);
//        List<String> userIds = new ArrayList<>();
//        for (FxcPjItemUser fxcPjItemUser : itemUserList) {
//            if(!userIds.contains(fxcPjItemUser.getUid())){
//                userIds.add(fxcPjItemUser.getUid());
//            }
//        }
        return new ArrayList<>();
    }

    @Override
    public void deleteMytaskByFlowtask() {
        this.baseMapper.deleteMytaskByFlowtask();
    }
}
