module_shop_act_duplicate.go 3.9 KB

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