module_shop_act_modify.go 7.2 KB

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