module_shop_act_duplicate.go 3.8 KB

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