package com.sys.bus.model;

import com.sys.bus.model.base.BaseKpi;
import com.sys.core.utils.Tool;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Kpi extends BaseKpi<Kpi> {
	public static final Kpi dao = new Kpi().dao();
	public <T> T get(String attr) {
		if(attr.equals("kpi_score")||attr.equals("kpi_value")||attr.equals("kpi_weight")||attr.equals("kpi_zpscore")||attr.equals("entry_selfkpiweight")){
			return (T)Tool.getStripZeros((BigDecimal) super.get(attr));
		}
		return super.get(attr);
	}

	public boolean save() {
		checkKpiLength();
		return super.save();
	}
	public boolean update() {
		checkKpiLength();
		return super.update();
	}
	/**
	 * kpi 录入长度检查
	 */
	public void checkKpiLength(){
		String name = this.getKpiName();
		String target = this.getKpiTarget();
		String define = this.getKpiDefine();
		String caption = this.getKpiCaption();
		String zp = this.getKpiZp();
		String info = this.getKpiInfo();
		int nameLen = 500;
		int targetLen = 100;
		int defineLen = 500;
		int captionLen = 5000;
		int zpLen = 1000;
		int infoLen = 1000;

		if(name !=null && name.length()>nameLen)
			throw new RuntimeException("指标("+name+") 长度太长，长度最大为"+nameLen+",目前为"+name.length());
		String title = "指标("+name+")以下信息录入太长：";
		if(StringUtils.isBlank(name))title = "有指标信息录入太长，请检查：";
		String msg = null;
		if(target !=null && target.length()>targetLen){
			msg = title + "目标值长度最大为"+targetLen+",目前为"+target.length();
		}

		if(define !=null && define.length()>defineLen){
			msg = title +  "指标定义长度最大为"+defineLen+",目前为"+define.length();
		}
		if(caption !=null && caption.length()>captionLen){
			msg = title +  "衡量标准最大为"+captionLen+",目前为"+caption.length();
		}
		if(zp !=null && zp.length()>zpLen){
			msg = title +  "自评(自评分、自评详细说明)长度最大为"+zpLen+",目前为"+zp.length();
		}
		if(info !=null && info.length()>infoLen){
			msg = title + "评分说明长度最大为"+infoLen+",目前为"+info.length();
		}
		if(msg != null){
			throw new RuntimeException(msg);
		}
	}
}
