module_shop_act_modify.go 8.9 KB

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