module_shop_act_modify.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. var lastID int64 = 0
  61. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  62. // Insert row
  63. res, err := tx.Exec(
  64. `INSERT INTO shop_products SET
  65. user = ?,
  66. currency = ?,
  67. price = ?,
  68. name = ?,
  69. alias = ?,
  70. briefly = ?,
  71. content = ?,
  72. datetime = ?,
  73. active = ?
  74. ;`,
  75. wrap.User.A_id,
  76. utils.StrToInt(pf_currency),
  77. utils.StrToFloat64(pf_price),
  78. pf_name,
  79. pf_alias,
  80. pf_briefly,
  81. pf_content,
  82. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  83. pf_active,
  84. )
  85. if err != nil {
  86. return err
  87. }
  88. // Get inserted product id
  89. lastID, err = res.LastInsertId()
  90. if err != nil {
  91. return err
  92. }
  93. // Block rows
  94. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
  95. return err
  96. }
  97. // Insert product and categories relations
  98. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  99. if len(catids) > 0 {
  100. var catsCount int
  101. err = tx.QueryRow(`
  102. SELECT
  103. COUNT(*)
  104. FROM
  105. shop_cats
  106. WHERE
  107. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  108. FOR UPDATE;`,
  109. ).Scan(
  110. &catsCount,
  111. )
  112. if err != nil {
  113. return err
  114. }
  115. if len(catids) != catsCount {
  116. return errors.New("Inner system error")
  117. }
  118. var bulkInsertArr []string
  119. for _, el := range catids {
  120. bulkInsertArr = append(bulkInsertArr, `(`+utils.Int64ToStr(lastID)+`,`+utils.IntToStr(el)+`)`)
  121. }
  122. if _, err = tx.Exec(
  123. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  124. ); err != nil {
  125. return err
  126. }
  127. }
  128. // Insert product and filter values relations
  129. for vid, _ := range filter_values {
  130. if _, err = tx.Exec(
  131. `INSERT INTO shop_filter_product_values SET
  132. product_id = ?,
  133. filter_value_id = ?
  134. ;`,
  135. lastID,
  136. vid,
  137. ); err != nil {
  138. return err
  139. }
  140. }
  141. return nil
  142. }); err != nil {
  143. wrap.MsgError(err.Error())
  144. return
  145. }
  146. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  147. } else {
  148. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  149. // Block rows
  150. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  151. return err
  152. }
  153. if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_currency)); err != nil {
  154. return err
  155. }
  156. if _, err := tx.Exec("SELECT product_id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  157. return err
  158. }
  159. if _, err := tx.Exec("SELECT product_id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  160. return err
  161. }
  162. // Update row
  163. if _, err := tx.Exec(
  164. `UPDATE shop_products SET
  165. currency = ?,
  166. price = ?,
  167. name = ?,
  168. alias = ?,
  169. briefly = ?,
  170. content = ?,
  171. active = ?
  172. WHERE
  173. id = ?
  174. ;`,
  175. utils.StrToInt(pf_currency),
  176. utils.StrToFloat64(pf_price),
  177. pf_name,
  178. pf_alias,
  179. pf_briefly,
  180. pf_content,
  181. pf_active,
  182. utils.StrToInt(pf_id),
  183. ); err != nil {
  184. return err
  185. }
  186. // Delete product and categories relations
  187. if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", pf_id); err != nil {
  188. return err
  189. }
  190. // Insert product and categories relations
  191. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  192. if len(catids) > 0 {
  193. var catsCount int
  194. err := tx.QueryRow(`
  195. SELECT
  196. COUNT(*)
  197. FROM
  198. shop_cats
  199. WHERE
  200. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  201. FOR UPDATE;`,
  202. ).Scan(
  203. &catsCount,
  204. )
  205. if err != nil {
  206. return err
  207. }
  208. if len(catids) != catsCount {
  209. return errors.New("Inner system error")
  210. }
  211. var bulkInsertArr []string
  212. for _, el := range catids {
  213. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  214. }
  215. if _, err := tx.Exec(
  216. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  217. ); err != nil {
  218. return err
  219. }
  220. }
  221. // Delete product and filter values relations
  222. if _, err := tx.Exec(
  223. `DELETE FROM shop_filter_product_values WHERE product_id = ?;`,
  224. utils.StrToInt(pf_id),
  225. ); err != nil {
  226. return err
  227. }
  228. // Insert product and filter values relations
  229. for vid, _ := range filter_values {
  230. if _, err := tx.Exec(
  231. `INSERT INTO shop_filter_product_values SET
  232. product_id = ?,
  233. filter_value_id = ?
  234. ;`,
  235. utils.StrToInt(pf_id),
  236. vid,
  237. ); err != nil {
  238. return err
  239. }
  240. }
  241. return nil
  242. }); err != nil {
  243. wrap.MsgError(err.Error())
  244. return
  245. }
  246. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  247. }
  248. })
  249. }