package nc.util.hbbb.workdraft.pub;

import java.util.List;

import nc.bs.logging.Logger;
import nc.pub.iufo.exception.UFOSrvException;
import nc.util.hbbb.HBVersionUtil;
import nc.util.hbbb.hbscheme.HBSchemeSrvUtils;
import nc.util.hbbb.service.HBBaseDocItfService;
import nc.vo.hbbb.adjustscheme.AdjustSchemeVO;
import nc.vo.hbbb.hbscheme.HBSchemeVO;
import nc.vo.iufo.measure.MeasureVO;
import nc.vo.iufo.verctrl.IDataVersionConsts;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ValidationException;
import nc.vo.pub.ValueObject;
import nc.vo.ufoc.workdrafttemp.MeasureInfoVO;
import nc.vo.ufoc.workdrafttemp.WorkDraftTempVO;

import com.ufsoft.table.Cell;

/**
 * 工作底稿基类
 * 
 * @version V6.0
 * @author litfb
 */
public class IWorkDraft extends ValueObject {

    private static final long serialVersionUID = 536764564567561L;

    public static String ISHBBB_CENTER = "ISHBBB_CENTER";

    /** 工作底稿模板 */
    private WorkDraftTempVO workdraftVO;
    /** 工作底稿模板指标信息 */
    private MeasureInfoVO[] measinfovos;

    protected ReportType reporttype;

    protected String pk_workdrafttemp;

    protected String pk_report;

    protected String pk_convertrule;

    protected String pk_hbscheme;

    private HBSchemeVO hbSchemevo;
    //xiangymf  专向用加的方法  ncm区域底稿，用存放单元格pk
    protected Cell[][] cells;

    private AdjustSchemeVO adjschemevo;
    // 用于合并报表组织显示
    protected String[] showorgs;
    // 用于固定指标的工作底稿
    protected MeasureVO[] mesurevos;
    // TODO: 用于报表之间联查的指标
    protected MeasureVO relamesurevos;
    // tabbedPane页签显示名称
    protected String name;

    public IWorkDraft(String newname, String newpk_report, ReportType newreporttype, String newpk_workdrafttemp) {
        this.name = newname;
        this.pk_report = newpk_report;
        this.pk_workdrafttemp = newpk_workdrafttemp;
        this.reporttype = newreporttype;
    }

    public IWorkDraft(String newname, String newpk_report, ReportType newreporttype, String newpk_workdrafttemp,
            String pk_hbscheme) {
        this.name = newname;
        this.pk_report = newpk_report;
        this.pk_workdrafttemp = newpk_workdrafttemp;
        this.reporttype = newreporttype;
        this.pk_hbscheme = pk_hbscheme;
    }

    public IWorkDraft(String newname, String newpk_report, ReportType newreporttype, String newpk_workdrafttemp,
            HBSchemeVO schemeVO) {
        this.name = newname;
        this.pk_report = newpk_report;
        this.pk_workdrafttemp = newpk_workdrafttemp;
        this.reporttype = newreporttype;
        this.hbSchemevo = schemeVO;
    }
    
    /**
     * 调整中心使用
     * jiaah
     * @param newname
     * @param newpk_report
     * @param newreporttype
     * @param newpk_workdrafttemp
     * @param schemeVO
     * @param adjustVO
     */
    public IWorkDraft(String newname, String newpk_report, ReportType newreporttype, String newpk_workdrafttemp,
            HBSchemeVO schemeVO,AdjustSchemeVO adjustVO) {
        this.name = newname;
        this.pk_report = newpk_report;
        this.pk_workdrafttemp = newpk_workdrafttemp;
        this.reporttype = newreporttype;
        this.hbSchemevo = schemeVO;
        this.adjschemevo = adjustVO;
    }

    public IWorkDraft(String newname, String newpk_report, ReportType newreporttype, String newpk_workdrafttemp,
            MeasureVO[] mesurevos, String pk_hbscheme) {
        this.name = newname;
        this.pk_report = newpk_report;
        this.pk_workdrafttemp = newpk_workdrafttemp;
        this.reporttype = newreporttype;
        this.mesurevos = mesurevos;
        this.pk_hbscheme = pk_hbscheme;
    }

    /**
     * 根据报表类型获得对应版本号
     * 
     * @param hbScheme
     * @param adjver
     * @return
     */
    public int getVersion(HBSchemeVO hbScheme, int adjver) {
        int hbver = 0;
        if(getHbSchemevo() != null)
        	hbver = getHbSchemevo().getVersion();
        int result = 0;
        if (getReporttype().equals(ReportType.SEP)) {
            return IDataVersionConsts.VERTYPE_SEPERATE;
        } else if (getReporttype().equals(ReportType.SEP_ADJ) || getReporttype().equals(ReportType.SEP_ADJ_DRAFT)) {
            return adjver;
        } else if (getReporttype().equals(ReportType.HB)) {
            return hbver;
        } else if (getReporttype().equals(ReportType.HB_ADJ) || getReporttype().equals(ReportType.HB_ADJ_DRAFT)) {
            StringBuilder DstStr = new StringBuilder();
            if (null != getHbSchemevo().getPk_adjustscheme()
                    && getHbSchemevo().getPk_adjustscheme().trim().length() > 0) {

                DstStr.append(IDataVersionConsts.VERTYPE_HBBB_ADJUST)
                        .append(getHbSchemevo().getVersion().toString()
                                .substring(2, getHbSchemevo().getVersion().toString().length()))
                        .append(getAdjschemevo().getVersion().toString()
                                .substring(2, getAdjschemevo().getVersion().toString().length()));

            } else {
                DstStr.append(IDataVersionConsts.VERTYPE_HBBB_ADJUST).append(
                        getHbSchemevo().getVersion().toString()
                                .substring(2, getHbSchemevo().getVersion().toString().length()));

            }

            result = Integer.valueOf(DstStr.toString());

            return result;
        } else if (getReporttype().equals(ReportType.HB_CONTRAST)) {
            return hbver;
        }else if (getReporttype().equals(ReportType.HBDIFF)) {
            return HBVersionUtil.getDiffByHBSchemeVO(hbScheme);
        }
        return result;
    }

