FadGoods.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package restcontroller.webmanage.saletool.fad;
  2. import beans.attachment.Attachment;
  3. import beans.datatag.DataTag;
  4. import beans.time.Time;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import common.Controller;
  8. import common.YosException;
  9. import common.annotation.API;
  10. import common.data.*;
  11. import org.apache.commons.lang.StringUtils;
  12. import restcontroller.R;
  13. import restcontroller.system.dataauth.DataAuthHelper;
  14. import java.util.ArrayList;
  15. /**
  16. * 商品管理
  17. */
  18. public class FadGoods extends Controller {
  19. /**
  20. * 构造函数
  21. *
  22. * @param content
  23. */
  24. public FadGoods(JSONObject content) throws YosException {
  25. super(content);
  26. }
  27. String sa_fad = "sa_fad";
  28. @API(title = "商品新增或编辑", apiversion = R.ID20240428154102.v1.class)
  29. public String insertOrUpdate() throws YosException {
  30. Long sa_fadid = content.getLongValue("sa_fadid");
  31. boolean canadjust = content.getBooleanValue("canadjust");
  32. String tag = content.getStringValue("tag");
  33. Long parentid = 0L;
  34. if (content.containsKey("sys_enterpriseid") && content.getLongValue("sys_enterpriseid") != 0) {
  35. sys_enterpriseid = content.getLongValue("sys_enterpriseid");
  36. }
  37. ArrayList<String> listSQL = new ArrayList<>();
  38. ArrayList<Long> sa_fadids = new ArrayList<>();
  39. Rows fadRows = dbConnect.runSqlQuery("SELECT * from sa_fad WHERE sa_fadid=" + sa_fadid + " and siteid='" + siteid + "'");
  40. if (sa_fadid <= 0 || fadRows.isEmpty()) {
  41. sa_fadid = createTableID(sa_fad);
  42. InsertSQL sqlFactory = FadHelper.getFadInsertSQL(this, sa_fadid, parentid);
  43. listSQL.add(sqlFactory.getSQL());
  44. content.put("sa_fadid", sa_fadid);
  45. } else {
  46. boolean temp_canadjust = fadRows.get(0).getBoolean("canadjust");
  47. long temp_sys_enterpriseid = fadRows.get(0).getLong("sys_enterpriseid");
  48. //允许经销商调整,数据是总部的,修改方为总部
  49. if (temp_canadjust && temp_sys_enterpriseid == 0 && sys_enterpriseid == 0) {
  50. UpdateSQL sqlFactory = FadHelper.getFadUpdateSQL(this, sa_fadid);
  51. listSQL.add(sqlFactory.getSQL());
  52. //更新parentid为sa_fadid的数据
  53. Rows rows = dbConnect.runSqlQuery("SELECT sa_fadid from sa_fad WHERE parentid=" + sa_fadid + " and siteid='" + siteid + "'");
  54. //修改项为禁止经销商修改
  55. if (!canadjust) {
  56. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "sa_fad");
  57. deleteSQL.setSiteid(siteid);
  58. deleteSQL.setWhere("parentid", sa_fadid);
  59. deleteSQL.delete();
  60. } else {
  61. sa_fadids = rows.toArrayList("sa_fadid", new ArrayList<>());
  62. for (Row row : rows) {
  63. sqlFactory = FadHelper.getFadUpdateSQL(this, row.getLong("sa_fadid"));
  64. listSQL.add(sqlFactory.getSQL());
  65. }
  66. }
  67. }
  68. //允许经销商调整,数据是总部的,修改方为经销商
  69. else if (temp_canadjust && temp_sys_enterpriseid == 0 && sys_enterpriseid > 0) {
  70. parentid = sa_fadid;
  71. sa_fadid = createTableID(sa_fad);
  72. InsertSQL sqlFactory = FadHelper.getFadInsertSQL(this, sa_fadid, parentid);
  73. listSQL.add(sqlFactory.getSQL());
  74. content.put("sa_fadid", sa_fadid);
  75. //修改
  76. content.put("ownertable", "sa_fad");
  77. content.put("ownerid", sa_fadid);
  78. Long sys_dataauthid = createTableID("sys_dataauth");
  79. listSQL.add(DataAuthHelper.getDataAuthInsertSQL(this, sys_dataauthid).getSQL());
  80. listSQL.add(DataAuthHelper.getDataAuthsInsertSQL(this, "sys_enterpriseid", sys_enterpriseid).getSQL());
  81. } else {
  82. UpdateSQL sqlFactory = FadHelper.getFadUpdateSQL(this, sa_fadid);
  83. listSQL.add(sqlFactory.getSQL());
  84. }
  85. }
  86. sa_fadids.add(sa_fadid);
  87. for (Long id : sa_fadids) {
  88. listSQL.add(FadHelper.getIsOnSalesUpdateSQL(this, id).getSQL());
  89. }
  90. //上下架
  91. dbConnect.runSqlUpdate(listSQL);
  92. for (Long id : sa_fadids) {
  93. DataTag.deleteTag(this, "sa_fad", id);
  94. DataTag.createSystemTag(this, "sa_fad", id, tag);
  95. }
  96. return detail();
  97. }
  98. @API(title = "商品详情", apiversion = R.ID20240428154202.v1.class)
  99. public String detail() throws YosException {
  100. Long sa_fadid = content.getLongValue("sa_fadid");
  101. String ownertable = sa_fad;
  102. Rows fadrows = dbConnect.runSqlQuery("select * from sa_fad where sa_fadid=" + sa_fadid);
  103. if (fadrows.isNotEmpty()) {
  104. siteid = fadrows.get(0).getString("siteid");
  105. }
  106. dbConnect.runSqlUpdate("UPDATE sa_fad SET readcount=readcount+1 WHERE sa_fadid=" + sa_fadid);
  107. QuerySQL querySQ = SQLFactory.createQuerySQL(this, sa_fad)
  108. .setTableAlias("t1");
  109. 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 + "'");
  110. querySQ.addQueryFields("iscollect", "CASE WHEN t2.sys_datacollectid>0 THEN 1 ELSE 0 END");
  111. querySQ.setSiteid(siteid);
  112. querySQ.setWhere("sa_fadid", sa_fadid);
  113. Rows rows = querySQ.query();
  114. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  115. Rows attRows = getAttachmentUrl(sa_fad, sa_fadid);
  116. attRows.sortby("sequence", "linksid");
  117. detailRow.put("attinfos", attRows);
  118. detailRow.put("appleturl", FadHelper.getAppletUrl(this, "FadGoodsUrl", "商品小程序链接") + sa_fadid);
  119. QuerySQL attachmentQuery = SQLFactory.createQuerySQL(this, "sys_attachment_links").setTableAlias("t1");
  120. attachmentQuery.setSiteid(detailRow.getString("siteid"));
  121. attachmentQuery.setWhere("ownertable", ownertable);
  122. attachmentQuery.setWhere("ownerid", sa_fadid);
  123. attachmentQuery.setWhere("usetype", ownertable);
  124. attachmentQuery.setOrderBy("t1.sequence,t1.linksid");
  125. Rows attachmentRows = attachmentQuery.query();
  126. ArrayList<Long> ids = attachmentRows.toArrayList("attachmentid", new ArrayList<>());
  127. RowsMap attRowsMap = Attachment.get(this, ids).toRowsMap("attachmentid");
  128. for (Row row : attachmentRows) {
  129. Rows attPicRows = new Rows();
  130. Rows tempAttRows = attRowsMap.getOrDefault(row.getString("attachmentid"), new Rows());
  131. for (Row tempAttRow : tempAttRows) {
  132. if (tempAttRow.getString("usetype").equals(ownertable)) {
  133. attPicRows.add(tempAttRow);
  134. }
  135. }
  136. row.put("attinfos", attPicRows);
  137. }
  138. detailRow.put("attinfos_pic", attachmentRows);
  139. detailRow.putIfAbsent("onsaledate", "");
  140. ArrayList<String> tags = DataTag.queryTag(this, "sa_fad", detailRow.getLong("sa_fadid"), true);
  141. detailRow.putIfAbsent("tag", StringUtils.join(tags, ","));
  142. detailRow.putIfAbsent("classnames", StringUtils.join(detailRow.getJSONArray("class"), ","));
  143. //查询官方信息:价格。。。
  144. Long parentid = detailRow.getLong("parentid");
  145. if (parentid == 0) {
  146. parentid = sa_fadid;
  147. }
  148. Rows officalrows = dbConnect.runSqlQuery("SELECT sa_fadid,price,price_store,price_rebate,price_deposit,price_original from sa_fad WHERE siteid='" + siteid + "' and sa_fadid=" + parentid);
  149. Row officalrow = officalrows.isNotEmpty() ? officalrows.get(0) : new Row();
  150. ArrayList<String> officaltags = DataTag.queryTag(this, "sa_fad", parentid, true);
  151. officalrow.putIfAbsent("tag", StringUtils.join(officaltags, ","));
  152. detailRow.put("officialinfo", officalrow);
  153. return getSucReturnObject().setData(detailRow).toString();
  154. }
  155. @API(title = "商品删除", apiversion = R.ID20240428154302.v1.class)
  156. public String delete() throws YosException {
  157. JSONArray sa_fadids = content.getJSONArray("sa_fadids");
  158. if (sa_fadids.size() == 0) {
  159. return getErrReturnObject().setErrMsg("请选择要删除的数据").toString();
  160. }
  161. DeleteSQL sqlFactory = SQLFactory.createDeleteSQL(this, sa_fad);
  162. sqlFactory.setSiteid(siteid);
  163. sqlFactory.setWhere("sa_fadid", sa_fadids.toArray());
  164. sqlFactory.delete();
  165. return getSucReturnObject().toString();
  166. }
  167. @API(title = "商品上下架", apiversion = R.ID20240428154402.v1.class)
  168. public String isonsale() throws YosException {
  169. JSONArray sa_fadids = content.getJSONArray("sa_fadids");
  170. int isonsale = content.getIntValue("isonsale");
  171. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sa_fad);
  172. updateSQL.setSiteid(siteid);
  173. updateSQL.setWhere("sa_fadid", sa_fadids);
  174. updateSQL.setValue("isonsale", isonsale);
  175. updateSQL.setValue("onsaledate", isonsale == 0 ? "null" : Time.getDateTime_Str());
  176. updateSQL.setValue("onsaleby", isonsale == 0 ? "null" : username);
  177. updateSQL.update();
  178. return getSucReturnObject().toString();
  179. }
  180. @API(title = "商品列表", apiversion = R.ID20240428154002.v1.class)
  181. public String list() throws YosException {
  182. /*
  183. 过滤条件设置
  184. */
  185. StringBuffer where = new StringBuffer(" 1=1 ");
  186. if (content.containsKey("where")) {
  187. JSONObject whereObject = content.getJSONObject("where");
  188. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  189. where.append(" and (");
  190. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  191. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  192. where.append("or t2.tag like'%").append(whereObject.getString("condition")).append("%' ");
  193. where.append(")");
  194. }
  195. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  196. where.append(" and (");
  197. where.append("t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  198. where.append(")");
  199. }
  200. if (whereObject.containsKey("canadjust") && !"".equals(whereObject.getString("canadjust"))) {
  201. where.append(" and (");
  202. where.append("t1.canadjust ='").append(whereObject.getString("canadjust")).append("' ");
  203. where.append(")");
  204. }
  205. if (whereObject.containsKey("begindate_create") && !"".equals(whereObject.getString("begindate_create"))) {
  206. where.append(" and (");
  207. where.append("t1.createdate >='").append(whereObject.getString("begindate_create")).append("' ");
  208. where.append(")");
  209. }
  210. if (whereObject.containsKey("enddate_create") && !"".equals(whereObject.getString("enddate_create"))) {
  211. where.append(" and (");
  212. where.append("t1.createdate <='").append(whereObject.getString("enddate_create")).append(" 23:59:59' ");
  213. where.append(")");
  214. }
  215. if (whereObject.containsKey("begindate_onsale") && !"".equals(whereObject.getString("begindate_onsale"))) {
  216. where.append(" and (");
  217. where.append("t1.onsaledate >='").append(whereObject.getString("begindate_onsale")).append("' ");
  218. where.append(")");
  219. }
  220. if (whereObject.containsKey("enddate_onsale") && !"".equals(whereObject.getString("enddate_onsale"))) {
  221. where.append(" and (");
  222. where.append("t1.onsaledate <='").append(whereObject.getString("enddate_onsale")).append(" 23:59:59' ");
  223. where.append(")");
  224. }
  225. if (whereObject.containsKey("class") && !"".equals(whereObject.getString("class"))) {
  226. where.append(" and (");
  227. where.append("JSON_CONTAINS(t1.class,'\"" + whereObject.getString("class") + "\"')");
  228. where.append(")");
  229. }
  230. }
  231. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sa_fad,
  232. "sa_fadid", "name", "isonsale", "canadjust", "model", "pricetype", "sequence", "createby", "createdate", "onsaledate", "class")
  233. .setTableAlias("t1");
  234. querySQL.addJoinTable(JOINTYPE.left, "sys_datatag", "t2", "t2.ownertable='sa_fad' and t2.ownerid=t1.sa_fadid and t2.siteid=t1.siteid");
  235. querySQL.setSiteid(siteid);
  236. querySQL.setWhere("classid", 2);
  237. querySQL.setWhere(where.toString());
  238. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  239. Rows rows = querySQL.query();
  240. ArrayList<Long> ids = rows.toArrayList("sa_fadid", new ArrayList<>());
  241. // 附件
  242. RowsMap RowsMap = getAttachmentUrl(sa_fad, ids);
  243. for (Row row : rows) {
  244. Rows attRows = RowsMap.getOrDefault(row.getString("sa_fadid"), new Rows());
  245. attRows.sortby("sequence", "linksid");
  246. row.put("attinfos", attRows);
  247. row.put("appleturl", FadHelper.getAppletUrl(this, "FadGoodsUrl", "商品小程序链接") + row.getString("sa_fadid"));
  248. row.putIfAbsent("onsaledate", "");
  249. ArrayList<String> tags = DataTag.queryTag(this, "sa_fad", row.getLong("sa_fadid"), true);
  250. row.putIfAbsent("tag", StringUtils.join(tags, ","));
  251. row.putIfAbsent("classnames", StringUtils.join(row.getJSONArray("class"), ","));
  252. }
  253. return getSucReturnObject().setData(rows).toString();
  254. }
  255. @API(title = "获取门店商品列表", apiversion = R.ID20240430094102.v1.class)
  256. public String getStoreList() throws YosException {
  257. /*
  258. 过滤条件设置
  259. */
  260. StringBuffer where = new StringBuffer(" 1=1 ");
  261. if (content.containsKey("where")) {
  262. JSONObject whereObject = content.getJSONObject("where");
  263. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  264. where.append(" and (");
  265. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  266. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  267. where.append("or t2.tag like'%").append(whereObject.getString("condition")).append("%' ");
  268. where.append(")");
  269. }
  270. if (whereObject.containsKey("isnew") && !"".equals(whereObject.getString("isnew"))) {
  271. where.append(" and (");
  272. where.append("t1.isnew ='").append(whereObject.getString("isnew")).append("' ");
  273. where.append(")");
  274. }
  275. if (whereObject.containsKey("class") && !"".equals(whereObject.getString("class"))) {
  276. where.append(" and (");
  277. where.append("JSON_CONTAINS(t1.class,'\"" + whereObject.getString("class") + "\"')");
  278. where.append(")");
  279. }
  280. if (whereObject.containsKey("iscollect") && !"".equals(whereObject.getString("iscollect"))) {
  281. if (whereObject.getString("iscollect").equals("1")) {
  282. where.append(" and (");
  283. where.append(" t1.sa_fadid in (SELECT ownerid from sys_datacollect WHERE ownertable='sa_fad' and type=1 and siteid='" + siteid + "' and userid=" + userid + ")");
  284. where.append(")");
  285. }
  286. }
  287. }
  288. ArrayList<Long> sys_enterpriseids = new ArrayList<Long>();
  289. Long sys_enterpriseid = content.getLongValue("sys_enterpriseid");
  290. if (sys_enterpriseid == -1) {
  291. return getSucReturnObject().setData(new Rows()).toString();
  292. }
  293. sys_enterpriseids.add(sys_enterpriseid);
  294. sys_enterpriseids.add(0L);
  295. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sa_fad,
  296. "sa_fadid", "name", "pricetype", "price", "price_deposit", "unitname")
  297. .setTableAlias("t1");
  298. querySQL.addJoinTable(JOINTYPE.left, "sys_datatag", "t2", "t2.ownertable='sa_fad' and t2.ownerid=t1.sa_fadid and t2.siteid=t1.siteid");
  299. querySQL.setSiteid(siteid);
  300. querySQL.setWhere("not EXISTS(SELECT 1 from sa_fad WHERE sys_enterpriseid=" + sys_enterpriseid + " and parentid=t1.sa_fadid and siteid=t1.siteid)");
  301. querySQL.setWhere("sys_enterpriseid", sys_enterpriseids);
  302. querySQL.setWhere("classid", 2);
  303. querySQL.setWhere("isonsale", 1);
  304. querySQL.setWhere(where.toString());
  305. querySQL.setDataAuth(true);
  306. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  307. Rows rows = querySQL.query();
  308. ArrayList<Long> ids = rows.toArrayList("sa_fadid", new ArrayList<>());
  309. // 附件
  310. RowsMap RowsMap = getAttachmentUrl(sa_fad, ids);
  311. for (Row row : rows) {
  312. Rows attRows = RowsMap.getOrDefault(row.getString("sa_fadid"), new Rows());
  313. attRows.sortby("sequence", "linksid");
  314. row.put("attinfos", attRows);
  315. ArrayList<String> tags = DataTag.queryTag(this, "sa_fad", row.getLong("sa_fadid"), true);
  316. row.putIfAbsent("tag", StringUtils.join(tags, ","));
  317. }
  318. return getSucReturnObject().setData(rows).toString();
  319. }
  320. @API(title = "获取门店调整商品列表", apiversion = R.ID20240515153202.v1.class)
  321. public String getAdjustStoreList() throws YosException {
  322. /*
  323. 过滤条件设置
  324. */
  325. StringBuffer where = new StringBuffer(" 1=1 ");
  326. if (content.containsKey("where")) {
  327. JSONObject whereObject = content.getJSONObject("where");
  328. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  329. where.append(" and (");
  330. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  331. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  332. where.append("or t2.tag like'%").append(whereObject.getString("condition")).append("%' ");
  333. where.append(")");
  334. }
  335. if (whereObject.containsKey("class") && !"".equals(whereObject.getString("class"))) {
  336. where.append(" and (");
  337. where.append("JSON_CONTAINS(t1.class,'\"" + whereObject.getString("class") + "\"')");
  338. where.append(")");
  339. }
  340. }
  341. ArrayList<Long> sys_enterpriseids = new ArrayList<Long>();
  342. Long sys_enterpriseid = content.getLongValue("sys_enterpriseid");
  343. if (sys_enterpriseid == -1) {
  344. return getSucReturnObject().setData(new Rows()).toString();
  345. }
  346. sys_enterpriseids.add(sys_enterpriseid);
  347. sys_enterpriseids.add(0L);
  348. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sa_fad,
  349. "sa_fadid", "name", "pricetype", "price", "price_deposit", "unitname")
  350. .setTableAlias("t1");
  351. querySQL.addJoinTable(JOINTYPE.left, "sys_datatag", "t2", "t2.ownertable='sa_fad' and t2.ownerid=t1.sa_fadid and t2.siteid=t1.siteid");
  352. querySQL.setSiteid(siteid);
  353. querySQL.setWhere("not EXISTS(SELECT 1 from sa_fad WHERE sys_enterpriseid=" + sys_enterpriseid + " and parentid=t1.sa_fadid and siteid=t1.siteid)");
  354. querySQL.setWhere("sys_enterpriseid", sys_enterpriseids);
  355. querySQL.setWhere("classid", 2);
  356. querySQL.setWhere("isonsale", 1);
  357. querySQL.setWhere("canadjust", 1);
  358. querySQL.setWhere(where.toString());
  359. querySQL.setDataAuth(true);
  360. querySQL.setOrderBy(pageSorting).setPage(pageSize, pageNumber);
  361. Rows rows = querySQL.query();
  362. ArrayList<Long> ids = rows.toArrayList("sa_fadid", new ArrayList<>());
  363. // 附件
  364. RowsMap RowsMap = getAttachmentUrl(sa_fad, ids);
  365. for (Row row : rows) {
  366. Rows attRows = RowsMap.getOrDefault(row.getString("sa_fadid"), new Rows());
  367. attRows.sortby("sequence", "linksid");
  368. row.put("attinfos", attRows);
  369. }
  370. return getSucReturnObject().setData(rows).toString();
  371. }
  372. }