package com.engine.organization.util;

import com.engine.organization.mapper.common.RefreshUseMapper;
import com.engine.organization.mapper.post.PostInfoMapper;
import com.engine.organization.mapper.scheme.GradeMapper;
import com.engine.organization.mapper.scheme.LevelMapper;
import com.engine.organization.mapper.scheme.SchemeMapper;
import com.engine.organization.mapper.sequence.SequenceMapper;
import com.engine.organization.mapper.staff.StaffMapper;
import com.engine.organization.mapper.staff.StaffPlanMapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/06/01
 * @version: 1.0
 */
public class RefreshIsUsedUtil {

    private static RefreshUseMapper getRefreshUseMapper() {
        return MapperProxyFactory.getProxy(RefreshUseMapper.class);
    }


    /**
     * 刷新等级方案表 引用状态
     * <p>
     * JCL_ORG_LEVEL,JCL_ORG_GRADE,JCL_ORG_SEQUENCE,JCL_ORG_JOB
     */
    public static void RefreshScheme(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(SchemeMapper.class).listUsedId();
        usedIds.removeIf(Objects::isNull);
        RefreshIsUsedStatus(tableName, usedIds);
    }

    /**
     * 刷新职等表 引用状态
     * <p>
     * JCL_ORG_GRADE,JCL_ORG_JOBDT
     */
    public static void RefreshLevel(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(LevelMapper.class).listUsedId();
        usedIds.removeIf(StringUtils::isBlank);
        RefreshIsUsedStatus(tableName, usedIds);
    }

    /**
     * 刷新职级表 引用状态
     * <p>
     * JCL_ORG_JOBDT
     */
    public static void RefreshGrade(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(GradeMapper.class).listUsedId();
        //usedIds.forEach(System.out ::println);
        usedIds.removeIf(StringUtils::isBlank);
        RefreshIsUsedStatus(tableName, usedIds);
    }

    /**
     * 刷新岗位序列表 引用状态
     * <p>
     * JCL_ORG_JOB
     */
    public static void RefreshSequence(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(SequenceMapper.class).listUsedId();
        usedIds.removeIf(StringUtils::isBlank);
        RefreshIsUsedStatus(tableName, usedIds);
    }

    /**
     * 刷新编制方案列表 引用状态
     * <p>
     * JCL_ORG_JOB
     */
    public static void RefreshStaffPlan(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(StaffPlanMapper.class).listUsedId();
        usedIds.removeIf(StringUtils::isBlank);
        RefreshIsUsedStatus(tableName, usedIds);
    }

    /**
     * 刷新编制表列表 引用状态
     * <p>
     * JCL_ORG_JOB
     */
    public static void RefreshStaff(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(StaffMapper.class).listUsedId();
        usedIds.removeIf(StringUtils::isBlank);
        RefreshIsUsedStatus(tableName, usedIds);
    }


    /**
     * 刷新职务信息
     * <p>
     * JCL_ORG_JOBEXT_DT1
     */
    public static void RefreshPostInfo(String tableName) {
        List<String> usedIds = MapperProxyFactory.getProxy(PostInfoMapper.class).listUsedId();
        usedIds.removeIf(StringUtils::isBlank);
        RefreshIsUsedStatus(tableName, usedIds);
    }

    /**
     * 更新指定表的引用状态
     *
     * @param tableName
     * @param usedIds
     */
    private static void RefreshIsUsedStatus(String tableName, List<String> usedIds) {
        if (CollectionUtils.isNotEmpty(usedIds)) {
            List<String> collect = Arrays.stream(String.join(",", usedIds).split(",")).collect(Collectors.toList());
            getRefreshUseMapper().updateIsUsedByIds(tableName, collect, 0);
            getRefreshUseMapper().updateIsUsedByIds(tableName, collect, 1);
        } else {
            getRefreshUseMapper().initIsUseStatus(tableName);
        }
    }


}
