package jnpf.model.personnels.bo;


import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.model.personnels.po.FtbPersonnelsRegistrationFormField;
import jnpf.model.personnels.po.FtbPersonnelsRegistrationFormType;
import jnpf.model.personnels.vo.roster.FtbRosterAttributesVO;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

/**
 * 花名册导入常量
 *
 * @author fantaibao
 * @date 2024/02/18
 */
public interface FtbRosterImportConstants {
    /**
     * 姓名校验正则表达式
     */
    String NAME_REGEX = "^(?![.·\\s])([\\u2E80-\\u9FFF·]{2,60}|[a-zA-Z\\s]{2,60})(?<![.·\\s])$";
    /**
     * 错误备注
     */
    String ERROR_REMARKS = "错误备注";
    /**
     * 花名册导入Sheet,Redis变量
     */
    String ROSTER_IMPORT_SHEET_REDIS_VARIABLES = "ftb:roster:import-sheet:%s";
    /**
     * 花名册导入Redis变量
     */
    String ROSTER_IMPORT_REDIS_VARIABLES = "ftb:roster:import-key:%s";
    /**
     * 花名册表头排除字段
     */
    Set<String> ROSTER_EXCLUDE_FIELDS = new HashSet<>(List.of(
            "上传身份证", "上传头像", "入职备注", "转正备注", "年龄", "工龄"
            , "入职所属组织", "入职所属岗位", "入职所属职级", "入职班组", "直属主管", "门店", "预计入职日期"
            , "上传银行卡", "意向组织",
            "合同上传", "试岗期","合同名称","合同类型","合同签署状态","签署次数",
            "身份证人像面", "身份证国徽面", "学历证书", "学位证书", "前公司离职证明", "员工照片", "健康证",
            "合同详情", "附件", "任务编号"
    ));
    /**
     * 花名册表头排除字段
     */
    Set<String> ROSTER_EXCLUDE_FIELDS_EXPORT = new HashSet<>(List.of(
            "上传身份证", "上传头像"
            , "入职所属组织", "入职所属岗位", "入职所属职级", "入职班组", "直属主管", "门店", "预计入职日期","入职所属门店"
            , "上传银行卡", "意向组织",
            "合同上传", "试岗期",
            "身份证人像面", "身份证国徽面", "学历证书", "学位证书", "前公司离职证明", "员工照片", "健康证",
            "附件"
    ));
    /**
     * 花名册表头工作表头滞后处理，取决于optionType字段
     * 角色组和考勤组
     */
    Set<Integer> ROSTER_HEADER_ADDITIONAL_REQUIRED_FIELDS = new HashSet<>(List.of(9, 10));

    List<FtbPersonnelsRegistrationFormField> ROSTER_NAME_MOBILE_NUMBER_REQUIRED = List.of(
            new FtbPersonnelsRegistrationFormField("姓名", 1, null),
            new FtbPersonnelsRegistrationFormField("手机号码", 1, RegexPool.MOBILE)
    );
    /**
     * 花名册导入工作信息额外字段
     */
    List<FtbPersonnelsRegistrationFormField> ROSTER_IMPORT_JOB_INFORMATION_EXTRA_FIELDS = List.of(
            new FtbPersonnelsRegistrationFormField("直属主管ID", 0, null, "currReportsToId")
    );
    /**
    * 花名册导出工作信息额外字段
    */
    List<FtbPersonnelsRegistrationFormField> ROSTER_EXPORT_JOB_INFORMATION_EXTRA_FIELDS = List.of(
            new FtbPersonnelsRegistrationFormField("完整组织", 0, null, "completeOrganization"),
            new FtbPersonnelsRegistrationFormField("直属主管", 0, null, "currReportsToId")
    );
    /**
     * 花名册导出个人材料额外字段
    */
    List<FtbPersonnelsRegistrationFormField> ROSTER_EXPORT_PERSONAL_MATERIALS_EXTRA_FIELDS = List.of(
            new FtbPersonnelsRegistrationFormField("健康证状态", 0, null, "healthCertificateStatus")
    );

