module_shop_attributes_act_modify.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package modules
  2. import (
  3. "context"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. func (this *Modules) RegisterAction_ShopAttributesModify() *Action {
  9. return this.newAction(AInfo{
  10. WantDB: true,
  11. Mount: "shop-attributes-modify",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_id := utils.Trim(wrap.R.FormValue("id"))
  15. pf_name := utils.Trim(wrap.R.FormValue("name"))
  16. pf_filter := utils.Trim(wrap.R.FormValue("filter"))
  17. if !utils.IsNumeric(pf_id) {
  18. wrap.MsgError(`Inner system error`)
  19. return
  20. }
  21. if pf_name == "" {
  22. wrap.MsgError(`Please specify attribute name`)
  23. return
  24. }
  25. if pf_filter == "" {
  26. wrap.MsgError(`Please specify attribute in filter`)
  27. return
  28. }
  29. // Collect fields and data
  30. filter_values := map[string]int{}
  31. for key, values := range wrap.R.PostForm {
  32. if len(key) > 6 && key[0:6] == "value." {
  33. for _, value := range values {
  34. if value != "" {
  35. filter_values[value] = utils.StrToInt(key[6:])
  36. }
  37. }
  38. }
  39. }
  40. if pf_id == "0" {
  41. var lastID int64 = 0
  42. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  43. // Insert row
  44. res, err := tx.Exec(
  45. `INSERT INTO shop_filters SET
  46. name = ?,
  47. filter = ?
  48. ;`,
  49. pf_name,
  50. pf_filter,
  51. )
  52. if err != nil {
  53. return err
  54. }
  55. // Get inserted id
  56. lastID, err = res.LastInsertId()
  57. if err != nil {
  58. return err
  59. }
  60. // Block rows
  61. if _, err := tx.Exec("SELECT id FROM shop_filters WHERE id = ? FOR UPDATE;", lastID); err != nil {
  62. return err
  63. }
  64. // Insert values
  65. for vname, _ := range filter_values {
  66. if _, err = tx.Exec(
  67. `INSERT INTO shop_filters_values SET
  68. filter_id = ?,
  69. name = ?
  70. ;`,
  71. lastID,
  72. vname,
  73. ); err != nil {
  74. return err
  75. }
  76. }
  77. return nil
  78. }); err != nil {
  79. wrap.MsgError(err.Error())
  80. return
  81. }
  82. wrap.RecreateProductXmlFile()
  83. wrap.ResetCacheBlocks()
  84. wrap.Write(`window.location='/cp/shop/attributes-modify/` + utils.Int64ToStr(lastID) + `/';`)
  85. } else {
  86. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  87. // Block rows
  88. if _, err := tx.Exec("SELECT id FROM shop_filters WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  89. return err
  90. }
  91. if _, err := tx.Exec("SELECT id FROM shop_filters_values WHERE filter_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  92. return err
  93. }
  94. if _, err := tx.Exec(
  95. `SELECT
  96. shop_filter_product_values.product_id
  97. FROM
  98. shop_filter_product_values
  99. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  100. WHERE
  101. shop_filters_values.id IS NOT NULL AND
  102. shop_filters_values.filter_id = ?
  103. FOR UPDATE;`,
  104. utils.StrToInt(pf_id),
  105. ); err != nil {
  106. return err
  107. }
  108. // Update row
  109. if _, err := tx.Exec(
  110. `UPDATE shop_filters SET
  111. name = ?,
  112. filter = ?
  113. WHERE
  114. id = ?
  115. ;`,
  116. pf_name,
  117. pf_filter,
  118. utils.StrToInt(pf_id),
  119. ); err != nil {
  120. return err
  121. }
  122. // Delete not existed rows
  123. ignore_ids := []string{}
  124. for _, vid := range filter_values {
  125. if vid != 0 {
  126. ignore_ids = append(ignore_ids, utils.IntToStr(vid))
  127. }
  128. }
  129. if len(ignore_ids) > 0 {
  130. if _, err := tx.Exec(
  131. `DELETE
  132. shop_filter_product_values
  133. FROM
  134. shop_filter_product_values
  135. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  136. WHERE
  137. shop_filters_values.id IS NOT NULL AND
  138. shop_filters_values.filter_id = ? AND
  139. shop_filter_product_values.filter_value_id NOT IN (`+strings.Join(ignore_ids, ",")+`)
  140. ;`,
  141. utils.StrToInt(pf_id),
  142. ); err != nil {
  143. return err
  144. }
  145. if _, err := tx.Exec(
  146. `DELETE FROM shop_filters_values WHERE filter_id = ? AND id NOT IN (`+strings.Join(ignore_ids, ",")+`);`,
  147. utils.StrToInt(pf_id),
  148. ); err != nil {
  149. return err
  150. }
  151. } else {
  152. if _, err := tx.Exec(
  153. `DELETE
  154. shop_filter_product_values
  155. FROM
  156. shop_filter_product_values
  157. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  158. WHERE
  159. shop_filters_values.id IS NOT NULL AND
  160. shop_filters_values.filter_id = ?
  161. ;`,
  162. utils.StrToInt(pf_id),
  163. ); err != nil {
  164. return err
  165. }
  166. if _, err := tx.Exec(
  167. `DELETE FROM shop_filters_values WHERE filter_id = ?;`,
  168. utils.StrToInt(pf_id),
  169. ); err != nil {
  170. return err
  171. }
  172. }
  173. // Insert new values, update existed rows
  174. for vname, vid := range filter_values {
  175. if vid == 0 {
  176. if _, err := tx.Exec(
  177. `INSERT INTO shop_filters_values SET
  178. filter_id = ?,
  179. name = ?
  180. ;`,
  181. utils.StrToInt(pf_id),
  182. vname,
  183. ); err != nil {
  184. return err
  185. }
  186. } else {
  187. if _, err := tx.Exec(
  188. `UPDATE shop_filters_values SET
  189. name = ?
  190. WHERE
  191. id = ? AND
  192. filter_id = ?
  193. ;`,
  194. vname,
  195. vid,
  196. utils.StrToInt(pf_id),
  197. ); err != nil {
  198. return err
  199. }
  200. }
  201. }
  202. return nil
  203. }); err != nil {
  204. wrap.MsgError(err.Error())
  205. return
  206. }
  207. wrap.RecreateProductXmlFile()
  208. wrap.ResetCacheBlocks()
  209. wrap.Write(`window.location='/cp/shop/attributes-modify/` + pf_id + `/';`)
  210. }
  211. })
  212. }