price_config.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/main/tv/internal/model"
  5. "go-common/library/log"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _getPriceConfigsByStatusAndPid = "SELECT `id`, `pid`, `platform`, `product_name`, `product_id`, `suit_type`, `month`, `sub_type`, `price`, `selected`, `remark`, `status`, `superscript`, `operator`, `oper_id`, `stime`, `etime`, `ctime`, `mtime` FROM `tv_price_config` WHERE `status`=? AND `pid`=? ORDER BY `ctime` ASC "
  10. _countPriceConfigByStatusAndPid = "SELECT count(*) FROM `tv_price_config` WHERE `status`=? AND `pid`=?"
  11. _getSaledPriceConfigsByStatusAndPid = "SELECT `id`, `pid`, `platform`, `product_name`, `product_id`, `suit_type`, `month`, `sub_type`, `price`, `selected`, `remark`, `status`, `superscript`, `operator`, `oper_id`, `stime`, `etime`, `ctime`, `mtime` FROM `tv_price_config` WHERE `status`=? AND `pid`!=0 AND `stime`<=now() AND `etime`>now() ORDER BY `ctime` ASC "
  12. _countSaledPriceConfigByStatusAndPid = "SELECT count(*) FROM `tv_price_config` WHERE `status`=? AND `pid`!=0 AND `stime`<=now() AND `etime`>now()"
  13. )
  14. // PriceConfigsByStatus quires rows from tv_price_config.
  15. func (d *Dao) PriceConfigsByStatus(c context.Context, status int8) (res []*model.PriceConfig, total int, err error) {
  16. res = make([]*model.PriceConfig, 0)
  17. pid := 0
  18. totalRow := d.db.QueryRow(c, _countPriceConfigByStatusAndPid, status, pid)
  19. if err = totalRow.Scan(&total); err != nil {
  20. log.Error("row.ScanCount error(%v)", err)
  21. err = errors.WithStack(err)
  22. return
  23. }
  24. rows, err := d.db.Query(c, _getPriceConfigsByStatusAndPid, status, pid)
  25. if err != nil {
  26. log.Error("db.Query(%s) error(%v)", _getPriceConfigsByStatusAndPid, err)
  27. err = errors.WithStack(err)
  28. return
  29. }
  30. defer rows.Close()
  31. for rows.Next() {
  32. pc := &model.PriceConfig{}
  33. if err = rows.Scan(&pc.ID, &pc.Pid, &pc.Platform, &pc.ProductName, &pc.ProductId, &pc.SuitType, &pc.Month, &pc.SubType, &pc.Price, &pc.Selected, &pc.Remark, &pc.Status, &pc.Superscript, &pc.Operator, &pc.OperId, &pc.Stime, &pc.Etime, &pc.Ctime, &pc.Mtime); err != nil {
  34. log.Error("rows.Scan() error(%v)", err)
  35. err = errors.WithStack(err)
  36. return
  37. }
  38. res = append(res, pc)
  39. }
  40. return
  41. }
  42. // SaledPriceConfigsByStatus quires rows from tv_price_config.
  43. func (d *Dao) SaledPriceConfigsByStatus(c context.Context, status int8) (res []*model.PriceConfig, total int, err error) {
  44. res = make([]*model.PriceConfig, 0)
  45. totalRow := d.db.QueryRow(c, _countSaledPriceConfigByStatusAndPid, status)
  46. if err = totalRow.Scan(&total); err != nil {
  47. log.Error("row.ScanCount error(%v)", err)
  48. err = errors.WithStack(err)
  49. return
  50. }
  51. rows, err := d.db.Query(c, _getSaledPriceConfigsByStatusAndPid, status)
  52. if err != nil {
  53. log.Error("db.Query(%s) error(%v)", _getPriceConfigsByStatusAndPid, err)
  54. err = errors.WithStack(err)
  55. return
  56. }
  57. defer rows.Close()
  58. for rows.Next() {
  59. pc := &model.PriceConfig{}
  60. if err = rows.Scan(&pc.ID, &pc.Pid, &pc.Platform, &pc.ProductName, &pc.ProductId, &pc.SuitType, &pc.Month, &pc.SubType, &pc.Price, &pc.Selected, &pc.Remark, &pc.Status, &pc.Superscript, &pc.Operator, &pc.OperId, &pc.Stime, &pc.Etime, &pc.Ctime, &pc.Mtime); err != nil {
  61. log.Error("rows.Scan() error(%v)", err)
  62. err = errors.WithStack(err)
  63. return
  64. }
  65. res = append(res, pc)
  66. }
  67. return
  68. }