package com.jeesite.modules.mes.web; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; import com.jeesite.common.config.Global; import com.jeesite.common.collect.ListUtils; import com.jeesite.common.entity.Page; import com.jeesite.common.lang.DateUtils; import com.jeesite.common.lang.StringUtils; import com.jeesite.common.mybatis.mapper.query.QueryType; import com.jeesite.common.utils.excel.ExcelExport; import com.jeesite.common.utils.excel.annotation.ExcelField.Type; import org.springframework.web.multipart.MultipartFile; import com.jeesite.common.web.BaseController; import com.jeesite.modules.erp.entity.MesProductOrder; import com.jeesite.modules.erp.service.MesProductOrderService; import com.jeesite.modules.mes.entity.MesMaterialBl; import com.jeesite.modules.mes.entity.MesOrderJjll; import com.jeesite.modules.mes.entity.MesProduct; import com.jeesite.modules.mes.service.MesMaterialBlService; import com.jeesite.modules.mes.service.MesOrderJjllService; import com.jeesite.modules.mes.service.MesProductService; import org.springframework.util.ObjectUtils; /** * 机加订单领料工件绑定Controller * @author sxl * @version 2026-10-26 */ @Controller @RequestMapping(value = "${adminPath}/mes/mesOrderJjll") public class MesOrderJjllController extends BaseController { @Autowired private MesOrderJjllService mesOrderJjllService; @Autowired private MesProductOrderService mesProductOrderService; @Autowired private MesMaterialBlService mesMaterialBlService; @Autowired private MesProductService mesProductService; /** * 获取数据 */ @ModelAttribute public MesOrderJjll get(String id, boolean isNewRecord) { return mesOrderJjllService.get(id, isNewRecord); } /** * 机加订单领料显示列表: * mes_product_order 中 lineSn 以 B0106 开头; * 返回订单编号、备料表 erpSn 以 B0110 开头的确认数量合计、MesOrderJjll 绑定记录数; * 支持 pageNo、pageSize 分页,并在返回 data 中带回。 */ @RequestMapping(value = "orderDisplayList") @ResponseBody public String orderDisplayList(HttpServletRequest request, HttpServletResponse response) { MesProductOrder orderQuery = new MesProductOrder(); orderQuery.getSqlMap().getWhere().and("a.line_sn", QueryType.RIGHT_LIKE, "B0106"); Page page = new Page<>(request, response); orderQuery.setPage(page); page = mesProductOrderService.findPage(orderQuery); List> list = new ArrayList<>(); if (page.getList() != null) { for (MesProductOrder order : page.getList()) { if (order == null || StringUtils.isBlank(order.getSn())) { continue; } BigDecimal numsQr = sumBlNumsQrDecimal(order.getSn(), "B0110"); MesOrderJjll jjllQuery = new MesOrderJjll(); jjllQuery.setOrderJj(order.getSn()); long jjllCount = mesOrderJjllService.findCount(jjllQuery); // 确认数量与已绑定数量相同则不返回 if (BigDecimal.valueOf(jjllCount).compareTo(numsQr) == 0) { continue; } Map item = new HashMap<>(); item.put("orderId", order.getSn()); item.put("numsQr", numsQr.stripTrailingZeros().toPlainString()); item.put("jjllCount", jjllCount); list.add(item); } } JSONObject resp = new JSONObject(); resp.put("result", Global.TRUE); resp.put("message", text("查询成功!")); resp.put("data", list); resp.put("pageNo", page.getPageNo()); resp.put("pageSize", page.getPageSize()); resp.put("count", page.getCount()); return resp.toJSONString(); } /** * 机加订单领料绑定工件码(同一工件码只能绑定一次;已绑定数不能大于备料确认数量) * 参数:orderJj=机加订单号,sn=工件码 */ @RequestMapping(value = "bandJjllSn") @ResponseBody public String bandJjllSn(String orderJj, String sn) { if (StringUtils.isBlank(orderJj)) { return renderResult(Global.FALSE, text("机加订单号不能为空!")); } if (StringUtils.isBlank(sn)) { return renderResult(Global.FALSE, text("工件码不能为空!")); } orderJj = orderJj.trim(); sn = sn.trim(); MesProduct mesProductQuery = new MesProduct(); mesProductQuery.setSn(sn); List productList = mesProductService.findList(mesProductQuery); if (productList == null || productList.isEmpty() || ObjectUtils.isEmpty(productList.get(0))) { return renderResult(Global.FALSE, text("工件不存在!")); } MesOrderJjll existQuery = new MesOrderJjll(); existQuery.setSn(sn); if (mesOrderJjllService.findCount(existQuery) > 0) { return renderResult(Global.FALSE, text("该工件码已绑定机加订单,不允许重复绑定!")); } MesProductOrder orderQuery = new MesProductOrder(); orderQuery.setSn(orderJj); List orderList = mesProductOrderService.findList(orderQuery); if (orderList == null || orderList.isEmpty() || ObjectUtils.isEmpty(orderList.get(0))) { return renderResult(Global.FALSE, text("机加订单不存在!")); } BigDecimal numsQrTotal = sumBlNumsQrDecimal(orderJj, "B0110"); if (numsQrTotal.compareTo(BigDecimal.ZERO) <= 0) { return renderResult(Global.FALSE, text("备料确认数量未配置!")); } MesOrderJjll boundQuery = new MesOrderJjll(); boundQuery.setOrderJj(orderJj); long boundCount = mesOrderJjllService.findCount(boundQuery); if (BigDecimal.valueOf(boundCount).compareTo(numsQrTotal) >= 0) { return renderResult(Global.FALSE, text("该订单已绑定数量已达备料确认数量,不允许再绑定!")); } MesOrderJjll mesOrderJjll = new MesOrderJjll(); mesOrderJjll.setOrderJj(orderJj); mesOrderJjll.setSn(sn); mesOrderJjllService.save(mesOrderJjll); return renderResult(Global.TRUE, text("绑定成功!")); } /** 按订单号汇总备料表中 erpSn 以前缀开头的确认数量 */ private String sumBlNumsQrByOrderAndErpPrefix(String orderId, String erpPrefix) { return sumBlNumsQrDecimal(orderId, erpPrefix).stripTrailingZeros().toPlainString(); } private BigDecimal sumBlNumsQrDecimal(String orderId, String erpPrefix) { MesMaterialBl blQuery = new MesMaterialBl(); blQuery.setOrderId(orderId); blQuery.getSqlMap().getWhere().and("a.erp_sn", QueryType.RIGHT_LIKE, erpPrefix); List blList = mesMaterialBlService.findList(blQuery); BigDecimal total = BigDecimal.ZERO; if (blList != null) { for (MesMaterialBl bl : blList) { if (bl == null || StringUtils.isBlank(bl.getNumsQr())) { continue; } try { total = total.add(new BigDecimal(bl.getNumsQr().trim())); } catch (Exception ignore) { // 非数字确认数量忽略 } } } return total; } /** * 查询列表 */ @RequiresPermissions("mes:mesOrderJjll:view") @RequestMapping(value = {"list", ""}) public String list(MesOrderJjll mesOrderJjll, Model model) { model.addAttribute("mesOrderJjll", mesOrderJjll); return "modules/mes/mesOrderJjllList"; } /** * 查询列表数据 */ @RequiresPermissions("mes:mesOrderJjll:view") @RequestMapping(value = "listData") @ResponseBody public Page listData(MesOrderJjll mesOrderJjll, HttpServletRequest request, HttpServletResponse response) { mesOrderJjll.setPage(new Page<>(request, response)); Page page = mesOrderJjllService.findPage(mesOrderJjll); return page; } /** * 查看编辑表单 */ @RequiresPermissions("mes:mesOrderJjll:view") @RequestMapping(value = "form") public String form(MesOrderJjll mesOrderJjll, Model model) { model.addAttribute("mesOrderJjll", mesOrderJjll); return "modules/mes/mesOrderJjllForm"; } /** * 保存数据 */ @RequiresPermissions("mes:mesOrderJjll:edit") @PostMapping(value = "save") @ResponseBody public String save(@Validated MesOrderJjll mesOrderJjll) { mesOrderJjllService.save(mesOrderJjll); return renderResult(Global.TRUE, text("保存机加订单领料工件绑定成功!")); } /** * 导出数据 */ @RequiresPermissions("mes:mesOrderJjll:view") @RequestMapping(value = "exportData") public void exportData(MesOrderJjll mesOrderJjll, HttpServletResponse response) { List list = mesOrderJjllService.findList(mesOrderJjll); String fileName = "机加订单领料工件绑定" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx"; try(ExcelExport ee = new ExcelExport("机加订单领料工件绑定", MesOrderJjll.class)){ ee.setDataList(list).write(response, fileName); } } /** * 下载模板 */ @RequiresPermissions("mes:mesOrderJjll:view") @RequestMapping(value = "importTemplate") public void importTemplate(HttpServletResponse response) { MesOrderJjll mesOrderJjll = new MesOrderJjll(); List list = ListUtils.newArrayList(mesOrderJjll); String fileName = "机加订单领料工件绑定模板.xlsx"; try(ExcelExport ee = new ExcelExport("机加订单领料工件绑定", MesOrderJjll.class, Type.IMPORT)){ ee.setDataList(list).write(response, fileName); } } /** * 导入数据 */ @ResponseBody @RequiresPermissions("mes:mesOrderJjll:edit") @PostMapping(value = "importData") public String importData(MultipartFile file) { try { String message = mesOrderJjllService.importData(file); return renderResult(Global.TRUE, "posfull:"+message); } catch (Exception ex) { return renderResult(Global.FALSE, "posfull:"+ex.getMessage()); } } /** * 删除数据 */ @RequiresPermissions("mes:mesOrderJjll:edit") @RequestMapping(value = "delete") @ResponseBody public String delete(MesOrderJjll mesOrderJjll) { mesOrderJjllService.delete(mesOrderJjll); return renderResult(Global.TRUE, text("删除机加订单领料工件绑定成功!")); } }