Fad.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package restcontroller.webmanage.saletool.fad;
  2. import beans.attachment.Attachment;
  3. import beans.time.Time;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.Controller;
  7. import common.YosException;
  8. import common.annotation.API;
  9. import common.data.*;
  10. import org.apache.commons.lang.StringUtils;
  11. import restcontroller.R;
  12. import java.util.ArrayList;
  13. import java.util.LinkedHashSet;
  14. /**
  15. * 单品管理
  16. */
  17. public class Fad extends Controller {
  18. /**
  19. * 构造函数
  20. *
  21. * @param content
  22. */
  23. public Fad(JSONObject content) throws YosException {
  24. super(content);
  25. }
  26. String sa_fad = "sa_fad";
  27. @API(title = "单品新增或编辑", apiversion = R.ID20240418140902.v1.class)
  28. public String insertOrUpdate() throws YosException {
  29. Long sa_fadid = content.getLongValue("sa_fadid");
  30. Long itemid = content.getLongValue("itemid");
  31. int isonsale = content.getIntValue("isonsale");
  32. JSONArray sa_fadclassids = new JSONArray();
  33. if (content.containsKey("sa_fadclassids")) {
  34. sa_fadclassids = content.getJSONArray("sa_fadclassids");
  35. for (Object obj : sa_fadclassids) {
  36. if (!(obj instanceof JSONArray)) {
  37. return getErrReturnObject().setErrMsg("分类格式不正确").toString();
  38. }
  39. }
  40. }
  41. if (sa_fadid <= 0) {
  42. sa_fadid = createTableID(sa_fad);
  43. InsertSQL sqlFactory = SQLFactory.createInsertSQL(this, sa_fad);
  44. sqlFactory.setSiteid(siteid);
  45. sqlFactory.setUniqueid(sa_fadid);
  46. sqlFactory.setValue("name", content.getStringValue("name"));
  47. sqlFactory.setValue("model", content.getStringValue("model"));
  48. sqlFactory.setValue("spec", content.getStringValue("spec"));
  49. sqlFactory.setValue("candownload", content.getBooleanValue("candownload"));
  50. sqlFactory.setValue("outurl", content.getStringValue("outurl", true));
  51. sqlFactory.setValue("isnew", content.getBooleanValue("isnew"));
  52. sqlFactory.setValue("sequence", content.getLongValue("sequence"));
  53. sqlFactory.setValue("content", content.getStringValue("contentstr", true));
  54. sqlFactory.setValue("price", content.getBigDecimalValue("price"));
  55. sqlFactory.setValue("offsaledate", content.getStringValue("offsaledate", true, "null"));
  56. sqlFactory.setValue("sa_fadclassids", sa_fadclassids);
  57. sqlFactory.insert();
  58. content.put("sa_fadid", sa_fadid);
  59. } else {
  60. UpdateSQL sqlFactory = SQLFactory.createUpdateSQL(this, sa_fad);
  61. sqlFactory.setUniqueid(sa_fadid);
  62. sqlFactory.setSiteid(siteid);
  63. sqlFactory.setValue("name", content.getStringValue("name"));
  64. sqlFactory.setValue("model", content.getStringValue("model"));
  65. sqlFactory.setValue("spec", content.getStringValue("spec"));
  66. sqlFactory.setValue("candownload", content.getBooleanValue("candownload"));
  67. sqlFactory.setValue("outurl", content.getStringValue("outurl", true));
  68. sqlFactory.setValue("isnew", content.getBooleanValue("isnew"));
  69. sqlFactory.setValue("sequence", content.getLongValue("sequence"));
  70. sqlFactory.setValue("content", content.getStringValue("contentstr", true));
  71. sqlFactory.setValue("price", content.getBigDecimalValue("price"));
  72. sqlFactory.setValue("offsaledate", content.getStringValue("offsaledate", true, "null"));
  73. sqlFactory.setValue("sa_fadclassids", sa_fadclassids);
  74. sqlFactory.update();
  75. }
  76. //上下架
  77. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sa_fad);
  78. updateSQL.setSiteid(siteid);
  79. updateSQL.setWhere("sa_fadid", sa_fadid);
  80. updateSQL.setValue("isonsale", isonsale);
  81. updateSQL.setValue("onsaledate", isonsale == 1 ? Time.getDateTime_Str() : "null");
  82. updateSQL.setValue("onsaleby", isonsale == 1 ? username : "null");
  83. updateSQL.update();
  84. //关联商品
  85. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_fad_link");
  86. insertSQL.setSiteid(siteid);
  87. insertSQL.setUniqueid(createTableID("sa_fad_link"));
  88. insertSQL.setValue("itemid", itemid);
  89. insertSQL.setValue("sa_fadid", sa_fadid);
  90. insertSQL.setWhere("not exists(select 1 from sa_fad_link where itemid=" + itemid + " and sa_fadid=" + sa_fadid + " )");
  91. insertSQL.insert();
  92. //插入图片
  93. Rows rows = Attachment.get(this, "plm_item", itemid, "cover");
  94. if (rows.isEmpty()) {
  95. rows = Attachment.get(this, "plm_item", itemid, "default");
  96. }
  97. if (rows.isNotEmpty()) {
  98. insertSQL = SQLFactory.createInsertSQL(this, "sys_attachment_links");
  99. insertSQL.setSiteid(siteid);
  100. insertSQL.setUniqueid(createTableID("sys_attachment_links"));
  101. insertSQL.setValue("usetype", sa_fad);
  102. insertSQL.setValue("attachmentid", rows.get(0).getLong("attachmentid"));
  103. insertSQL.setValue("sequence", 1);
  104. insertSQL.setValue("ownertable", sa_fad);
  105. insertSQL.setValue("ownerid", sa_fadid);
  106. 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 + " )");
  107. insertSQL.insert();
  108. }
  109. return detail();
  110. }
  111. @API(title = "单品详情", apiversion = R.ID20240418141002.v1.class)
  112. public String detail() throws YosException {
  113. Long sa_fadid = content.getLongValue("sa_fadid");
  114. String ownertable = sa_fad;
  115. QuerySQL querySQ = SQLFactory.createQuerySQL(this, sa_fad)
  116. .setTableAlias("t1");
  117. querySQ.addJoinTable(JOINTYPE.left, "sys_datacollect", "t2", "t2.siteid=t1.siteid and t2.ownertable='sa_fad' and type=1 and t2.ownerid=t1.sa_fadid and t2.userid='" + userid + "'");
  118. querySQ.addQueryFields("iscollect", "CASE WHEN t2.sys_datacollectid>0 THEN 1 ELSE 0 END");
  119. querySQ.setSiteid(siteid);
  120. querySQ.setWhere("sa_fadid", sa_fadid);
  121. Rows rows = querySQ.query();
  122. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  123. Rows attRows = getAttachmentUrl(sa_fad, sa_fadid);
  124. detailRow.put("attinfos", attRows);
  125. detailRow.put("appleturl", "xxx/pages/product/ctw/share?id=" + sa_fadid);
  126. QuerySQL attachmentQuery = SQLFactory.createQuerySQL(this, "sys_attachment_links").setTableAlias("t1");
  127. attachmentQuery.setSiteid(detailRow.getString("siteid"));
  128. attachmentQuery.setWhere("ownertable", ownertable);
  129. attachmentQuery.setWhere("ownerid", sa_fadid);
  130. attachmentQuery.setWhere("usetype", ownertable);
  131. attachmentQuery.setOrderBy("t1.sequence");
  132. Rows attachmentRows = attachmentQuery.query();
  133. ArrayList<Long> ids = attachmentRows.toArrayList("attachmentid", new ArrayList<>());
  134. RowsMap attRowsMap = Attachment.get(this, ids).toRowsMap("attachmentid");
  135. for (Row row : attachmentRows) {
  136. Rows attPicRows = new Rows();
  137. Rows tempAttRows = attRowsMap.getOrDefault(row.getString("attachmentid"), new Rows());
  138. for (Row tempAttRow : tempAttRows) {
  139. if (tempAttRow.getString("usetype").equals(ownertable)) {
  140. attPicRows.add(tempAttRow);
  141. }
  142. }
  143. row.put("attinfos", attPicRows);
  144. }
  145. detailRow.put("attinfos_pic", attachmentRows);
  146. detailRow.putIfAbsent("offsaledate", "");
  147. detailRow.putIfAbsent("onsaledate", "");
  148. detailRow.put("classnames",getClassnames(detailRow.getJSONArray("sa_fadclassids")));
  149. return getSucReturnObject().setData(detailRow).toString();
  150. }
  151. @API(title = "单品删除", apiversion = R.ID20240418141102.v1.class)
  152. public String delete() throws YosException {
  153. JSONArray sa_fadids = content.getJSONArray("sa_fadids");
  154. if (sa_fadids.size() == 0) {
  155. return getErrReturnObject().setErrMsg("请选择要删除的数据").toString();
  156. }
  157. DeleteSQL sqlFactory = SQLFactory.createDeleteSQL(this, sa_fad);
  158. sqlFactory.setSiteid(siteid);
  159. sqlFactory.setWhere("sa_fadid", sa_fadids.toArray());
  160. sqlFactory.delete();
  161. return getSucReturnObject().toString();
  162. }
  163. @API(title = "单品上下架", apiversion = R.ID20240418141202.v1.class)
  164. public String isonsale() throws YosException {
  165. JSONArray sa_fadids = content.getJSONArray("sa_fadids");
  166. int isonsale = content.getIntValue("isonsale");
  167. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sa_fad);
  168. updateSQL.setSiteid(siteid);
  169. updateSQL.setWhere("sa_fadid", sa_fadids);
  170. updateSQL.setValue("isonsale", isonsale);
  171. updateSQL.setValue("onsaledate", isonsale == 0 ? "null" : Time.getDateTime_Str());
  172. updateSQL.setValue("onsaleby", isonsale == 0 ? "null" : username);
  173. updateSQL.update();
  174. return getSucReturnObject().toString();
  175. }
  176. @API(title = "单品列表", apiversion = R.ID20240418141302.v1.class)
  177. public String list() throws YosException {
  178. /*
  179. 过滤条件设置
  180. */
  181. StringBuffer where = new StringBuffer(" 1=1 ");
  182. if (content.containsKey("where")) {
  183. JSONObject whereObject = content.getJSONObject("where");
  184. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  185. where.append(" and (");
  186. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  187. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  188. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  189. where.append(")");
  190. }
  191. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  192. where.append(" and (");
  193. where.append("t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  194. where.append(")");
  195. }
  196. if (whereObject.containsKey("begindate_create") && !"".equals(whereObject.getString("begindate_create"))) {
  197. where.append(" and (");
  198. where.append("t1.createdate >='").append(whereObject.getString("begindate_create")).append("' ");
  199. where.append(")");
  200. }
  201. if (whereObject.containsKey("enddate_create") && !"".equals(whereObject.getString("enddate_create"))) {
  202. where.append(" and (");
  203. where.append("t1.createdate <='").append(whereObject.getString("enddate_create")).append(" 23:59:59' ");
  204. where.append(")");
  205. }
  206. if (whereObject.containsKey("begindate_onsale") && !"".equals(whereObject.getString("begindate_onsale"))) {
  207. where.append(" and (");
  208. where.append("t1.onsaledate >='").append(whereObject.getString("begindate_onsale")).append("' ");
  209. where.append(")");
  210. }
  211. if (whereObject.containsKey("enddate_onsale") && !"".equals(whereObject.getString("enddate_onsale"))) {
  212. where.append(" and (");
  213. where.append("t1.onsaledate <='").append(whereObject.getString("enddate_onsale")).append(" 23:59:59' ");
  214. where.append(")");
  215. }
  216. if (whereObject.containsKey("sa_fadclassids") && !"".equals(whereObject.getString("sa_fadclassids"))) {
  217. JSONArray sa_fadclassids = whereObject.getJSONArray("sa_fadclassids");
  218. if (sa_fadclassids.size() > 0) {
  219. where.append(" and (1=2");
  220. for (Object obj : sa_fadclassids) {
  221. JSONArray array = (JSONArray) obj;
  222. for (Object obj2 : array) {
  223. where.append(" or (");
  224. where.append("JSON_CONTAINS(t1.sa_fadclassids,'" + obj2 + "')");
  225. where.append(")");
  226. }
  227. }
  228. where.append(")");
  229. }
  230. }
  231. }
  232. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sa_fad,
  233. "sa_fadid", "name", "isonsale", "model", "spec", "price", "sequence", "createby", "createdate", "onsaledate")
  234. .setTableAlias("t1");
  235. querySQL.setSiteid(siteid);
  236. querySQL.setWhere(where.toString());
  237. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  238. Rows rows = querySQL.query();
  239. ArrayList<Long> ids = rows.toArrayList("sa_fadid", new ArrayList<>());
  240. // 附件
  241. RowsMap RowsMap = getAttachmentUrl(sa_fad, ids);
  242. for (Row row : rows) {
  243. Rows Rows = RowsMap.getOrDefault(row.getString("sa_fadid"), new Rows());
  244. row.put("attinfos", Rows);
  245. row.put("appleturl", "xxx/pages/product/ctw/share?id=" + row.getString("sa_fadid"));
  246. row.putIfAbsent("onsaledate", "");
  247. }
  248. return getSucReturnObject().setData(rows).toString();
  249. }
  250. @API(title = "选择商品列表", apiversion = R.ID20240418141402.v1.class)
  251. public String chooseItemlist() {
  252. return getSucReturnObject().toString();
  253. }
  254. @API(title = "关联商品列表", apiversion = R.ID20240418141502.v1.class)
  255. public String relateItemlist() {
  256. return getSucReturnObject().toString();
  257. }
  258. @API(title = "关联商品", apiversion = R.ID20240418141602.v1.class)
  259. public String relateItem() {
  260. return getSucReturnObject().toString();
  261. }
  262. @API(title = "删除商品", apiversion = R.ID20240418141702.v1.class)
  263. public String deleteItem() {
  264. return getSucReturnObject().toString();
  265. }
  266. public String getClassnames( JSONArray sa_fadclassids) throws YosException {
  267. //[[1,2,3,6],[1,2]]
  268. RowsMap rowsMap = dbConnect.runSqlQuery("SELECT sa_fadclassid,classname from sa_fadclass WHERE siteid='" + siteid + "'").toRowsMap("sa_fadclassid");
  269. ArrayList<String> classnames = new ArrayList<>();
  270. for (Object object : sa_fadclassids) {
  271. ArrayList<String> temp = new ArrayList<>();
  272. if (object instanceof JSONArray) {
  273. JSONArray array = (JSONArray) object;
  274. for (Object obj : array) {
  275. temp.add(rowsMap.get(obj.toString()).get(0).getString("classname"));
  276. }
  277. }
  278. classnames.add(String.join("-", temp));
  279. }
  280. return String.join(";", classnames);
  281. }
  282. }