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

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.sischeme.dto.InsuranceSchemeDTO;
import com.engine.salary.entity.sischeme.dto.InsuranceSchemeDetailDTO;
import com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO;
import com.engine.salary.entity.sischeme.po.InsuranceSchemePO;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.enums.sicategory.IsPaymentEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author weaver_cl
 * @Description: 福利方案业务方法
 * @Date 2022/3/8
 * @Version V1.0
 **/
public class InsuranceSchemeBO {

    private InsuranceSchemeBO() {
        throw new SalaryRunTimeException("该类不需要创建实例");
    }


    public static InsuranceSchemePO convert2BatchPO(InsuranceSchemeDTO dto, Long employeeId) {
        if (Objects.isNull(dto)) {
            return null;
        }

        return InsuranceSchemePO.builder()
                .schemeName(dto.getSchemeName())
                .paymentType(dto.getPaymentType().getValue())
                .welfareType(dto.getWelfareType().getValue())
                .sharedType(dto.getSharedType())
                .taxAgentIds(dto.getTaxAgentIds())
                .isUse(1)
                .paymentArea(dto.getPaymentArea())
                .remarks(dto.getRemarks())
                .creator(employeeId)
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .createTime(new Date())
                .updateTime(new Date())
                .deleteType(0)
                .build();
    }


    public static List<InsuranceSchemeDetailPO> convertToInsuranceSchemeDetailPoList(List<InsuranceSchemeDetailDTO> list, long employeeId,Long primaryId) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(item -> InsuranceSchemeDetailPO.builder()
                .insuranceId(item.getInsuranceId())
                .primaryId(primaryId)
                .createTime(new Date())
                .creator(employeeId)
                .deleteType(DeleteTypeEnum.NOT_DELETED.getValue())
                .effectiveTime(item.getEffectiveTime())
                .expirationTime(item.getExpirationTime())
                .fixedCost(item.getFixedCost() != null ? item.getFixedCost().toString() : "0.000")
                .isPayment(item.getIsPayment() ? IsPaymentEnum.YES.getValue() : IsPaymentEnum.NO.getValue())
                .lowerLimit(item.getLowerLimit() != null ? item.getLowerLimit().toString() : "0.000")
                .paymentScope(item.getPaymentScopeValue())
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .upperLimit(item.getUpperLimit() != null ? item.getUpperLimit().toString() : "0.000")
                .paymentProportion(item.getPaymentProportion() != null ? item.getPaymentProportion().toString() : "0.0000")
                .rententionRule(item.getRententionRule() != null ? Integer.valueOf(item.getRententionRule()) : null)
                .paymentCycle(Integer.parseInt(item.getPaymentCycle()))
                .cycleSetting(item.getCycleSetting())
                .accountType(Integer.parseInt(item.getAccountType()))
                .updateTime(new Date())
                .validNum(item.getValidNum()==null ? 2 : item.getValidNum())
                .build()
        ).collect(Collectors.toList());
    }

    public static InsuranceSchemePO buildInsuranceSchemePO(InsuranceSchemePO po, InsuranceSchemeDTO dto) {
        po.setUpdateTime(new Date());
        po.setPaymentArea(dto.getPaymentArea());
        po.setPaymentType(dto.getPaymentType().getValue());
        po.setSchemeName(dto.getSchemeName());
        po.setRemarks(dto.getRemarks());
        po.setSharedType(dto.getSharedType());
        po.setTaxAgentIds(dto.getTaxAgentIds());
        return po;
    }
}
