module_shop_act_modify.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. WHERE
  217. id = ?
  218. ;`,
  219. utils.StrToInt(pf_currency),
  220. utils.StrToFloat64(pf_price),
  221. utils.StrToFloat64(pf_price_old),
  222. pf_gname,
  223. pf_name,
  224. pf_alias,
  225. pf_vendor,
  226. utils.StrToInt(pf_quantity),
  227. utils.StrToInt(pf_category),
  228. pf_briefly,
  229. pf_content,
  230. utils.StrToInt(pf_active),
  231. utils.StrToInt(pf_id),
  232. ); err != nil {
  233. return err
  234. }
  235. // Update custom field 1
  236. if _, ok := wrap.R.Form["custom1"]; ok {
  237. if _, err := tx.Exec(
  238. `UPDATE shop_products SET
  239. custom1 = ?
  240. WHERE
  241. id = ?
  242. ;`,
  243. pf_custom1,
  244. utils.StrToInt(pf_id),
  245. ); err != nil {
  246. return err
  247. }
  248. }
  249. // Update custom field 2
  250. if _, ok := wrap.R.Form["custom2"]; ok {
  251. if _, err := tx.Exec(
  252. `UPDATE shop_products SET
  253. custom2 = ?
  254. WHERE
  255. id = ?
  256. ;`,
  257. pf_custom2,
  258. utils.StrToInt(pf_id),
  259. ); err != nil {
  260. return err
  261. }
  262. }
  263. // Delete product and categories relations
  264. if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
  265. return err
  266. }
  267. // Insert product and categories relations
  268. catids := utils.GetPostArrayInt("cats[]", wrap.R)
  269. if len(catids) > 0 {
  270. var catsCount int
  271. err := tx.QueryRow(`
  272. SELECT
  273. COUNT(*)
  274. FROM
  275. shop_cats
  276. WHERE
  277. id IN(` + strings.Join(utils.ArrayOfIntToArrayOfString(catids), ",") + `)
  278. FOR UPDATE;`,
  279. ).Scan(
  280. &catsCount,
  281. )
  282. if *wrap.LogCpError(&err) != nil {
  283. return err
  284. }
  285. if len(catids) != catsCount {
  286. return errors.New("Inner system error")
  287. }
  288. var bulkInsertArr []string
  289. for _, el := range catids {
  290. bulkInsertArr = append(bulkInsertArr, `(`+pf_id+`,`+utils.IntToStr(el)+`)`)
  291. }
  292. if _, err := tx.Exec(
  293. `INSERT INTO shop_cat_product_rel (product_id,category_id) VALUES ` + strings.Join(bulkInsertArr, ",") + `;`,
  294. ); err != nil {
  295. return err
  296. }
  297. }
  298. // Delete product and filter values relations
  299. if _, err := tx.Exec(
  300. `DELETE FROM shop_filter_product_values WHERE product_id = ?;`,
  301. utils.StrToInt(pf_id),
  302. ); err != nil {
  303. return err
  304. }
  305. // Insert product and filter values relations
  306. for vid, _ := range filter_values {
  307. if _, err := tx.Exec(
  308. `INSERT INTO shop_filter_product_values SET
  309. product_id = ?,
  310. filter_value_id = ?
  311. ;`,
  312. utils.StrToInt(pf_id),
  313. vid,
  314. ); err != nil {
  315. return err
  316. }
  317. }
  318. return nil
  319. }); err != nil {
  320. wrap.MsgError(err.Error())
  321. return
  322. }
  323. wrap.RecreateProductXmlFile()
  324. wrap.ResetCacheBlocks()
  325. wrap.Write(`window.location='/cp/shop/modify/` + pf_id + `/';`)
  326. }
  327. })
  328. }