module_shop_act_duplicate.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 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 fave_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 fave_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 fave_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 fave_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. fave_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 fave_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. ctx,
  92. `SELECT
  93. product_id,
  94. category_id
  95. FROM
  96. fave_shop_cat_product_rel
  97. WHERE
  98. product_id = ?
  99. ;`,
  100. utils.StrToInt(pf_id),
  101. ); err == nil {
  102. defer cat_rows.Close()
  103. for cat_rows.Next() {
  104. var product_id int
  105. var category_id int
  106. if err := cat_rows.Scan(&product_id, &category_id); *wrap.LogCpError(&err) == nil {
  107. cat_sqls = append(cat_sqls, `
  108. INSERT INTO fave_shop_cat_product_rel SET
  109. product_id = `+utils.Int64ToStr(lastID)+`,
  110. category_id = `+utils.IntToStr(category_id)+`
  111. ;
  112. `)
  113. }
  114. }
  115. }
  116. for _, sql_query := range cat_sqls {
  117. tx.Exec(ctx, sql_query)
  118. }
  119. // Duplicate attributes
  120. attributes_sqls := []string{}
  121. if attributes_rows, err := tx.Query(
  122. ctx,
  123. `SELECT
  124. product_id,
  125. filter_value_id
  126. FROM
  127. fave_shop_filter_product_values
  128. WHERE
  129. product_id = ?
  130. ;`,
  131. utils.StrToInt(pf_id),
  132. ); err == nil {
  133. defer attributes_rows.Close()
  134. for attributes_rows.Next() {
  135. var product_id int
  136. var filter_value_id int
  137. if err := attributes_rows.Scan(&product_id, &filter_value_id); *wrap.LogCpError(&err) == nil {
  138. attributes_sqls = append(attributes_sqls, `
  139. INSERT INTO fave_shop_filter_product_values SET
  140. product_id = `+utils.Int64ToStr(lastID)+`,
  141. filter_value_id = `+utils.IntToStr(filter_value_id)+`
  142. ;
  143. `)
  144. }
  145. }
  146. }
  147. for _, sql_query := range attributes_sqls {
  148. tx.Exec(ctx, sql_query)
  149. }
  150. return nil
  151. }); err != nil {
  152. wrap.MsgError(err.Error())
  153. return
  154. }
  155. // Navigate to new product page
  156. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  157. })
  158. }