|
@@ -0,0 +1,308 @@
|
|
|
+package restcontroller.webmanage.sale.bom;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import common.Controller;
|
|
|
+import common.YosException;
|
|
|
+import common.annotation.API;
|
|
|
+import common.annotation.CACHEING;
|
|
|
+import common.annotation.CACHEING_CLEAN;
|
|
|
+import common.data.*;
|
|
|
+import restcontroller.R;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+@API(title = "bom结构")
|
|
|
+public class bom extends Controller {
|
|
|
+
|
|
|
+ public bom(JSONObject content) throws YosException {
|
|
|
+ super(content);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增修改bom
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @API(title = "bom新增修改", apiversion = R.ID2025071010100903.v1.class)
|
|
|
+ @CACHEING_CLEAN(apiClass = {bom.class})
|
|
|
+ public String insertormodify_bom() throws YosException {
|
|
|
+ long plm_bomid = content.getLong("plm_bomid");
|
|
|
+ long parentid = content.getLong("parentid");
|
|
|
+ String bomname = content.getString("bomname");
|
|
|
+ String num = content.getStringValue("num");
|
|
|
+ ArrayList<String> sqllist = new ArrayList<>();
|
|
|
+ if (plm_bomid <= 0 || dbConnect
|
|
|
+ .runSqlQuery("select plm_bomid from plm_bom where plm_bomid=" + plm_bomid).isEmpty()) {
|
|
|
+
|
|
|
+ Rows rows = dbConnect.runSqlQuery("SELECT COUNT(*) count FROM plm_bom WHERE bomname = '" + bomname + "' and siteid='" + siteid + "'");
|
|
|
+ if (!rows.isEmpty() && rows.get(0).getLong("count") > 0) {
|
|
|
+ return getErrReturnObject().setErrMsg("bom名称已存在").toString();
|
|
|
+ }
|
|
|
+ InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "plm_bom");
|
|
|
+ plm_bomid = createTableID("plm_bom");
|
|
|
+ insertSQL.setUniqueid(plm_bomid);
|
|
|
+ insertSQL.setSiteid(siteid);
|
|
|
+ insertSQL.setValue("bomname", bomname);
|
|
|
+ insertSQL.setValue("parentid", parentid);
|
|
|
+ insertSQL.setValue("bomfullname",
|
|
|
+ getUppebomfullname(bomname, parentid, plm_bomid));
|
|
|
+ insertSQL.setValue("num", num);
|
|
|
+ insertSQL.setValue("isdeep", 1);
|
|
|
+ insertSQL.setValue("level", 1);
|
|
|
+ if (parentid > 0) {
|
|
|
+ sqllist = getbomlevel(1, parentid, plm_bomid, sqllist);
|
|
|
+ sqllist.add("update plm_bom set isdeep=0 where plm_bomid=" + parentid);
|
|
|
+ }
|
|
|
+ insertSQL.setValue("createby", username);
|
|
|
+ insertSQL.setDateValue("createdate");
|
|
|
+ sqllist.add(insertSQL.getSQL());
|
|
|
+ } else {
|
|
|
+ Rows rows = dbConnect.runSqlQuery("SELECT COUNT(*) count FROM plm_bom WHERE plm_bomid!=" + content.getLong("plm_bomid") + " and bomname = '" + bomname
|
|
|
+ + "' and siteid='" + siteid + "'");
|
|
|
+ Rows bomnameOldRows = dbConnect.runSqlQuery(
|
|
|
+ "SELECT bomname,bomfullname FROM plm_bom WHERE plm_bomid=" + content.getLong("plm_bomid") + " and siteid='"
|
|
|
+ + siteid + "'");
|
|
|
+ if (!rows.isEmpty() && rows.get(0).getLong("count") > 0) {
|
|
|
+ return getErrReturnObject().setErrMsg("bom已存在").toString();
|
|
|
+ }
|
|
|
+// if (!rowsname.isEmpty() && rowsname.get(0).getLong("count") > 0) {
|
|
|
+// return getErrReturnObject().setErrMsg("bom已存在").toString();
|
|
|
+// }
|
|
|
+ String bomnameOld = "";
|
|
|
+ if (!bomnameOldRows.isEmpty()) {
|
|
|
+ bomnameOld = bomnameOldRows.get(0).getString("bomname");
|
|
|
+ }
|
|
|
+ UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "plm_bom");
|
|
|
+ updateSQL.setUniqueid(plm_bomid);
|
|
|
+ updateSQL.setSiteid(siteid);
|
|
|
+ updateSQL.setValue("bomname", bomname);
|
|
|
+ updateSQL.setValue("changeby", username);
|
|
|
+ updateSQL.setDateValue("changedate");
|
|
|
+ if (!bomnameOldRows.get(0).getString("bomfullname").contains("/")) {
|
|
|
+ updateSQL.setValue("bomfullname", bomname);
|
|
|
+ } else {
|
|
|
+ updateSQL.setValue("bomfullname",
|
|
|
+ bomnameOldRows.get(0).getString("bomfullname").substring(0,
|
|
|
+ bomnameOldRows.get(0).getString("bomfullname").lastIndexOf("/")) + "/"
|
|
|
+ + bomname);
|
|
|
+ }
|
|
|
+ updateSQL.setValue("num", num);
|
|
|
+ if (!bomnameOld.equals("")) {
|
|
|
+ sqllist.addAll(getLowerbomUpdateSql(plm_bomid, bomnameOld, bomname));
|
|
|
+ }
|
|
|
+ sqllist.add(updateSQL.getSQL());
|
|
|
+ }
|
|
|
+ dbConnect.runSqlUpdate(sqllist);
|
|
|
+
|
|
|
+ return getSucReturnObject().toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @API(title = "bomNum新增修改", apiversion = R.ID2025071010102203.v1.class)
|
|
|
+ @CACHEING_CLEAN(apiClass = {bom.class})
|
|
|
+ public String modify_bomNum() throws YosException {
|
|
|
+ JSONArray bominfos = content.getJSONArray("bominfos");
|
|
|
+ ArrayList<String> sqlList = new ArrayList<>();
|
|
|
+ for (Object obj : bominfos) {
|
|
|
+ JSONObject bominfo = (JSONObject) obj;
|
|
|
+ sqlList.add("update plm_bom set num='"+bominfo.getString("num")+"' where plm_bomid="+bominfo.getLong("plm_bomid"));
|
|
|
+ }
|
|
|
+ dbConnect.runSqlUpdate(sqlList);
|
|
|
+ return getSucReturnObject().toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询bom
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @API(title = "查询", apiversion = R.ID2025071010103303.v1.class)
|
|
|
+ @CACHEING
|
|
|
+ public String querybom() throws YosException {
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 查询所有分类
|
|
|
+ */
|
|
|
+ SQLFactory sqlFactory = new SQLFactory(this, "查询所有分类");
|
|
|
+ sqlFactory.addParameter("siteid", siteid);
|
|
|
+
|
|
|
+
|
|
|
+ String sql = sqlFactory.getSQL();
|
|
|
+ Rows allrows = dbConnect.runSqlQuery(sql);
|
|
|
+ /*
|
|
|
+ * 获取所有一级分类
|
|
|
+ */
|
|
|
+ Rows rows2 =new Rows();
|
|
|
+ Row row =new Row();
|
|
|
+ Rows toprows = allrows.toRowsMap("parentid").get("0");
|
|
|
+ Collections.sort(toprows, new SortByNum());
|
|
|
+ /*
|
|
|
+ * 递归查询
|
|
|
+ */
|
|
|
+ for (Row row1 : toprows) {
|
|
|
+ Rows rows1 = getSubbom(row1, allrows);
|
|
|
+ //Collections.sort(rows1, new SortByNum());
|
|
|
+ row1.put("subdep", rows1);
|
|
|
+ }
|
|
|
+ row.put("bom", toprows);
|
|
|
+ rows2.add(row);
|
|
|
+ if (rows2.isNotEmpty()) {
|
|
|
+ if (((Rows) rows2.get(0).get("bom")).isNotEmpty()) {
|
|
|
+ Collections.sort((Rows) rows2.get(0).get("bom"), new SortByNum());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return getSucReturnObject().setData(rows2).toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询末级bom
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @API(title = "查询", apiversion = R.ID2025071010104503.v1.class)
|
|
|
+ @CACHEING
|
|
|
+ public String querybomLaststage() throws YosException {
|
|
|
+ // long sa_brandid = content.getLong("sa_brandid");
|
|
|
+ /*
|
|
|
+ * 过滤条件设置
|
|
|
+ */
|
|
|
+ String where = " 1=1 ";
|
|
|
+ if (content.containsKey("where")) {
|
|
|
+ JSONObject whereObject = content.getJSONObject("where");
|
|
|
+
|
|
|
+ if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
|
|
|
+ where = where + " and (t1.bomname like'%" + whereObject.getString("condition") + "%') ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SQLFactory factory = new SQLFactory(this, "bom末级列表查询");
|
|
|
+ factory.addParameter("siteid", siteid);
|
|
|
+ factory.addParameter_SQL("where", where);
|
|
|
+ Rows rows = dbConnect.runSqlQuery(factory.getSQL());
|
|
|
+
|
|
|
+ return getSucReturnObject().setData(rows).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @API(title = "删除", apiversion = R.ID2025071010105503.v1.class)
|
|
|
+ @CACHEING_CLEAN(apiClass = {bom.class})
|
|
|
+ public String delete() throws YosException {
|
|
|
+ Long plm_bomid = content.getLong("plm_bomid");
|
|
|
+
|
|
|
+ Rows rowscount = dbConnect.runSqlQuery(
|
|
|
+ "select isdeep,ifnull(parentid,0) parentid from plm_bom where plm_bomid=" + plm_bomid);
|
|
|
+ if (rowscount.isEmpty()) {
|
|
|
+ return getErrReturnObject().setErrMsg("此bom不存在,无法删除").toString();
|
|
|
+ } else {
|
|
|
+ if (!rowscount.get(0).getBoolean("isdeep")) {
|
|
|
+ return getErrReturnObject().setErrMsg("此bom非末级,无法删除").toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Rows row = dbConnect.runSqlQuery("select itemid from sa_itemsalebom where plm_bomid=" + plm_bomid);
|
|
|
+ if (!row.isEmpty()) {
|
|
|
+ return getErrReturnObject().setErrMsg("此bom下存在数据,无法删除").toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ ArrayList<String> sqllist = new ArrayList<>();
|
|
|
+ sqllist.add("update plm_bom set isdeep=1 where plm_bomid=" + rowscount.get(0).getLong("parentid"));
|
|
|
+ String deletesql = "DELETE FROM plm_bom WHERE plm_bomid = '" + plm_bomid + "'";
|
|
|
+ sqllist.add(deletesql);
|
|
|
+
|
|
|
+ dbConnect.runSqlUpdate(sqllist);
|
|
|
+ return getSucReturnObject().toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归查询下级产品类别
|
|
|
+ *
|
|
|
+ * @param root
|
|
|
+ * @param allboms
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Rows getSubbom(Row root, Rows allboms) {
|
|
|
+ Rows childrenRows = allboms.toRowsMap("parentid").get(root.getString("plm_bomid"));
|
|
|
+ Collections.sort(childrenRows, new SortByNum());
|
|
|
+ for (Row row : childrenRows) {
|
|
|
+ row.put("subdep", getSubbom(row, allboms));
|
|
|
+ }
|
|
|
+ return childrenRows;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ArrayList<String> getbomlevel(int level, long parentid, long plm_bomid, ArrayList<String> sqllist)
|
|
|
+ throws YosException {
|
|
|
+ if (parentid > 0) {
|
|
|
+ Rows rows = dbConnect.runSqlQuery(
|
|
|
+ "select ifnull(parentid,0) as parentid from plm_bom where plm_bomid= '"
|
|
|
+ + parentid + "'");
|
|
|
+ if (!rows.isEmpty()) {
|
|
|
+ level++;
|
|
|
+ sqllist.add("update plm_bom set level='" + level + "' where plm_bomid= '" + parentid + "'");
|
|
|
+ getbomlevel(level, rows.get(0).getLong("parentid"), rows.get(0).getLong("plm_bomid"), sqllist);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return sqllist;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getUppebomfullname(String fullname, long parentid, long plm_bomid) throws YosException {
|
|
|
+ String fullnum = "";
|
|
|
+ if (parentid > 0) {
|
|
|
+ Rows rows = dbConnect.runSqlQuery(
|
|
|
+ "select ifnull(parentid,0) as parentid,bomfullname from plm_bom where plm_bomid= '"
|
|
|
+ + parentid + "'");
|
|
|
+ if (!rows.isEmpty()) {
|
|
|
+ fullnum = rows.get(0).getString("bomfullname") + "/" + fullname;
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ fullnum = fullname;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return fullnum;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据指定的bomid获取下级bomid及营销路径名
|
|
|
+ *
|
|
|
+ * @param controller
|
|
|
+ * @param plm_bomid
|
|
|
+ * @throws YosException
|
|
|
+ */
|
|
|
+ public static Rows getSubbom(Controller controller, long plm_bomid) throws YosException {
|
|
|
+ SQLFactory sqlFactory = new SQLFactory(controller, "下级bom获取");
|
|
|
+ sqlFactory.addParameter("siteid", controller.siteid);
|
|
|
+ sqlFactory.addParameter("plm_bomid", plm_bomid);
|
|
|
+ Rows rows = controller.dbConnect.runSqlQuery(sqlFactory.getSQL());
|
|
|
+
|
|
|
+ return rows;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ArrayList<String> getLowerbomUpdateSql(long plm_bomid, String bomnameOld,
|
|
|
+ String bomnameNew) throws YosException {
|
|
|
+ ArrayList<String> sqlList = new ArrayList<String>();
|
|
|
+ Rows rows = getSubbom(this, plm_bomid);
|
|
|
+ if (!rows.isEmpty()) {
|
|
|
+ for (Row row : rows) {
|
|
|
+ if (row.getString("bomfullname").contains(bomnameOld)) {
|
|
|
+ String bomfullname = row.getString("bomfullname").replace(bomnameOld,
|
|
|
+ bomnameNew);
|
|
|
+ String sql = "update plm_bom set bomfullname='" + bomfullname
|
|
|
+ + "' where plm_bomid=" + row.getLong("plm_bomid");
|
|
|
+ sqlList.add(sql);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sqlList;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|