module_shop_act_modify.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. `INSERT INTO shop_products SET
  94. user = ?,
  95. currency = ?,
  96. price = ?,
  97. price_old = ?,
  98. price_promo = ?,
  99. gname = ?,
  100. name = ?,
  101. alias = ?,
  102. vendor = ?,
  103. quantity = ?,
  104. category = ?,
  105. briefly = ?,
  106. content = ?,
  107. datetime = ?,
  108. active = ?,
  109. custom1 = ?,
  110. custom2 = ?
  111. ;`,
  112. wrap.User.A_id,
  113. utils.StrToInt(pf_currency),
  114. utils.StrToFloat64(pf_price),
  115. utils.StrToFloat64(pf_price_old),
  116. utils.StrToFloat64(pf_price_promo),
  117. pf_gname,
  118. pf_name,
  119. pf_alias,
  120. pf_vendor,
  121. utils.StrToInt(pf_quantity),
  122. utils.StrToInt(pf_category),
  123. pf_briefly,
  124. pf_content,
  125. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  126. utils.StrToInt(pf_active),
  127. pf_custom1,
  128. pf_custom2,
  129. )
  130. if err != nil {
  131. return err
  132. }
  133. // Get inserted product id
  134. lastID, err = res.LastInsertId()
  135. if err != nil {
  136. return err
  137. }
  138. // Block rows
  139. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
  140. return err
  141. }
  142. // Insert product and categories relations
  143. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  144. if len(catids) > 0 {
  145. var catsCount int
  146. err = tx.QueryRow(`
  147. SELECT
  148. COUNT(*)
  149. FROM
  150. shop_cats
  151. WHERE
  152. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  153. FOR UPDATE;`,
  154. ).Scan(
  155. &catsCount,
  156. )
  157. if *wrap.LogCpError(&err) != nil {
  158. return err
  159. }
  160. if len(catids) != catsCount {
  161. return errors.New("Inner system error")
  162. }
  163. var bulkInsertArr []string
  164. for _, el := range catids {
  165. bulkInsertArr = append(bulkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  166. }
  167. if _, err = tx.Exec(
  168. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  169. ); err != nil {
  170. return err
  171. }
  172. }
  173. // Insert product and filter values relations
  174. for vid, _ := range filter_values {
  175. if _, err = tx.Exec(
  176. `INSERT INTO shop_filter_product_values SET
  177. product_id = ?,
  178. filter_value_id = ?
  179. ;`,
  180. lastID,
  181. vid,
  182. ); err != nil {
  183. return err
  184. }
  185. }
  186. return nil
  187. }); err != nil {
  188. wrap.MsgError(err.Error())
  189. return
  190. }
  191. wrap.RecreateProductXmlFile()
  192. wrap.ResetCacheBlocks()
  193. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  194. } else {
  195. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  196. // Block rows
  197. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  198. return err
  199. }
  200. if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_currency)); err != nil {
  201. return err
  202. }
  203. if _, err := tx.Exec("SELECT product_id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  204. return err
  205. }
  206. if _, err := tx.Exec("SELECT product_id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  207. return err
  208. }
  209. // Update row
  210. if _, err := tx.Exec(
  211. `UPDATE shop_products SET
  212. currency = ?,
  213. price = ?,
  214. price_old = ?,
  215. price_promo = ?,
  216. gname = ?,
  217. name = ?,
  218. alias = ?,
  219. vendor = ?,
  220. quantity = ?,
  221. category = ?,
  222. briefly = ?,
  223. content = ?,
  224. active = ?
  225. WHERE
  226. id = ?
  227. ;`,
  228. utils.StrToInt(pf_currency),
  229. utils.StrToFloat64(pf_price),
  230. utils.StrToFloat64(pf_price_old),
  231. utils.StrToFloat64(pf_price_promo),
  232. pf_gname,
  233. pf_name,
  234. pf_alias,
  235. pf_vendor,
  236. utils.StrToInt(pf_quantity),
  237. utils.StrToInt(pf_category),
  238. pf_briefly,
  239. pf_content,
  240. utils.StrToInt(pf_active),
  241. utils.StrToInt(pf_id),
  242. ); err != nil {
  243. return err
  244. }
  245. // Update custom field 1
  246. if _, ok := wrap.R.Form["custom1"]; ok {
  247. if _, err := tx.Exec(
  248. `UPDATE shop_products SET
  249. custom1 = ?
  250. WHERE
  251. id = ?
  252. ;`,
  253. pf_custom1,
  254. utils.StrToInt(pf_id),
  255. ); err != nil {
  256. return err
  257. }
  258. }
  259. // Update custom field 2
  260. if _, ok := wrap.R.Form["custom2"]; ok {
  261. if _, err := tx.Exec(
  262. `UPDATE shop_products SET
  263. custom2 = ?
  264. WHERE
  265. id = ?
  266. ;`,
  267. pf_custom2,
  268. utils.StrToInt(pf_id),
  269. ); err != nil {
  270. return err
  271. }
  272. }
  273. // Delete product and categories relations
  274. if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  275. return err
  276. }
  277. // Insert product and categories relations
  278. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  279. if len(catids) > 0 {
  280. var catsCount int
  281. err := tx.QueryRow(`
  282. SELECT
  283. COUNT(*)
  284. FROM
  285. shop_cats
  286. WHERE
  287. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  288. FOR UPDATE;`,
  289. ).Scan(
  290. &catsCount,
  291. )
  292. if *wrap.LogCpError(&err) != nil {
  293. return err
  294. }
  295. if len(catids) != catsCount {
  296. return errors.New("Inner system error")
  297. }
  298. var bulkInsertArr []string
  299. for _, el := range catids {
  300. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  301. }
  302. if _, err := tx.Exec(
  303. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  304. ); err != nil {
  305. return err
  306. }
  307. }
  308. // Delete product and filter values relations
  309. if _, err := tx.Exec(
  310. `DELETE FROM shop_filter_product_values WHERE product_id = ?;`,
  311. utils.StrToInt(pf_id),
  312. ); err != nil {
  313. return err
  314. }
  315. // Insert product and filter values relations
  316. for vid, _ := range filter_values {
  317. if _, err := tx.Exec(
  318. `INSERT INTO shop_filter_product_values SET
  319. product_id = ?,
  320. filter_value_id = ?
  321. ;`,
  322. utils.StrToInt(pf_id),
  323. vid,
  324. ); err != nil {
  325. return err
  326. }
  327. }
  328. return nil
  329. }); err != nil {
  330. wrap.MsgError(err.Error())
  331. return
  332. }
  333. wrap.RecreateProductXmlFile()
  334. wrap.ResetCacheBlocks()
  335. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  336. }
  337. })
  338. }