module_shop_act_duplicate.go 3.9 KB

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