package com.engine.salary.entity.salaryitem.bo;

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salaryitem.dto.SalaryItemFormDTO;
import com.engine.salary.entity.salaryitem.dto.SysSalaryItemListDTO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salaryitem.po.SysSalaryItemPO;
import com.engine.salary.enums.SalaryRoundingModeEnum;
import com.engine.salary.enums.SalarySystemTypeEnum;
import com.engine.salary.enums.SalaryValueTypeEnum;
import com.engine.salary.enums.salaryitem.SalaryDataTypeEnum;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.IdGenerator;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 系统薪资项目
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SysSalaryItemBO {
    /**
     * 查询参数构建queryWrapper
     *
     * @param searchParam 系统薪资项目列表查询参数
     * @return
     */
//    public static Wrapper<SysSalaryItemPO> buildQueryWrapper(SysSalaryItemSearchParam searchParam) {
//        LambdaQueryWrapper<SysSalaryItemPO> wrapper = Wrappers.lambdaQuery();
//        wrapper.eq(SysSalaryItemPO::getTenantKey, SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
//                .eq(SysSalaryItemPO::getDeleteType, 0)
//                .like(SalaryEntityUtil.isNotNullOrEmpty(searchParam.getName()), SysSalaryItemPO::getName, searchParam.getName())
//                .notIn(SalaryEntityUtil.isNotNullOrEmpty(searchParam.getExcludeIds()), SysSalaryItemPO::getId, searchParam.getExcludeIds());
//        if (Objects.nonNull(searchParam.getDataType())) {
//            wrapper.eq(SysSalaryItemPO::getDataType, searchParam.getDataType().getValue());
//        }
//        wrapper.orderByDesc(SysSalaryItemPO::getId);
//        return wrapper;
//    }

    /**
     * 系统薪资项目po转换成系统薪资项目列表dto
     *
     * @param sysSalaryItems 系统薪资项目集合
     * @return
     */
    public static List<SysSalaryItemListDTO> convert2ListDTO(Collection<SysSalaryItemPO> sysSalaryItems) {
        if (CollectionUtils.isEmpty(sysSalaryItems)) {
            return Collections.emptyList();
        }
        return sysSalaryItems.stream().map(sysSalaryItemPO -> {
                    SalaryRoundingModeEnum salaryRoundingModeEnum = SalaryRoundingModeEnum.parseByValue(sysSalaryItemPO.getRoundingMode());
                    SalaryValueTypeEnum salaryValueTypeEnum = SalaryValueTypeEnum.parseByValue(sysSalaryItemPO.getValueType());
                    SalaryDataTypeEnum salaryDataTypeEnum = SalaryDataTypeEnum.parseByValue(sysSalaryItemPO.getDataType());
                    return SysSalaryItemListDTO.builder()
                            .id(sysSalaryItemPO.getId())
                            .name(sysSalaryItemPO.getName())
                            .roundingMode(Optional.ofNullable(salaryRoundingModeEnum)
                                    .map(e -> SalaryI18nUtil.getI18nLabel(e.getLabelId(), e.getDefaultLabel()))
                                    .orElse(StringUtils.EMPTY))
                            .pattern(sysSalaryItemPO.getPattern())
                            .valueType(Optional.ofNullable(salaryValueTypeEnum)
                                    .map(e -> SalaryI18nUtil.getI18nLabel(e.getLabelId(), e.getDefaultLabel()))
                                    .orElse(StringUtils.EMPTY))
                            .dataType(Optional.ofNullable(salaryDataTypeEnum)
                                    .map(e -> SalaryI18nUtil.getI18nLabel(e.getLabelId(), e.getDefaultLabel()))
                                    .orElse(StringUtils.EMPTY))
                            .taxDeclarationColumn(SalaryItemBO.buildTaxDeclarationColumn(sysSalaryItemPO.getCode()))
                            .build();
                }
        ).collect(Collectors.toList());
    }

    /**
     * 转换成薪资项目详情dto
     *
     * @param sysSalaryItemPO
     * @return
     */
    public static SalaryItemFormDTO convert2FormDTO(SysSalaryItemPO sysSalaryItemPO) {
        return new SalaryItemFormDTO()
                .setId(sysSalaryItemPO.getId())
                .setName(sysSalaryItemPO.getName())
                .setCanEdit(sysSalaryItemPO.getCanEdit())
                .setSystemType(sysSalaryItemPO.getSystemType())
                .setUseDefault(sysSalaryItemPO.getUseDefault())
                .setHideDefault(0)
                .setUseInEmployeeSalary(sysSalaryItemPO.getUseInEmployeeSalary())
                .setRoundingMode(sysSalaryItemPO.getRoundingMode())
                .setPattern(sysSalaryItemPO.getPattern())
                .setValueType(sysSalaryItemPO.getValueType())
                .setDataType(sysSalaryItemPO.getDataType())
                .setFormulaId(sysSalaryItemPO.getFormulaId())
                .setDescription(sysSalaryItemPO.getDescription())
                .setSharedType(sysSalaryItemPO.getSharedType())
                .setTaxAgentIds(sysSalaryItemPO.getTaxAgentIds());
    }

    /**
     * 系统薪资项目转换成自定义薪资项目
     *
     * @param sysSalaryItems 系统薪资项目集合
     * @return
     */
    public static List<SalaryItemPO> convert2SalaryItemPO(Collection<SysSalaryItemPO> sysSalaryItems, Long employeeId) {
        if (CollectionUtils.isEmpty(sysSalaryItems)) {
            return Collections.emptyList();
        }
        LocalDateTime now = LocalDateTime.now();
        return sysSalaryItems.stream()
                .map(e -> convert2SalaryItemPO(e, employeeId))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    /**
     * 系统薪资项目转换成自定义薪资项目
     *
     * @param sysSalaryItemPO 系统薪资项目
     * @param employeeId      人员id
     * @return
     */
    private static SalaryItemPO convert2SalaryItemPO(SysSalaryItemPO sysSalaryItemPO, Long employeeId) {
        if (sysSalaryItemPO == null) {
            return null;
        }
        long id = IdGenerator.generate();
        return SalaryItemPO.builder()
                .id(id)
                .code(sysSalaryItemPO.getCode())
                .name(sysSalaryItemPO.getName())
                .systemType(SalarySystemTypeEnum.SYSTEM.getValue())
                .sysSalaryItemId(sysSalaryItemPO.getId())
                .useDefault(sysSalaryItemPO.getUseDefault())
                .useInEmployeeSalary(sysSalaryItemPO.getUseInEmployeeSalary())
                .roundingMode(sysSalaryItemPO.getRoundingMode())
                .pattern(sysSalaryItemPO.getPattern())
                .valueType(sysSalaryItemPO.getValueType())
                .dataType(sysSalaryItemPO.getDataType())
                .formulaId(sysSalaryItemPO.getFormulaId())
                .description(sysSalaryItemPO.getDescription())
                .canEdit(sysSalaryItemPO.getCanEdit())
                .creator(employeeId)
                .deleteType(NumberUtils.INTEGER_ZERO)
                .createTime(new Date())
                .updateTime(new Date())
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .taxAgentIds(sysSalaryItemPO.getTaxAgentIds())
                .sharedType(sysSalaryItemPO.getSharedType())
                .build();
    }
}
