package com.engine.salary.enums.auth;

import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.enums.BaseEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import weaver.conn.RecordSet;

import java.util.*;

/**
 * 登录人信息类型
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public enum LoginPersonTargetTypeEnum implements BaseEnum<Integer> {


    self(1, "本人", 87627) {
        @Override
        public Set<Long> getEmpIds(DataCollectionEmployee employee) {
            Set<Long> memberList = new HashSet<>();
            memberList.add(employee.getEmployeeId());
            return memberList;
        }
    },
    SUBORDINATE(2, "下属", 87626) {
        @Override
        public Set<Long> getEmpIds(DataCollectionEmployee employee) {
            String sql = "select id from hrmresource where status not in (7) and (accounttype is null or accounttype = 0)  and managerstr like '%," + employee.getEmployeeId() + ",%' and ";
            Set<Long> memberList = new HashSet<>();
            RecordSet rs = new RecordSet();
            if (rs.execute(sql)) {
                while (rs.next()) {
                    memberList.add(Long.parseLong(rs.getString("id")));
                }
            }
            return memberList;
        }
    },
    DEPARTMENT(3, "本部门", 87626) {
        @Override
        public Set<Long> getEmpIds(DataCollectionEmployee employee) {
            String sql = "select id from hrmresource where status not in (7) and (accounttype is null or accounttype = 0)  and departmentid =" + employee.getDepartmentId();
            Set<Long> memberList = new HashSet<>();
            RecordSet rs = new RecordSet();
            if (rs.execute(sql)) {
                while (rs.next()) {
                    memberList.add(Long.parseLong(rs.getString("id")));
                }
            }
            return memberList;
        }
    },
    SUB_COMPANY(4, "本分部", 87626) {
        @Override
        public Set<Long> getEmpIds(DataCollectionEmployee employee) {
            String sql = "select id from hrmresource where status not in (7) and (accounttype is null or accounttype = 0)  and subcompanyid1 =" + employee.getSubcompanyid();
            Set<Long> memberList = new HashSet<>();
            RecordSet rs = new RecordSet();
            if (rs.execute(sql)) {
                while (rs.next()) {
                    memberList.add(Long.parseLong(rs.getString("id")));
                }
            }
            return memberList;
        }
    },
    JOB(5, "本岗位", 87626) {
        @Override
        public Set<Long> getEmpIds(DataCollectionEmployee employee) {
            String sql = "select id from hrmresource where status not in (7) and (accounttype is null or accounttype = 0)  and jobtitle =" + employee.getJobtitleId();
            Set<Long> memberList = new HashSet<>();
            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;

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

    public abstract Set<Long> getEmpIds(DataCollectionEmployee employee);


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

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

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

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