FadGoods.java 19 KB

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