package com.bonus.system.att.service;

import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.att.dao.AttDetailByMonthDao;
import com.bonus.system.att.dao.AttGroupDao;
import com.bonus.system.att.entity.*;
import com.bonus.system.basic.domain.SysOrg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 月异常报表-业务层
 * @author zys
 */
@Service("attDetailByMonthService")
@Slf4j
public class AttDetailByMonthServiceImpl implements AttDetailByMonthService{

    @Resource(name = "attDetailByMonthDao")
    private AttDetailByMonthDao attDetailByMonthDao;
//
//    @Override
//    public List<AttDetailByMonthBean> selectAttDetailList(AttDetailByMonthBean data)
//    {
//        List<AttDetailByMonthBean> list = new ArrayList<>();
//        List<AttDetailByMonthBean> listTemp = new ArrayList<>();
//        List<Map<String,Object>> parentIdList = new ArrayList<>();
//        List<Map<String,Object>> sonsIdList = new ArrayList<>();
//        Map<String,Object> map = new HashMap<>();
//        try {
//            if(data.getOrgId()==null){
//                listTemp  = attDetailByMonthDao.getAllParentId();
//                for (int i = 0; i < listTemp.size(); i++) {
//                    findSonsIds(listTemp.get(i).getOrgId(),sonsIdList);
//                }
//            }else {
//                findSonsIds(data.getOrgId(), sonsIdList);
//            }
//                for (int i = 0; i < sonsIdList.size(); i++) {
//                    map = sonsIdList.get(i);
//                    findParentIds(map,parentIdList);
//                    sonsIdList.get(i).put("orgName",parentIdList.get(i).get("orgName"));
//                }
//                int[] orgIdsTemp = new int[sonsIdList.size()];
//                for (int i = 0; i < sonsIdList.size(); i++){
//                    orgIdsTemp[i] = (int) sonsIdList.get(i).get("id");
//                }
//                data.setOrgIds(orgIdsTemp);
//                list = attDetailByMonthDao.selectAttAllList(data);
//                for (int i = 0; i < list.size(); i++) {
//                    for (int j = 0; j < sonsIdList.size(); j++) {
//                        if(sonsIdList.get(j).get("id")==list.get(i).getOrgId()){
//                            list.get(i).setOrgName((String) sonsIdList.get(j).get("orgName"));
//                        }
//                    }
//                }
//
//
//        } catch (Exception e) {
//            log.error("查询月异常报表列表", e);
//        }
//        return list;
//    }


    @Override
    public List<AttDetailByMonthBean> selectAttDetailList(AttDetailByMonthBean data)
    {
        List<AttDetailByMonthBean> list = new ArrayList<>();
        try {
            list = attDetailByMonthDao.selectAttAllList(data);
            // 查询打卡记录
            list.forEach(c -> {
                int clockingRecordNum = attDetailByMonthDao.getClockingRecordNum(c);
                c.setClockingRecordNum(clockingRecordNum);
            });
        } catch (Exception e) {
            log.error("查询月异常报表列表", e);
        }
        return list;
    }

    @Override
    public List<AttDetailBean> getDetailList(AttDetailBean data)
    {
        List<AttDetailBean> list = new ArrayList<>();
        try {
            list = attDetailByMonthDao.getDetailList(data);
        } catch (Exception e) {
            log.error("查询打卡详细信息", e);
        }
        return list;
    }

    @Override
    public List<AttDetailLeaveBean> getDetailLeaveList(AttDetailLeaveBean data)
    {
        List<AttDetailLeaveBean> list = new ArrayList<>();
        try {
            list = attDetailByMonthDao.getDetailLeaveList(data);

        } catch (Exception e) {
            log.error("查询打卡详细信息", e);
        }
        return list;
    }


    /**
     * 从父级补充所属部门名称
     * @param parentMap
     * @param parentIdList
     */
    public  void findParentIds(Map<String,Object> parentMap,List<Map<String,Object>> parentIdList) {
        Map<String, Object> map = attDetailByMonthDao.getDepartmentById(parentMap);
        if (map!=null && map.get("parentId") != null) {
            // 递归查找上级部门
            findParentIds(map,parentIdList);
        }else if(map!=null){
            parentMap.put("orgName",map.get("orgName"));
            parentIdList.add(parentMap);
        }else{
            parentIdList.add(parentMap);
        }
    }

    /**
     * 获取该部门编号下的所有部门编号
     * @param orgId
     * @param sonsIdList
     */
    public  void findSonsIds(int orgId, List<Map<String,Object>> sonsIdList) {
        Map<String, Object> mapFinish = new HashMap<>();
        Map<String, Object> mapTemp = new HashMap<>();
        List<Map<String, Object>> listSon= attDetailByMonthDao.getSonsById(orgId);
        if(listSon.size()!=0){
            for (int i = 0; i < listSon.size(); i++) {
                mapTemp = listSon.get(i);
                if(mapTemp.get("parentId")==null){
                    sonsIdList.add(mapTemp);
                }else{
                    findSonsIds((Integer) mapTemp.get("id"),sonsIdList);
                }
            }
        }else{
            mapFinish = attDetailByMonthDao.getSonsFinish(orgId);
            sonsIdList.add(mapFinish);
        }
    }
}