    /**
     * 获得工作底稿模板
     * 
     * @return
     */
    public WorkDraftTempVO getWorkDraftTempVO() {
        if (workdraftVO == null && !StringUtil.isEmptyWithTrim(pk_workdrafttemp)) {
            try {
            	workdraftVO = HBBaseDocItfService.getRemoteWorkDraft().getWorkDraftTempWithDXTypeSeq(pk_workdrafttemp);
            } catch (BusinessException e) {
                Logger.error("查询工作底稿模板出错!", e);
            }
        }
        return workdraftVO;
    }

    /**
     * 是否使用了底稿模板
     * 
     * @return
     */
    public boolean isdraft() {
        return reporttype.isdraft();
    }

    /**
     * 获得调整方案版本号
     * 
     * @return
     */
    public int getAdjVersion() {
        AdjustSchemeVO adjustSchemeVO = getAdjschemevo();
        if (adjustSchemeVO == null) {
            return 0;
        }
        return adjustSchemeVO.getVersion();
    }

    public String getEditor() {
        return reporttype.getEditorClassName();
    }

    @Override
    public String getEntityName() {
        return null;
    }

    @Override
    public void validate() throws ValidationException {

    }

    /**
     * 获得工作底稿模板指标信息
     * 
     * @return
     */
    public MeasureInfoVO[] getMeasinfovos() {
        if (measinfovos == null && pk_workdrafttemp != null) {
            StringBuilder strwhere = new StringBuilder();
            strwhere.append("measurevos='").append(pk_workdrafttemp).append("'");
            try {
                measinfovos = HBBaseDocItfService.getRemoteWorkDraft().qryAllSortLinenumWherePart(strwhere.toString());
            } catch (BusinessException e) {
                nc.bs.logging.Logger.error(e.getMessage(), e);
            }
        }
        return measinfovos;
    }

    public ReportType getReporttype() {
        return reporttype;
    }

    public void setReporttype(ReportType reporttype) {
        this.reporttype = reporttype;
    }

    public String getPk_workdrafttemp() {
        return pk_workdrafttemp;
    }

    public void setPk_workdrafttemp(String pk_workdrafttemp) {
        this.pk_workdrafttemp = pk_workdrafttemp;
    }

    public String getPk_report() {
        return pk_report;
    }

    public void setPk_report(String pk_report) {
        this.pk_report = pk_report;
    }

    public String getPk_convertrule() {
        return pk_convertrule;
    }

    public void setPk_convertrule(String pk_convertrule) {
        this.pk_convertrule = pk_convertrule;
    }

    public String getPk_hbscheme() {
        return pk_hbscheme;
    }

    public void setPk_hbscheme(String pk_hbscheme) {
        this.pk_hbscheme = pk_hbscheme;
    }

    public HBSchemeVO getHbSchemevo() {
        if (hbSchemevo == null) {
            try {
                hbSchemevo = HBSchemeSrvUtils.getHBSchemeByHBSchemeId(pk_hbscheme);
            } catch (UFOSrvException e) {
                Logger.error(e.getMessage(), e);
            }
        }
        return hbSchemevo;
    }

    public AdjustSchemeVO getAdjschemevo() {
        if (adjschemevo == null) {
            try {
                String pk_adjustscheme = getHbSchemevo().getPk_adjustscheme();
                if (!StringUtil.isEmptyWithTrim(pk_adjustscheme)) {
                    adjschemevo = (AdjustSchemeVO) HBBaseDocItfService.getRemoteUAPQueryBS().retrieveByPK(AdjustSchemeVO.class,
                            pk_adjustscheme);
                }
            } catch (BusinessException e) {
                Logger.error(e.getMessage(), e);
            }
        }
        return adjschemevo;
    }

    public String[] getShoworgs() {
        return showorgs;
    }

    public void setShoworgs(String[] showorgs) {
        this.showorgs = showorgs;
    }

    public MeasureVO[] getMesurevos() {
        return mesurevos;
    }

    public void setMesurevos(MeasureVO[] mesurevos) {
        this.mesurevos = mesurevos;
    }

    public MeasureVO getRelamesurevos() {
        return relamesurevos;
    }

    public void setRelamesurevos(MeasureVO relamesurevos) {
        this.relamesurevos = relamesurevos;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
  //xiangymf  专向用加的方法  ncm
	public Cell[][] getCells() {
		return cells;
	}
	//xiangymf  专向用加的方法  ncm
	public void setCells(Cell[][] cells) {
		this.cells = cells;
	}

	

}
