module_shop_act_duplicate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. parent_id,
  34. user,
  35. currency,
  36. price,
  37. name,
  38. alias,
  39. vendor,
  40. quantity,
  41. category,
  42. briefly,
  43. content,
  44. datetime,
  45. active
  46. ) SELECT
  47. parent_id,
  48. user,
  49. currency,
  50. price,
  51. CONCAT(name, ' (Copy)'),
  52. CONCAT(REGEXP_REPLACE(alias, '-c[0-9]+$', ''), '-c', '`+utils.Int64ToStr(time.Now().Unix())+`'),
  53. vendor,
  54. quantity,
  55. category,
  56. briefly,
  57. content,
  58. '`+utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp())+`',
  59. 0
  60. FROM
  61. shop_products
  62. WHERE
  63. id = ?
  64. ;`,
  65. utils.StrToInt(pf_id),
  66. )
  67. if err != nil {
  68. return err
  69. }
  70. // Get inserted product id
  71. lastID, err = res.LastInsertId()
  72. if err != nil {
  73. return err
  74. }
  75. // Block new product row
  76. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
  77. return err
  78. }
  79. // Duplicate categories
  80. cat_sqls := []string{}
  81. if cat_rows, err := tx.Query(
  82. `SELECT
  83. product_id,
  84. category_id
  85. FROM
  86. shop_cat_product_rel
  87. WHERE
  88. product_id = ?
  89. ;`,
  90. utils.StrToInt(pf_id),
  91. ); err == nil {
  92. defer cat_rows.Close()
  93. for cat_rows.Next() {
  94. var product_id int
  95. var category_id int
  96. if err := cat_rows.Scan(&product_id, &category_id); *wrap.LogCpError(&err) == nil {
  97. cat_sqls = append(cat_sqls, `
  98. INSERT INTO shop_cat_product_rel SET
  99. product_id = `+utils.Int64ToStr(lastID)+`,
  100. category_id = `+utils.IntToStr(category_id)+`
  101. ;
  102. `)
  103. }
  104. }
  105. }
  106. for _, sql_query := range cat_sqls {
  107. tx.Exec(sql_query)
  108. }
  109. // Duplicate attributes
  110. attributes_sqls := []string{}
  111. if attributes_rows, err := tx.Query(
  112. `SELECT
  113. product_id,
  114. filter_value_id
  115. FROM
  116. shop_filter_product_values
  117. WHERE
  118. product_id = ?
  119. ;`,
  120. utils.StrToInt(pf_id),
  121. ); err == nil {
  122. defer attributes_rows.Close()
  123. for attributes_rows.Next() {
  124. var product_id int
  125. var filter_value_id int
  126. if err := attributes_rows.Scan(&product_id, &filter_value_id); *wrap.LogCpError(&err) == nil {
  127. attributes_sqls = append(attributes_sqls, `
  128. INSERT INTO shop_filter_product_values SET
  129. product_id = `+utils.Int64ToStr(lastID)+`,
  130. filter_value_id = `+utils.IntToStr(filter_value_id)+`
  131. ;
  132. `)
  133. }
  134. }
  135. }
  136. for _, sql_query := range attributes_sqls {
  137. tx.Exec(sql_query)
  138. }
  139. return nil
  140. }); err != nil {
  141. wrap.MsgError(err.Error())
  142. return
  143. }
  144. // Navigate to new product page
  145. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  146. })
  147. }