module_shop_act_modify.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package modules
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "golang-fave/engine/wrapper"
  7. "golang-fave/utils"
  8. )
  9. func (this *Modules) RegisterAction_ShopModify() *Action {
  10. return this.newAction(AInfo{
  11. WantDB: true,
  12. Mount: "shop-modify",
  13. WantAdmin: true,
  14. }, func(wrap *wrapper.Wrapper) {
  15. pf_id := utils.Trim(wrap.R.FormValue("id"))
  16. pf_gname := utils.Trim(wrap.R.FormValue("gname"))
  17. pf_name := utils.Trim(wrap.R.FormValue("name"))
  18. pf_price := utils.Trim(wrap.R.FormValue("price"))
  19. pf_price_old := utils.Trim(wrap.R.FormValue("price_old"))
  20. pf_price_promo := utils.Trim(wrap.R.FormValue("price_promo"))
  21. pf_currency := utils.Trim(wrap.R.FormValue("currency"))
  22. pf_alias := utils.Trim(wrap.R.FormValue("alias"))
  23. pf_vendor := utils.Trim(wrap.R.FormValue("vendor"))
  24. pf_quantity := utils.Trim(wrap.R.FormValue("quantity"))
  25. pf_category := utils.Trim(wrap.R.FormValue("category"))
  26. pf_briefly := utils.Trim(wrap.R.FormValue("briefly"))
  27. pf_content := utils.Trim(wrap.R.FormValue("content"))
  28. pf_active := utils.Trim(wrap.R.FormValue("active"))
  29. pf_custom1 := utils.Trim(wrap.R.FormValue("custom1"))
  30. pf_custom2 := utils.Trim(wrap.R.FormValue("custom2"))
  31. if pf_active == "" {
  32. pf_active = "0"
  33. }
  34. if !utils.IsNumeric(pf_id) {
  35. wrap.MsgError(`Inner system error`)
  36. return
  37. }
  38. if !utils.IsFloat(pf_price) {
  39. wrap.MsgError(`Inner system error`)
  40. return
  41. }
  42. if !utils.IsFloat(pf_price_old) {
  43. wrap.MsgError(`Inner system error`)
  44. return
  45. }
  46. if !utils.IsFloat(pf_price_promo) {
  47. wrap.MsgError(`Inner system error`)
  48. return
  49. }
  50. if !utils.IsNumeric(pf_currency) {
  51. wrap.MsgError(`Inner system error`)
  52. return
  53. }
  54. if !utils.IsNumeric(pf_quantity) {
  55. wrap.MsgError(`Inner system error`)
  56. return
  57. }
  58. if !utils.IsNumeric(pf_category) {
  59. wrap.MsgError(`Inner system error`)
  60. return
  61. }
  62. if pf_name == "" {
  63. wrap.MsgError(`Please specify product name`)
  64. return
  65. }
  66. if pf_alias == "" {
  67. pf_alias = utils.GenerateSingleAlias(pf_name)
  68. }
  69. if !utils.IsValidSingleAlias(pf_alias) {
  70. wrap.MsgError(`Please specify correct product alias`)
  71. return
  72. }
  73. // Default is ROOT
  74. if pf_category == "0" {
  75. pf_category = "1"
  76. }
  77. // Collect fields and data for filter values
  78. filter_values := map[int]int{}
  79. for key, values := range wrap.R.PostForm {
  80. if len(key) > 6 && key[0:6] == "value." {
  81. for _, value := range values {
  82. if value != "" {
  83. filter_values[utils.StrToInt(value)] = utils.StrToInt(key[6:])
  84. }
  85. }
  86. }
  87. }
  88. if pf_id == "0" {
  89. var lastID int64 = 0
  90. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  91. // Insert row
  92. res, err := tx.Exec(
  93. ctx,
  94. `INSERT INTO shop_products SET
  95. user = ?,
  96. currency = ?,
  97. price = ?,
  98. price_old = ?,
  99. price_promo = ?,
  100. gname = ?,
  101. name = ?,
  102. alias = ?,
  103. vendor = ?,
  104. quantity = ?,
  105. category = ?,
  106. briefly = ?,
  107. content = ?,
  108. datetime = ?,
  109. active = ?,
  110. custom1 = ?,
  111. custom2 = ?
  112. ;`,
  113. wrap.User.A_id,
  114. utils.StrToInt(pf_currency),
  115. utils.StrToFloat64(pf_price),
  116. utils.StrToFloat64(pf_price_old),
  117. utils.StrToFloat64(pf_price_promo),
  118. pf_gname,
  119. pf_name,
  120. pf_alias,
  121. pf_vendor,
  122. utils.StrToInt(pf_quantity),
  123. utils.StrToInt(pf_category),
  124. pf_briefly,
  125. pf_content,
  126. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  127. utils.StrToInt(pf_active),
  128. pf_custom1,
  129. pf_custom2,
  130. )
  131. if err != nil {
  132. return err
  133. }
  134. // Get inserted product id
  135. lastID, err = res.LastInsertId()
  136. if err != nil {
  137. return err
  138. }
  139. // Block rows
  140. if _, err := tx.Exec(ctx, "SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
  141. return err
  142. }
  143. // Insert product and categories relations
  144. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  145. if len(catids) > 0 {
  146. var catsCount int
  147. err = tx.QueryRow(`
  148. SELECT
  149. COUNT(*)
  150. FROM
  151. shop_cats
  152. WHERE
  153. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  154. FOR UPDATE;`,
  155. ).Scan(
  156. &catsCount,
  157. )
  158. if *wrap.LogCpError(&err) != nil {
  159. return err
  160. }
  161. if len(catids) != catsCount {
  162. return errors.New("Inner system error")
  163. }
  164. var bulkInsertArr []string
  165. for _, el := range catids {
  166. bulkInsertArr = append(bulkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  167. }
  168. if _, err = tx.Exec(
  169. ctx,
  170. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES `+strings.Join(bulkInsertArr, ",")+`;`,
  171. ); err != nil {
  172. return err
  173. }
  174. }
  175. // Insert product and filter values relations
  176. for vid, _ := range filter_values {
  177. if _, err = tx.Exec(
  178. ctx,
  179. `INSERT INTO shop_filter_product_values SET
  180. product_id = ?,
  181. filter_value_id = ?
  182. ;`,
  183. lastID,
  184. vid,
  185. ); err != nil {
  186. return err
  187. }
  188. }
  189. return nil
  190. }); err != nil {
  191. wrap.MsgError(err.Error())
  192. return
  193. }
  194. wrap.RecreateProductXmlFile()
  195. wrap.ResetCacheBlocks()
  196. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  197. } else {
  198. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  199. // Block rows
  200. if _, err := tx.Exec(ctx, "SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  201. return err
  202. }
  203. if _, err := tx.Exec(ctx, "SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_currency)); err != nil {
  204. return err
  205. }
  206. if _, err := tx.Exec(ctx, "SELECT product_id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  207. return err
  208. }
  209. if _, err := tx.Exec(ctx, "SELECT product_id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  210. return err
  211. }
  212. // Update row
  213. if _, err := tx.Exec(
  214. ctx,
  215. `UPDATE shop_products SET
  216. currency = ?,
  217. price = ?,
  218. price_old = ?,
  219. price_promo = ?,
  220. gname = ?,
  221. name = ?,
  222. alias = ?,
  223. vendor = ?,
  224. quantity = ?,
  225. category = ?,
  226. briefly = ?,
  227. content = ?,
  228. active = ?
  229. WHERE
  230. id = ?
  231. ;`,
  232. utils.StrToInt(pf_currency),
  233. utils.StrToFloat64(pf_price),
  234. utils.StrToFloat64(pf_price_old),
  235. utils.StrToFloat64(pf_price_promo),
  236. pf_gname,
  237. pf_name,
  238. pf_alias,
  239. pf_vendor,
  240. utils.StrToInt(pf_quantity),
  241. utils.StrToInt(pf_category),
  242. pf_briefly,
  243. pf_content,
  244. utils.StrToInt(pf_active),
  245. utils.StrToInt(pf_id),
  246. ); err != nil {
  247. return err
  248. }
  249. // Update custom field 1
  250. if _, ok := wrap.R.Form["custom1"]; ok {
  251. if _, err := tx.Exec(
  252. ctx,
  253. `UPDATE shop_products SET
  254. custom1 = ?
  255. WHERE
  256. id = ?
  257. ;`,
  258. pf_custom1,
  259. utils.StrToInt(pf_id),
  260. ); err != nil {
  261. return err
  262. }
  263. }
  264. // Update custom field 2
  265. if _, ok := wrap.R.Form["custom2"]; ok {
  266. if _, err := tx.Exec(
  267. ctx,
  268. `UPDATE shop_products SET
  269. custom2 = ?
  270. WHERE
  271. id = ?
  272. ;`,
  273. pf_custom2,
  274. utils.StrToInt(pf_id),
  275. ); err != nil {
  276. return err
  277. }
  278. }
  279. // Delete product and categories relations
  280. if _, err := tx.Exec(ctx, "DELETE FROM shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  281. return err
  282. }
  283. // Insert product and categories relations
  284. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  285. if len(catids) > 0 {
  286. var catsCount int
  287. err := tx.QueryRow(`
  288. SELECT
  289. COUNT(*)
  290. FROM
  291. shop_cats
  292. WHERE
  293. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  294. FOR UPDATE;`,
  295. ).Scan(
  296. &catsCount,
  297. )
  298. if *wrap.LogCpError(&err) != nil {
  299. return err
  300. }
  301. if len(catids) != catsCount {
  302. return errors.New("Inner system error")
  303. }
  304. var bulkInsertArr []string
  305. for _, el := range catids {
  306. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  307. }
  308. if _, err := tx.Exec(
  309. ctx,
  310. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES `+strings.Join(bulkInsertArr, ",")+`;`,
  311. ); err != nil {
  312. return err
  313. }
  314. }
  315. // Delete product and filter values relations
  316. if _, err := tx.Exec(
  317. ctx,
  318. `DELETE FROM shop_filter_product_values WHERE product_id = ?;`,
  319. utils.StrToInt(pf_id),
  320. ); err != nil {
  321. return err
  322. }
  323. // Insert product and filter values relations
  324. for vid, _ := range filter_values {
  325. if _, err := tx.Exec(
  326. ctx,
  327. `INSERT INTO shop_filter_product_values SET
  328. product_id = ?,
  329. filter_value_id = ?
  330. ;`,
  331. utils.StrToInt(pf_id),
  332. vid,
  333. ); err != nil {
  334. return err
  335. }
  336. }
  337. return nil
  338. }); err != nil {
  339. wrap.MsgError(err.Error())
  340. return
  341. }
  342. wrap.RecreateProductXmlFile()
  343. wrap.ResetCacheBlocks()
  344. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  345. }
  346. })
  347. }