liuwei 2 дней назад
Родитель
Сommit
75ff8008ba

+ 10 - 2
src/main/java/com/jeesite/modules/mes/dao/MesProductQmDao.java

@@ -1,8 +1,11 @@
 package com.jeesite.modules.mes.dao;
 
+import java.util.List;
+
+import org.apache.ibatis.annotations.Param;
+
 import com.jeesite.common.dao.CrudDao;
 import com.jeesite.common.mybatis.annotation.MyBatisDao;
-import com.jeesite.modules.mes.entity.MesProductCcd;
 import com.jeesite.modules.mes.entity.MesProductQm;
 
 /**
@@ -13,4 +16,9 @@ import com.jeesite.modules.mes.entity.MesProductQm;
 @MyBatisDao
 public interface MesProductQmDao extends CrudDao<MesProductQm> {
     MesProductQm findInfo(MesProductQm mesProductQm);
-}
+
+    List<MesProductQm> findLatestList(@Param("q") MesProductQm q, @Param("offset") int offset,
+                                      @Param("limit") int limit);
+
+    long findLatestCount(@Param("q") MesProductQm q);
+}

+ 47 - 0
src/main/java/com/jeesite/modules/mes/service/MesProductQmService.java

@@ -1,11 +1,14 @@
 package com.jeesite.modules.mes.service;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import com.jeesite.common.lang.ObjectUtils;
 import com.jeesite.common.lang.StringUtils;
 import com.jeesite.common.mybatis.mapper.query.QueryType;
 import com.jeesite.modules.mes.entity.*;
+import com.jeesite.modules.mes.resp.BindMaterialResp;
+import com.jeesite.modules.mes.resp.ParamsResp;
 import com.jeesite.modules.mes.util.CommonUitl;
 import com.jeesite.modules.mes.util.CopyUtil;
 //import com.sun.org.apache.xpath.internal.operations.Bool;
@@ -41,6 +44,9 @@ public class MesProductQmService extends CrudService<MesProductQmDao, MesProduct
 	@Autowired
 	private MesLineService mesLineService;
 
+	@Autowired
+	private MesProductHfService mesProductHfService;
+
 	/**
 	 * 获取单条数据
 	 * @param mesProductQm
@@ -80,6 +86,30 @@ public class MesProductQmService extends CrudService<MesProductQmDao, MesProduct
 	}
 
 	/**
+	 * 气密记录列表:每个工件只返回测试时间最新的一条。
+	 * 使用 NOT EXISTS 兼容 MySQL 5.7,避免 only_full_group_by 下 GROUP BY 非聚合字段报错。
+	 */
+	public Page<MesProductQm> findLatestPage(MesProductQm mesProductQm) {
+		Page<MesProductQm> page = mesProductQm.getPage();
+		if (page == null) {
+			page = new Page<>();
+		}
+
+		long count = mesProductQmDao.findLatestCount(mesProductQm);
+		page.setCount(count);
+
+		int pageNo = page.getPageNo() < 1 ? 1 : page.getPageNo();
+		int pageSize = page.getPageSize() < 1 ? 20 : page.getPageSize();
+		int offset = (pageNo - 1) * pageSize;
+		if (count > 0 && offset < count) {
+			page.setList(mesProductQmDao.findLatestList(mesProductQm, offset, pageSize));
+		} else {
+			page.setList(new ArrayList<>());
+		}
+		return page;
+	}
+
+	/**
 	 * 查询列表数据
 	 * @param mesProductQm
 	 * @return
@@ -195,6 +225,23 @@ public class MesProductQmService extends CrudService<MesProductQmDao, MesProduct
 //		}
 	}
 
+	/**
+	 * 气密结果、不良记录和工位结果在同一事务内落库。
+	 * 异常向上抛出,由 Controller 返回明确错误,避免半提交。
+	 */
+	@Transactional(rollbackFor = Exception.class)
+	public void addQmResultAndUpdateStation(MesProductQm mesProductQm, MesProductHf mesProductHf,
+			String sn, String oprno, String content, String userCode, List<ParamsResp> params,
+			String lineSn, List<BindMaterialResp> bindMaterials) {
+		mesProductRecordService.ensureOp206StationRecord(sn, oprno, lineSn, userCode);
+		add(mesProductQm);
+		if (mesProductHf != null) {
+			mesProductHfService.save(mesProductHf);
+		}
+		mesProductRecordService.updateRecordResult(null, sn, oprno, "100000", "", content, userCode,
+				params, lineSn, true, bindMaterials, "0");
+	}
+
 	public String getSj(String oprno,String sdate,String edate){
 		MesProductQm mesProductQm = new MesProductQm();
 		mesProductQm.getSqlMap().getWhere()

+ 98 - 0
src/main/java/com/jeesite/modules/mes/service/MesProductRecordService.java

@@ -885,6 +885,104 @@ public class MesProductRecordService extends CrudService<MesProductRecordDao, Me
 	}
 
 	/**
+	 * 气密结果提交前保障 OP206 主结果记录存在。
+	 * 仅作用于 CZ060 的 OP206,其他产线和其他气密工位保持原有逻辑。
+	 */
+	@Transactional
+	public void ensureOp206StationRecord(String sn, String oprno, String lineSn, String userCode) {
+		if (StringUtils.isBlank(sn) || StringUtils.isBlank(oprno) || !"CZ060".equals(lineSn)
+				|| !"OP206".equals(CommonUitl.formatOprno(oprno))) {
+			return;
+		}
+
+		String normalizedSn = sn.trim();
+		MesProductRecord targetQuery = new MesProductRecord();
+		targetQuery.getSqlMap().getWhere()
+				.and("a.sn", QueryType.EQ, normalizedSn)
+				.and("a.oprno", QueryType.EQ, "OP206")
+				.and("a.craft", QueryType.EQ, "100000")
+				.and("a.line_sn", QueryType.EQ, "CZ060");
+		if (!ListUtils.isEmpty(findList(targetQuery))) {
+			return;
+		}
+
+		MesProductRecord anchorQuery = new MesProductRecord();
+		anchorQuery.getSqlMap().getWhere()
+				.and("a.sn", QueryType.EQ, normalizedSn)
+				.and("a.line_sn", QueryType.EQ, "CZ060")
+				.and("a.tid", QueryType.NE, "");
+		anchorQuery.setOrderBy("a.serial ASC, a.create_date ASC, a.id ASC");
+		List<MesProductRecord> anchors = findList(anchorQuery);
+		if (ListUtils.isEmpty(anchors)) {
+			throw new ServiceException(text("未找到该工件的生产记录,无法补建 OP206 工位记录!"));
+		}
+
+		MesProductRecord anchor = null;
+		MesTemplateItems op206TemplateItem = null;
+		for (MesProductRecord candidate : anchors) {
+			MesTemplateItems sourceTemplateItem = mesTemplateItemsService.get(candidate.getTid());
+			if (sourceTemplateItem == null || StringUtils.isBlank(sourceTemplateItem.getTemplateId())) {
+				continue;
+			}
+
+			MesTemplateItems itemQuery = new MesTemplateItems();
+			itemQuery.setTemplateId(sourceTemplateItem.getTemplateId());
+			itemQuery.setStatus("0");
+			itemQuery.getSqlMap().getWhere()
+					.and("a.oprno", QueryType.EQ, "OP206")
+					.and("a.craft", QueryType.EQ, "100000");
+			MesTemplateItems matchedItem = mesTemplateItemsService.findInfo(itemQuery);
+			if (matchedItem != null) {
+				anchor = candidate;
+				op206TemplateItem = matchedItem;
+				break;
+			}
+		}
+
+		if (anchor == null || op206TemplateItem == null) {
+			throw new ServiceException(text("工件的生产模板未配置 OP206 工位结果,请先执行 OP206 配置脚本!"));
+		}
+
+		MesTemplateItems listQuery = new MesTemplateItems();
+		listQuery.setTemplateId(op206TemplateItem.getTemplateId());
+		listQuery.setStatus("0");
+		List<MesTemplateItems> templateItems = mesTemplateItemsService.findList(listQuery);
+		if (ListUtils.isEmpty(templateItems)) {
+			throw new ServiceException(text("生产模板明细为空,无法补建 OP206 工位记录!"));
+		}
+		int serial = -1;
+		for (int i = 0; i < templateItems.size(); i++) {
+			if (op206TemplateItem.getId().equals(templateItems.get(i).getId())) {
+				serial = i + 1;
+				break;
+			}
+		}
+		if (serial < 0) {
+			throw new ServiceException(text("无法确定 OP206 工位在模板中的排序,请检查生产模板!"));
+		}
+
+		MesProductRecord missingRecord = new MesProductRecord();
+		missingRecord.setSn(normalizedSn);
+		missingRecord.setOprno("OP206");
+		missingRecord.setCraft("100000");
+		missingRecord.setSerial(serial);
+		missingRecord.setCateSn(anchor.getCateSn());
+		missingRecord.setPlanId(anchor.getPlanId());
+		missingRecord.setLineSn("CZ060");
+		missingRecord.setTid(op206TemplateItem.getId());
+		missingRecord.setPid("0");
+		missingRecord.setTreeLevel(0);
+		missingRecord.setCdate(Long.valueOf(CommonUitl.Date2TimeStamp(new Date())));
+		missingRecord.setContent("UDF");
+		missingRecord.setSource("0");
+		if (StringUtils.isNotBlank(userCode)) {
+			missingRecord.setCreateBy(userCode);
+			missingRecord.setUpdateBy(userCode);
+		}
+		save(missingRecord);
+	}
+
+	/**
 	 * 质量上传(总结果) 100000
 	 */
 	@Transactional

