package com.engine.salary.enums.auth;

import com.engine.common.service.HrmCommonService;
import com.engine.common.service.impl.HrmCommonServiceImpl;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.taxagent.po.TaxAgentEmpPO;
import com.engine.salary.enums.BaseEnum;
import com.engine.salary.enums.datacollection.UseEmployeeTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.impl.SalaryEmployeeServiceImpl;
import com.engine.salary.service.impl.TaxAgentEmpServiceImpl;
import com.engine.salary.util.SalaryEntityUtil;
import com.google.common.collect.Lists;
import weaver.conn.RecordSet;

import java.util.*;

/**
 * 对象类型，1：人员、2：部门、3：岗位、4：分部、5：角色、6：安全级别  、7：所有人 、8：登录人、9：账套、10：sql
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public enum DataTargetTypeEnum implements BaseEnum<Integer> {


    EMP(1, "人员", 87627) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            Set<Long> list = new HashSet<>();
            list.add(Long.valueOf(target));
            return list;
        }
    },
    DEPARTMENT(2, "部门", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            return new HashSet<>(salaryEmployeeService.listByDepartment(Collections.singletonList(Long.valueOf(target))));
        }
    },
    JOB(3, "岗位", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            return new HashSet<>(salaryEmployeeService.listByJob(Collections.singletonList(Long.valueOf(target))));
        }
    },
    SUB_COMPANY(4, "分部", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            return new HashSet<>(salaryEmployeeService.listBySubCompany(Collections.singletonList(Long.valueOf(target))));
        }
    },
    ROLE(5, "角色", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            HrmCommonService hrmCommonService = new HrmCommonServiceImpl();
            Set<Long> memberList = new HashSet<>();
            List<Object> ids = hrmCommonService.getRoleMembers(target, "2");
            ids.forEach(id -> memberList.add(Long.parseLong(id.toString())));
            return memberList;
        }
    },
    LEVEL(6, "安全级别", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            String[] split = target.split("-");
            int minLevel = split[0] == null ? 0 : Integer.parseInt(split[0]);
            int maxLevel = split[1] == null ? 100 : Integer.parseInt(split[1]);
            Set<Long> memberList = new HashSet<>();
            RecordSet rs = new RecordSet();
            if (rs.execute("select id from hrmresource where level between " + minLevel + " and " + maxLevel + " and status not in (7) and (accounttype is null or accounttype = 0) ")) {
                while (rs.next()) {
                    memberList.add(Long.parseLong(rs.getString("id")));
                }
            }
            return memberList;
        }
    },
//    ALL(7, "所有人", 87626) {
//        @Override
//        public Set<Long> getEmpIds(String target, Object... param) {
//            Set<Long> memberList = new HashSet<>();
//            RecordSet rs = new RecordSet();
//            if (rs.execute("select id from hrmresource where status not in (7) and (accounttype is null or accounttype = 0) ")) {
//                while (rs.next()) {
//                    memberList.add(Long.parseLong(rs.getString("id")));
//                }
//            }
//            return memberList;
//        }
//    },
//    LOGIN_PERSON(8, "登录人", 87626) {
//        //参数1，登录人信息
//        @Override
//        public Set<Long> getEmpIds(String target, Object... param) {
//            LoginPersonTargetTypeEnum loginPersonTargetTypeEnum = LoginPersonTargetTypeEnum.parseByValue(Integer.parseInt(target));
//            return loginPersonTargetTypeEnum.getEmpIds((DataCollectionEmployee) param[0]);
//        }
//    },
    SOB(9, "账套", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            SalaryEmployeeServiceImpl salaryEmployeeService = new SalaryEmployeeServiceImpl();
            List<DataCollectionEmployee> employeeList = salaryEmployeeService.listBySalarySobId(Long.valueOf(target),true);
            return SalaryEntityUtil.properties(employeeList, DataCollectionEmployee::getEmployeeId);
        }
    },
    TAX(10, "扣缴义务人", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            TaxAgentEmpServiceImpl taxAgentEmpServiceImpl = new TaxAgentEmpServiceImpl();
            List<Long> taxIds = Lists.newArrayList(Long.valueOf(target));
            List<TaxAgentEmpPO> taxAgentEmpPOS = taxAgentEmpServiceImpl.listByTaxAgentIds(taxIds, UseEmployeeTypeEnum.ORG);
            return SalaryEntityUtil.properties(taxAgentEmpPOS, TaxAgentEmpPO::getEmployeeId);
        }
    },
    SQL(11, "SQL", 87626) {
        @Override
        public Set<Long> getEmpIds(String target, Object... param) {
            //参数1，登录人信息
//            DataCollectionEmployee employee = (DataCollectionEmployee) param[0];
//            target = target.replaceAll("$uid$", Util.null2String(employee.getEmployeeId()));
            Set<Long> memberList = new HashSet<>();
            RecordSet rs = new RecordSet();
            if (rs.execute(target)) {
                while (rs.next()) {
                    memberList.add(Long.parseLong(rs.getString("id")));
                }
            }
            return memberList;
        }
    };


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

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

    public abstract Set<Long> getEmpIds(String target, Object... param);


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

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

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

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