module_shop_attributes_act_modify.go 5.7 KB

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