itemgroup.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package restcontroller.webmanage.sale.itemgroup;
  2. import beans.brand.Brand;
  3. import beans.data.BatchDeleteErr;
  4. import beans.datatag.DataTag;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import common.Controller;
  9. import common.YosException;
  10. import common.annotation.API;
  11. import common.annotation.CACHEING;
  12. import common.annotation.CACHEING_CLEAN;
  13. import common.annotation.cm;
  14. import common.data.Row;
  15. import common.data.Rows;
  16. import common.data.RowsMap;
  17. import common.data.SQLFactory;
  18. import restcontroller.R;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. @API(title = "商品组")
  22. public class itemgroup extends Controller {
  23. public itemgroup(JSONObject arg0) throws YosException {
  24. super(arg0);
  25. // TODO Auto-generated constructor stub
  26. }
  27. /**
  28. * 新增修改商品组
  29. *
  30. * @return
  31. */
  32. @API(title = "商品组新增修改", apiversion = R.ID20220922164303.v1.class,intervaltime = 200)
  33. @CACHEING_CLEAN(cms = {@cm(clazz = itemgroup.class, method = {"queryItemgroup"})})
  34. public String insertormodify_itemgroup() throws YosException {
  35. long sa_itemgroupid = content.getLongValue("sa_itemgroupid");
  36. long sa_brandid = content.getLong("sa_brandid");
  37. String groupname = content.getString("groupname");
  38. String itemno = content.getString("itemno");
  39. String tradefield = content.getStringValue("tradefield");
  40. Long sequence = content.getLongValue("sequence");
  41. JSONArray tagArray = content.getJSONArray("tag");
  42. ArrayList<String> list = (ArrayList<String>) JSONObject.parseArray(tagArray.toJSONString(), String.class);
  43. Rows itemgroupRows = dbConnect.runSqlQuery("select sa_itemgroupid,sequence,isonsale from sa_itemgroup where sa_itemgroupid=" + sa_itemgroupid + " and siteid='" + siteid + "'");
  44. ArrayList<String> sqllist = new ArrayList<>();
  45. if (sa_itemgroupid <= 0 || itemgroupRows.isEmpty()) {
  46. // Rows rows = dbConnect.runSqlQuery("SELECT COUNT(*) count FROM sa_itemgroup WHERE sa_brandid='" + sa_brandid + "' and groupname = '" + groupname + "' and siteid='" + siteid + "'");
  47. // if (!rows.isEmpty() && rows.get(0).getLong("count") > 0) {
  48. // return getErrReturnObject().setErrMsg("商品组已存在").toString();
  49. // }
  50. Rows rows1 = dbConnect.runSqlQuery("select sequence from sa_itemgroup where siteid = '" + siteid + "' order by sequence asc");
  51. if (rows1.isNotEmpty() && rows1.toArrayList("sequence").contains(sequence.toString())) {
  52. if (sequence <= rows1.getLastRow().getLong("sequence")) {
  53. sqllist.add("update sa_itemgroup set sequence = sequence+1 where siteid = '" + siteid + "' and sequence >= " + sequence);
  54. }
  55. }
  56. SQLFactory sqlAddFactory = new SQLFactory(this, "商品组新增");
  57. //String billcode=createBillCode("");
  58. //String billcode="123456789";
  59. String billcode = createBillCode("itemgroup");
  60. sa_itemgroupid = createTableID("sa_itemgroup");
  61. sqlAddFactory.addParameter("sa_itemgroupid", sa_itemgroupid);
  62. sqlAddFactory.addParameter("siteid", siteid);
  63. sqlAddFactory.addParameter("groupname", groupname);
  64. sqlAddFactory.addParameter("itemno", itemno);
  65. sqlAddFactory.addParameter("username", username);
  66. sqlAddFactory.addParameter("groupnum", billcode);
  67. sqlAddFactory.addParameter("tradefield", tradefield);
  68. sqlAddFactory.addParameter("sa_brandid", sa_brandid);
  69. sqlAddFactory.addParameter("sequence", sequence);
  70. content.put("sa_itemgroupid", sa_itemgroupid);
  71. DataTag.updateTag(this, "sa_itemgroup", sa_itemgroupid, list);
  72. sqllist.add(sqlAddFactory.getSQL());
  73. } else {
  74. // Rows rows = dbConnect.runSqlQuery("SELECT COUNT(*) count FROM sa_itemgroup WHERE sa_brandid='" + sa_brandid + "' and sa_itemgroupid!=" + content.getLong("sa_itemgroupid") + " and groupname = '" + groupname + "' and siteid='" + siteid + "'");
  75. // if (!rows.isEmpty() && rows.get(0).getLong("count") > 0) {
  76. // return getErrReturnObject().setErrMsg("商品组已存在").toString();
  77. // }
  78. if (!itemgroupRows.isEmpty() && itemgroupRows.get(0).getBoolean("isonsale")) {
  79. return getErrReturnObject().setErrMsg("商品组已上架,无法修改").toString();
  80. }
  81. if (sequence < itemgroupRows.get(0).getLong("sequence")) {
  82. sqllist.add("update sa_itemgroup set sequence = sequence+1 where siteid = '" + siteid + "' and sequence between " + sequence + " and " + itemgroupRows.get(0).getLong("sequence"));
  83. } else if (sequence > itemgroupRows.get(0).getLong("sequence")) {
  84. sqllist.add("update sa_itemgroup set sequence = sequence-1 where siteid = '" + siteid + "' and sequence between " + itemgroupRows.get(0).getLong("sequence") + " and " + sequence);
  85. }
  86. SQLFactory sqlUpdateFactory = new SQLFactory(this, "商品组更新");
  87. sqlUpdateFactory.addParameter("sa_itemgroupid", sa_itemgroupid);
  88. sqlUpdateFactory.addParameter("groupname", groupname);
  89. sqlUpdateFactory.addParameter("itemno", itemno);
  90. sqlUpdateFactory.addParameter("tradefield", tradefield);
  91. sqlUpdateFactory.addParameter("sequence", sequence);
  92. sqlUpdateFactory.addParameter("username", username);
  93. DataTag.updateTag(this, "sa_itemgroup", sa_itemgroupid, list);
  94. sqllist.add(sqlUpdateFactory.getSQL());
  95. }
  96. dbConnect.runSqlUpdate(sqllist);
  97. return queryItemgroupMain();
  98. }
  99. /**
  100. * 查询商品组
  101. *
  102. * @return
  103. */
  104. @API(title = "查询", apiversion = R.ID20220922164403.v1.class)
  105. @CACHEING
  106. public String queryItemgroup() throws YosException {
  107. /*
  108. 过滤条件设置
  109. */
  110. String where = " 1=1 ";
  111. if (content.containsKey("where")) {
  112. JSONObject whereObject = content.getJSONObject("where");
  113. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  114. where = where + " and (t1.groupname like'%" + whereObject.getString("condition")
  115. + "%' or t1.groupnum like'%" + whereObject.getString("condition")
  116. + "%' or exists(select 1 from sa_itemgroupmx s1 left join plm_item s2 on s1.itemid=s2.itemid and s1.siteid=s2.siteid where (s2.itemname like '%"
  117. + whereObject.getString("condition") + "%' or s2.itemno like '%" + whereObject.getString("condition")
  118. + "%') and s1.sa_itemgroupid=t1.sa_itemgroupid) ) ";
  119. }
  120. if (whereObject.containsKey("brandname") && !"".equals(whereObject.getString("brandname"))) {
  121. where = where + " and (t3.brandname ='" + whereObject.getString("brandname") + "') ";
  122. }
  123. if (whereObject.containsKey("tradefield") && !"".equals(whereObject.getString("tradefield"))) {
  124. where = where + " and (t1.tradefield like '%" + whereObject.getString("tradefield") + "%') ";
  125. }
  126. if (whereObject.containsKey("itemclassname") && !"".equals(whereObject.getString("itemclassname"))) {
  127. where = where + " and (t5.itemclassname ='" + whereObject.getString("itemclassname") + "') ";
  128. }
  129. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  130. where = where + " and (t1.isonsale ='" + whereObject.getString("isonsale") + "') ";
  131. }
  132. if (whereObject.containsKey("iteminfo") && !"".equals(whereObject.getString("iteminfo"))) {
  133. where=where+" and t1.sa_itemgroupid in(select t1.sa_itemgroupid from sa_itemgroupmx t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where (t2.itemno like '%"+whereObject.getString("iteminfo")+"%' or t2.itemname like '%"+whereObject.getString("iteminfo")+"%')) ";
  134. }
  135. }
  136. /*
  137. SQL查询参数设置并查询
  138. */
  139. SQLFactory factory = new SQLFactory(this, "商品组列表查询", pageSize, pageNumber, pageSorting);
  140. factory.addParameter("siteid", siteid);
  141. factory.addParameter_SQL("where", where);
  142. String sql = factory.getSQL();
  143. Rows rows = dbConnect.runSqlQuery(sql);
  144. for (Row row2 : rows) {
  145. ArrayList<String> list = DataTag.queryTag(this, "sa_itemgroup", row2.getLong("sa_itemgroupid"), false);
  146. row2.put("tag1", JSONArray.parseArray(JSON.toJSONString(list)));
  147. }
  148. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  149. //默认商品图片
  150. Rows defaultImageRows = getAttachmentUrl("system", (long) 1, "defaultImage");
  151. //封面cover
  152. RowsMap coverRowsMap = getAttachmentUrl("plm_item", ids, "cover");
  153. //附件
  154. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  155. Rows rowsitemclass = dbConnect.runSqlQuery(
  156. " select t7.itemclassname,t6.itemid,t8.brandname from sa_itemsaleclass t6 inner JOIN plm_itemclass t7 ON t7.itemclassid = t6.itemclassid AND t7.siteid = t6.siteid LEFT JOIN sa_brand t8 ON t8.sa_brandid = t7.sa_brandid AND t8.siteid = t7.siteid where t6.siteid='"
  157. + siteid + "'");
  158. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  159. for (Row row : rows) {
  160. Rows coverRows = coverRowsMap.get(row.getString("itemid"));
  161. if (coverRows.isEmpty()) {
  162. if (!defaultImageRows.isEmpty()) {
  163. row.put("cover", defaultImageRows.get(0).getString("url"));
  164. } else {
  165. row.put("cover", "");
  166. }
  167. } else {
  168. row.put("cover", coverRows.get(0).getString("url"));
  169. }
  170. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  171. row.put("itemclass", itemclassRowsMap.get(row.getString("itemid")));
  172. }
  173. return getSucReturnObject().setData(rows).toString();
  174. }
  175. @API(title = "商品组详情", apiversion = R.ID20220923091903.v1.class)
  176. public String queryItemgroupMain() throws YosException {
  177. Long sa_itemgroupid = content.getLong("sa_itemgroupid");
  178. SQLFactory sqlFactory = new SQLFactory(this, "商品组详情查询");
  179. sqlFactory.addParameter("sa_itemgroupid", sa_itemgroupid);
  180. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  181. Rows rowsitemclass = dbConnect.runSqlQuery(
  182. " select t7.itemclassname,t6.itemid,t8.brandname from sa_itemsaleclass t6 inner JOIN plm_itemclass t7 ON t7.itemclassid = t6.itemclassid AND t7.siteid = t6.siteid LEFT JOIN sa_brand t8 ON t8.sa_brandid = t7.sa_brandid AND t8.siteid = t7.siteid where t6.siteid='"
  183. + siteid + "'");
  184. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  185. if (!rows.isEmpty()) {
  186. ArrayList<String> list = DataTag.queryTag(this, "sa_itemgroup", rows.get(0).getLong("sa_itemgroupid"), false);
  187. rows.get(0).put("tag1", JSONArray.parseArray(JSON.toJSONString(list)));
  188. rows.get(0).put("itemclass", itemclassRowsMap.get(rows.get(0).getString("itemid")));
  189. }
  190. return getSucReturnObject().setData(rows.size() > 0 ? rows.get(0) : new Row()).toString();
  191. }
  192. /**
  193. * 商品组上架
  194. *
  195. * @return
  196. */
  197. @API(title = "商品组上架", apiversion = R.ID20220923143603.v1.class)
  198. @CACHEING_CLEAN(cms = {@cm(clazz = itemgroup.class, method = {"queryItemgroup", "queryItemgroupMain"})})
  199. public String release() throws YosException {
  200. JSONArray sa_itemgroupids = content.getJSONArray("sa_itemgroupids");
  201. String[] s = {"sa_itemgroupids"};
  202. for (String s1 : s) {
  203. if (!content.containsKey(s1))
  204. return getErrReturnObject().setErrMsg("缺少" + s1 + "参数").toString();
  205. }
  206. List<Long> list = sa_itemgroupids.toJavaList(Long.class);
  207. Long[] stringArray = list.toArray(new Long[0]);
  208. // SQLFactory sqlFactoryquery = new SQLFactory(this, "商品组状态查询");
  209. // sqlFactoryquery.addParameter("siteid", siteid);
  210. // sqlFactoryquery.addParameter_in("sa_itemgroupids", stringArray);
  211. String whereSql = "select isonsale,groupname,sa_itemgroupid from sa_itemgroup where sa_itemgroupid in " + list + " and siteid='" + siteid + "'";
  212. whereSql = whereSql.replace("[", "(").replace("]", ")");
  213. Rows rows = dbConnect.runSqlQuery(whereSql);
  214. for (Row row : rows) {
  215. if (row.getBoolean("isonsale")) {
  216. return getErrReturnObject().setErrMsg("【" + row.getString("groupname") + "】商品组已上架,无法再次上架").toString();
  217. }
  218. Rows rowscount = dbConnect.runSqlQuery("select distinct ifnull(t2.sa_customschemeid,0) sa_customschemeid from sa_itemgroupmx t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.siteid='"+siteid+"' and t1.sa_itemgroupid="+row.getLong("sa_itemgroupid"));
  219. if(rowscount.size()>1){
  220. return getErrReturnObject().setErrMsg("【" + row.getString("groupname") + "】商品组存在两个以上方案的商品,无法上架").toString();
  221. }
  222. }
  223. SQLFactory sqlFactoryupdate = new SQLFactory(this, "商品组上架");
  224. sqlFactoryupdate.addParameter("siteid", siteid);
  225. sqlFactoryupdate.addParameter_in("sa_itemgroupids", stringArray);
  226. dbConnect.runSqlUpdate(sqlFactoryupdate);
  227. // //发送消息
  228. // for (String id : stringArray) {
  229. // sendMsg(Long.parseLong(id));
  230. // }
  231. return getSucReturnObject().toString();
  232. }
  233. /**
  234. * 商品组下架
  235. *
  236. * @return
  237. */
  238. @API(title = "下架", apiversion = R.ID20220923143703.v1.class)
  239. @CACHEING_CLEAN(cms = {@cm(clazz = itemgroup.class, method = {"queryItemgroup", "queryItemgroupMain"})})
  240. public String undercarriage() throws YosException {
  241. JSONArray sa_itemgroupids = content.getJSONArray("sa_itemgroupids");
  242. String[] s = {"sa_itemgroupids"};
  243. for (String s1 : s) {
  244. if (!content.containsKey(s1))
  245. return getErrReturnObject().setErrMsg("缺少" + s1 + "参数").toString();
  246. }
  247. List<Long> list = sa_itemgroupids.toJavaList(Long.class);
  248. Long[] stringArray = list.toArray(new Long[0]);
  249. String whereSql = "select isonsale,groupname from sa_itemgroup where sa_itemgroupid in " + list + " and siteid='" + siteid + "'";
  250. whereSql = whereSql.replace("[", "(").replace("]", ")");
  251. Rows rows = dbConnect.runSqlQuery(whereSql);
  252. for (Row row : rows) {
  253. if (!row.getBoolean("isonsale")) {
  254. return getErrReturnObject().setErrMsg("【" + row.getString("groupname") + "】商品组已下架,无法再次下架").toString();
  255. }
  256. }
  257. SQLFactory sqlFactoryupdate = new SQLFactory(this, "商品组下架");
  258. sqlFactoryupdate.addParameter("siteid", siteid);
  259. sqlFactoryupdate.addParameter_in("sa_itemgroupids", stringArray);
  260. dbConnect.runSqlUpdate(sqlFactoryupdate);
  261. return getSucReturnObject().toString();
  262. }
  263. @API(title = "新建或修改商品组商品明细", apiversion = R.ID20220923110303.v1.class,intervaltime = 200)
  264. @CACHEING_CLEAN(cms = {
  265. @cm(clazz = itemgroup.class, method = {"queryItemgroupMxList", "queryItemgList"})})
  266. public String insertormodify_itemgroupmx() throws YosException {
  267. Long sa_itemgroupid = content.getLong("sa_itemgroupid");
  268. JSONArray iteminfos = content.getJSONArray("itemclassinfos");
  269. ArrayList<String> sqlList = new ArrayList<>();
  270. Rows rowscount = dbConnect.runSqlQuery(
  271. "select isonsale from sa_itemgroup where sa_itemgroupid=" + sa_itemgroupid);
  272. if (!rowscount.isEmpty()) {
  273. if (rowscount.get(0).getBoolean("isonsale")) {
  274. return getErrReturnObject().setErrMsg("上架状态的商品组无法新建及修改").toString();
  275. }
  276. }
  277. int i = 0;
  278. long maxid = 0;
  279. long[] sa_itemgroupmxid = createTableID("sa_itemgroupmx", iteminfos.size());
  280. Rows maxidRows = dbConnect.runSqlQuery("select MAX(sequence) sequence from sa_itemgroupmx where sa_itemgroupid=" + sa_itemgroupid);
  281. if (!maxidRows.isEmpty()) {
  282. maxid = maxidRows.get(0).getLong("sequence");
  283. }
  284. for (Object obj : iteminfos) {
  285. JSONObject iteminfo = (JSONObject) obj;
  286. if (iteminfo.getLong("sa_itemgroupmxid") <= 0
  287. || dbConnect.runSqlQuery("select sa_itemgroupmxid from sa_itemgroupmx where sa_itemgroupmxid="
  288. + iteminfo.getLong("sa_itemgroupmxid")).isEmpty()) {
  289. SQLFactory saleFactory = new SQLFactory(this, "商品组商品明细新增");
  290. saleFactory.addParameter("siteid", siteid);
  291. saleFactory.addParameter("sequence", maxid + i + 1);
  292. saleFactory.addParameter("sa_itemgroupmxid", sa_itemgroupmxid[i]);
  293. //saleFactory.addParameter("itemno", iteminfo.getString("itemno"));
  294. saleFactory.addParameter("itemid", iteminfo.getString("itemid"));
  295. Rows rows = dbConnect.runSqlQuery("select itemno from plm_item where itemid=" + iteminfo.getLong("itemid"));
  296. if (rows.isEmpty()) {
  297. saleFactory.addParameter("itemno", "null");
  298. } else {
  299. saleFactory.addParameter("itemno", rows.get(0).getString("itemno"));
  300. }
  301. saleFactory.addParameter("sa_itemgroupid", sa_itemgroupid);
  302. sqlList.add(saleFactory.getSQL());
  303. i++;
  304. } else {
  305. SQLFactory saleFactory = new SQLFactory(this, "商品组商品明细更新");
  306. saleFactory.addParameter("sa_itemgroupmxid", iteminfo.getLong("sa_itemgroupmxid"));
  307. saleFactory.addParameter("sequence", iteminfo.getLongValue("sequence"));
  308. //saleFactory.addParameter("itemno", iteminfo.getString("itemno"));
  309. saleFactory.addParameter("itemid", iteminfo.getString("itemid"));
  310. Rows rows = dbConnect.runSqlQuery("select itemno from plm_item where itemid=" + iteminfo.getLong("itemid"));
  311. if (rows.isEmpty()) {
  312. saleFactory.addParameter("itemno", "null");
  313. } else {
  314. saleFactory.addParameter("itemno", rows.get(0).getString("itemno"));
  315. }
  316. sqlList.add(saleFactory.getSQL());
  317. }
  318. }
  319. dbConnect.runSqlUpdate(sqlList);
  320. updateTradefield(sa_itemgroupid);
  321. return queryItemgroupMxList();
  322. }
  323. @API(title = "商品列表查询", apiversion = R.ID20220923112503.v1.class)
  324. @CACHEING
  325. public String queryItemgList() throws YosException {
  326. /*
  327. * 过滤条件设置
  328. */
  329. String where = " 1=1 ";
  330. if (content.containsKey("where")) {
  331. JSONObject whereObject = content.getJSONObject("where");
  332. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  333. where = where + " and (t1.itemname like '%" + whereObject.getString("condition") + "%'"
  334. + " or t1.itemno like '%" + whereObject.getString("condition") + "%')";
  335. }
  336. if (whereObject.containsKey("brandname") && !"".equals(whereObject.getString("brandname"))) {
  337. where = where + " and t5.brandname ='" + whereObject.getString("brandname") + "'";
  338. }
  339. }
  340. Long sa_itemgroupid = content.getLong("sa_itemgroupid");
  341. // String hrid = content.getString("hrid");
  342. SQLFactory sqlFactory = new SQLFactory(this, "商品查询", pageSize, pageNumber, pageSorting);
  343. sqlFactory.addParameter_SQL("where", where);
  344. sqlFactory.addParameter("sa_itemgroupid", sa_itemgroupid);
  345. sqlFactory.addParameter("siteid", siteid);
  346. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  347. //附件
  348. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  349. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  350. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  351. Rows rowsitemclass = dbConnect.runSqlQuery(" select t7.itemclassname,t6.itemid,t8.brandname from sa_itemsaleclass t6 LEFT JOIN plm_itemclass t7 ON t7.itemclassid = t6.itemclassid AND t7.siteid = t6.siteid LEFT JOIN sa_brand t8 ON t8.sa_brandid = t7.sa_brandid AND t8.siteid = t7.siteid where t6.siteid='" + siteid + "'");
  352. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  353. for (Row row : rows) {
  354. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  355. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  356. row.put("itemclass", itemclassRowsMap.get(row.getString("itemid")));
  357. }
  358. return getSucReturnObject().setData(rows).toString();
  359. }
  360. @API(title = "商品组商品明细列表", apiversion = R.ID20220923101603.v1.class)
  361. @CACHEING
  362. public String queryItemgroupMxList() throws YosException {
  363. /*
  364. * 过滤条件设置
  365. */
  366. String where = " 1=1 ";
  367. if (content.containsKey("where")) {
  368. JSONObject whereObject = content.getJSONObject("where");
  369. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  370. where = where + "and t2.itemname like'%" + whereObject.getString("condition") + "%'";
  371. }
  372. }
  373. Long sa_itemgroupid = content.getLong("sa_itemgroupid");
  374. // String hrid = content.getString("hrid");
  375. SQLFactory sqlFactory = new SQLFactory(this, "商品组商品明细查询", pageSize, pageNumber, pageSorting);
  376. sqlFactory.addParameter_SQL("where", where);
  377. sqlFactory.addParameter("sa_itemgroupid", sa_itemgroupid);
  378. sqlFactory.addParameter("siteid", siteid);
  379. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  380. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  381. //默认商品图片
  382. Rows defaultImageRows = getAttachmentUrl("system", (long) 1, "defaultImage");
  383. // 封面cover
  384. RowsMap coverRowsMap = getAttachmentUrl("plm_item", ids, "cover");
  385. // 附件
  386. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  387. Rows technicalinforows = dbConnect.runSqlQuery("select plm_technicalinfoid,itemid from plm_technicalinfo_item");
  388. RowsMap technicalinfoRowsMap = technicalinforows.toRowsMap("itemid");
  389. Rows technicalinforows1 = dbConnect.runSqlQuery("select plm_technicalinfoid from plm_technicalinfo");
  390. ArrayList<Long> plm_technicalinfoids = technicalinforows1.toArrayList("plm_technicalinfoid", new ArrayList<>());
  391. // 产品技术资料附件
  392. RowsMap plm_technicalinfoRowsMap = getAttachmentUrl("plm_technicalinfo", plm_technicalinfoids);
  393. Rows rowsitemclass = dbConnect.runSqlQuery(
  394. " select t7.itemclassname,t6.itemid,t8.brandname from sa_itemsaleclass t6 LEFT JOIN plm_itemclass t7 ON t7.itemclassid = t6.itemclassid AND t7.siteid = t6.siteid LEFT JOIN sa_brand t8 ON t8.sa_brandid = t7.sa_brandid AND t8.siteid = t7.siteid where t6.siteid='"
  395. + siteid + "'");
  396. RowsMap itemclassRowsMap = rowsitemclass.toRowsMap("itemid");
  397. for (Row row : rows) {
  398. JSONArray jsonArray = new JSONArray();
  399. Rows coverRows = coverRowsMap.get(row.getString("itemid"));
  400. if (coverRows.isEmpty()) {
  401. if (!defaultImageRows.isEmpty()) {
  402. row.put("cover", defaultImageRows.get(0).getString("url"));
  403. } else {
  404. row.put("cover", "");
  405. }
  406. } else {
  407. row.put("cover", coverRows.get(0).getString("url"));
  408. }
  409. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  410. row.put("itemclass", itemclassRowsMap.get(row.getString("itemid")));
  411. Rows technicalinforowsforitem = technicalinfoRowsMap.get(row.getString("itemid"));
  412. for (Row row2 : technicalinforowsforitem) {
  413. jsonArray.add(plm_technicalinfoRowsMap.get(row2.getString("plm_technicalinfoid")));
  414. }
  415. row.put("technicalinfo", jsonArray);
  416. }
  417. return getSucReturnObject().setData(rows).toString();
  418. }
  419. @API(title = "删除明细", apiversion = R.ID20220923105103.v1.class)
  420. @CACHEING_CLEAN(cms = {@cm(clazz = itemgroup.class, method = {"queryItemgroupMxList", "queryItemgList"})})
  421. public String deletemx() throws YosException {
  422. ArrayList<String> sqlList = new ArrayList<>();
  423. Long sa_itemgroupmxid = content.getLong("sa_itemgroupmxid");
  424. Long sa_itemgroupid = content.getLong("sa_itemgroupid");
  425. Rows rowscount = dbConnect.runSqlQuery(
  426. "select isonsale from sa_itemgroup where sa_itemgroupid=" + sa_itemgroupid);
  427. if (!rowscount.isEmpty()) {
  428. if (rowscount.get(0).getBoolean("isonsale")) {
  429. return getErrReturnObject().setErrMsg("上架状态的商品组明细无法删除").toString();
  430. }
  431. }
  432. String sql = "DELETE FROM sa_itemgroupmx WHERE sa_itemgroupmxid = '" + sa_itemgroupmxid + "'";
  433. sqlList.add(sql);
  434. // sqlList.add("DELETE FROM sa_salesforecastbill WHERE sa_salesforecastmodelid = '" + sa_salesforecastmodelid + "'");
  435. // sqlList.add("DELETE FROM sa_salesforecasthr WHERE sa_salesforecastmodelid = '" + sa_salesforecastmodelid + "'");
  436. dbConnect.runSqlUpdate(sqlList);
  437. updateTradefield(sa_itemgroupid);
  438. return getSucReturnObject().toString();
  439. }
  440. @API(title = "删除", apiversion = R.ID20220922164503.v1.class)
  441. @CACHEING_CLEAN(cms = {@cm(clazz = itemgroup.class, method = {"queryItemgroup"})})
  442. public String delete() throws YosException {
  443. JSONArray sa_itemgroupids = content.getJSONArray("sa_itemgroupids");
  444. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_itemgroupids.size());
  445. ArrayList<String> sqllist = new ArrayList<>();
  446. for (Object o : sa_itemgroupids) {
  447. long sa_itemgroupid = Long.parseLong(o.toString());
  448. Rows rowscount = dbConnect.runSqlQuery("select isonsale from sa_itemgroup where sa_itemgroupid=" + sa_itemgroupid);
  449. if (rowscount.isEmpty()) {
  450. batchDeleteErr.addErr(sa_itemgroupid, "此商品组不存在,无法删除");
  451. } else {
  452. if (rowscount.get(0).getBoolean("isonsale")) {
  453. batchDeleteErr.addErr(sa_itemgroupid, "此商品组已上架,无法删除");
  454. }
  455. }
  456. String deletesql = "DELETE FROM sa_itemgroup WHERE sa_itemgroupid = '" + sa_itemgroupid + "'";
  457. sqllist.add(deletesql);
  458. }
  459. dbConnect.runSqlUpdate(sqllist);
  460. return batchDeleteErr.getReturnObject().toString();
  461. }
  462. /**
  463. * 获取商品组领域
  464. *
  465. * @return
  466. * @throws YosException
  467. */
  468. public void updateTradefield(Long sa_itemgroupid) throws YosException {
  469. String sql = "";
  470. String tradefield = "";
  471. Rows rows = dbConnect.runSqlQuery("select distinct t2.tradefield from sa_itemgroupmx t1 inner join plm_item_tradefield t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where sa_itemgroupid=" + sa_itemgroupid);
  472. if (!rows.isEmpty()) {
  473. for (Row row : rows) {
  474. tradefield = tradefield + row.getString("tradefield") + ",";
  475. }
  476. }
  477. if (!tradefield.equals("")) {
  478. tradefield = tradefield.substring(0, tradefield.length() - 1);
  479. dbConnect.runSqlUpdate("update sa_itemgroup set tradefield='" + tradefield + "' where sa_itemgroupid=" + sa_itemgroupid);
  480. }
  481. }
  482. }