package com.engine.salary.enums.auth;

import com.engine.common.service.HrmCommonService;
import com.engine.common.service.impl.HrmCommonServiceImpl;
import com.engine.salary.enums.BaseEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.impl.SalaryEmployeeServiceImpl;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 成员类型，1：人员、2：部门、3：岗位、4：分部、5：角色、6：sql
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public enum MemberTargetTypeEnum implements BaseEnum<Integer> {


    EMP(1, "人员", 87627) {
        @Override
        public List<Long> getEmpIds(Set<String> targets) {
            return targets.stream().map(Long::valueOf).collect(Collectors.toList());
        }
    },
    DEPARTMENT(2, "部门", 87626) {
        @Override
        public List<Long> getEmpIds(Set<String> targets) {
            List<Long> deptIds = targets.stream().map(Long::valueOf).collect(Collectors.toList());
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            return salaryEmployeeService.listByDepartment(deptIds);
        }
    },
    JOB(3, "岗位", 87626) {
        @Override
        public List<Long> getEmpIds(Set<String> targets) {
            List<Long> ids = targets.stream().map(Long::valueOf).collect(Collectors.toList());
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            return salaryEmployeeService.listByJob(ids);
        }
    },
    SUB_COMPANY(4, "分部", 87626) {
        @Override
        public List<Long> getEmpIds(Set<String> targets) {
            List<Long> ids = targets.stream().map(Long::valueOf).collect(Collectors.toList());
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            return salaryEmployeeService.listBySubCompany(ids);
        }
    },
    ROLE(5, "角色", 87626) {
        @Override
        public List<Long> getEmpIds(Set<String> targets) {
            HrmCommonService hrmCommonService = new HrmCommonServiceImpl();
            List<Object> memberList = new ArrayList<>();
            targets.forEach(roleId -> {
                memberList.addAll(hrmCommonService.getRoleMembers(roleId, "2"));
            });
            return memberList.stream().map(id -> Long.parseLong(id.toString())).collect(Collectors.toList());
        }
    },
    SQL(6, "sql", 87626) {
        @Override
        public List<Long> getEmpIds(Set<String> targets) {
            List<Long> memberList = new ArrayList<>();
            targets.forEach(sql -> {
                RecordSet rs = new RecordSet();
                if (rs.execute(sql)) {
                    while (rs.next()) {
                        memberList.add(Long.parseLong(rs.getString("id")));
                    }
                }
            });
            return memberList;
        }
    };


    private Integer value;
    private String defaultLabel;
    private int labelId;

    MemberTargetTypeEnum(Integer value, String defaultLabel, int labelId) {
        this.value = value;
        this.defaultLabel = defaultLabel;
        this.labelId = labelId;
    }

    public abstract List<Long> getEmpIds(Set<String> targets);


    @Override
    public Integer getValue() {
        return value;
    }

    @Override
    public String getDefaultLabel() {
        return defaultLabel;
    }

    @Override
    public Integer getLabelId() {
        return labelId;
    }

    public static MemberTargetTypeEnum parseByValue(int value) {
        for (MemberTargetTypeEnum typeEnum : MemberTargetTypeEnum.values()) {
            if (Objects.equals(typeEnum.getValue(), value)) {
                return typeEnum;
            }
        }
        throw new SalaryRunTimeException("未找到对应的枚举");
    }
}
