123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package booktype
-
- import (
- "spaceofcheng/services/models"
- "spaceofcheng/services/models/model"
- "spaceofcheng/services/utils"
- "strconv"
- "strings"
- "time"
-
- "github.com/go-xorm/xorm"
- )
-
- // BooktypeDAO 当前数据库操作对象
- type BooktypeDAO struct {
- ctx *utils.Context
- db *xorm.Session
- }
-
- // NewBooktypeDAO New Inst
- func NewBooktypeDAO(ctx *utils.Context) *BooktypeDAO {
- return &BooktypeDAO{
- ctx: ctx,
- db: ctx.DB,
- }
- }
-
- type BookTypeList struct {
- model.TaBookType `xorm:"extends"`
- BookCount int
- }
-
- func (m *BooktypeDAO) GetBookTypeList(caseid string, page, pageSize int) ([]BookTypeList, error) {
- var booktype []BookTypeList
- // sql := `select a.*,
- // (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
- // where
- // a.status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
- // and a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')
- // order by a.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
- sql := `SELECT
- a.*, b.book_count
- FROM
- ta_book_type a
- LEFT JOIN (
- SELECT
- book_type_id,
- count(1) AS book_count
- FROM
- ta_book
- WHERE
- STATUS = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
- GROUP BY
- book_type_id
- ) b ON a.book_type_id = b.book_type_id
- WHERE
- a. STATUS = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
- AND a.case_id IN (
- '` + strings.Replace(caseid, ",", "','", -1) + `'
- )
- ORDER BY
- a.create_date DESC
- LIMIT ` + strconv.Itoa((page-1)*pageSize) + `,` + strconv.Itoa(pageSize)
- err := m.db.Sql(sql).Find(&booktype)
- return booktype, err
- }
- func (m *BooktypeDAO) GetBookTypeListCount(caseid string) (int, error) {
- var booktype []model.TaBookType
- sql := `select * from ta_book_type where status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
- and case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
- err := m.db.Sql(sql).Find(&booktype)
- return len(booktype), err
- }
-
- func (m *BooktypeDAO) GetBookTypeById(bookTypeId string) (*model.TaBookType, error) {
- var booktype []model.TaBookType
- sql := `select * from ta_book_type where book_type_id = '` + bookTypeId + `'`
- err := m.db.Sql(sql).Find(&booktype)
- if len(booktype) <= 0 {
- return nil, err
- }
- return &booktype[0], err
- }
- func (m *BooktypeDAO) AddBookType(booktype model.TaBookType) (*model.TaBookType, error) {
- booktype.Status = models.STATUS_NORMAL
- booktype.CreateDate = time.Now()
- booktype.BookTypeId = utils.GetGUID()
- _, err := m.db.Insert(booktype)
- return &booktype, err
- }
- func (m *BooktypeDAO) EditBookType(booktype model.TaBookType) (*model.TaBookType, error) {
- var cols = []string{
- "book_type_name",
- "book_type_img",
- }
- _, err := m.db.Cols(cols...).Where("book_type_id =?", booktype.BookTypeId).Update(booktype)
- return &booktype, err
-
- }
- func (m *BooktypeDAO) DeleteBookType(bookTypeId string) error {
- var booktype = model.TaBookType{
- BookTypeId: bookTypeId,
- Status: models.STATUS_DEL,
- }
- var cols = []string{
- "status",
- }
- _, err := m.db.Cols(cols...).Where("book_type_id = ?", booktype.BookTypeId).Update(booktype)
- return err
- }
-
- func (m *BooktypeDAO) IsExistBook(bookTypeId string) (int, error) {
- var book []model.TaBook
- sql := `select * from ta_book where book_type_id = '` + bookTypeId + `'
- and status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'`
- err := m.db.Sql(sql).Find(&book)
- return len(book), err
- }
-
- func (m *BooktypeDAO) IsNameExist(bookTypeName, caseId string) (int, error) {
- var book []model.TaBookType
- sql := `select * from ta_book_type where book_type_name = '` + bookTypeName + `'
- and case_id = '` + caseId + `'
- and status ='` + strconv.Itoa(models.STATUS_NORMAL) + `'`
- err := m.db.Sql(sql).Find(&book)
- return len(book), err
- }
|