module_shop_act_modify.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package modules
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "golang-fave/engine/utils"
  7. "golang-fave/engine/wrapper"
  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 fave_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 fave_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. ctx,
  149. `SELECT
  150. COUNT(*)
  151. FROM
  152. fave_shop_cats
  153. WHERE
  154. id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
  155. FOR UPDATE;`,
  156. ).Scan(
  157. &catsCount,
  158. )
  159. if *wrap.LogCpError(&err) != nil {
  160. return err
  161. }
  162. if len(catids) != catsCount {
  163. return errors.New("Inner system error")
  164. }
  165. var bulkInsertArr []string
  166. for _, el := range catids {
  167. bulkInsertArr = append(bulkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  168. }
  169. if _, err = tx.Exec(
  170. ctx,
  171. `INSERT INTO fave_shop_cat_product_rel (product_id,category_id) VALUES `+strings.Join(bulkInsertArr, ",")+`;`,
  172. ); err != nil {
  173. return err
  174. }
  175. }
  176. // Insert product and filter values relations
  177. for vid, _ := range filter_values {
  178. if _, err = tx.Exec(
  179. ctx,
  180. `INSERT INTO fave_shop_filter_product_values SET
  181. product_id = ?,
  182. filter_value_id = ?
  183. ;`,
  184. lastID,
  185. vid,
  186. ); err != nil {
  187. return err
  188. }
  189. }
  190. return nil
  191. }); err != nil {
  192. wrap.MsgError(err.Error())
  193. return
  194. }
  195. wrap.RecreateProductXmlFile()
  196. wrap.ResetCacheBlocks()
  197. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  198. } else {
  199. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  200. // Block rows
  201. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  202. return err
  203. }
  204. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_currency)); err != nil {
  205. return err
  206. }
  207. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  208. return err
  209. }
  210. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  211. return err
  212. }
  213. // Update row
  214. if _, err := tx.Exec(
  215. ctx,
  216. `UPDATE fave_shop_products SET
  217. currency = ?,
  218. price = ?,
  219. price_old = ?,
  220. price_promo = ?,
  221. gname = ?,
  222. name = ?,
  223. alias = ?,
  224. vendor = ?,
  225. quantity = ?,
  226. category = ?,
  227. briefly = ?,
  228. content = ?,
  229. active = ?
  230. WHERE
  231. id = ?
  232. ;`,
  233. utils.StrToInt(pf_currency),
  234. utils.StrToFloat64(pf_price),
  235. utils.StrToFloat64(pf_price_old),
  236. utils.StrToFloat64(pf_price_promo),
  237. pf_gname,
  238. pf_name,
  239. pf_alias,
  240. pf_vendor,
  241. utils.StrToInt(pf_quantity),
  242. utils.StrToInt(pf_category),
  243. pf_briefly,
  244. pf_content,
  245. utils.StrToInt(pf_active),
  246. utils.StrToInt(pf_id),
  247. ); err != nil {
  248. return err
  249. }
  250. // Update custom field 1
  251. if _, ok := wrap.R.Form["custom1"]; ok {
  252. if _, err := tx.Exec(
  253. ctx,
  254. `UPDATE fave_shop_products SET
  255. custom1 = ?
  256. WHERE
  257. id = ?
  258. ;`,
  259. pf_custom1,
  260. utils.StrToInt(pf_id),
  261. ); err != nil {
  262. return err
  263. }
  264. }
  265. // Update custom field 2
  266. if _, ok := wrap.R.Form["custom2"]; ok {
  267. if _, err := tx.Exec(
  268. ctx,
  269. `UPDATE fave_shop_products SET
  270. custom2 = ?
  271. WHERE
  272. id = ?
  273. ;`,
  274. pf_custom2,
  275. utils.StrToInt(pf_id),
  276. ); err != nil {
  277. return err
  278. }
  279. }
  280. // Delete product and categories relations
  281. if _, err := tx.Exec(ctx, "DELETE FROM fave_shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  282. return err
  283. }
  284. // Insert product and categories relations
  285. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  286. if len(catids) > 0 {
  287. var catsCount int
  288. err := tx.QueryRow(
  289. ctx,
  290. `SELECT
  291. COUNT(*)
  292. FROM
  293. fave_shop_cats
  294. WHERE
  295. id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
  296. FOR UPDATE;`,
  297. ).Scan(
  298. &catsCount,
  299. )
  300. if *wrap.LogCpError(&err) != nil {
  301. return err
  302. }
  303. if len(catids) != catsCount {
  304. return errors.New("Inner system error")
  305. }
  306. var bulkInsertArr []string
  307. for _, el := range catids {
  308. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  309. }
  310. if _, err := tx.Exec(
  311. ctx,
  312. `INSERT INTO fave_shop_cat_product_rel (product_id,category_id) VALUES `+strings.Join(bulkInsertArr, ",")+`;`,
  313. ); err != nil {
  314. return err
  315. }
  316. }
  317. // Delete product and filter values relations
  318. if _, err := tx.Exec(
  319. ctx,
  320. `DELETE FROM fave_shop_filter_product_values WHERE product_id = ?;`,
  321. utils.StrToInt(pf_id),
  322. ); err != nil {
  323. return err
  324. }
  325. // Insert product and filter values relations
  326. for vid, _ := range filter_values {
  327. if _, err := tx.Exec(
  328. ctx,
  329. `INSERT INTO fave_shop_filter_product_values SET
  330. product_id = ?,
  331. filter_value_id = ?
  332. ;`,
  333. utils.StrToInt(pf_id),
  334. vid,
  335. ); err != nil {
  336. return err
  337. }
  338. }
  339. return nil
  340. }); err != nil {
  341. wrap.MsgError(err.Error())
  342. return
  343. }
  344. wrap.RecreateProductXmlFile()
  345. wrap.ResetCacheBlocks()
  346. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  347. }
  348. })
  349. }