package com.engine.salary.entity.salarysob.po;

import com.engine.salary.entity.salaryformula.po.FormulaPO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.enums.SalaryRoundingModeEnum;
import com.engine.salary.enums.SalaryValueTypeEnum;
import com.engine.salary.enums.salaryitem.SalaryDataTypeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Harryxzy
 * @date 2022/11/15 16:06
 * @description 薪资账套回算薪资项目
 * //hrsa_salary_sob_back_item")
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("SalarySobBackItem")
public class SalarySobBackItemPO{

    /**
     * 主键id
     */
    @XStreamAlias("id")
    @XStreamAsAttribute
    private Long id;

    /**
     * 薪资账套id
     */
    @XStreamAlias("salarySobId")
    @XStreamAsAttribute
    private Long salarySobId;

    /**
     * 薪资项目id
     */
    @XStreamAlias("salaryItemId")
    @XStreamAsAttribute
    private Long salaryItemId;

    /**
     * 薪资项目code
     */
    @XStreamAlias("salaryItemCode")
    @XStreamAsAttribute
    private String salaryItemCode;

    /**
     * 字段类型
     *
     * @see SalaryDataTypeEnum
     */
    @XStreamAlias("dataType")
    @XStreamAsAttribute
    private String dataType;

    /**
     * 进位规则
     *
     * @see SalaryRoundingModeEnum
     */
    @XStreamAlias("roundingMode")
    @XStreamAsAttribute
    private Integer roundingMode;

    /**
     * 保留的小数位数
     */
    @XStreamAlias("pattern")
    @XStreamAsAttribute
    private Integer pattern;

    /**
     * 取值方式
     *
     * @see SalaryValueTypeEnum
     */
    @XStreamAlias("valueType")
    @XStreamAsAttribute
    private Integer valueType;

    /**
     * 公式id
     */
    @XStreamAlias("formulaId")
    @XStreamAsAttribute
    private Long formulaId;

    /**
     * 0:已发项目、1:补发薪资项目
     */
    @XStreamAlias("backCalcType")
    @XStreamAsAttribute
    private Integer backCalcType;

    /**
     * 默认值
     */
    @XStreamAlias("defaultValue")
    @XStreamAsAttribute
    private String defaultValue;

    /**
     * 租户key
     */
    @XStreamAlias("tenantKey")
    @XStreamAsAttribute
    @JsonIgnore
    private String tenantKey;

    /**
     * 创建人id
     */
    @XStreamAlias("creator")
    @XStreamAsAttribute
    @JsonIgnore
    private Long creator;

    /**
     * 是否删除
     */
    @XStreamAlias("deleteType")
    @XStreamAsAttribute
    @JsonIgnore
    private Integer deleteType;

    /**
     * 创建时间
     */
    @XStreamAlias("createTime")
    @XStreamAsAttribute
    @JsonIgnore
    private Date createTime;

    /**
     * 更新时间
     */
    @XStreamAlias("updateTime")
    @XStreamAsAttribute
    @JsonIgnore
    private Date updateTime;



    @XStreamAlias("SalaryItem")
    private SalaryItemPO salaryItem;

    @XStreamAlias("Formula")
    FormulaPO formula;


    /**
     * 薪资项目id
     */
    private Collection<Long> salaryItemIds;

    public String toCompareString() {
        return "SalarySobBackItemPO{" +
                "salarySobId=" + salarySobId +
                ", salaryItemId=" + salaryItemId +
                ", salaryItemCode='" + salaryItemCode + '\'' +
                ", dataType='" + dataType + '\'' +
                ", roundingMode=" + roundingMode +
                ", pattern=" + pattern +
                ", valueType=" + valueType +
                ", formulaId=" + formulaId +
                ", backCalcType=" + backCalcType +
                ", defaultValue=" + defaultValue +
                '}';
    }

    public static String toCompareString(List<SalarySobBackItemPO> salarySobBackItems) {
        if (CollectionUtils.isEmpty(salarySobBackItems)) {
            return "";
        }
        return salarySobBackItems.stream()
                .sorted(Comparator.comparingLong(SalarySobBackItemPO::getSalaryItemId))
                .map(SalarySobBackItemPO::toCompareString)
                .collect(Collectors.joining(","));
    }
}