module_shop_act_modify.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. Mount: "shop-modify",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_id := utils.Trim(wrap.R.FormValue("id"))
  15. pf_gname := utils.Trim(wrap.R.FormValue("gname"))
  16. pf_name := utils.Trim(wrap.R.FormValue("name"))
  17. pf_price := utils.Trim(wrap.R.FormValue("price"))
  18. pf_price_old := utils.Trim(wrap.R.FormValue("price_old"))
  19. pf_price_promo := utils.Trim(wrap.R.FormValue("price_promo"))
  20. pf_currency := utils.Trim(wrap.R.FormValue("currency"))
  21. pf_alias := utils.Trim(wrap.R.FormValue("alias"))
  22. pf_vendor := utils.Trim(wrap.R.FormValue("vendor"))
  23. pf_quantity := utils.Trim(wrap.R.FormValue("quantity"))
  24. pf_category := utils.Trim(wrap.R.FormValue("category"))
  25. pf_briefly := utils.Trim(wrap.R.FormValue("briefly"))
  26. pf_content := utils.Trim(wrap.R.FormValue("content"))
  27. pf_active := utils.Trim(wrap.R.FormValue("active"))
  28. pf_custom1 := utils.Trim(wrap.R.FormValue("custom1"))
  29. pf_custom2 := utils.Trim(wrap.R.FormValue("custom2"))
  30. if pf_active == "" {
  31. pf_active = "0"
  32. }
  33. if !utils.IsNumeric(pf_id) {
  34. wrap.MsgError(`Inner system error`)
  35. return
  36. }
  37. if !utils.IsFloat(pf_price) {
  38. wrap.MsgError(`Inner system error`)
  39. return
  40. }
  41. if !utils.IsFloat(pf_price_old) {
  42. wrap.MsgError(`Inner system error`)
  43. return
  44. }
  45. if !utils.IsFloat(pf_price_promo) {
  46. wrap.MsgError(`Inner system error`)
  47. return
  48. }
  49. if !utils.IsNumeric(pf_currency) {
  50. wrap.MsgError(`Inner system error`)
  51. return
  52. }
  53. if !utils.IsNumeric(pf_quantity) {
  54. wrap.MsgError(`Inner system error`)
  55. return
  56. }
  57. if !utils.IsNumeric(pf_category) {
  58. wrap.MsgError(`Inner system error`)
  59. return
  60. }
  61. if pf_name == "" {
  62. wrap.MsgError(`Please specify product name`)
  63. return
  64. }
  65. if pf_alias == "" {
  66. pf_alias = utils.GenerateSingleAlias(pf_name)
  67. }
  68. if !utils.IsValidSingleAlias(pf_alias) {
  69. wrap.MsgError(`Please specify correct product alias`)
  70. return
  71. }
  72. // Default is ROOT
  73. if pf_category == "0" {
  74. pf_category = "1"
  75. }
  76. // Collect fields and data for filter values
  77. filter_values := map[int]int{}
  78. for key, values := range wrap.R.PostForm {
  79. if len(key) > 6 && key[0:6] == "value." {
  80. for _, value := range values {
  81. if value != "" {
  82. filter_values[utils.StrToInt(value)] = utils.StrToInt(key[6:])
  83. }
  84. }
  85. }
  86. }
  87. if pf_id == "0" {
  88. var lastID int64 = 0
  89. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  90. // Insert row
  91. res, err := tx.Exec(
  92. ctx,
  93. `INSERT INTO fave_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(ctx, "SELECT id FROM fave_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. ctx,
  148. `SELECT
  149. COUNT(*)
  150. FROM
  151. fave_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 fave_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 fave_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 fave_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 fave_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 fave_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 fave_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 fave_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 fave_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 fave_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 fave_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. ctx,
  289. `SELECT
  290. COUNT(*)
  291. FROM
  292. fave_shop_cats
  293. WHERE
  294. id IN(`+strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",")+`)
  295. FOR UPDATE;`,
  296. ).Scan(
  297. &catsCount,
  298. )
  299. if *wrap.LogCpError(&err) != nil {
  300. return err
  301. }
  302. if len(catids) != catsCount {
  303. return errors.New("Inner system error")
  304. }
  305. var bulkInsertArr []string
  306. for _, el := range catids {
  307. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  308. }
  309. if _, err := tx.Exec(
  310. ctx,
  311. `INSERT INTO fave_shop_cat_product_rel (product_id,category_id) VALUES `+strings.Join(bulkInsertArr, ",")+`;`,
  312. ); err != nil {
  313. return err
  314. }
  315. }
  316. // Delete product and filter values relations
  317. if _, err := tx.Exec(
  318. ctx,
  319. `DELETE FROM fave_shop_filter_product_values WHERE product_id = ?;`,
  320. utils.StrToInt(pf_id),
  321. ); err != nil {
  322. return err
  323. }
  324. // Insert product and filter values relations
  325. for vid, _ := range filter_values {
  326. if _, err := tx.Exec(
  327. ctx,
  328. `INSERT INTO fave_shop_filter_product_values SET
  329. product_id = ?,
  330. filter_value_id = ?
  331. ;`,
  332. utils.StrToInt(pf_id),
  333. vid,
  334. ); err != nil {
  335. return err
  336. }
  337. }
  338. return nil
  339. }); err != nil {
  340. wrap.MsgError(err.Error())
  341. return
  342. }
  343. wrap.RecreateProductXmlFile()
  344. wrap.ResetCacheBlocks()
  345. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  346. }
  347. })
  348. }