booktype.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package booktype
  2. import (
  3. "spaceofcheng/services/models"
  4. "spaceofcheng/services/models/model"
  5. "spaceofcheng/services/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/go-xorm/xorm"
  10. )
  11. // BooktypeDAO 当前数据库操作对象
  12. type BooktypeDAO struct {
  13. ctx *utils.Context
  14. db *xorm.Session
  15. }
  16. // NewBooktypeDAO New Inst
  17. func NewBooktypeDAO(ctx *utils.Context) *BooktypeDAO {
  18. return &BooktypeDAO{
  19. ctx: ctx,
  20. db: ctx.DB,
  21. }
  22. }
  23. type BookTypeList struct {
  24. model.TaBookType `xorm:"extends"`
  25. BookCount int
  26. }
  27. func (m *BooktypeDAO) GetBookTypeList(caseid string, page, pageSize int) ([]BookTypeList, error) {
  28. var booktype []BookTypeList
  29. // sql := `select a.*,
  30. // (SELECT COUNT(1)FROM ta_book b where a.book_type_id = b.book_type_id AND b.status = '` + strconv.Itoa(models.STATUS_NORMAL) + `') as book_count
  31. // where
  32. // a.status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
  33. // and a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')
  34. // order by a.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
  35. sql := `SELECT
  36. a.*, b.book_count
  37. FROM
  38. ta_book_type a
  39. LEFT JOIN (
  40. SELECT
  41. book_type_id,
  42. count(1) AS book_count
  43. FROM
  44. ta_book
  45. WHERE
  46. STATUS = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
  47. GROUP BY
  48. book_type_id
  49. ) b ON a.book_type_id = b.book_type_id
  50. WHERE
  51. a. STATUS = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
  52. AND a.case_id IN (
  53. '` + strings.Replace(caseid, ",", "','", -1) + `'
  54. )
  55. ORDER BY
  56. a.create_date DESC
  57. LIMIT ` + strconv.Itoa((page-1)*pageSize) + `,` + strconv.Itoa(pageSize)
  58. err := m.db.Sql(sql).Find(&booktype)
  59. return booktype, err
  60. }
  61. func (m *BooktypeDAO) GetBookTypeListCount(caseid string) (int, error) {
  62. var booktype []model.TaBookType
  63. sql := `select * from ta_book_type where status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
  64. and case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
  65. err := m.db.Sql(sql).Find(&booktype)
  66. return len(booktype), err
  67. }
  68. func (m *BooktypeDAO) GetBookTypeById(bookTypeId string) (*model.TaBookType, error) {
  69. var booktype []model.TaBookType
  70. sql := `select * from ta_book_type where book_type_id = '` + bookTypeId + `'`
  71. err := m.db.Sql(sql).Find(&booktype)
  72. if len(booktype) <= 0 {
  73. return nil, err
  74. }
  75. return &booktype[0], err
  76. }
  77. func (m *BooktypeDAO) AddBookType(booktype model.TaBookType) (*model.TaBookType, error) {
  78. booktype.Status = models.STATUS_NORMAL
  79. booktype.CreateDate = time.Now()
  80. booktype.BookTypeId = utils.GetGUID()
  81. _, err := m.db.Insert(booktype)
  82. return &booktype, err
  83. }
  84. func (m *BooktypeDAO) EditBookType(booktype model.TaBookType) (*model.TaBookType, error) {
  85. var cols = []string{
  86. "book_type_name",
  87. "book_type_img",
  88. }
  89. _, err := m.db.Cols(cols...).Where("book_type_id =?", booktype.BookTypeId).Update(booktype)
  90. return &booktype, err
  91. }
  92. func (m *BooktypeDAO) DeleteBookType(bookTypeId string) error {
  93. var booktype = model.TaBookType{
  94. BookTypeId: bookTypeId,
  95. Status: models.STATUS_DEL,
  96. }
  97. var cols = []string{
  98. "status",
  99. }
  100. _, err := m.db.Cols(cols...).Where("book_type_id = ?", booktype.BookTypeId).Update(booktype)
  101. return err
  102. }
  103. func (m *BooktypeDAO) IsExistBook(bookTypeId string) (int, error) {
  104. var book []model.TaBook
  105. sql := `select * from ta_book where book_type_id = '` + bookTypeId + `'
  106. and status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'`
  107. err := m.db.Sql(sql).Find(&book)
  108. return len(book), err
  109. }
  110. func (m *BooktypeDAO) IsNameExist(bookTypeName, caseId string) (int, error) {
  111. var book []model.TaBookType
  112. sql := `select * from ta_book_type where book_type_name = '` + bookTypeName + `'
  113. and case_id = '` + caseId + `'
  114. and status ='` + strconv.Itoa(models.STATUS_NORMAL) + `'`
  115. err := m.db.Sql(sql).Find(&book)
  116. return len(book), err
  117. }