module_shop_act_modify.go 8.1 KB

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