+ 2 - 3
src/main/java/com/jeesite/modules/mes/web/MesProductQmController.java

@@ -76,7 +76,7 @@ public class MesProductQmController extends BaseController {
 	@RequiresPermissions("mes:mesProductQm:view")
 	@RequestMapping(value = {"list3", ""})
 	public String list3(MesProductQm mesProductQm, Model model) {
-		mesProductQm.setOprno("OP235");
+		mesProductQm.setOprno("OP206");
 		model.addAttribute("mesProductQm", mesProductQm);
 		return "modules/mes/mesProductQmList";
 	}
@@ -110,8 +110,7 @@ public class MesProductQmController extends BaseController {
 	@ResponseBody
 	public Page<MesProductQm> listData2(MesProductQm mesProductQm, HttpServletRequest request, HttpServletResponse response) {
 		mesProductQm.setPage(new Page<>(request, response));
-		mesProductQm.getSqlMap().put("groupByColumn", "a.component_num");
-		Page<MesProductQm> page = mesProductQmService.findPage(mesProductQm);
+		Page<MesProductQm> page = mesProductQmService.findLatestPage(mesProductQm);
 		return page;
 	}
 

+ 15 - 11
src/main/java/com/jeesite/modules/mes/web/MesProductRecordController.java

@@ -4742,7 +4742,6 @@ public class MesProductRecordController extends BaseController {
 
 	@PostMapping(value = "qmresult")
 	@ResponseBody
-	@Transactional
 	public String qmresult(MesProductQmReq mesProductQmReq,HttpServletRequest request) {
 		mesLogService.apisavexz(request);
 		String ldFlagParam = request.getParameter("ldFlag");
@@ -4814,15 +4813,14 @@ public class MesProductRecordController extends BaseController {
 			mesProductQm.setTitle(title);
 			mesProductQm.setRemark(remark);
 			mesProductQm.setLineSn(lineSn);
-			mesProductQmService.add(mesProductQm);
+			MesProductHf mesProductHf = null;
 			if(result.equals("NG")){
-				MesProductHf mesProductHf = new MesProductHf();
+				mesProductHf = new MesProductHf();
 				mesProductHf.setSn(sn);
 				mesProductHf.setOprno(oprno);
 				mesProductHf.setLineSn(lineSn);
 				mesProductHf.setHfFlag(ldFlag);
 				mesProductHf.setResult(result);
-				mesProductHfService.save(mesProductHf);
 			}
 
 			List<ParamsResp> params = new ArrayList<>();
@@ -4832,10 +4830,15 @@ public class MesProductRecordController extends BaseController {
 			}
 			// 2.判断物料批次是否符合绑定
 			List<BindMaterialResp> bmlists = new ArrayList<>();
-			mesProductRecordService.updateRecordResult(null,sn,oprno,"100000","",content,userCode,params,lineSn,true,bmlists,"0");
+			mesProductQmService.addQmResultAndUpdateStation(mesProductQm, mesProductHf, sn, oprno, content,
+					userCode, params, lineSn, bmlists);
 		}catch (Exception e){
 			e.printStackTrace();
-			return renderResult(Global.FALSE, text("操作失败!"));
+			String msg = e.getMessage();
+			if(StringUtils.isEmpty(msg)){
+				msg = "操作失败!";
+			}
+			return renderResult(Global.FALSE, text(msg));
 		}
 
 		return renderResult(Global.TRUE, text("操作成功!"));
@@ -4902,15 +4905,14 @@ public class MesProductRecordController extends BaseController {
 			mesProductQm.setCsDuration(cs);
 			mesProductQm.setByDuration(by);
 			mesProductQm.setCqDuration(cq);
-			mesProductQmService.add(mesProductQm);
+			MesProductHf mesProductHf = null;
 			if(result.equals("NG")){
-				MesProductHf mesProductHf = new MesProductHf();
+				mesProductHf = new MesProductHf();
 				mesProductHf.setSn(sn);
 				mesProductHf.setOprno(oprno);
 				mesProductHf.setLineSn(lineSn);
 				mesProductHf.setHfFlag(ldFlag);
 				mesProductHf.setResult(result);
-				mesProductHfService.save(mesProductHf);
 			}
 
 			List<ParamsResp> params = new ArrayList<>();
@@ -4920,10 +4922,12 @@ public class MesProductRecordController extends BaseController {
 			}
 			// 2.判断物料批次是否符合绑定
 			List<BindMaterialResp> bmlists = new ArrayList<>();
-			mesProductRecordService.updateRecordResult(null,sn,oprno,"100000","",content,userCode,params,lineSn,true,bmlists,"0");
+			mesProductQmService.addQmResultAndUpdateStation(mesProductQm, mesProductHf, sn, oprno, content,
+					userCode, params, lineSn, bmlists);
 		}catch (Exception e){
 			logger.error("xzqmresult fail sn="+sn+" oprno="+oprno, e);
-			resp.setErrMsg("操作失败");
+			String msg = e.getMessage();
+			resp.setErrMsg(StringUtils.isEmpty(msg) ? "操作失败" : msg);
 			resp.setIsSuccess(false);
 			return resp;
 		}

+ 42 - 0
src/main/resources/mappings/modules/mes/MesProductQmDao.xml

@@ -31,4 +31,46 @@
         ORDER BY ${sqlMap.order.toSql()}
         LIMIT 1
     </select>
+
+    <sql id="latestRecordFilter">
+        <where>
+            ${q.sqlMap.where.toSql()}
+            AND NOT EXISTS (
+                SELECT 1
+                FROM mes_product_qm b
+                WHERE b.component_num = a.component_num
+                  AND b.oprno = a.oprno
+                  AND COALESCE(b.product_cate, '') = COALESCE(a.product_cate, '')
+                  AND (
+                      COALESCE(b.test_time, '1000-01-01 00:00:00')
+                          &gt; COALESCE(a.test_time, '1000-01-01 00:00:00')
+                      OR (
+                          COALESCE(b.test_time, '1000-01-01 00:00:00')
+                              = COALESCE(a.test_time, '1000-01-01 00:00:00')
+                          AND b.create_date &gt; a.create_date
+                      )
+                      OR (
+                          COALESCE(b.test_time, '1000-01-01 00:00:00')
+                              = COALESCE(a.test_time, '1000-01-01 00:00:00')
+                          AND b.create_date = a.create_date
+                          AND b.id &gt; a.id
+                      )
+                  )
+            )
+        </where>
+    </sql>
+
+    <select id="findLatestList" resultType="com.jeesite.modules.mes.entity.MesProductQm">
+        SELECT ${q.sqlMap.column.toSql()}
+        FROM mes_product_qm a
+        <include refid="latestRecordFilter"/>
+        ORDER BY a.test_time DESC, a.create_date DESC, a.id DESC
+        LIMIT #{offset}, #{limit}
+    </select>
+
+    <select id="findLatestCount" resultType="long">
+        SELECT COUNT(*)
+        FROM mes_product_qm a
+        <include refid="latestRecordFilter"/>
+    </select>
 </mapper>