Fad.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package restcontroller.webmanage.saletool.fad;
  2. import beans.attachment.Attachment;
  3. import beans.brand.Brand;
  4. import beans.datacontrllog.DataContrlLog;
  5. import beans.itemclass.ItemClass;
  6. import beans.time.Time;
  7. import com.alibaba.excel.write.handler.RowWriteHandler;
  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.data.*;
  14. import org.apache.commons.lang.StringUtils;
  15. import restcontroller.R;
  16. import java.util.ArrayList;
  17. import java.util.LinkedHashSet;
  18. /**
  19. * 单品管理
  20. */
  21. public class Fad extends Controller {
  22. /**
  23. * 构造函数
  24. *
  25. * @param content
  26. */
  27. public Fad(JSONObject content) throws YosException {
  28. super(content);
  29. }
  30. String sa_fad = "sa_fad";
  31. @API(title = "单品新增或编辑", apiversion = R.ID20240418140902.v1.class)
  32. public String insertOrUpdate() throws YosException {
  33. Long sa_fadid = content.getLongValue("sa_fadid");
  34. Long itemid = content.getLongValue("itemid");
  35. int isonsale = content.getIntValue("isonsale");
  36. JSONArray sa_fadclassids = new JSONArray();
  37. if (content.containsKey("sa_fadclassids")) {
  38. sa_fadclassids = content.getJSONArray("sa_fadclassids");
  39. for (Object obj : sa_fadclassids) {
  40. if (!(obj instanceof JSONArray)) {
  41. return getErrReturnObject().setErrMsg("分类格式不正确").toString();
  42. }
  43. }
  44. }
  45. String offsaledate = content.getStringValue("offsaledate", true, "null");
  46. boolean isstop = true;
  47. if (offsaledate.equals("null")) {
  48. isstop = false;
  49. }
  50. if (sa_fadid <= 0) {
  51. sa_fadid = createTableID(sa_fad);
  52. InsertSQL sqlFactory = SQLFactory.createInsertSQL(this, sa_fad);
  53. sqlFactory.setSiteid(siteid);
  54. sqlFactory.setUniqueid(sa_fadid);
  55. sqlFactory.setValue("name", content.getStringValue("name"));
  56. sqlFactory.setValue("model", content.getStringValue("model"));
  57. sqlFactory.setValue("spec", content.getStringValue("spec"));
  58. sqlFactory.setValue("candownload", content.getBooleanValue("candownload"));
  59. sqlFactory.setValue("outurl", content.getStringValue("outurl", true));
  60. sqlFactory.setValue("isnew", content.getBooleanValue("isnew"));
  61. sqlFactory.setValue("sequence", content.getLongValue("sequence"));
  62. sqlFactory.setValue("content", content.getStringValue("contentstr", true));
  63. sqlFactory.setValue("price", content.getBigDecimalValue("price"));
  64. sqlFactory.setValue("offsaledate", offsaledate);
  65. sqlFactory.setValue("sa_fadclassids", sa_fadclassids);
  66. sqlFactory.setValue("classid", 1);
  67. sqlFactory.setValue("isstop", isstop);
  68. sqlFactory.insert();
  69. content.put("sa_fadid", sa_fadid);
  70. DataContrlLog.createLog(this, "sa_fad", sa_fadid, "新建", "新建单品 " + content.getString("name")).insert();
  71. } else {
  72. UpdateSQL sqlFactory = SQLFactory.createUpdateSQL(this, sa_fad);
  73. sqlFactory.setUniqueid(sa_fadid);
  74. sqlFactory.setSiteid(siteid);
  75. sqlFactory.setValue("name", content.getStringValue("name"));
  76. sqlFactory.setValue("model", content.getStringValue("model"));
  77. sqlFactory.setValue("spec", content.getStringValue("spec"));
  78. sqlFactory.setValue("candownload", content.getBooleanValue("candownload"));
  79. sqlFactory.setValue("outurl", content.getStringValue("outurl", true));
  80. sqlFactory.setValue("isnew", content.getBooleanValue("isnew"));
  81. sqlFactory.setValue("sequence", content.getLongValue("sequence"));
  82. sqlFactory.setValue("content", content.getStringValue("contentstr", true));
  83. sqlFactory.setValue("price", content.getBigDecimalValue("price"));
  84. sqlFactory.setValue("offsaledate", offsaledate);
  85. sqlFactory.setValue("sa_fadclassids", sa_fadclassids);
  86. sqlFactory.setValue("isstop", isstop);
  87. sqlFactory.update();
  88. DataContrlLog.createLog(this, "sa_fad", sa_fadid, "编辑", "编辑单品 " + content.getString("name")).insert();
  89. }
  90. //上下架
  91. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sa_fad);
  92. updateSQL.setSiteid(siteid);
  93. updateSQL.setWhere("sa_fadid", sa_fadid);
  94. updateSQL.setValue("isonsale", isonsale);
  95. updateSQL.setValue("onsaledate", isonsale == 1 ? Time.getDateTime_Str() : "null");
  96. updateSQL.setValue("onsaleby", isonsale == 1 ? username : "null");
  97. updateSQL.update();
  98. //关联商品
  99. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_fad_link");
  100. insertSQL.setSiteid(siteid);
  101. insertSQL.setUniqueid(createTableID("sa_fad_link"));
  102. insertSQL.setValue("itemid", itemid);
  103. insertSQL.setValue("sa_fadid", sa_fadid);
  104. insertSQL.setWhere("not exists(select 1 from sa_fad_link where itemid=" + itemid + " and sa_fadid=" + sa_fadid + " )");
  105. insertSQL.insert();
  106. //插入图片
  107. Rows rows = Attachment.get(this, "plm_item", itemid, "cover");
  108. if (rows.isEmpty()) {
  109. rows = Attachment.get(this, "plm_item", itemid, "default");
  110. }
  111. if (rows.isNotEmpty()) {
  112. insertSQL = SQLFactory.createInsertSQL(this, "sys_attachment_links");
  113. insertSQL.setSiteid(siteid);
  114. insertSQL.setUniqueid(createTableID("sys_attachment_links"));
  115. insertSQL.setValue("usetype", sa_fad);
  116. insertSQL.setValue("attachmentid", rows.get(0).getLong("attachmentid"));
  117. insertSQL.setValue("sequence", 1);
  118. insertSQL.setValue("ownertable", sa_fad);
  119. insertSQL.setValue("ownerid", sa_fadid);
  120. insertSQL.setWhere("not exists(select 1 from sys_attachment_links where attachmentid=" + rows.get(0).getLong("attachmentid") + " and ownertable='sa_fad' and siteid='" + siteid + "' and ownerid=" + sa_fadid + " )");
  121. insertSQL.insert();
  122. }
  123. Row row = new Row();
  124. row.put("sa_fadid", sa_fadid);
  125. return getSucReturnObject().setData(row).toString();
  126. }
  127. @API(title = "单品详情", apiversion = R.ID20240418141002.v1.class, accesstoken = false)
  128. public String detail() throws YosException {
  129. Long sa_fadid = content.getLongValue("sa_fadid");
  130. String ownertable = sa_fad;
  131. Rows fadrows = dbConnect.runSqlQuery("select * from sa_fad where sa_fadid=" + sa_fadid);
  132. if (fadrows.isNotEmpty()) {
  133. siteid = fadrows.get(0).getString("siteid");
  134. }
  135. dbConnect.runSqlUpdate("UPDATE sa_fad SET readcount=readcount+1 WHERE sa_fadid=" + sa_fadid);
  136. QuerySQL querySQ = SQLFactory.createQuerySQL(this, sa_fad)
  137. .setTableAlias("t1");
  138. querySQ.addJoinTable(JOINTYPE.left, "sys_datacollect", "t2", "t2.siteid=t1.siteid and t2.ownertable='sa_fad' and t2.type=1 and t2.ownerid=t1.sa_fadid and t2.userid='" + userid + "'");
  139. querySQ.addQueryFields("iscollect", "CASE WHEN t2.sys_datacollectid>0 THEN 1 ELSE 0 END");
  140. querySQ.setSiteid(siteid);
  141. querySQ.setWhere("sa_fadid", sa_fadid);
  142. Rows rows = querySQ.query();
  143. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  144. Rows attRows = getAttachmentUrl(sa_fad, sa_fadid);
  145. attRows.sortby("sequence", "linksid");
  146. detailRow.put("attinfos", attRows);
  147. detailRow.put("appleturl", userInfo.getWechatAppPage("marketingtool", "FadUrl") + sa_fadid);
  148. QuerySQL attachmentQuery = SQLFactory.createQuerySQL(this, "sys_attachment_links").setTableAlias("t1");
  149. attachmentQuery.setSiteid(detailRow.getString("siteid"));
  150. attachmentQuery.setWhere("ownertable", ownertable);
  151. attachmentQuery.setWhere("ownerid", sa_fadid);
  152. attachmentQuery.setWhere("usetype", ownertable);
  153. attachmentQuery.setOrderBy("t1.sequence,t1.linksid");
  154. Rows attachmentRows = attachmentQuery.query();
  155. ArrayList<Long> ids = attachmentRows.toArrayList("attachmentid", new ArrayList<>());
  156. RowsMap attRowsMap = Attachment.get(this, ids).toRowsMap("attachmentid");
  157. for (Row row : attachmentRows) {
  158. Rows attPicRows = new Rows();
  159. Rows tempAttRows = attRowsMap.getOrDefault(row.getString("attachmentid"), new Rows());
  160. for (Row tempAttRow : tempAttRows) {
  161. // if (tempAttRow.getString("usetype").equals(ownertable)) {
  162. attPicRows.add(tempAttRow);
  163. // }
  164. }
  165. row.put("attinfos", attPicRows);
  166. }
  167. detailRow.put("attinfos_pic", attachmentRows);
  168. detailRow.putIfAbsent("offsaledate", "");
  169. detailRow.putIfAbsent("onsaledate", "");
  170. detailRow.put("classnames", getClassnames(detailRow.getJSONArray("sa_fadclassids"), false));
  171. detailRow.put("mainclassnames", getClassnames(detailRow.getJSONArray("sa_fadclassids"), true));
  172. return getSucReturnObject().setData(detailRow).toString();
  173. }
  174. @API(title = "单品删除", apiversion = R.ID20240418141102.v1.class)
  175. public String delete() throws YosException {
  176. JSONArray sa_fadids = content.getJSONArray("sa_fadids");
  177. if (sa_fadids.size() == 0) {
  178. return getErrReturnObject().setErrMsg("请选择要删除的数据").toString();
  179. }
  180. DeleteSQL sqlFactory = SQLFactory.createDeleteSQL(this, sa_fad);
  181. sqlFactory.setSiteid(siteid);
  182. sqlFactory.setWhere("sa_fadid", sa_fadids.toArray());
  183. sqlFactory.delete();
  184. return getSucReturnObject().toString();
  185. }
  186. @API(title = "单品上下架", apiversion = R.ID20240418141202.v1.class)
  187. public String isonsale() throws YosException {
  188. JSONArray sa_fadids = content.getJSONArray("sa_fadids");
  189. int isonsale = content.getIntValue("isonsale");
  190. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sa_fad);
  191. updateSQL.setSiteid(siteid);
  192. updateSQL.setWhere("sa_fadid", sa_fadids);
  193. updateSQL.setValue("isonsale", isonsale);
  194. updateSQL.setValue("onsaledate", isonsale == 0 ? "null" : Time.getDateTime_Str());
  195. updateSQL.setValue("onsaleby", isonsale == 0 ? "null" : username);
  196. updateSQL.update();
  197. //操作记录
  198. for (Object object : sa_fadids) {
  199. long sa_fadid = Long.parseLong(object.toString());
  200. Row row = dbConnect.runSqlQuery(0, "SELECT * from sa_fad WHERE siteid='" + siteid + "' and sa_fadid=" + sa_fadid);
  201. if (isonsale == 0) {
  202. DataContrlLog.createLog(this, "sa_fad", sa_fadid, "下架", "下架单品 " + row.getString("name")).insert();
  203. } else {
  204. DataContrlLog.createLog(this, "sa_fad", sa_fadid, "上架", "上架单品 " + row.getString("name")).insert();
  205. }
  206. }
  207. return getSucReturnObject().toString();
  208. }
  209. @API(title = "单品列表", apiversion = R.ID20240418141302.v1.class)
  210. public String list() throws YosException {
  211. /*
  212. 过滤条件设置
  213. */
  214. StringBuffer where = new StringBuffer(" 1=1 ");
  215. if (content.containsKey("where")) {
  216. JSONObject whereObject = content.getJSONObject("where");
  217. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  218. where.append(" and (");
  219. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  220. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  221. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  222. where.append(")");
  223. }
  224. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  225. where.append(" and (");
  226. where.append("t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  227. where.append(")");
  228. }
  229. if (whereObject.containsKey("isstop") && !"".equals(whereObject.getString("isstop"))) {
  230. where.append(" and (");
  231. where.append("t1.isstop ='").append(whereObject.getString("isstop")).append("' ");
  232. where.append(")");
  233. }
  234. if (whereObject.containsKey("begindate_create") && !"".equals(whereObject.getString("begindate_create"))) {
  235. where.append(" and (");
  236. where.append("t1.createdate >='").append(whereObject.getString("begindate_create")).append("' ");
  237. where.append(")");
  238. }
  239. if (whereObject.containsKey("enddate_create") && !"".equals(whereObject.getString("enddate_create"))) {
  240. where.append(" and (");
  241. where.append("t1.createdate <='").append(whereObject.getString("enddate_create")).append(" 23:59:59' ");
  242. where.append(")");
  243. }
  244. if (whereObject.containsKey("begindate_onsale") && !"".equals(whereObject.getString("begindate_onsale"))) {
  245. where.append(" and (");
  246. where.append("t1.onsaledate >='").append(whereObject.getString("begindate_onsale")).append("' ");
  247. where.append(")");
  248. }
  249. if (whereObject.containsKey("enddate_onsale") && !"".equals(whereObject.getString("enddate_onsale"))) {
  250. where.append(" and (");
  251. where.append("t1.onsaledate <='").append(whereObject.getString("enddate_onsale")).append(" 23:59:59' ");
  252. where.append(")");
  253. }
  254. if (whereObject.containsKey("sa_fadclassids") && !"".equals(whereObject.getString("sa_fadclassids"))) {
  255. JSONArray sa_fadclassids = whereObject.getJSONArray("sa_fadclassids");
  256. if (sa_fadclassids.size() > 0) {
  257. where.append(" and (1=2");
  258. for (Object obj : sa_fadclassids) {
  259. JSONArray array = (JSONArray) obj;
  260. if (array.size() > 0) {
  261. where.append(" or (");
  262. where.append("JSON_CONTAINS(t1.sa_fadclassids,'" + array.get(array.size() - 1) + "')");
  263. where.append(")");
  264. }
  265. }
  266. where.append(")");
  267. }
  268. }
  269. if (whereObject.containsKey("iscollect") && !"".equals(whereObject.getString("iscollect"))) {
  270. if (whereObject.getString("iscollect").equals("1")) {
  271. where.append(" and (");
  272. where.append(" t1.sa_fadid in (SELECT ownerid from sys_datacollect WHERE ownertable='sa_fad' and type=1 and siteid='" + siteid + "' and userid=" + userid + ")");
  273. where.append(")");
  274. }
  275. }
  276. }
  277. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sa_fad,
  278. "sa_fadid", "name", "isonsale", "model", "spec", "price", "sequence", "createby", "createdate", "onsaledate", "isstop")
  279. .setTableAlias("t1");
  280. querySQL.setSiteid(siteid);
  281. querySQL.setWhere("classid", 1);
  282. querySQL.setWhere(where.toString());
  283. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  284. Rows rows = querySQL.query();
  285. ArrayList<Long> ids = rows.toArrayList("sa_fadid", new ArrayList<>());
  286. // 附件
  287. RowsMap RowsMap = getAttachmentUrl(sa_fad, ids);
  288. for (Row row : rows) {
  289. Rows attRows = RowsMap.getOrDefault(row.getString("sa_fadid"), new Rows());
  290. attRows.sortby("sequence", "linksid");
  291. row.put("attinfos", attRows);
  292. row.put("appleturl", userInfo.getWechatAppPage("marketingtool", "FadUrl") + row.getString("sa_fadid"));
  293. row.putIfAbsent("onsaledate", "");
  294. }
  295. return getSucReturnObject().setData(rows).toString();
  296. }
  297. @API(title = "选择商品列表", apiversion = R.ID20240418141402.v1.class)
  298. public String chooseItemlist() throws YosException {
  299. StringBuffer where = new StringBuffer(" 1=1 ");
  300. if (content.containsKey("where")) {
  301. JSONObject whereObject = content.getJSONObject("where");
  302. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  303. where.append(" and(");
  304. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  305. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  306. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  307. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  308. where.append("or t1.color like'%").append(whereObject.getString("condition")).append("%' ");
  309. where.append(")");
  310. }
  311. }
  312. Long sa_fadid = content.getLongValue("sa_fadid");
  313. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "plm_item"
  314. , "itemid", "itemname", "itemno", "model", "spec", "color", "marketprice", "standards");
  315. querySQL.setTableAlias("t1");
  316. querySQL.addJoinTable(JOINTYPE.left, "plm_itemclass", "t2", "t2.itemclassid = t1.marketingcategory AND t2.siteid = t1.siteid",
  317. "itemclassname");
  318. querySQL.setWhere("t1.siteid", siteid);
  319. querySQL.setWhere(where.toString());
  320. querySQL.setWhere("not exists(select 1 from sa_fad_link where sa_fadid=" + sa_fadid + " and itemid=t1.itemid and siteid='" + siteid + "' )");
  321. querySQL.setOrderBy(pageSorting);
  322. querySQL.setPage(pageSize, pageNumber);
  323. Rows rows = querySQL.query();
  324. // 默认商品图片
  325. Rows defaultImageRows = getAttachmentUrl("system", (long) 1, "defaultImage");
  326. // 附件
  327. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  328. RowsMap attRowsMapCover = getAttachmentUrl("plm_item", ids, "cover");
  329. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  330. // 商品领域
  331. RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids);
  332. RowsMap itemclassRowsMap = ItemClass.getAllItemClassRowsMap(this, ids);
  333. for (Row row : rows) {
  334. if (attRowsMapCover.getOrDefault(row.getString("itemid"), new Rows()).isNotEmpty()) {
  335. row.put("attinfos", attRowsMapCover.getOrDefault(row.getString("itemid"), new Rows()));
  336. } else if (attRowsMap.getOrDefault(row.getString("itemid"), new Rows()).isNotEmpty()) {
  337. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  338. } else {
  339. row.put("attinfos", defaultImageRows);
  340. }
  341. String[] tradefield = tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()).toArray("tradefield");
  342. row.put("tradefield", StringUtils.join(tradefield, ","));
  343. String[] itemclass = itemclassRowsMap.getOrDefault(row.getString("itemid"), new Rows()).toArray("itemclassname");
  344. row.put("itemclass", StringUtils.join(itemclass, ","));
  345. }
  346. return getSucReturnObject().setData(rows).toString();
  347. }
  348. @API(title = "关联商品列表", apiversion = R.ID20240418141502.v1.class)
  349. public String relateItemlist() throws YosException {
  350. StringBuffer where = new StringBuffer(" 1=1 ");
  351. if (content.containsKey("where")) {
  352. JSONObject whereObject = content.getJSONObject("where");
  353. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  354. where.append(" and(");
  355. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  356. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  357. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  358. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  359. where.append("or t1.color like'%").append(whereObject.getString("condition")).append("%' ");
  360. where.append(")");
  361. }
  362. }
  363. Long sa_fadid = content.getLongValue("sa_fadid");
  364. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "plm_item"
  365. , "itemid", "itemname", "itemno", "model", "spec", "color", "marketprice", "standards");
  366. querySQL.setTableAlias("t1");
  367. querySQL.addJoinTable(JOINTYPE.left, "plm_itemclass", "t2", "t2.itemclassid = t1.marketingcategory AND t2.siteid = t1.siteid",
  368. "itemclassname");
  369. querySQL.addJoinTable(JOINTYPE.inner, "sa_fad_link", "t3", "t3.itemid = t1.itemid AND t3.siteid = t1.siteid",
  370. "sa_fad_linkid");
  371. querySQL.setWhere("t1.siteid", siteid);
  372. querySQL.setWhere(where.toString());
  373. querySQL.setWhere("t3.sa_fadid", sa_fadid);
  374. querySQL.setOrderBy(pageSorting);
  375. querySQL.setPage(pageSize, pageNumber);
  376. Rows rows = querySQL.query();
  377. // 默认商品图片
  378. Rows defaultImageRows = getAttachmentUrl("system", (long) 1, "defaultImage");
  379. // 附件
  380. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  381. RowsMap attRowsMapCover = getAttachmentUrl("plm_item", ids, "cover");
  382. RowsMap attRowsMap = getAttachmentUrl("plm_item", ids);
  383. // 商品领域
  384. RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids);
  385. RowsMap itemclassRowsMap = ItemClass.getAllItemClassRowsMap(this, ids);
  386. for (Row row : rows) {
  387. if (attRowsMapCover.getOrDefault(row.getString("itemid"), new Rows()).isNotEmpty()) {
  388. row.put("attinfos", attRowsMapCover.getOrDefault(row.getString("itemid"), new Rows()));
  389. } else if (attRowsMap.getOrDefault(row.getString("itemid"), new Rows()).isNotEmpty()) {
  390. row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  391. } else {
  392. row.put("attinfos", defaultImageRows);
  393. }
  394. String[] tradefield = tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()).toArray("tradefield");
  395. row.put("tradefield", StringUtils.join(tradefield, ","));
  396. String[] itemclass = itemclassRowsMap.getOrDefault(row.getString("itemid"), new Rows()).toArray("itemclassname");
  397. row.put("itemclass", StringUtils.join(itemclass, ","));
  398. }
  399. return getSucReturnObject().setData(rows).toString();
  400. }
  401. @API(title = "关联商品", apiversion = R.ID20240418141602.v1.class)
  402. public String relateItem() throws YosException {
  403. Long sa_fadid = content.getLongValue("sa_fadid");
  404. JSONArray itemids = content.getJSONArray("itemids");
  405. ArrayList<String> sqlList = new ArrayList<>();
  406. for (Object obj : itemids) {
  407. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_fad_link");
  408. insertSQL.setSiteid(siteid);
  409. insertSQL.setUniqueid(createTableID("sa_fad_link"));
  410. insertSQL.setValue("itemid", obj);
  411. insertSQL.setValue("sa_fadid", sa_fadid);
  412. sqlList.add(insertSQL.getSQL());
  413. Row row = dbConnect.runSqlQuery(0, "SELECT * from plm_item WHERE siteid='" + siteid + "' and itemid=" + obj);
  414. sqlList.add(DataContrlLog.createLog(this, "sa_fad", sa_fadid, "添加关联商品", "添加关联商品 " + row.getString("itemname")).getSQL());
  415. }
  416. dbConnect.runSqlUpdate(sqlList);
  417. return getSucReturnObject().toString();
  418. }
  419. @API(title = "删除商品", apiversion = R.ID20240418141702.v1.class)
  420. public String deleteItem() throws YosException {
  421. JSONArray sa_fad_linkids = content.getJSONArray("sa_fad_linkids");
  422. for (Object obj : sa_fad_linkids) {
  423. Row row = dbConnect.runSqlQuery(0, "SELECT * from sa_fad_link WHERE siteid='" + siteid + "' and sa_fad_linkid=" + obj);
  424. Long sa_fadid = row.getLong("sa_fadid");
  425. Long itemid = row.getLong("itemid");
  426. row = dbConnect.runSqlQuery(0, "SELECT * from plm_item WHERE siteid='" + siteid + "' and itemid=" + itemid);
  427. DataContrlLog.createLog(this, "sa_fad", sa_fadid, "删除关联商品", "删除关联商品 " + row.getString("itemname")).insert();
  428. }
  429. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "sa_fad_link");
  430. deleteSQL.setSiteid(siteid);
  431. deleteSQL.setWhere("sa_fad_linkid", sa_fad_linkids);
  432. deleteSQL.delete();
  433. return getSucReturnObject().toString();
  434. }
  435. public String getClassnames(JSONArray sa_fadclassids, boolean isMain) throws YosException {
  436. //[[1,2,3,6],[1,2]]
  437. RowsMap rowsMap = dbConnect.runSqlQuery("SELECT sa_fadclassid,classname from sa_fadclass WHERE siteid='" + siteid + "'").toRowsMap("sa_fadclassid");
  438. LinkedHashSet<String> classnames = new LinkedHashSet<>();
  439. for (Object object : sa_fadclassids) {
  440. ArrayList<String> temp = new ArrayList<>();
  441. if (object instanceof JSONArray) {
  442. JSONArray array = (JSONArray) object;
  443. for (Object obj : array) {
  444. temp.add(rowsMap.get(obj.toString()).get(0).getString("classname"));
  445. }
  446. }
  447. if (isMain) {
  448. classnames.add(temp.get(0));
  449. } else {
  450. classnames.add(String.join("-", temp));
  451. }
  452. }
  453. return String.join(";", classnames);
  454. }
  455. }