package eman.event.productionView;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import eman.bean.monitor.EquipBean;
import eman.bean.monitor.OperationActualHoursBean;
import eman.bean.monitor.ScanHumanInfo;
import eman.bean.productionView.MonitorTimeBean;
import eman.bean.productionView.MouldPartOperationBean;
import eman.bean.productionView.QueryMouldInfo.MouldBean;
import eman.bean.productionView.QueryMouldInfo.MouldPartInfoBean;
import eman.bean.productionView.QueryMouldInfo.QueryMouldStateInfoForm;
import eman.bean.system.Result;
import eman.event.EventHandlerBase;
import eman.sql.productionView.QueryMouldInfoSql;
import eman.sys.SysConfig;
import eman.tool.ChineseSwitcher;
/**
* 模具进度信息查询时间,模具计划,模具简单详细视图共用事件 History: 1.0 谢俊 1.5 易平 性能优化 2.0 sam 2005-10-4
* 隐藏没有工序的工件 2.0.1 husha 2005-1-7 根据传递的标识来判断显示已完工或未完工的模具 2.0.1 husha 2006-1-11
* 增加正在加工工序的最近一次开始时间 2.0.3 sam 2006-2-20 增加模具延期天数 yp 3.0.1.23 2006-03-26
* 增加模具加工阶段的查询条件* yaominxi 2006-04-06 修改默认查询条件下有异常出现的bug 胡孟琦 3.1.1 添加交货期和模具类型查询
* 2006.5.7 fq 06-11-04 添加根据模具内部编号模糊查询 cz 2006-12-22 加入在event中的排序字段 searchTtype
* yxb 2007-05-18 新建模具进度详细查询事件 hmq 3.5 添加工时与质检信息 2007.7.3 myh 2007-12-21 EM-8362
* 将工件信息添加到对应的模具 提交
*/
public class MouldStateDetailQueryEvent extends EventHandlerBase {
private static Logger log4j = Logger.getRootLogger();
/**
* 步骤: 1.查询数据库得到所有数据 1.1.查询选择的模具 1.2.查询模具下的所有工件 1.3.过滤:查询模具下未完工的工序信息
* ,工件、状态排序 不过滤:查询模具下所有的工序信息,工件、状态排序 1.4.过滤: 查询所选模具的未完成工序的所有监控记录
* 不过滤:查询所选模具的所有监控记录 1.5.查询生产资源和操作工信息 2.对数据进行处理(综合监控信息和工序信息)
* 2.1计算实际开始时间和实际完成时间 ,查询实际资源、实际操作工 2.2查询对于未完工模具的计划完成时间,计划加工生产资源
* 2.3按照层次结构组织数据结构(工件中工序按照状态分类)
*
* 数据结构: MouldBean MouldPartInfoBean (状态分类) MouldPartOperationBean todo joly
* 2.0 050621加上时间查询条件 时间范围判断规则: 1.对于已经加工完的工序:指实际完成时间在此范围 2.暂停工序: 3.未开始加工的工序:
* 4.正在加工的工序: History: yp 3.0.1.23 2006-03-26 增加模具加工阶段的查询条件 cz 2006-12-21
* 增加按模号排序需求 sam 3.5.1_lbl 2008-03-12 修正实际工时为零时产生的bug
*/
@SuppressWarnings("unchecked")
public void process(ServletContext sc, HttpServletRequest request,
HttpServletResponse response, HashMap bean) {
QueryMouldStateInfoForm form = new QueryMouldStateInfoForm();
try {
/**
* @author husha Get the over or doing flag to sort the table by
* mould layer
*/
String doingflag = (String) request.getParameter("doingflag");
if (doingflag == null) {
doingflag = "false";
} else {
doingflag = "true";
}
String overflag = (String) request.getParameter("overflag");
if (overflag == null) {
overflag = "false";
} else {
overflag = "true";
}
int mouldWorkingState = 0;
if (request.getParameter("mouldWorkingState") != null
&& !(request.getParameter("mouldWorkingState").equals(""))) {
mouldWorkingState = Integer.parseInt(request
.getParameter("mouldWorkingState"));
}
Result result = null;
if (request.getAttribute("result") != null) {
result = (Result) request.getAttribute("result");
} else {
result = new Result();
}
String pattern = "##0.##";
DecimalFormat df = new DecimalFormat(pattern);
String mouldID;
mouldID = request.getParameter("mouldTextID"); // 模具ID
// 工件监控编号,共多页面使用
// String partMonitorID=request.getParameter("partMonitorID");
QueryMouldInfoSql queryMouldInfoSql = new QueryMouldInfoSql();
if (request.getParameter("mouldPartID") != null) {
mouldID = queryMouldInfoSql.queryMouldIDFromPartID(request
.getParameter("mouldPartID").trim(), result);
}
String operationID = "";
if (request.getParameter("operationID") != null) {
operationID = request.getParameter("operationID");
request.setAttribute("operationID", operationID);
}
// 加入筛选,可以删掉完工工序,判断checkboxSelect是否为空,为空表示不过滤
String[] checkboxSelect = request.getParameterValues("isHideDone");
// 判断是否隐藏完工工件
request.setAttribute("isHideDone", checkboxSelect);
HashMap mouldStateInfo = null;
if (mouldID.equals("all")) {
mouldID = "";
}
long startTime = System.currentTimeMillis();
String percent1 = "0"; // 计划工时与实际工时百分比
String percent2 = ""; // 计划工时与实际工时百分比
if (request.getParameter("mouldPartID") != null) {
percent1 = "";
} else {
percent1 = request.getParameter("percent1");
}
if (request.getParameter("mouldPartID") != null) {
percent2 = "";
} else {
percent2 = request.getParameter("percent2");
}
if (percent1 == null) {
percent1 = "";
}
if (percent2 == null) {
percent2 = "";
}
// 加上获取时间查询参数
String actualStartDate; // 查询开始时间
String actualStopDate; // 查询结束时间
String planStartDate; // 查询开始时间
String planStopDate; // 查询结束时间
// 选择了时间范围
String[] checkActualTimeSelect = request
.getParameterValues("actualTimeSelect");
String[] checkPlanTimeboxSelect = request
.getParameterValues("planTimeSelect");
if (checkActualTimeSelect != null) { // 选择了实际时间范围
actualStartDate = request.getParameter("begDate");
actualStopDate = request.getParameter("endDate");
request.setAttribute("actualStartDate", actualStartDate);
request.setAttribute("actualStopDate", actualStopDate);
}
if (checkPlanTimeboxSelect != null) { // 选择了计划时间范围
planStartDate = request.getParameter("begTime");
planStopDate = request.getParameter("endTime");
request.setAttribute("planStartDate", planStartDate);
request.setAttribute("planStopDate", planStopDate);
}
// 选择了时间范围
/*
* if((startDate!=null)&&(!startDate.equals(""))&&(stopDate!=null)&&(!stopDate.equals(""))){
* //1.查询数据库得到所有数据 if (checkboxSelect == null) { //不过滤已经完成的部分
* mouldStateInfo =
* queryMouldInfoSql.queryMouldStateInfo(dateTimeFrom,dateTimeTo,mouldID,
* 0, 0, result); } else { mouldStateInfo =
* queryMouldInfoSql.queryMouldStateInfo(dateTimeFrom,dateTimeTo,mouldID,
* 5, 0, result); } }else
* if((startDate==null)||(startDate.equals(""))||(stopDate==null)||(stopDate.equals(""))){
*/
// 1.查询数据库得到所有数据
String mouldName = "";
if (request.getParameter("mouldName") != null) {
mouldName = ChineseSwitcher.toShow(
request.getParameter("mouldName")).trim();
}
String queryStartTime = "";
if (request.getParameter("queryStartDate") != null) {
queryStartTime = request.getParameter("queryStartDate");
}
String queryEndTime = "";
if (request.getParameter("queryEndDate") != null) {
queryEndTime = request.getParameter("queryEndDate");
}
String mouldClassID = "";
if (request.getParameter("mouldClassID") != null) {
mouldClassID = request.getParameter("mouldClassID");
}
String searchType = "leadTimeAll";
if (request.getParameter("searchType") != null) {
searchType = request.getParameter("searchType");
}
String mouldInteriorIDName = ChineseSwitcher.toShow(request
.getParameter("mouldInteriorIDName"));
// 改变参数列表,将参数封装到form。直接在原来的基础上改的,有些参数不知道有什么用,所以保持不变,在该方法中可能没有用到 fq
// 06-11-04
form.setMouldID(mouldID);
form.setMouldName(mouldName);
form.setPage(0);
form.setMouldWorkingState(mouldWorkingState);
form.setQueryStartTime(queryStartTime);
form.setQueryEndTime(queryEndTime);
form.setMouldClassID(mouldClassID);
form.setMouldInteriorID(mouldInteriorIDName);
form.setSearchType(searchType);
if (checkboxSelect == null) { // 不过滤已经完成的部分
form.setState(0);
} else {
form.setState(5);
}
mouldStateInfo = queryMouldInfoSql.queryMouldStateInfoByForm(form,
result);
System.out.println("database access耗时为*"
+ ((double) System.currentTimeMillis() - startTime) / 1000
+ "*秒");
long startTime2 = System.currentTimeMillis();
// 2.对数据进行处理(综合监控信息和工序信息)
// * 2.1计算实际开始时间和实际完成时间 ,查询实际资源、实际操作工
// * 2.2查询对于未完工模具的计划完成时间,计划加工生产资源
// * 2.3按照层次结构组织数据结构(工件中工序按照状态分类)
ArrayList moulds = (ArrayList) mouldStateInfo.get("moulds");
HashMap mouldsMap = (HashMap) mouldStateInfo.get("mouldsMap");
ArrayList parts = (ArrayList) mouldStateInfo.get("parts");
HashMap partsMap = (HashMap) mouldStateInfo.get("partsMap");
ArrayList operations = (ArrayList) mouldStateInfo.get("operations");
HashMap operationsMap = (HashMap) mouldStateInfo
.get("operationsMap");
ArrayList monitors = (ArrayList) mouldStateInfo.get("monitors");
HashMap operators = (HashMap) mouldStateInfo.get("operators");
HashMap resources = (HashMap) mouldStateInfo.get("resources");
HashMap distributeHoursMap = (HashMap) mouldStateInfo
.get("distributeHoursMap");
if(distributeHoursMap == null)
distributeHoursMap = new HashMap();
// xhy 2009-03-13 OperationActualHours表字段
List operationHoursList = (List) mouldStateInfo
.get("operationHoursList");
// * 2.1 计算实际开始时间和实际完成时间 ,查询实际资源、实际操作工
ArrayList opMonitorList = new ArrayList();
for (int i = 0; i < monitors.size(); i++) {
MonitorTimeBean temp1 = (MonitorTimeBean) monitors.get(i);
opMonitorList.add(temp1);
MonitorTimeBean tempNext = null;
if (!(i + 1 == monitors.size())) { // i为最后一个元素
tempNext = (MonitorTimeBean) monitors.get(i + 1);
if (tempNext.getOperationID() == temp1.getOperationID()) {
continue; // 还有更多监控记录
}
}
// 一个工序的监控记录读取完毕
long hourTime = 0; // 工时
java.sql.Timestamp start = null;
java.sql.Timestamp stop = null;
String resourceIDs = "";
String resourceNames = "";
String operatorIDs = "";
String operatorNames = "";
ArrayList allActualResource = new ArrayList();
ArrayList allActualOperator = new ArrayList();
HashMap allActualOperatorMap = new HashMap();
int opID = 0;
// //////////////
for (int j = 0; j < opMonitorList.size(); j++) {
MonitorTimeBean monitor = (MonitorTimeBean) opMonitorList
.get(j);
// 开始时间和结束时间,工时
java.sql.Timestamp timePoint = monitor.getMonitorTime();
MonitorTimeBean monitorNext = null;
long temHour = 0;
if (j == 0) {
start = timePoint;
} else if (j + 1 == opMonitorList.size()) {
stop = timePoint; // last point,but may be a start
// time
}
// 20050802修改实际工时定义,多个人的乘以人数
String operatorIDNum = monitor.getOperatorID();
int operatorIDNumInt = operatorIDNum.length() / 6;
if (j + 1 != opMonitorList.size() && j % 2 == 0) { // if
// now
// is
// start
// ,and
// has
// next
// point
monitorNext = (MonitorTimeBean) opMonitorList
.get(j + 1);
java.sql.Timestamp timePointNext = monitorNext
.getMonitorTime();
temHour = timePointNext.getTime() - timePoint.getTime();
hourTime = hourTime + (temHour * operatorIDNumInt); // hour
// add
}
// 工序ID
opID = monitor.getOperationID();
// 生产资源
String resourceID = monitor.getResourceID();
if (resourceIDs.indexOf(resourceID) < 0) { // 以前没有保存
resourceIDs += resourceID;
allActualResource.add(resourceID);
allActualResource.add(((EquipBean) resources
.get(resourceID)).getEquipName());
if (resourceIDs.length() == resourceID.length()) { // first
resourceNames += ((EquipBean) resources
.get(resourceID)).getEquipName();
} else {
resourceNames += "; "
+ ((EquipBean) resources.get(resourceID))
.getEquipName();
}
}
// 操作工
String operatorID = monitor.getOperatorID();
ScanHumanInfo scanHumanInfo = new ScanHumanInfo();
scanHumanInfo.setHumanMonitorID(((ScanHumanInfo) operators.get(operatorID)).getHumanMonitorID());
scanHumanInfo.setHumanName(((ScanHumanInfo) operators.get(operatorID)).getHumanName());
scanHumanInfo.setHumanID(operatorID);
scanHumanInfo.setActualDistributeHour(temHour);
allActualOperatorMap.put(operatorID, scanHumanInfo);
if(operatorNames == null || operatorNames.indexOf(operatorID) ==-1){
allActualOperator.add(scanHumanInfo);
operatorNames += "; " + scanHumanInfo.getHumanName() + "(" + operatorID + ")";
}
}
// operation ...plan and actual Info
MouldPartOperationBean operation = (MouldPartOperationBean) operationsMap
.get(new Integer(opID));
operation.setActuralStartTime(start);
if (operation.getState().equals("over")) {
operation.setActuralFinishTime(stop);
} else if (operation.getState().equals("pause")) {
operation.setLastPauseTime(stop);
}
// husha@060111
else if (operation.getState().equals("working")) {
if (stop != null) {
operation.setLastStartTime(stop);
} else {
operation.setLastStartTime(start);
}
}
operation.setActuralWorkHour(((hourTime / (1000.0f * 3600))));
operation.setActuralResource(resourceNames);
operation.setOperatorName(operatorNames);
operation.setActuralResources(allActualResource);
operation.setOperators(allActualOperator);
StringBuffer actualDistributeHourInfo = new StringBuffer();
StringBuffer hourErrorInfo = new StringBuffer();
StringBuffer planDistributeHourInfo = new StringBuffer();
String hoursInfo = "";
if (operation.getState().equals("over")) {
float totalActualDistributeHour = 0;
float totalPlanDistributeHour = 0;
for (int k = 0; k < allActualOperator.size(); k++) {
ScanHumanInfo scanHumanInfo = (ScanHumanInfo) allActualOperator
.get(k);
float actualDistributeHour = scanHumanInfo
.getActualDistributeHour()
/ (1000.0f * 3600);
float planDistributeHour = 0;
float hourError = 0;
if (allActualOperator.size() == 1) {
actualDistributeHour = operation
.getActuralWorkHour();
planDistributeHour = Float.parseFloat(operation
.getPlanWorkingHour());
} else {
if (k == allActualOperator.size() - 1) {
actualDistributeHour = operation
.getActuralWorkHour()
- totalActualDistributeHour;
planDistributeHour = Float.parseFloat(operation
.getPlanWorkingHour())
- totalPlanDistributeHour;
} else {
String humanOperationID = operation
.getOperationID()
+ scanHumanInfo.getHumanID();
if (distributeHoursMap.get(humanOperationID) != null) {
planDistributeHour = Float
.parseFloat((String) distributeHoursMap
.get(humanOperationID));
} else {
try {
if (operation.getActuralWorkHour() == 0) {
planDistributeHour = 0;
} else {
planDistributeHour = actualDistributeHour
* Float
.parseFloat(operation
.getPlanWorkingHour())
/ operation
.getActuralWorkHour();
}
} catch (Exception e) {
planDistributeHour = 0;
}
}
}
}
actualDistributeHour = ((actualDistributeHour));
planDistributeHour = ((planDistributeHour));
if ((planDistributeHour + actualDistributeHour) == 0) {
hourError = 0;
} else {
hourError = (planDistributeHour - actualDistributeHour)
/ (planDistributeHour + actualDistributeHour);
}
totalActualDistributeHour += actualDistributeHour;
totalPlanDistributeHour += planDistributeHour;
actualDistributeHourInfo.append(
scanHumanInfo.getHumanName()).append(":")
.append(df.format(actualDistributeHour))
.append("");
planDistributeHourInfo.append(
scanHumanInfo.getHumanName()).append(":")
.append(df.format(planDistributeHour)).append(
"");
// hourErrorInfo.append(scanHumanInfo.getHumanName()).
// append(":").append(df.format(hourError)).append("");
// xhy 2009-03-16 添加去掉重复后的工时字段
float hours = 0;
if (operationHoursList != null) {
for (int j = 0; j < operationHoursList.size(); j++) {
OperationActualHoursBean operationBean = (OperationActualHoursBean) operationHoursList
.get(j);
if (opID == operationBean.getOpID()) {
if (operationBean.getOperatorID().indexOf(
scanHumanInfo.getHumanMonitorID()) != -1) {
ArrayList reList = operation
.getActuralResources();
if (reList != null) {
for (int s = 0; s < reList.size(); s++) {
String reStr = (String) reList
.get(s);
if (operationBean
.getResourceID()
.equals(reStr)) {
float h = operationBean
.getHours();
// 计算多个人同时加工的平均工时
int len = 0;
for (int n = 0; n < operationBean
.getOperatorID()
.length(); n++) {
char c = operationBean
.getOperatorID()
.charAt(n);
if (c == '+') {
len++;
}
}
if (len > 1) {
// 平均工时
h = operationBean
.getHours()
/ len;
}
hours += h;
break;
}
}
}
}
}
}
}
hoursInfo += scanHumanInfo.getHumanName() + ":"
+ df.format(hours) + "";
// xhy 2009-04-22 修改工时差公式为:工时差=(计划工时-去重叠工时)/(计划工时+去重叠工时)
if ((planDistributeHour + hours) == 0
|| ("0".equals(df.format(hours)) && "0"
.equals(df.format(planDistributeHour)))) {
hourError = 0;
} else {
hourError = (planDistributeHour - hours)
/ (planDistributeHour + hours);
}
hourErrorInfo.append(scanHumanInfo.getHumanName())
.append(":").append(df.format(hourError))
.append("");
}
}
operation.setActuralWorkHour(Float.parseFloat(df
.format(operation.getActuralWorkHour())));
operation.setActualDistributeHourInfo(actualDistributeHourInfo
.toString());
operation.setHourErrorInfo(hourErrorInfo.toString());
operation.setPlanDistributeHourInfo(planDistributeHourInfo
.toString());
operation.setHoursInfo(hoursInfo);
opMonitorList.clear();
}
ArrayList undoOperation = new ArrayList();
ArrayList doingOperation = new ArrayList();
ArrayList doneOperation = new ArrayList();
ArrayList pauseOperation = new ArrayList();
java.sql.Timestamp lastFinishTime // for cal lastFinishTime of
// part
= new java.sql.Timestamp(0);
// * 2.2查询对于未完工模具的计划完成时间,计划加工生产资源
// * 2.3按照层次结构组织数据结构(工件中工序按照状态分类)
// save ops to their parts---
for (int i = 0; i < operations.size(); i++) {
MouldPartOperationBean operation = (MouldPartOperationBean) operations
.get(i);
MouldPartOperationBean operationNext = null;
if (i + 1 != operations.size()) {
operationNext = (MouldPartOperationBean) operations
.get(i + 1);
}
// 添加质检责任工序
if (operation.getReasonOperationID() != 0) {
if (operationsMap.get(new Integer(operation
.getReasonOperationID())) != null) {
operation
.setReasonOperationName(((MouldPartOperationBean) operationsMap
.get(new Integer(operation
.getReasonOperationID())))
.getOperationName());
}
}
if (operation != null
&& resources != null
&& operation.getPlanResourceID() != null
&& !operation.getPlanResourceID().equals("")
&& (EquipBean) resources.get(operation
.getPlanResourceID()) != null) {
operation.setEquip(((EquipBean) resources.get(operation
.getPlanResourceID())).getEquipName());
} else {
operation.setEquip("");
}
if (operation.getPlanFinishTime() != null
&& operation.getPlanFinishTime().getTime() > lastFinishTime
.getTime()) {
lastFinishTime.setTime(operation.getPlanFinishTime()
.getTime()); // part's last
}
String newPartID = operation.getMouldPartID();
if (operation.getState().equals("waiting")) {
undoOperation.add(operation);
} else if (operation.getState().equals("working")) {
doingOperation.add(operation);
} else if (operation.getState().equals("over")) {
doneOperation.add(operation);
} else if (operation.getState().equals("pause")) {
pauseOperation.add(operation);
}
if (operationNext == null
|| !newPartID.equals(operationNext.getMouldPartID())) { // last
// or
// next
// part
// is
// diff
// part
// save a part ,then next part or out
eman.bean.productionView.QueryMouldInfo.MouldPartInfoBean partTemp = ((eman.bean.productionView.QueryMouldInfo.MouldPartInfoBean) partsMap
.get(newPartID));
partTemp.setUndoOperationList(undoOperation);
partTemp.setDoingOperationList(doingOperation);
partTemp.setDoneOperationList(doneOperation);
partTemp.setPauseOperationList(pauseOperation);
partTemp.setLastPlanFinishTime(new Timestamp(lastFinishTime
.getTime()));
// back to init state
if (operationNext != null) {
lastFinishTime = new java.sql.Timestamp(0);
undoOperation = new ArrayList();
doingOperation = new ArrayList();
doneOperation = new ArrayList();
pauseOperation = new ArrayList();
}
}
}
// myh 2007-12-21 将工件信息添加到对应的模具
java.sql.Timestamp lastFinishTimeMould = new java.sql.Timestamp(0);
for (int i = 0; i < parts.size(); i++) {
MouldPartInfoBean part = (MouldPartInfoBean) parts.get(i);
if (part.getLastPlanFinishTime() != null
&& part.getLastPlanFinishTime().getTime() > lastFinishTimeMould
.getTime()) {
lastFinishTimeMould.setTime(part.getLastPlanFinishTime()
.getTime());
}
String newMouldID = part.getMouldID();
if (((MouldBean) mouldsMap.get(newMouldID)).getMouldpartList() == null) {
((MouldBean) mouldsMap.get(newMouldID))
.setMouldpartList(new ArrayList());
}
if (lastFinishTimeMould.getTime() != 0) {
((MouldBean) mouldsMap.get(newMouldID))
.setMouldPanFinishTime(new Timestamp(
lastFinishTimeMould.getTime()));
}
((MouldBean) mouldsMap.get(newMouldID)).getMouldpartList().add(
part);
}
String interiorID = ((SysConfig) sc.getAttribute("sysConfig"))
.getInteriorID();
if (interiorID == null || interiorID.equals("")) {
interiorID = "内部编码";
}
String clientID = ((SysConfig) sc.getAttribute("sysConfig"))
.getClientID();
if (clientID == null || clientID.equals("")) {
clientID = "客户编码";
}
request.setAttribute("pageControl", mouldStateInfo
.get("pageControl"));
request.setAttribute("isByOperationOrderID", "false");
request.setAttribute("doingflag", doingflag);
request.setAttribute("overflag", overflag);
request.setAttribute("result", result);
Collections.sort(moulds, new java.util.Comparator() {
public int compare(Object obj1, Object obj2) {
return ((MouldBean) obj1).getMouldInteriorID().compareTo(
((MouldBean) obj2).getMouldInteriorID());
}
});
request.setAttribute("mouldList", moulds);
request.setAttribute("percent1", percent1);
request.setAttribute("percent2", percent2);
request.setAttribute("interiorID", interiorID);
request.setAttribute("clientID", clientID);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
整个星期六就死在这段代码上了。写这段代码的人,我诅咒你一辈子找不到老婆,我要疯了。