module_shop_act_duplicate.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package modules
  2. import (
  3. "context"
  4. "time"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. func (this *Modules) RegisterAction_ShopDuplicate() *Action {
  9. return this.newAction(AInfo{
  10. Mount: "shop-duplicate",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. pf_attach := utils.Trim(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(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  21. // Block rows
  22. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  23. return err
  24. }
  25. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  26. return err
  27. }
  28. if _, err := tx.Exec(ctx, "SELECT product_id FROM fave_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. ctx,
  38. `INSERT INTO fave_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. fave_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(ctx, "SELECT id FROM fave_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. ctx,
  91. `SELECT
  92. product_id,
  93. category_id
  94. FROM
  95. fave_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 fave_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. ctx,
  122. `SELECT
  123. product_id,
  124. filter_value_id
  125. FROM
  126. fave_shop_filter_product_values
  127. WHERE
  128. product_id = ?
  129. ;`,
  130. utils.StrToInt(pf_id),
  131. ); err == nil {
  132. defer attributes_rows.Close()
  133. for attributes_rows.Next() {
  134. var product_id int
  135. var filter_value_id int
  136. if err := attributes_rows.Scan(&product_id, &filter_value_id); *wrap.LogCpError(&err) == nil {
  137. attributes_sqls = append(attributes_sqls, `
  138. INSERT INTO fave_shop_filter_product_values SET
  139. product_id = `+utils.Int64ToStr(lastID)+`,
  140. filter_value_id = `+utils.IntToStr(filter_value_id)+`
  141. ;
  142. `)
  143. }
  144. }
  145. }
  146. for _, sql_query := range attributes_sqls {
  147. tx.Exec(ctx, sql_query)
  148. }
  149. return nil
  150. }); err != nil {
  151. wrap.MsgError(err.Error())
  152. return
  153. }
  154. // Navigate to new product page
  155. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  156. })
  157. }