module_shop_act_duplicate.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 := wrap.R.FormValue("id")
  14. if !utils.IsNumeric(pf_id) {
  15. wrap.MsgError(`Inner system error`)
  16. return
  17. }
  18. var lastID int64 = 0
  19. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  20. // Block rows
  21. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  22. return err
  23. }
  24. if _, err := tx.Exec("SELECT product_id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  25. return err
  26. }
  27. if _, err := tx.Exec("SELECT product_id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  28. return err
  29. }
  30. // Duplicate product
  31. res, err := tx.Exec(
  32. `INSERT INTO shop_products (
  33. user,
  34. currency,
  35. price,
  36. name,
  37. alias,
  38. vendor,
  39. quantity,
  40. category,
  41. briefly,
  42. content,
  43. datetime,
  44. active
  45. ) SELECT
  46. user,
  47. currency,
  48. price,
  49. CONCAT(name, ' (Copy)'),
  50. CONCAT(alias, '-', 'copy-`+utils.Int64ToStr(time.Now().Unix())+`'),
  51. vendor,
  52. quantity,
  53. category,
  54. briefly,
  55. content,
  56. datetime,
  57. 0
  58. FROM
  59. shop_products
  60. WHERE
  61. id = ?
  62. ;`,
  63. utils.StrToInt(pf_id),
  64. )
  65. if err != nil {
  66. return err
  67. }
  68. // Get inserted product id
  69. lastID, err = res.LastInsertId()
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }); err != nil {
  75. wrap.MsgError(err.Error())
  76. return
  77. }
  78. // Delete products XML cache
  79. wrap.RemoveProductXmlCacheFile()
  80. wrap.ResetCacheBlocks()
  81. // Navigate to new product page
  82. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  83. })
  84. }