itemgroup.java 34 KB

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