package com.pqs9300.serviceimpl.technical;

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

import javax.annotation.Resource;

import com.njcn.shiro.token.TokenManager;
import com.pqs9300.pojo.transaction.Supervise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njcn.mapper.user.DeptsLineMapper;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.user.Depts;
import com.njcn.pojo.user.DeptsLine;
import com.njcn.utils.AppConfig;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.pqs9300.mapper.technical.TechnicalInfoMapper;
import com.pqs9300.pojo.technical.TechnicalInfo;
import com.pqs9300.pojo.transaction.OverRunLog;
import com.pqs9300.service.technical.TechnicalInfoService;
import org.springframework.util.CollectionUtils;

@Service
public class TechnicalInfoServiceImpl implements TechnicalInfoService {

    @Autowired
    private AppConfig appConfig;
    @Autowired
    private UserUtil userUtil;
    
    @Resource
    TechnicalInfoMapper technicalInfoMapper;

    @Resource
    DeptsLineMapper deptsLineMapper;
    
    @Override
    public List<TechnicalInfo> getTechnicalInfo(String startTime, String endTime, int progress, int supType) {
        DatePojo datePojo = PubUtils.validateDate(startTime, endTime, "getTechnicalInfo");
        List<TechnicalInfo> supervises = new ArrayList<>();
        //获取当前用户部门,根据用户部门遍历出所有子部门
        Depts depts = userUtil.getCurrentDept();
        List<Depts> deptsList = userUtil.getDeptsByNode(depts.getDeptsIndex());
        //首先查出upUser为空的，当前用户权限部门下的所有事件
        List<String> deptsIndexs = new ArrayList<>();
        deptsIndexs.add(depts.getDeptsIndex());
        for (Depts i : deptsList) {
            deptsIndexs.add(i.getDeptsIndex());
        }
        if (!org.springframework.util.CollectionUtils.isEmpty(deptsIndexs)) {
            List<TechnicalInfo> temps = technicalInfoMapper.getTransactionDataByDepts(deptsIndexs, datePojo.getStartTime(), datePojo.getEndTime(), supType, progress);
            if (!org.springframework.util.CollectionUtils.isEmpty(temps)) {
                supervises.addAll(temps);
            }
        }
        //其次查出已经被当前用户处理过的事务，此时理论上，就不需要部门条件了，因为是从上线流程下来的
        String userIndex = TokenManager.getUserId();
        List<TechnicalInfo> temps = technicalInfoMapper.getTransactionDataByUserIndex(userIndex, datePojo.getStartTime(), datePojo.getEndTime(), supType, progress);
        if (!org.springframework.util.CollectionUtils.isEmpty(temps)) {
            supervises.addAll(temps);
        }
        return supervises;
    }
    
    @Override
    public List<Depts> getDepts(){
        Depts dept = userUtil.getCurrentDept();
        
        List<Depts> depts = new ArrayList<>();
        depts = userUtil.getDeptsByNode(dept.getDeptsIndex());
        depts.add(0, dept);
        
        return depts;
    }

    @Override
    public int getStartSwitch(List<String> index) {
        int result = 0;
        try {
            result = technicalInfoMapper.getStartSwitch(index);
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }
}
