module_shop_act_duplicate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package modules
  2. import (
  3. "time"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_ShopDuplicate() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "shop-duplicate",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := wrap.R.FormValue("id")
  14. if !utils.IsNumeric(pf_id) {
  15. wrap.MsgError(`Inner system error`)
  16. return
  17. }
  18. var lastID int64 = 0
  19. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  20. // Block rows
  21. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  22. return err
  23. }
  24. if _, err := tx.Exec("SELECT product_id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  25. return err
  26. }
  27. if _, err := tx.Exec("SELECT product_id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  28. return err
  29. }
  30. // Duplicate product
  31. res, err := tx.Exec(
  32. `INSERT INTO shop_products (
  33. user,
  34. currency,
  35. price,
  36. name,
  37. alias,
  38. vendor,
  39. quantity,
  40. category,
  41. briefly,
  42. content,
  43. datetime,
  44. active
  45. ) SELECT
  46. user,
  47. currency,
  48. price,
  49. CONCAT(name, ' (Copy)'),
  50. CONCAT(REGEXP_REPLACE(alias, '-c[0-9]+$', ''), '-c', '`+utils.Int64ToStr(time.Now().Unix())+`'),
  51. vendor,
  52. quantity,
  53. category,
  54. briefly,
  55. content,
  56. '`+utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp())+`',
  57. 0
  58. FROM
  59. shop_products
  60. WHERE
  61. id = ?
  62. ;`,
  63. utils.StrToInt(pf_id),
  64. )
  65. if err != nil {
  66. return err
  67. }
  68. // Get inserted product id
  69. lastID, err = res.LastInsertId()
  70. if err != nil {
  71. return err
  72. }
  73. // Block new product row
  74. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
  75. return err
  76. }
  77. // Duplicate categories
  78. cat_sqls := []string{}
  79. if cat_rows, err := tx.Query(
  80. `SELECT
  81. product_id,
  82. category_id
  83. FROM
  84. shop_cat_product_rel
  85. WHERE
  86. product_id = ?
  87. ;`,
  88. utils.StrToInt(pf_id),
  89. ); err == nil {
  90. defer cat_rows.Close()
  91. for cat_rows.Next() {
  92. var product_id int
  93. var category_id int
  94. if err := cat_rows.Scan(&product_id, &category_id); err == nil {
  95. cat_sqls = append(cat_sqls, `
  96. INSERT INTO shop_cat_product_rel SET
  97. product_id = `+utils.Int64ToStr(lastID)+`,
  98. category_id = `+utils.IntToStr(category_id)+`
  99. ;
  100. `)
  101. }
  102. }
  103. }
  104. for _, sql_query := range cat_sqls {
  105. tx.Exec(sql_query)
  106. }
  107. // Duplicate attributes
  108. attributes_sqls := []string{}
  109. if attributes_rows, err := tx.Query(
  110. `SELECT
  111. product_id,
  112. filter_value_id
  113. FROM
  114. shop_filter_product_values
  115. WHERE
  116. product_id = ?
  117. ;`,
  118. utils.StrToInt(pf_id),
  119. ); err == nil {
  120. defer attributes_rows.Close()
  121. for attributes_rows.Next() {
  122. var product_id int
  123. var filter_value_id int
  124. if err := attributes_rows.Scan(&product_id, &filter_value_id); err == nil {
  125. attributes_sqls = append(attributes_sqls, `
  126. INSERT INTO shop_filter_product_values SET
  127. product_id = `+utils.Int64ToStr(lastID)+`,
  128. filter_value_id = `+utils.IntToStr(filter_value_id)+`
  129. ;
  130. `)
  131. }
  132. }
  133. }
  134. for _, sql_query := range attributes_sqls {
  135. tx.Exec(sql_query)
  136. }
  137. return nil
  138. }); err != nil {
  139. wrap.MsgError(err.Error())
  140. return
  141. }
  142. // Navigate to new product page
  143. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  144. })
  145. }