panel.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/vip/model"
  8. xsql "go-common/library/database/sql"
  9. xtime "go-common/library/time"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _inVipPriceConfigSQL = "INSERT INTO vip_price_config_v2(platform,product_name,product_id,suit_type,month,sub_type,original_price,selected,remark,status,operator,oper_id,superscript,start_build,end_build) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  14. _inVipDPriceConfigSQL = "INSERT INTO vip_price_discount_config_v2(vpc_id,product_id,discount_price,stime,etime,remark,operator,oper_id,first_price) VALUES(?,?,?,?,?,?,?,?,?)"
  15. _upVipPriceConfigIDSQL = "UPDATE vip_price_config_v2 SET platform = ?, product_name = ?, product_id = ?, suit_type = ?, month = ?, sub_type = ?, original_price = ?, remark = ?, status = ?, operator = ?, oper_id = ?, selected = ?, superscript = ?, start_build = ?, end_build = ? WHERE id = ?"
  16. _upVipDPriceConfigSQL = "UPDATE vip_price_discount_config_v2 SET product_id = ?, discount_price = ?, stime = ?, etime =?, remark = ?, operator = ?, oper_id = ?,first_price = ? WHERE id = ?"
  17. _delVipPriceConfigIDSQL = "DELETE FROM vip_price_config_v2 WHERE id = ?"
  18. _delVipDPriceConfigIDSQL = "DELETE FROM vip_price_discount_config_v2 WHERE id = ?"
  19. _selVipPriceConfigUQCheckSQL = "SELECT COUNT(*) FROM vip_price_config_v2 WHERE platform = ? AND month = ? AND sub_type = ? AND suit_type = ? %s;"
  20. _selVipPriceConfigsSQL = "SELECT id,platform,product_name,product_id,suit_type,month,sub_type,original_price,selected,remark,status,operator,oper_id,ctime,mtime,superscript,start_build,end_build FROM vip_price_config_v2 ORDER BY id DESC"
  21. _selVipPriceConfigIDSQL = "SELECT id,platform,product_name,product_id,suit_type,month,sub_type,original_price,selected,remark,status,operator,oper_id,ctime,mtime,superscript,start_build,end_build FROM vip_price_config_v2 WHERE id = ?"
  22. _selVipDPriceConfigsSQL = "SELECT id,vpc_id,product_id,discount_price,stime,etime,remark,operator,oper_id,ctime,mtime,first_price FROM vip_price_discount_config_v2 WHERE vpc_id = ? ORDER BY stime ASC"
  23. _selVipDPriceConfigIDSQL = "SELECT id,vpc_id,product_id,discount_price,stime,etime,remark,operator,oper_id,ctime,mtime,first_price FROM vip_price_discount_config_v2 WHERE id = ?"
  24. _selVipDPriceConfigUQTimeSQL = "SELECT id,vpc_id,product_id,discount_price,stime,etime,remark,ctime,mtime FROM vip_price_discount_config_v2 WHERE vpc_id = ? AND ((etime <> '1970-01-01 08:00:00' AND (etime >= ? OR etime >= ?) AND stime <= ?) OR (etime = '1970-01-01 08:00:00' AND stime <= ?))"
  25. _selVipPriceDiscountConfigsSQL = "SELECT vpc_id,product_id,discount_price,stime,etime,remark,ctime,mtime FROM vip_price_discount_config_v2 WHERE stime <= ? AND ((etime > ? AND etime <> '1970-01-01 08:00:00') OR (etime = '1970-01-01 08:00:00'))"
  26. _selVipMaxPriceDiscountSQL = "SELECT MAX(discount_price) FROM vip_price_discount_config_v2 WHERE vpc_id = ?"
  27. _countVipPriceConfigByplatSQL = "SELECT COUNT(*) FROM vip_price_config_v2 WHERE platform = ?"
  28. )
  29. // AddVipPriceConfig insert vip price config .
  30. func (d *Dao) AddVipPriceConfig(c context.Context, v *model.VipPriceConfig) (err error) {
  31. if _, err = d.db.Exec(c, _inVipPriceConfigSQL, v.Plat, v.PdName, v.PdID, v.SuitType, v.Month, v.SubType, v.OPrice, v.Selected, v.Remark, v.Status, v.Operator, v.OpID, v.Superscript, v.StartBuild, v.EndBuild); err != nil {
  32. err = errors.WithStack(err)
  33. }
  34. return
  35. }
  36. // AddVipDPriceConfig insert vip discount price config .
  37. func (d *Dao) AddVipDPriceConfig(c context.Context, v *model.VipDPriceConfig) (err error) {
  38. if _, err = d.db.Exec(c, _inVipDPriceConfigSQL, v.ID, v.PdID, v.DPrice, v.STime, v.ETime, v.Remark, v.Operator, v.OpID, v.FirstPrice); err != nil {
  39. err = errors.WithStack(err)
  40. }
  41. return
  42. }
  43. // UpVipPriceConfig update vip price config .
  44. func (d *Dao) UpVipPriceConfig(c context.Context, v *model.VipPriceConfig) (err error) {
  45. if _, err = d.db.Exec(c, _upVipPriceConfigIDSQL, v.Plat, v.PdName, v.PdID, v.SuitType, v.Month, v.SubType, v.OPrice, v.Remark, v.Status, v.Operator, v.OpID, v.Selected, v.Superscript, v.StartBuild, v.EndBuild, v.ID); err != nil {
  46. err = errors.WithStack(err)
  47. }
  48. return
  49. }
  50. // UpVipDPriceConfig update vip discount price config .
  51. func (d *Dao) UpVipDPriceConfig(c context.Context, v *model.VipDPriceConfig) (err error) {
  52. if _, err = d.db.Exec(c, _upVipDPriceConfigSQL, v.PdID, v.DPrice, v.STime, v.ETime, v.Remark, v.Operator, v.OpID, v.FirstPrice, v.DisID); err != nil {
  53. err = errors.WithStack(err)
  54. }
  55. return
  56. }
  57. // DelVipPriceConfig delete vip price config .
  58. func (d *Dao) DelVipPriceConfig(c context.Context, arg *model.ArgVipPriceID) (err error) {
  59. if _, err = d.db.Exec(c, _delVipPriceConfigIDSQL, arg.ID); err != nil {
  60. err = errors.WithStack(err)
  61. }
  62. return
  63. }
  64. // DelVipDPriceConfig delete vip discount price config .
  65. func (d *Dao) DelVipDPriceConfig(c context.Context, arg *model.ArgVipDPriceID) (err error) {
  66. if _, err = d.db.Exec(c, _delVipDPriceConfigIDSQL, arg.DisID); err != nil {
  67. err = errors.WithStack(err)
  68. }
  69. return
  70. }
  71. // VipPriceConfigUQCheck count vip price config unquie check.
  72. func (d *Dao) VipPriceConfigUQCheck(c context.Context, arg *model.ArgAddOrUpVipPrice) (count int64, err error) {
  73. sqlPostfix := ""
  74. if arg.EndBuild > 0 {
  75. sqlPostfix += fmt.Sprintf(" AND (start_build<= %d )", arg.EndBuild)
  76. }
  77. if arg.StartBuild > 0 {
  78. sqlPostfix += fmt.Sprintf(" AND (end_build >= %d OR end_build = 0 )", arg.StartBuild)
  79. }
  80. if arg.ID > 0 {
  81. // for update
  82. sqlPostfix += fmt.Sprintf(" AND id != %d ", arg.ID)
  83. }
  84. if err = d.db.QueryRow(c,
  85. fmt.Sprintf(_selVipPriceConfigUQCheckSQL, sqlPostfix),
  86. arg.Plat,
  87. arg.Month,
  88. arg.SubType,
  89. arg.SuitType).Scan(&count); err != nil {
  90. err = errors.WithStack(err)
  91. }
  92. return
  93. }
  94. // VipPriceConfigs vip price configs
  95. func (d *Dao) VipPriceConfigs(c context.Context) (vpcs []*model.VipPriceConfig, err error) {
  96. var rows *xsql.Rows
  97. if rows, err = d.db.Query(c, _selVipPriceConfigsSQL); err != nil {
  98. err = errors.WithStack(err)
  99. return
  100. }
  101. defer rows.Close()
  102. for rows.Next() {
  103. vpc := new(model.VipPriceConfig)
  104. if err = rows.Scan(&vpc.ID, &vpc.Plat, &vpc.PdName, &vpc.PdID, &vpc.SuitType, &vpc.Month, &vpc.SubType,
  105. &vpc.OPrice, &vpc.Selected, &vpc.Remark, &vpc.Status, &vpc.Operator, &vpc.OpID, &vpc.CTime, &vpc.MTime,
  106. &vpc.Superscript, &vpc.StartBuild, &vpc.EndBuild); err != nil {
  107. if err == xsql.ErrNoRows {
  108. err = nil
  109. vpc = nil
  110. } else {
  111. err = errors.WithStack(err)
  112. }
  113. return
  114. }
  115. vpcs = append(vpcs, vpc)
  116. }
  117. return
  118. }
  119. // VipPriceConfigID vip price config by id
  120. func (d *Dao) VipPriceConfigID(c context.Context, arg *model.ArgVipPriceID) (vpc *model.VipPriceConfig, err error) {
  121. row := d.db.QueryRow(c, _selVipPriceConfigIDSQL, arg.ID)
  122. vpc = new(model.VipPriceConfig)
  123. if err = row.Scan(&vpc.ID, &vpc.Plat, &vpc.PdName, &vpc.PdID, &vpc.SuitType, &vpc.Month, &vpc.SubType, &vpc.OPrice, &vpc.Selected,
  124. &vpc.Remark, &vpc.Status, &vpc.Operator, &vpc.OpID, &vpc.CTime, &vpc.MTime, &vpc.Superscript, &vpc.StartBuild, &vpc.EndBuild); err != nil {
  125. if err != sql.ErrNoRows {
  126. err = errors.WithStack(err)
  127. return
  128. }
  129. err = nil
  130. vpc = nil
  131. }
  132. return
  133. }
  134. // VipDPriceConfigs vip discount price configs
  135. func (d *Dao) VipDPriceConfigs(c context.Context, arg *model.ArgVipPriceID) (res []*model.VipDPriceConfig, err error) {
  136. var rows *xsql.Rows
  137. if rows, err = d.db.Query(c, _selVipDPriceConfigsSQL, arg.ID); err != nil {
  138. err = errors.WithStack(err)
  139. return
  140. }
  141. defer rows.Close()
  142. for rows.Next() {
  143. v := new(model.VipDPriceConfig)
  144. if err = rows.Scan(&v.DisID, &v.ID, &v.PdID, &v.DPrice, &v.STime, &v.ETime, &v.Remark, &v.Operator, &v.OpID, &v.CTime, &v.MTime, &v.FirstPrice); err != nil {
  145. if err == xsql.ErrNoRows {
  146. err = nil
  147. res = nil
  148. } else {
  149. err = errors.WithStack(err)
  150. }
  151. return
  152. }
  153. res = append(res, v)
  154. }
  155. return
  156. }
  157. // VipDPriceConfigID vip discount price config by id
  158. func (d *Dao) VipDPriceConfigID(c context.Context, arg *model.ArgVipDPriceID) (res *model.VipDPriceConfig, err error) {
  159. row := d.db.QueryRow(c, _selVipDPriceConfigIDSQL, arg.DisID)
  160. res = new(model.VipDPriceConfig)
  161. if err = row.Scan(&res.DisID, &res.ID, &res.PdID, &res.DPrice, &res.STime, &res.ETime, &res.Remark, &res.Operator, &res.OpID, &res.CTime, &res.MTime, &res.FirstPrice); err != nil {
  162. if err != xsql.ErrNoRows {
  163. err = errors.WithStack(err)
  164. return
  165. }
  166. err = nil
  167. res = nil
  168. }
  169. return
  170. }
  171. // VipDPriceConfigUQTime count vip discount price config unquie check time.
  172. func (d *Dao) VipDPriceConfigUQTime(c context.Context, arg *model.ArgAddOrUpVipDPrice) (mvd map[int64]*model.VipDPriceConfig, err error) {
  173. var (
  174. rows *xsql.Rows
  175. etime = arg.ETime
  176. )
  177. if etime == 0 {
  178. etime = xtime.Time(time.Now().AddDate(1000, 0, 0).Unix())
  179. }
  180. mvd = make(map[int64]*model.VipDPriceConfig)
  181. if rows, err = d.db.Query(c, _selVipDPriceConfigUQTimeSQL, arg.ID, arg.STime, etime, etime, arg.STime); err != nil {
  182. err = errors.WithStack(err)
  183. return
  184. }
  185. defer rows.Close()
  186. for rows.Next() {
  187. vdc := new(model.VipDPriceConfig)
  188. if err = rows.Scan(&vdc.DisID, &vdc.ID, &vdc.PdID, &vdc.DPrice, &vdc.STime, &vdc.ETime, &vdc.Remark, &vdc.CTime, &vdc.MTime); err != nil {
  189. if err != xsql.ErrNoRows {
  190. err = errors.WithStack(err)
  191. return
  192. }
  193. mvd = nil
  194. err = nil
  195. return
  196. }
  197. mvd[vdc.DisID] = vdc
  198. }
  199. err = rows.Err()
  200. return
  201. }
  202. // VipPriceDiscountConfigs get vip price discount configs.
  203. func (d *Dao) VipPriceDiscountConfigs(c context.Context) (mvd map[int64]*model.VipDPriceConfig, err error) {
  204. var (
  205. rows *xsql.Rows
  206. now = time.Now()
  207. )
  208. mvd = make(map[int64]*model.VipDPriceConfig)
  209. if rows, err = d.db.Query(c, _selVipPriceDiscountConfigsSQL, now, now); err != nil {
  210. err = errors.WithStack(err)
  211. return
  212. }
  213. defer rows.Close()
  214. for rows.Next() {
  215. vdc := new(model.VipDPriceConfig)
  216. if err = rows.Scan(&vdc.ID, &vdc.PdID, &vdc.DPrice, &vdc.STime, &vdc.ETime, &vdc.Remark, &vdc.CTime, &vdc.MTime); err != nil {
  217. if err != xsql.ErrNoRows {
  218. err = errors.WithStack(err)
  219. return
  220. }
  221. mvd = nil
  222. err = nil
  223. return
  224. }
  225. mvd[vdc.ID] = vdc
  226. }
  227. err = rows.Err()
  228. return
  229. }
  230. // VipMaxPriceDiscount max price discount.
  231. func (d *Dao) VipMaxPriceDiscount(c context.Context, arg *model.ArgAddOrUpVipPrice) (res float64, err error) {
  232. var max sql.NullFloat64
  233. row := d.db.QueryRow(c, _selVipMaxPriceDiscountSQL, arg.ID)
  234. if err = row.Scan(&max); err != nil {
  235. return
  236. }
  237. res = max.Float64
  238. return
  239. }
  240. // CountVipPriceConfigByPlat count vip price config by platform id.
  241. func (d *Dao) CountVipPriceConfigByPlat(c context.Context, plat int64) (count int64, err error) {
  242. row := d.db.QueryRow(c, _countVipPriceConfigByplatSQL, plat)
  243. if err = row.Scan(&count); err != nil {
  244. err = errors.WithStack(err)
  245. }
  246. return
  247. }