package QCM.JHY01.JHY0101; import java.math.BigDecimal; import java.sql.ResultSet; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.TreeSet; import java.util.UUID; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import CoreFS.SA01.CoreIComponent; import QCM.JHY01.VO.JhySampleDItemModel; import QCM.JHY01.VO.JhySampleDModel; public class DuplicateSample extends CoreIComponent{ SqlSession SqlSession = QCM.COMMUNAL.SqlSessionBuilder.openSqlSession(); QCM.COMMUNAL.OrderSqMapper mapper = SqlSession.getMapper(QCM.COMMUNAL.OrderSqMapper.class); Logger logger = Logger.getLogger(this.getClass().getName()); public synchronized String doDuplicateSample(JSONArray list){ logger.info("复样:==========复样"+list.toString()); String msg = checkInfo(list); if (!"".equals(msg)) { return msg; } try{ createQltySample(list); createChemSample(list); SqlSession.commit(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); SqlSession.rollback(); }finally { SqlSession.close(); } if ("".equals(msg)) { return "1"; }else{ return "生成失败!"; } } private void createChemSample(JSONArray lists) { // TODO Auto-generated method stub StringBuffer sqlucomm = new StringBuffer(); for (Object li : lists) { JSONObject list =(JSONObject) li; //判断是否存在材质 if (!"1".equals(list.getString("IS_CHEM"))) { continue; } sqlucomm.setLength(0); sqlucomm.append("t.smp_no,t.specimen_no,t.heat_no,t.batch_no,t.inspection_lot,t.freq_code,t.freq_name,"); sqlucomm.append("t.material_no,t.smp_type_code,t.smp_type_name,t.consign_no,t.consign_no_seq,t.test_qty,"); sqlucomm.append("t.smp_qty,t.smp_location,t.board_no,t.quote_consign_no,t.old_consign_no,t.pline_code,"); sqlucomm.append("t.pline_name,t.status,t.validflag,t.source,t.guid,t.send_id,t.send_name,t.send_time,"); sqlucomm.append(" t.chem_item,t.product_cnt,t.create_time,t.smp_location_code from QCM_JHY_SAMPLE_CONSIGN_D t inner join " + "QCM_JUDGE_CHEMICAL c on t.smp_no=c.smp_no and t.smp_no='"+list.getString("QLTY_SMP_NO")+"' and c.cic_result_code='2'"); List results=mapper.query(sqlucomm.toString()); if (results==null || results.size()<1) { continue; } String specimen_no = ""; for (HashMap result : results) { for (int i = 0; i < 2; i++) { //生成新的复样的取样编号 specimen_no = createSpecimen_no((String)result.get("SMP_NO")); JhySampleDModel model=getSampleD(specimen_no,result); mapper.insertSampleD(model); } } } } private JhySampleDModel getSampleD(String specimen_no, HashMap result) { // TODO Auto-generated method stub JhySampleDModel model=new JhySampleDModel(); model.setSmp_no((String)result.get("SMP_NO")); model.setSpecimen_no(specimen_no); model.setHeat_no((String)result.get("HEAT_NO")); model.setBatch_no((String)result.get("BATCH_NO")); model.setBoard_no((String)result.get("BOARD_NO")); model.setInspection_lot((String)result.get("INSPECTION_LOT")); model.setMaterial_no((String)result.get("MATERIAL_NO")); model.setFreq_code((String)result.get("FREQ_CODE")); model.setFreq_name((String)result.get("FREQ_NAME")); //试验次数 model.setTest_qty((BigDecimal)result.get("TEST_QTY")); model.setSmp_qty((BigDecimal)result.get("SMP_QTY"));; //样品类型 model.setSmp_type_code("1"); model.setSmp_type_name("复样"); model.setSmp_location((String)result.get("SMP_LOCATION")); //初样取样编号(复样不能为空) model.setOld_consign_no((String)result.get("SPECIMEN_NO")); model.setPline_code((String)result.get("PLINE_CODE")); model.setPline_name((String)result.get("PLINE_NAME")); model.setValidflag("1"); model.setStatus("0"); model.setSource("0"); model.setProduct_cnt((BigDecimal)result.get("PRODUCT_CNT")); model.setChem_item((String)result.get("CHEM_ITEM")); model.setGuid(this.getUUID()); return model; } private void createQltySample(JSONArray lists) throws Exception { // TODO Auto-generated method stub StringBuffer sqlucomm = new StringBuffer(); for (Object li : lists) { JSONObject list =(JSONObject) li; //判断是否存在材质 if (!"1".equals(list.getString("IS_QTLY"))) { continue; } sqlucomm.setLength(0); //查询试样号向下的检验结果 sqlucomm.append("select r.*,d.FREQ_CODE,d.freq_name,d.test_qty,d.smp_qty,d.board_no,d.smp_location,d.pline_code,d.pline_name,d.product_cnt,d.smp_location_code " + " from QCM_JUDGE_PHYSICAL_RESULT r left join QCM_JHY_SAMPLE_CONSIGN_D d on r.smp_no=d.smp_no and r.specimen_no=d.specimen_no " + " where r.smp_no='"+list.getString("QLTY_SMP_NO")+"' and r.IS_PASS='1'"); List results=mapper.query(sqlucomm.toString()); if (results==null || results.size()<1) { continue; } for (HashMap result : results) { //查询试样号对应的合同信息 sqlucomm.setLength(0); sqlucomm.append("SELECT DESIGN_KEY,PSC,SMP_TYPE_CODE,CERT_INST_CODE FROM QCM_JHY_SAMPLE_R_ORD WHERE SMP_NO='"+result.get("SMP_NO")+"'"); List resultsM=mapper.query(sqlucomm.toString()); if (resultsM==null || resultsM.size()<1) { continue; } //根据合同信息和项目找到复样倍数信息 HashMap sampleM=resultsM.get(0); if ("HA".equals(((String)result.get("PHY_CODE_L")))) { sqlucomm.setLength(0); sqlucomm.append("SELECT * FROM QCM_ORD_DESIGN_SAMPLE_M WHERE DESIGN_KEY='"+sampleM.get("DESIGN_KEY")+"'" + " and PSC='"+sampleM.get("PSC")+"' and SAMPLE_STYLE_CODE='"+sampleM.get("SMP_TYPE_CODE")+"' and ORG_CODE='"+sampleM.get("CERT_INST_CODE")+"' " + " and RATE_CODE='"+result.get("FREQ_CODE")+"' and PHY_CODE_L='HA' "); // sqlucomm.append("SELECT * FROM QCM_ORD_DESIGN_SAMPLE_M WHERE DESIGN_KEY='"+sampleM.get("DESIGN_KEY")+"'" // + " and PSC='"+sampleM.get("PSC")+"' and SAMPLE_STYLE_CODE='"+sampleM.get("SMP_TYPE_CODE")+"' and ORG_CODE='"+sampleM.get("CERT_INST_CODE")+"' " // + "and PHY_CODE_L='"+result.get("PHY_CODE_L")+"' and RATE_CODE='"+result.get("FREQ_CODE")+"' and PHY_CODE_L='HA' " // + "and SMP_POSITION_CODE='"+result.get("SMP_POSITION_CODE")+"'"); }else if("HJ".equals(((String)result.get("PHY_CODE_L")))){ sqlucomm.setLength(0); sqlucomm.append("SELECT * FROM QCM_ORD_DESIGN_SAMPLE_M WHERE DESIGN_KEY='"+sampleM.get("DESIGN_KEY")+"'" + " and PSC='"+sampleM.get("PSC")+"' and SAMPLE_STYLE_CODE='"+sampleM.get("SMP_TYPE_CODE")+"' and ORG_CODE='"+sampleM.get("CERT_INST_CODE")+"' " + " and RATE_CODE='"+result.get("FREQ_CODE")+"' and PHY_CODE_L='HJ' "); }else{ sqlucomm.setLength(0); sqlucomm.append("SELECT * FROM QCM_ORD_DESIGN_SAMPLE_M WHERE DESIGN_KEY='"+sampleM.get("DESIGN_KEY")+"'" + " and PSC='"+sampleM.get("PSC")+"' and SAMPLE_STYLE_CODE='"+sampleM.get("SMP_TYPE_CODE")+"' and ORG_CODE='"+sampleM.get("CERT_INST_CODE")+"' " + "and PHY_CODE_L='"+result.get("PHY_CODE_L")+"' and PHY_CODE_M='"+result.get("PHY_CODE_M")+"' " + " and PHY_CODE_S='"+result.get("PHY_CODE_S")+"'"); // sqlucomm.append("SELECT * FROM QCM_ORD_DESIGN_SAMPLE_M WHERE DESIGN_KEY='"+sampleM.get("DESIGN_KEY")+"'" // + " and PSC='"+sampleM.get("PSC")+"' and SAMPLE_STYLE_CODE='"+sampleM.get("SMP_TYPE_CODE")+"' and ORG_CODE='"+sampleM.get("CERT_INST_CODE")+"' " // + "and PHY_CODE_L='"+result.get("PHY_CODE_L")+"' and PHY_CODE_M='"+result.get("PHY_CODE_M")+"' and SMP_POSITION_CODE='"+result.get("SMP_POSITION_CODE")+"'" // + " and PHY_CODE_S='"+result.get("PHY_CODE_S")+"'"); if (result.get("ITEM_CODE_D")!=null) { sqlucomm.append(" and ITEM_CODE_D='"+result.get("ITEM_CODE_D")+"'"); } if (result.get("ITEM_CODE_T")!=null) { sqlucomm.append( " and ITEM_CODE_T='"+result.get("ITEM_CODE_T")+"'"); } if (result.get("ITEM_CODE_S")!=null) { sqlucomm.append(" and ITEM_CODE_S='"+result.get("ITEM_CODE_S")+"'"); } } List qtlyResource=mapper.query(sqlucomm.toString()); int time=1; if (qtlyResource==null || qtlyResource.size()<1){ continue; } for (int j = 0; j < qtlyResource.size(); j++) { HashMap resource=qtlyResource.get(j); if (null!=resource.get("RETEST_QTY")) { time=((BigDecimal)resource.get("RETEST_QTY")).intValue(); } //查询当前频率是否存在已经存在的取样要求信息 sqlucomm.setLength(0); sqlucomm.append("SELECT SPECIMEN_NO FROM QCM_JHY_SAMPLE_CONSIGN_D WHERE SMP_NO='"+result.get("SMP_NO")+"' " +" AND SMP_TYPE_CODE='1' AND FREQ_CODE='"+resource.get("RATE_CODE")+"' and STATUS='0' "); // sqlucomm.append("SELECT SPECIMEN_NO FROM QCM_JHY_SAMPLE_CONSIGN_D WHERE SMP_NO='"+result.get("SMP_NO")+"' " // +" AND SMP_TYPE_CODE='1' AND FREQ_CODE='"+result.get("FREQ_CODE")+"' and STATUS='0' and SMP_LOCATION_CODE='"+result.get("SMP_LOCATION_CODE")+"'"); List resultsD=mapper.query(sqlucomm.toString()); String specimen_no = ""; if (resultsD==null || resultsD.size()<1) { //生成新的复样的取样编号 specimen_no = createSpecimen_no((String)result.get("SMP_NO")); JhySampleDModel model=getSampleD(specimen_no,result); mapper.insertSampleD(model); //生成复样的项目信息 for (int i = 0; i < time; i++) { JhySampleDItemModel item=getSampleItem((String)result.get("SMP_NO"),specimen_no,resource); mapper.insertSampleItem(item); } continue; } //判断是否已经存在该项目,避免复样在取复样情况 specimen_no=(String)resultsD.get(0).get("SPECIMEN_NO"); sqlucomm.setLength(0); sqlucomm.append("SELECT * FROM QCM_JHY_SAMPLE_CONSIGN_D_ITEM WHERE SPECIMEN_NO='"+specimen_no+"'" + " and SMP_NO='"+result.get("SMP_NO")+"' " + "and PHY_CODE_L='"+resource.get("PHY_CODE_L")+"' and PHY_CODE_M='"+resource.get("PHY_CODE_M")+"'" + " and PHY_CODE_S='"+resource.get("PHY_CODE_S")+"'"); if (result.get("ITEM_CODE_D")!=null) { sqlucomm.append(" and ITEM_CODE_D='"+resource.get("ITEM_CODE_D")+"'"); } if (result.get("ITEM_CODE_T")!=null) { sqlucomm.append( " and ITEM_CODE_T='"+resource.get("ITEM_CODE_T")+"'"); } if (result.get("ITEM_CODE_S")!=null) { sqlucomm.append(" and ITEM_CODE_S='"+resource.get("ITEM_CODE_S")+"'"); } List resultsI=mapper.query(sqlucomm.toString()); if (resultsI==null || resultsI.size()<1) { for (int i = 0; i < time; i++) { JhySampleDItemModel item=getSampleItem((String)result.get("SMP_NO"),specimen_no,resource); mapper.insertSampleItem(item); } } } } //更新合同表改为待判 sqlucomm.setLength(0); sqlucomm.append("update QCM_JHY_SAMPLE_R_ORD t set t.judge_status='0',t.judge_result_code=null,t.judge_result_desc=null,"); sqlucomm.append("t.judge_time=null,t.cic_id=null,t.phy_id=null,t.judge_name=null,t.judge_memo=null where t.smp_no='"+list.getString("QLTY_SMP_NO")+"'"); mapper.updateJudgeStatus(sqlucomm.toString()); } } private JhySampleDItemModel getSampleItem(String smp_no, String specimen_no, HashMap resource) throws Exception { // TODO Auto-generated method stub JhySampleDItemModel model=new JhySampleDItemModel(); model.setSmp_no(smp_no); model.setSpecimen_no(specimen_no); model.setPhy_code_l((String)resource.get("PHY_CODE_L"));; model.setPhy_code_m((String)resource.get("PHY_CODE_M")); model.setPhy_code_s((String)resource.get("PHY_CODE_S")); model.setPhy_name_l((String)resource.get("PHY_NAME_L")); model.setPhy_name_m((String)resource.get("PHY_NAME_M")); model.setPhy_name_s((String)resource.get("PHY_NAME_S")); model.setItem_code_d((String)resource.get("ITEM_CODE_D")); model.setItem_code_t((String)resource.get("ITEM_CODE_T")); model.setItem_code_s((String)resource.get("ITEM_CODE_S")); model.setItem_code_l((String)resource.get("ITEM_CODE_L")); model.setItem_name_d((String)resource.get("ITEM_NAME_D")); model.setItem_name_t((String)resource.get("ITEM_NAME_T")); model.setItem_name_s((String)resource.get("ITEM_NAME_S")); model.setItem_desc_l((String)resource.get("ITEM_DESC_L")); model.setIsjudge((String)resource.get("ISJUDGE")); model.setPhy_unit((String)resource.get("PHY_UNIT")); model.setGroup_seq((String)resource.get("GROUP_SEQ")); model.setSpecl_fl((String)resource.get("SPECL_FL")); //试验次数 model.setTest_qty((BigDecimal)resource.get("SMP_COUNT")); model.setSmp_qty((BigDecimal)resource.get("SMP_QTY")); model.setCreate_id((String)resource.get("CREATE_ID")); model.setCreate_name((String)resource.get("CREATE_NAME")); model.setCreate_time(new Date()); model.setSeq(createItemSeq(smp_no,specimen_no)); //样品类型 return model; } /** * 创建项目seq * @param specimen_no * @param smp_no * @param cn * @return * @throws Exception */ private Integer createItemSeq(String smp_no, String specimen_no) throws Exception { // TODO Auto-generated method stub StringBuffer sqlucomm = new StringBuffer(); sqlucomm.append("SELECT max(SEQ) SEQ FROM QCM_JHY_SAMPLE_CONSIGN_D_ITEM WHERE SMP_NO ='"+smp_no+"' and SPECIMEN_NO='"+specimen_no+"'"); HashMap result=mapper.queryOne(sqlucomm.toString()); Integer SEQ=0; if (result!=null && result.get("SEQ")!=null) { SEQ=((BigDecimal)result.get("SEQ")).intValue(); } SEQ++; return SEQ; } /** * 生成不带 "-" 的UUID * * @return */ private String getUUID() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); String uuidStr = str.replace("-", ""); return uuidStr; } /** * 获取取样编号 * @param smp_no * @return */ private String createSpecimen_no(String smp_no) { // TODO Auto-generated method stub String specimen_no=""; StringBuffer sqlucomm = new StringBuffer(); sqlucomm.append("SELECT max(to_number(substr(SPECIMEN_NO,13))) SPECIMEN_NO FROM QCM_JHY_SAMPLE_CONSIGN_D " + "WHERE SPECIMEN_NO like '"+smp_no+"%' and SMP_NO='"+smp_no+"'"); HashMap result=mapper.queryOne(sqlucomm.toString()); BigDecimal seq=BigDecimal.ZERO; if (result!=null && result.get("SPECIMEN_NO") !=null) { seq=(BigDecimal)result.get("SPECIMEN_NO"); } seq=seq.add(BigDecimal.ONE); specimen_no=smp_no+ String.format("%02d", seq.intValue()); return specimen_no; } private String checkInfo(JSONArray lists) { // TODO Auto-generated method stub String msg = ""; if (lists == null || lists.size() < 1) { return "未寻到或无可生成检验委托的数据"; } String inspection_lot = ""; TreeSet treeSet = new TreeSet(); ResultSet rs = null; for (Object list : lists) { JSONObject map = (JSONObject) list; // 检验号 msg = checkField("INSPECTION_LOT", "检验号", map); if (!"".equals(msg)) { return msg; } // 获取共同检验号 inspection_lot = map.getString("INSPECTION_LOT"); treeSet.add(inspection_lot); // DESIGN_KEY msg = checkField("DESIGN_KEY", "DESIGN_KEY", map); if (!"".equals(msg)) { return msg; } // 炉号 msg = checkField("HEAT_NO", "炉号", map); if (!"".equals(msg)) { return msg; } // 轧批号 msg = checkField("BATCH_NO", "轧批号", map); if (!"".equals(msg)) { return msg; } // 全程产线号 msg = checkField("MSC_PLINE", "全程产线号", map); if (!"".equals(msg)) { return msg; } // 全程产线代码 msg = checkField("PLINE_CODE", "产线代码", map); if (!"".equals(msg)) { return msg; } // 全程产线名称 msg = checkField("PLINE_NAME", "产线名称", map); if (!"".equals(msg)) { return msg; } // //交货状态 // msg=checkField("DELIVERY_STATE_CODE","交货状态代码",map); // if (!"".equals(msg)) { // return msg; // } // //交货状态 // msg=checkField("DELIVERY_STATE_DESC","交货状态",map); // if (!"".equals(msg)) { // return msg; // } // 工序代码 // msg=checkField("PROCESS_CODE","工序代码",map); // if (!"".equals(msg)) { // return msg; // } // 厚款长 msg = checkField("THICK", "厚度", map); if (!"".equals(msg)) { return msg; } /* * msg=checkField("WIDTH","宽度",map); if (!"".equals(msg)) { return * msg; } msg=checkField("LENGTH","长度",map); if (!"".equals(msg)) { * return msg; } */ msg = checkField("PSC", "产品码", map); if (!"".equals(msg)) { return msg; } msg = checkField("QLTY_SMP_NO", "原试样号", map); if (!"".equals(msg)) { return msg; } } if (treeSet.size() != 1) { return "检验号不一致"; } return msg; } /** * 验证字段 * * @param field * 字段 * @param name * 字段名 * @param map * map对象 * @return */ private String checkField(String field, String name, JSONObject map) { if (map.get(field) == null) { return name + "不存在"; } String ran = (String) map.get(field); if (ran == null || "".equals(ran.trim())) { return name + "为空"; } return ""; } }