    Predicate<? super FtbRosterAttributesVO> phonePredicate = a -> "*手机号码".equals(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> userNamePredicate = a -> "*姓名".equals(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> organizationPredicate = a -> "*所属组织".contains(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> postPredicate = a -> "*岗位".contains(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> gradePredicate = a -> "*职级".contains(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> team = a -> "*班组".contains(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> actualDateOfJoining = a -> "*实际入职日期".contains(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> employeeStatus = a -> "*员工状态".contains(a.getAttributeName());
    Predicate<? super FtbRosterAttributesVO> probationPeriod = a -> "*试用期".contains(a.getAttributeName());

    /**
     * 花名册导入登记表配置通用查询
     *
     * @return {@link LambdaQueryWrapper}<{@link FtbPersonnelsRegistrationFormType}>
     */
    static LambdaQueryWrapper<FtbPersonnelsRegistrationFormType> getRegistrationFormTypeQueryWrapper() {
        // 登记类型配置
        LambdaQueryWrapper<FtbPersonnelsRegistrationFormType> formTypeLambdaQueryWrapper = Wrappers.lambdaQuery();
        formTypeLambdaQueryWrapper.eq(FtbPersonnelsRegistrationFormType::getStatus, 0);
        formTypeLambdaQueryWrapper.eq(FtbPersonnelsRegistrationFormType::getEnabledMark, 0);
        //formTypeLambdaQueryWrapper.notIn(FtbPersonnelsRegistrationFormType::getName, List.of("个人材料"));
        formTypeLambdaQueryWrapper.notIn(FtbPersonnelsRegistrationFormType::getId, "5");
        formTypeLambdaQueryWrapper.orderByAsc(FtbPersonnelsRegistrationFormType::getSorts);
        return formTypeLambdaQueryWrapper;
    }

    /**
     * 花名册导出登记表配置通用查询
     *
     * @return {@link LambdaQueryWrapper}<{@link FtbPersonnelsRegistrationFormType}>
     */
    static LambdaQueryWrapper<FtbPersonnelsRegistrationFormType> getRegistrationFormTypeQueryWrapperExport() {
        // 登记类型配置
        LambdaQueryWrapper<FtbPersonnelsRegistrationFormType> formTypeLambdaQueryWrapper = Wrappers.lambdaQuery();
        formTypeLambdaQueryWrapper.eq(FtbPersonnelsRegistrationFormType::getStatus, 0);
        formTypeLambdaQueryWrapper.eq(FtbPersonnelsRegistrationFormType::getEnabledMark, 0);
        formTypeLambdaQueryWrapper.orderByAsc(FtbPersonnelsRegistrationFormType::getSorts);
        return formTypeLambdaQueryWrapper;
    }

    /**
     * 花名册导入登记表属性通用查询
     *
     * @param formTypeId 表单类型 ID
     * @return {@link LambdaQueryWrapper}<{@link FtbPersonnelsRegistrationFormField}>
     */
    static LambdaQueryWrapper<FtbPersonnelsRegistrationFormField> getRegistrationFormFieldQueryWrapper(String formTypeId) {
        // 登记表属性
        LambdaQueryWrapper<FtbPersonnelsRegistrationFormField> formFieldLambdaQueryWrapper = Wrappers.lambdaQuery();
        formFieldLambdaQueryWrapper.eq(StrUtil.isNotBlank(formTypeId), FtbPersonnelsRegistrationFormField::getFormTypeId, formTypeId);
        formFieldLambdaQueryWrapper.eq(FtbPersonnelsRegistrationFormField::getEnabledMark, 0);
        formFieldLambdaQueryWrapper.eq(FtbPersonnelsRegistrationFormField::getStatus, 0);
        formFieldLambdaQueryWrapper.notIn(FtbPersonnelsRegistrationFormField::getType, 5, 6);
        formFieldLambdaQueryWrapper.orderByAsc(FtbPersonnelsRegistrationFormField::getSorts);
        return formFieldLambdaQueryWrapper;
    }

    /**
     * 添加额外字段和排除额外字段
     */
    static void nameAndMobileNumberAdded(List<FtbPersonnelsRegistrationFormField> ftbPersonnelsRegistrationFormFieldList, String formTypeName) {
        for (int i = 0; i < ROSTER_NAME_MOBILE_NUMBER_REQUIRED.size(); i++) {
            int finalI = i;
            FtbPersonnelsRegistrationFormField data = ftbPersonnelsRegistrationFormFieldList.stream()
                    .filter(
                            e -> ROSTER_NAME_MOBILE_NUMBER_REQUIRED.get(finalI).getName().equals(e.getName())
                    ).findFirst().orElse(null);
            if (data == null) {
                ftbPersonnelsRegistrationFormFieldList.add(finalI, ROSTER_NAME_MOBILE_NUMBER_REQUIRED.get(i));
            }
        }
        if ("工作信息".equals(formTypeName)) {
            ftbPersonnelsRegistrationFormFieldList.addAll(FtbRosterImportConstants.ROSTER_IMPORT_JOB_INFORMATION_EXTRA_FIELDS);
            for (Integer rosterHeaderAdditionalRequiredField : ROSTER_HEADER_ADDITIONAL_REQUIRED_FIELDS) {
                FtbPersonnelsRegistrationFormField attendanceGroup = ftbPersonnelsRegistrationFormFieldList.stream()
                        .filter(jobInformationTypeMatching(rosterHeaderAdditionalRequiredField)).findFirst().orElse(null);
                ftbPersonnelsRegistrationFormFieldList.removeIf(jobInformationTypeMatching(rosterHeaderAdditionalRequiredField));
                if (Objects.nonNull(attendanceGroup)) {
                    ftbPersonnelsRegistrationFormFieldList.add(attendanceGroup);
                }
            }
        }
        // 排除额外项
        ftbPersonnelsRegistrationFormFieldList.removeIf(personnelsRegistrationFormField -> ROSTER_EXCLUDE_FIELDS.contains(personnelsRegistrationFormField.getName()));
    }

    /**
     * 添加额外字段和排除额外字段
     */
    static void nameAndMobileNumberAddedNew(List<FtbPersonnelsRegistrationFormField> ftbPersonnelsRegistrationFormFieldList, String formTypeId) {
        // 排除额外项
        ftbPersonnelsRegistrationFormFieldList.removeIf(personnelsRegistrationFormField -> ROSTER_EXCLUDE_FIELDS.contains(personnelsRegistrationFormField.getName()));
        if ("3".equals(formTypeId)) {
            ftbPersonnelsRegistrationFormFieldList.addAll(FtbRosterImportConstants.ROSTER_IMPORT_JOB_INFORMATION_EXTRA_FIELDS);
        }
    }

    /**
     * 导出添加额外字段和排除额外字段
     */
    static void nameAndMobileNumberAddedNewExport(List<FtbPersonnelsRegistrationFormField> ftbPersonnelsRegistrationFormFieldList, String formTypeId) {
        // 排除额外项
        ftbPersonnelsRegistrationFormFieldList.removeIf(personnelsRegistrationFormField -> ROSTER_EXCLUDE_FIELDS_EXPORT.contains(personnelsRegistrationFormField.getName()));
        if ("3".equals(formTypeId)) {
            ftbPersonnelsRegistrationFormFieldList.addAll(FtbRosterImportConstants.ROSTER_EXPORT_JOB_INFORMATION_EXTRA_FIELDS);
        }
        // 个人材料健康证状态
        if ("7".equals(formTypeId)) {
            ftbPersonnelsRegistrationFormFieldList.addAll(FtbRosterImportConstants.ROSTER_EXPORT_PERSONAL_MATERIALS_EXTRA_FIELDS);
        }
    }


    /**
     * 导入项滞后处理
     *
     * @param optionType 选项类型
     * @return {@link Predicate }<{@link FtbPersonnelsRegistrationFormField }>
     */
    static Predicate<FtbPersonnelsRegistrationFormField> jobInformationTypeMatching(Integer optionType) {
        return a -> optionType.equals(a.getOptionType());
    }


}
