module_shop_act_modify.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package modules
  2. import (
  3. "errors"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. func (this *Modules) RegisterAction_ShopModify() *Action {
  9. return this.newAction(AInfo{
  10. WantDB: true,
  11. Mount: "shop-modify",
  12. WantAdmin: true,
  13. }, func(wrap *wrapper.Wrapper) {
  14. pf_id := wrap.R.FormValue("id")
  15. pf_name := wrap.R.FormValue("name")
  16. pf_price := wrap.R.FormValue("price")
  17. pf_currency := wrap.R.FormValue("currency")
  18. pf_alias := wrap.R.FormValue("alias")
  19. pf_briefly := wrap.R.FormValue("briefly")
  20. pf_content := wrap.R.FormValue("content")
  21. pf_active := wrap.R.FormValue("active")
  22. if pf_active == "" {
  23. pf_active = "0"
  24. }
  25. if !utils.IsNumeric(pf_id) {
  26. wrap.MsgError(`Inner system error`)
  27. return
  28. }
  29. if !utils.IsFloat(pf_price) {
  30. wrap.MsgError(`Inner system error`)
  31. return
  32. }
  33. if !utils.IsNumeric(pf_currency) {
  34. wrap.MsgError(`Inner system error`)
  35. return
  36. }
  37. if pf_name == "" {
  38. wrap.MsgError(`Please specify product name`)
  39. return
  40. }
  41. if pf_alias == "" {
  42. pf_alias = utils.GenerateSingleAlias(pf_name)
  43. }
  44. if !utils.IsValidSingleAlias(pf_alias) {
  45. wrap.MsgError(`Please specify correct product alias`)
  46. return
  47. }
  48. // Collect fields and data for filter values
  49. filter_values := map[int]int{}
  50. for key, values := range wrap.R.PostForm {
  51. if len(key) > 6 && key[0:6] == "value." {
  52. for _, value := range values {
  53. if value != "" {
  54. filter_values[utils.StrToInt(value)] = utils.StrToInt(key[6:])
  55. }
  56. }
  57. }
  58. }
  59. if pf_id == "0" {
  60. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  61. // Insert row
  62. res, err := tx.Exec(
  63. `INSERT INTO shop_products SET
  64. user = ?,
  65. currency = ?,
  66. price = ?,
  67. name = ?,
  68. alias = ?,
  69. briefly = ?,
  70. content = ?,
  71. datetime = ?,
  72. active = ?
  73. ;`,
  74. wrap.User.A_id,
  75. utils.StrToInt(pf_currency),
  76. utils.StrToFloat64(pf_price),
  77. pf_name,
  78. pf_alias,
  79. pf_briefly,
  80. pf_content,
  81. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  82. pf_active,
  83. )
  84. if err != nil {
  85. return err
  86. }
  87. // Get inserted product id
  88. lastID, err := res.LastInsertId()
  89. if err != nil {
  90. return err
  91. }
  92. // Block rows
  93. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
  94. return err
  95. }
  96. // Insert product and categories relations
  97. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  98. if len(catids) > 0 {
  99. var catsCount int
  100. err = tx.QueryRow(`
  101. SELECT
  102. COUNT(*)
  103. FROM
  104. shop_cats
  105. WHERE
  106. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  107. FOR UPDATE;`,
  108. ).Scan(
  109. &catsCount,
  110. )
  111. if err != nil {
  112. return err
  113. }
  114. if len(catids) != catsCount {
  115. return errors.New("Inner system error")
  116. }
  117. var bulkInsertArr []string
  118. for _, el := range catids {
  119. bulkInsertArr = append(bulkInsertArr, `(NULL,`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  120. }
  121. if _, err = tx.Exec(
  122. `INSERT INTO shop_cat_product_rel (id,product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  123. ); err != nil {
  124. return err
  125. }
  126. }
  127. // Insert product and filter values relations
  128. for vid, _ := range filter_values {
  129. if _, err = tx.Exec(
  130. `INSERT INTO shop_filter_product_values SET
  131. product_id = ?,
  132. filter_value_id = ?
  133. ;`,
  134. lastID,
  135. vid,
  136. ); err != nil {
  137. return err
  138. }
  139. }
  140. return nil
  141. }); err != nil {
  142. wrap.MsgError(err.Error())
  143. return
  144. }
  145. wrap.Write(`window.location='/cp/shop/';`)
  146. } else {
  147. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  148. // Block rows
  149. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  150. return err
  151. }
  152. if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_currency)); err != nil {
  153. return err
  154. }
  155. if _, err := tx.Exec("SELECT id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  156. return err
  157. }
  158. if _, err := tx.Exec("SELECT id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  159. return err
  160. }
  161. // Update row
  162. if _, err := tx.Exec(
  163. `UPDATE shop_products SET
  164. currency = ?,
  165. price = ?,
  166. name = ?,
  167. alias = ?,
  168. briefly = ?,
  169. content = ?,
  170. active = ?
  171. WHERE
  172. id = ?
  173. ;`,
  174. utils.StrToInt(pf_currency),
  175. utils.StrToFloat64(pf_price),
  176. pf_name,
  177. pf_alias,
  178. pf_briefly,
  179. pf_content,
  180. pf_active,
  181. utils.StrToInt(pf_id),
  182. ); err != nil {
  183. return err
  184. }
  185. // Delete product and categories relations
  186. if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", pf_id); err != nil {
  187. return err
  188. }
  189. // Insert product and categories relations
  190. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  191. if len(catids) > 0 {
  192. var catsCount int
  193. err := tx.QueryRow(`
  194. SELECT
  195. COUNT(*)
  196. FROM
  197. shop_cats
  198. WHERE
  199. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  200. FOR UPDATE;`,
  201. ).Scan(
  202. &catsCount,
  203. )
  204. if err != nil {
  205. return err
  206. }
  207. if len(catids) != catsCount {
  208. return errors.New("Inner system error")
  209. }
  210. var bulkInsertArr []string
  211. for _, el := range catids {
  212. bulkInsertArr = append(bulkInsertArr, `(NULL,`+pf_id+`,`+utils.IntToStr(el)+`)`)
  213. }
  214. if _, err := tx.Exec(
  215. `INSERT INTO shop_cat_product_rel (id,product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  216. ); err != nil {
  217. return err
  218. }
  219. }
  220. // Delete product and filter values relations
  221. if _, err := tx.Exec(
  222. `DELETE FROM shop_filter_product_values WHERE product_id = ?;`,
  223. utils.StrToInt(pf_id),
  224. ); err != nil {
  225. return err
  226. }
  227. // Insert product and filter values relations
  228. for vid, _ := range filter_values {
  229. if _, err := tx.Exec(
  230. `INSERT INTO shop_filter_product_values SET
  231. product_id = ?,
  232. filter_value_id = ?
  233. ;`,
  234. utils.StrToInt(pf_id),
  235. vid,
  236. ); err != nil {
  237. return err
  238. }
  239. }
  240. return nil
  241. }); err != nil {
  242. wrap.MsgError(err.Error())
  243. return
  244. }
  245. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  246. }
  247. })
  248. }