module_shop_act_modify.go 7.5 KB

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