module_shop_act_modify.go 7.7 KB

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