package cn.net.connor.std.processedit.ldy;

import java.math.BigDecimal;

import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class CutterWordTimeOperation extends AbstractAIFOperation {
	
//	private TCSession session;
	private TCComponentBOMLine target;
	
	public CutterWordTimeOperation(TCSession session, TCComponentBOMLine target) {
//		this.session = session;
		this.target = target;
	}

	@Override
	public void executeOperation() throws Exception {
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			BigDecimal loadingTime = new BigDecimal(0), measureTime = new BigDecimal(0);
			StringBuilder message = new StringBuilder("");
			for(AIFComponentContext c : target.getChildren()) {
				TCComponentBOMLine bom = (TCComponentBOMLine) c.getComponent();
				TCComponentItemRevision rev = bom.getItemRevision();
				if(!rev.isTypeOf("LD6_ComtooldesRevision"))
					continue;
				String ld6_loadingTime = rev.getProperty("ld6_loadingTime");
				if(ld6_loadingTime.isEmpty())
					ld6_loadingTime = "0";
				String ld6_measureTime = rev.getProperty("ld6_measureTime");
				if(ld6_measureTime.isEmpty())
					ld6_measureTime = "0";
				String bl_quantity = bom.getProperty("bl_quantity");
				if(bl_quantity.isEmpty())
					bl_quantity = "1";
				try {
//					loadingTime += Double.parseDouble(ld6_loadingTime) * Double.parseDouble(bl_quantity);
					loadingTime = loadingTime.add(new BigDecimal(ld6_loadingTime).multiply(new BigDecimal(bl_quantity)));
				}catch(Exception e) {
					e.printStackTrace();
					message.append("组合刀具【").append(rev).append("】的装刀时间有错误无法计算\n");
				}
				try {
//					measureTime += Double.parseDouble(ld6_measureTime) * Double.parseDouble(bl_quantity);
					measureTime = measureTime.add(new BigDecimal(ld6_measureTime).multiply(new BigDecimal(bl_quantity)));
				}catch(Exception e) {
					e.printStackTrace();
					message.append("组合刀具【").append(rev).append("】的量刀时间有错误无法计算\n");
				}
			}
			target.getItemRevision().setProperty("ld6_loadingTime", ""+loadingTime);
			target.getItemRevision().setProperty("ld6_measureTime", ""+measureTime);
			target.refresh();
			
			if(message.length()==0)
				MessageBox.post(desktop, "刀具工时统计完成", "INFO", MessageBox.INFORMATION);
			else
				MessageBox.post(desktop, message.toString(), "WARN", MessageBox.WARNING);
		} catch (Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
		}
	}
}
