module_shop_attributes_act_modify.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package modules
  2. import (
  3. "context"
  4. "strings"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  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. ctx,
  46. `INSERT INTO fave_shop_filters SET
  47. name = ?,
  48. filter = ?
  49. ;`,
  50. pf_name,
  51. pf_filter,
  52. )
  53. if err != nil {
  54. return err
  55. }
  56. // Get inserted id
  57. lastID, err = res.LastInsertId()
  58. if err != nil {
  59. return err
  60. }
  61. // Block rows
  62. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_filters WHERE id = ? FOR UPDATE;", lastID); err != nil {
  63. return err
  64. }
  65. // Insert values
  66. for vname, _ := range filter_values {
  67. if _, err = tx.Exec(
  68. ctx,
  69. `INSERT INTO fave_shop_filters_values SET
  70. filter_id = ?,
  71. name = ?
  72. ;`,
  73. lastID,
  74. vname,
  75. ); err != nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. }); err != nil {
  81. wrap.MsgError(err.Error())
  82. return
  83. }
  84. wrap.RecreateProductXmlFile()
  85. wrap.ResetCacheBlocks()
  86. wrap.Write(`window.location='/cp/shop/attributes-modify/` + utils.Int64ToStr(lastID) + `/';`)
  87. } else {
  88. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  89. // Block rows
  90. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_filters WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  91. return err
  92. }
  93. if _, err := tx.Exec(ctx, "SELECT id FROM fave_shop_filters_values WHERE filter_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  94. return err
  95. }
  96. if _, err := tx.Exec(
  97. ctx,
  98. `SELECT
  99. fave_shop_filter_product_values.product_id
  100. FROM
  101. fave_shop_filter_product_values
  102. LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
  103. WHERE
  104. fave_shop_filters_values.id IS NOT NULL AND
  105. fave_shop_filters_values.filter_id = ?
  106. FOR UPDATE;`,
  107. utils.StrToInt(pf_id),
  108. ); err != nil {
  109. return err
  110. }
  111. // Update row
  112. if _, err := tx.Exec(
  113. ctx,
  114. `UPDATE fave_shop_filters SET
  115. name = ?,
  116. filter = ?
  117. WHERE
  118. id = ?
  119. ;`,
  120. pf_name,
  121. pf_filter,
  122. utils.StrToInt(pf_id),
  123. ); err != nil {
  124. return err
  125. }
  126. // Delete not existed rows
  127. ignore_ids := []string{}
  128. for _, vid := range filter_values {
  129. if vid != 0 {
  130. ignore_ids = append(ignore_ids, utils.IntToStr(vid))
  131. }
  132. }
  133. if len(ignore_ids) > 0 {
  134. if _, err := tx.Exec(
  135. ctx,
  136. `DELETE
  137. fave_shop_filter_product_values
  138. FROM
  139. fave_shop_filter_product_values
  140. LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
  141. WHERE
  142. fave_shop_filters_values.id IS NOT NULL AND
  143. fave_shop_filters_values.filter_id = ? AND
  144. fave_shop_filter_product_values.filter_value_id NOT IN (`+strings.Join(ignore_ids, ",")+`)
  145. ;`,
  146. utils.StrToInt(pf_id),
  147. ); err != nil {
  148. return err
  149. }
  150. if _, err := tx.Exec(
  151. ctx,
  152. `DELETE FROM fave_shop_filters_values WHERE filter_id = ? AND id NOT IN (`+strings.Join(ignore_ids, ",")+`);`,
  153. utils.StrToInt(pf_id),
  154. ); err != nil {
  155. return err
  156. }
  157. } else {
  158. if _, err := tx.Exec(
  159. ctx,
  160. `DELETE
  161. fave_shop_filter_product_values
  162. FROM
  163. fave_shop_filter_product_values
  164. LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
  165. WHERE
  166. fave_shop_filters_values.id IS NOT NULL AND
  167. fave_shop_filters_values.filter_id = ?
  168. ;`,
  169. utils.StrToInt(pf_id),
  170. ); err != nil {
  171. return err
  172. }
  173. if _, err := tx.Exec(
  174. ctx,
  175. `DELETE FROM fave_shop_filters_values WHERE filter_id = ?;`,
  176. utils.StrToInt(pf_id),
  177. ); err != nil {
  178. return err
  179. }
  180. }
  181. // Insert new values, update existed rows
  182. for vname, vid := range filter_values {
  183. if vid == 0 {
  184. if _, err := tx.Exec(
  185. ctx,
  186. `INSERT INTO fave_shop_filters_values SET
  187. filter_id = ?,
  188. name = ?
  189. ;`,
  190. utils.StrToInt(pf_id),
  191. vname,
  192. ); err != nil {
  193. return err
  194. }
  195. } else {
  196. if _, err := tx.Exec(
  197. ctx,
  198. `UPDATE fave_shop_filters_values SET
  199. name = ?
  200. WHERE
  201. id = ? AND
  202. filter_id = ?
  203. ;`,
  204. vname,
  205. vid,
  206. utils.StrToInt(pf_id),
  207. ); err != nil {
  208. return err
  209. }
  210. }
  211. }
  212. return nil
  213. }); err != nil {
  214. wrap.MsgError(err.Error())
  215. return
  216. }
  217. wrap.RecreateProductXmlFile()
  218. wrap.ResetCacheBlocks()
  219. wrap.Write(`window.location='/cp/shop/attributes-modify/` + pf_id + `/';`)
  220. }
  221. })
  222. }