module_shop_act_modify.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. utils.StrToInt(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. // Delete products XML cache
  168. wrap.RemoveProductXmlCacheFile()
  169. wrap.ResetCacheBlocks()
  170. wrap.Write(`window.location='/cp/shop/modify/` + utils.Int64ToStr(lastID) + `/';`)
  171. } else {
  172. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  173. // Block rows
  174. if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  175. return err
  176. }
  177. if _, err := tx.Exec("SELECT id FROM shop_currencies WHERE id = ? FOR UPDATE;", utils.StrToInt(pf_currency)); err != nil {
  178. return err
  179. }
  180. if _, err := tx.Exec("SELECT product_id FROM shop_cat_product_rel WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  181. return err
  182. }
  183. if _, err := tx.Exec("SELECT product_id FROM shop_filter_product_values WHERE product_id = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  184. return err
  185. }
  186. // Update row
  187. if _, err := tx.Exec(
  188. `UPDATE shop_products SET
  189. currency = ?,
  190. price = ?,
  191. name = ?,
  192. alias = ?,
  193. vendor = ?,
  194. quantity = ?,
  195. category = ?,
  196. briefly = ?,
  197. content = ?,
  198. active = ?
  199. WHERE
  200. id = ?
  201. ;`,
  202. utils.StrToInt(pf_currency),
  203. utils.StrToFloat64(pf_price),
  204. pf_name,
  205. pf_alias,
  206. pf_vendor,
  207. utils.StrToInt(pf_quantity),
  208. utils.StrToInt(pf_category),
  209. pf_briefly,
  210. pf_content,
  211. utils.StrToInt(pf_active),
  212. utils.StrToInt(pf_id),
  213. ); err != nil {
  214. return err
  215. }
  216. // Delete product and categories relations
  217. if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  218. return err
  219. }
  220. // Insert product and categories relations
  221. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  222. if len(catids) > 0 {
  223. var catsCount int
  224. err := tx.QueryRow(`
  225. SELECT
  226. COUNT(*)
  227. FROM
  228. shop_cats
  229. WHERE
  230. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  231. FOR UPDATE;`,
  232. ).Scan(
  233. &catsCount,
  234. )
  235. if err != nil {
  236. return err
  237. }
  238. if len(catids) != catsCount {
  239. return errors.New("Inner system error")
  240. }
  241. var bulkInsertArr []string
  242. for _, el := range catids {
  243. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  244. }
  245. if _, err := tx.Exec(
  246. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  247. ); err != nil {
  248. return err
  249. }
  250. }
  251. // Delete product and filter values relations
  252. if _, err := tx.Exec(
  253. `DELETE FROM shop_filter_product_values WHERE product_id = ?;`,
  254. utils.StrToInt(pf_id),
  255. ); err != nil {
  256. return err
  257. }
  258. // Insert product and filter values relations
  259. for vid, _ := range filter_values {
  260. if _, err := tx.Exec(
  261. `INSERT INTO shop_filter_product_values SET
  262. product_id = ?,
  263. filter_value_id = ?
  264. ;`,
  265. utils.StrToInt(pf_id),
  266. vid,
  267. ); err != nil {
  268. return err
  269. }
  270. }
  271. return nil
  272. }); err != nil {
  273. wrap.MsgError(err.Error())
  274. return
  275. }
  276. // Delete products XML cache
  277. wrap.RemoveProductXmlCacheFile()
  278. wrap.ResetCacheBlocks()
  279. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  280. }
  281. })
  282. }