module_shop_act_duplicate.go 3.8 KB

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