package thyyxxk.webserver.entity.reports.illegalchargesanalysis;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.utils.StringUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dj
 */
@Data
public class IllegalChargeTemplate implements Serializable {

    private static final long serialVersionUID = 6293651779985385524L;

    /**
     * 模板唯一id，主键
     */
    private String id;

    /**
     * 模板名称
     */
    private String label;

    /**
     * 模板具体内容
     */
    private String value;

    /**
     * 超量收费上限
     */
    private Integer maxChargeNum;

    /**
     * 超量收费计算单位（day,week,month）
     * */
    private String maxChargeUnit;

    /**
     * 父模板id
     */
    private String parent;

    /**
     * 是否属于个人模板：0-公共模板，1-个人模板
     */
    private Integer personal;

    /**
     * 模板属性：1-同时收费模板 2-超量收费模板
     */
    private Integer attribute;

    /**
     * 项目类型：0-目录，1-项目
     */
    private Integer type;

    @TableField(exist = false)
    private List<IllegalChargeTemplate> children = new ArrayList<>();

    @TableField(exist = false)
    private List<CodeName> mainCharges = new ArrayList<>();

    @TableField(exist = false)
    private List<CodeName> conflictCharges = new ArrayList<>();

    @TableField(exist = false)
    private String start;

    @TableField(exist = false)
    private String end;

    @TableField(exist = false)
    private List<String> medtypes;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private long currentPage;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private long pageSize;

    
    public void analyzeValue() {
        if (StringUtil.notBlank(value)) {
            String[] allCharges = value.split("\\$");
            setMainCharges(genChargeListFromValue(allCharges[0]));
            if (allCharges.length > 1) {
                if (attribute == 1) {
                    setConflictCharges(genChargeListFromValue(allCharges[1]));
                } else {
                    String[] maxVal = allCharges[1].split("\\|");
                    maxChargeNum = Integer.valueOf(maxVal[0]);
                    maxChargeUnit = maxVal[1];
                }
            }
        }
    }

    private List<CodeName> genChargeListFromValue(String charge) {
        List<CodeName> list = new ArrayList<>();
        String[] charges = charge.split("#");
        for (String s : charges) {
            String[] item = s.split("\\|");
            CodeName codeName = new CodeName(item[0], item[1]);
            list.add(codeName);
        }
        return list;
    }
}
