胡轶钦 6 years ago
parent
commit
026a5b95b4
5 changed files with 179 additions and 19 deletions
  1. 81
    18
      controllers/book/book.go
  2. 49
    0
      models/book/book.go
  3. 1
    0
      models/model/ta_book.go
  4. 12
    0
      routers/common.go
  5. 36
    1
      service/book/book.go

+ 81
- 18
controllers/book/book.go View File

@@ -1,10 +1,13 @@
1 1
 package book
2 2
 
3 3
 import (
4
+	"io/ioutil"
4 5
 	"spaceofcheng/services/controllers"
5 6
 	"spaceofcheng/services/models/model"
6 7
 	"spaceofcheng/services/service/book"
7 8
 	"strings"
9
+
10
+	"github.com/tealeg/xlsx"
8 11
 )
9 12
 
10 13
 // CaseController 信息
@@ -75,6 +78,7 @@ func (c *BookController) GetMineRecord() {
75 78
 	c.ResponseJSON(list)
76 79
 }
77 80
 
81
+// GetInStockChangeByBookId 获取图书盘点列表
78 82
 func (c *BookController) GetInStockChangeByBookId() {
79 83
 	page, _ := c.GetInt("page")
80 84
 	pagesize, _ := c.GetInt("pagesize")
@@ -88,7 +92,7 @@ func (c *BookController) GetInStockChangeByBookId() {
88 92
 
89 93
 }
90 94
 
91
-// 新增库存盘点
95
+// AddChangeRecord 新增库存盘点
92 96
 func (c *BookController) AddChangeRecord() {
93 97
 	change := model.TaInStockChange{}
94 98
 	if err := c.ParseForm(&change); err != nil {
@@ -104,6 +108,8 @@ func (c *BookController) AddChangeRecord() {
104 108
 
105 109
 	c.ResponseJSON("添加成功")
106 110
 }
111
+
112
+// BorrowBook 借书
107 113
 func (c *BookController) BorrowBook() {
108 114
 	bookIds := c.GetString(":bookIds")
109 115
 	customerId := c.GetString(":customerId")
@@ -119,6 +125,8 @@ func (c *BookController) BorrowBook() {
119 125
 	}
120 126
 	c.ResponseJSON("借阅成功")
121 127
 }
128
+
129
+// ReturnBook 还书
122 130
 func (c *BookController) ReturnBook() {
123 131
 	bookIds := c.GetString(":borrowIds")
124 132
 	bookId := strings.Split(bookIds, ",")
@@ -140,6 +148,8 @@ func (c *BookController) ReserveBook() {
140 148
 	}
141 149
 	c.ResponseJSON(record)
142 150
 }
151
+
152
+// GetCustomerReturnList 获取用户还书记录
143 153
 func (c *BookController) GetCustomerReturnList() {
144 154
 	customerInfo := c.GetString(":customerInfo")
145 155
 	caseIDs := c.GetString("caseid")
@@ -156,24 +166,77 @@ func (c *BookController) GetCustomerReturnList() {
156 166
 	c.ResponseJSON(list)
157 167
 }
158 168
 
159
-// func (c *BookController)ExcelInpuData(){
169
+// ExcelInpuData EXCEL导入数据
170
+func (c *BookController) ExcelInpuData() {
171
+	caseId := c.GetString("caseId")
172
+	org := c.Context.Get("org").(model.SysOrg)
173
+	file, _, err := c.GetFile("excel")
174
+	if err != nil {
175
+		c.ResponseError(err)
176
+	}
177
+	defer file.Close()
160 178
 
161
-// 	file, _, err := c.GetFile("excel")
162
-// 	if err != nil {
163
-// 		c.ResponseError(err)
164
-// 	}
165
-// 	defer file.Close()
179
+	fileBytes, err := ioutil.ReadAll(file)
180
+	if err != nil {
181
+		c.ResponseError(err)
182
+	}
183
+	xlFile, err := xlsx.OpenBinary(fileBytes)
184
+	if err != nil {
185
+		c.ResponseError(err)
186
+	}
166 187
 
167
-// 	fileBytes, err := ioutil.ReadAll(file)
168
-// 	if err != nil {
169
-// 		c.ResponseError(err)
170
-// 	}
171
-// 	excel, err : = xlsx.OpenBinary(fileBytes)
172
-// 	if err != nil {
173
-// 		c.ResponseError(err)
174
-// 	}
188
+	for _, sheet := range xlFile.Sheets {
189
+		for _, row := range sheet.Rows {
190
+			total, _ := row.Cells[2].Int()
191
+			Days, _ := row.Cells[3].Int()
192
+			var book = model.TaBook{
193
+				CaseId:      caseId,
194
+				BookBarcode: row.Cells[0].String(),
195
+				BookName:    row.Cells[1].String(),
196
+				InStock:     total,
197
+				LeftNum:     total,
198
+				BorrowNum:   0,
199
+				BorrowDays:  Days,
200
+				OrgId:       org.OrgId,
201
+				IsRecommend: 0,
202
+			}
203
+			_, err := c.serv.AddBook(book)
204
+			if err != nil {
205
+				c.ResponseError(err)
206
+			}
175 207
 
176
-// 	for _,sheet:=rang excel.Sheets{
208
+		}
209
+	}
210
+	c.ResponseJSON("添加成功")
211
+}
177 212
 
178
-// 	}
179
-// }
213
+// GetCustomerByCustomerInfo 根据用户手机或条形码获取用户信息
214
+func (c *BookController) GetCustomerByCustomerInfo() {
215
+	customerInfo := c.GetString(":customerInfo")
216
+	customer, err := c.serv.GetCustomerByCustomerInfo(customerInfo)
217
+	if err != nil {
218
+		c.ResponseError(err)
219
+	}
220
+	c.ResponseJSON(customer)
221
+}
222
+
223
+// GetRecordList 获取借书,预约记录列表
224
+func (c *BookController) GetRecordList() {
225
+	borrowStatus := c.GetString("borrowstatus")
226
+	caseIDs := c.GetString("caseid")
227
+	if caseIDs == "" {
228
+		cases := c.Context.Get("cases").([]model.SysUserCase)
229
+		caseIDs = c.GetCaseIDs(cases)
230
+	}
231
+	customerName := c.GetString("customername")
232
+	customerPhone := c.GetString("customerphone")
233
+	bookName := c.GetString("bookname")
234
+	barcode := c.GetString("barcode")
235
+	page, _ := c.GetInt("page")
236
+	pagesize, _ := c.GetInt("pagesize")
237
+	list, err := c.serv.GetRecordList(borrowStatus, caseIDs, customerName, customerPhone, bookName, barcode, page, pagesize)
238
+	if err != nil {
239
+		c.ResponseError(err)
240
+	}
241
+	c.ResponseJSON(list)
242
+}

+ 49
- 0
models/book/book.go View File

@@ -157,6 +157,36 @@ FROM
157 157
 
158 158
 }
159 159
 
160
+func (m *BookDAO) GetRecordListCount(borrowStatus, caseid, customerName, customerPhone, bookName, barcode string) (int, error) {
161
+	var record []BorrowRecord
162
+	sql := `SELECT
163
+	a.*,
164
+	b.book_name,
165
+	b.book_barcode
166
+FROM
167
+	ta_book_borrow_record a
168
+	INNER JOIN ta_book b ON a.book_id = b.book_id
169
+	where a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
170
+	if borrowStatus != "" {
171
+		sql += ` and a.borrow_status = '` + borrowStatus + `' `
172
+	}
173
+	if customerName != "" {
174
+		sql += ` and a.customer_name = '` + customerName + `' `
175
+	}
176
+	if customerPhone != "" {
177
+		sql += ` and a.customer_phone ='` + customerPhone + `' `
178
+	}
179
+	if bookName != "" {
180
+		sql += ` and b.book_name = '` + bookName + `' `
181
+	}
182
+	if barcode != "" {
183
+		sql += ` and b.book_barcode = '` + barcode + `'`
184
+	}
185
+	err := m.db.Sql(sql).Find(&record)
186
+	return len(record), err
187
+
188
+}
189
+
160 190
 func (m *BookDAO) GetBookRecommendList(caseid string, page, pageSize int) ([]model.TaBook, error) {
161 191
 	var book []model.TaBook
162 192
 	sql := `select * from ta_book where status = '` + strconv.Itoa(models.STATUS_NORMAL) + `'
@@ -356,3 +386,22 @@ WHERE
356 386
 	err := m.db.Sql(sql).Find(&record)
357 387
 	return len(record), err
358 388
 }
389
+func (m *BookDAO) GetCustomerByCustomerInfo(customerInfo string) (*model.TaCustomer, error) {
390
+	var customer []model.TaCustomer
391
+	sql := `select * from ta_customer a 
392
+	 where a.customer_phone = '` + customerInfo + `' 
393
+	 OR d.barcode = '` + customerInfo + `' )`
394
+	err := m.db.Sql(sql).Find(&customer)
395
+	if len(customer) <= 0 {
396
+		return nil, err
397
+	}
398
+	return &customer[0], err
399
+
400
+}
401
+func (m *BookDAO) GetAllRecord() ([]model.TaBookBorrowRecord, error) {
402
+	var record []model.TaBookBorrowRecord
403
+	sql := `select * from ta_book_borrow_record where 
404
+	a.borrow_status IN ( '` + models.BORROW_TYPE_BORROWED + `', '` + models.BORROW_TYPE_LATE + `' )`
405
+	err := m.db.Sql(sql).Find(&record)
406
+	return record, err
407
+}

+ 1
- 0
models/model/ta_book.go View File

@@ -16,6 +16,7 @@ type TaBook struct {
16 16
 	InStock         int       `xorm:"INT(11)"`
17 17
 	LeftNum         int       `xorm:"INT(11)"`
18 18
 	BorrowNum       int       `xorm:"INT(11)"`
19
+	BorrowDays      int       `xorm:"INT(11)"`
19 20
 	BookDescription string    `xorm:"TEXT"`
20 21
 	IsRecommend     int       `xorm:"SMALLINT(6)"`
21 22
 	Status          int       `xorm:"SMALLINT(6)"`

+ 12
- 0
routers/common.go View File

@@ -3,6 +3,7 @@ package routers
3 3
 import (
4 4
 	"spaceofcheng/services/controllers"
5 5
 	"spaceofcheng/services/controllers/bodychecklist"
6
+	"spaceofcheng/services/controllers/book"
6 7
 	"spaceofcheng/services/controllers/calendar"
7 8
 	"spaceofcheng/services/controllers/card"
8 9
 	"spaceofcheng/services/controllers/cases"
@@ -345,5 +346,16 @@ func getCommonRoutes(prefix string) beego.LinkNamespace {
345 346
 		beego.NSRouter("/calendar/:calendarId/:imgType", &calendar.CalendarController{}, "get:DownloadImg"),
346 347
 		beego.NSRouter("/calendar/:calendarIds", &calendar.CalendarController{}, "put:ChangeMakeStaus"),
347 348
 		beego.NSRouter("/calendar/excel", &calendar.CalendarController{}, "get:GetCalendarListExcel"),
349
+
350
+		// book 图书管理
351
+		beego.NSRouter("/book", &book.BookController{}, "get:GetBookList"),
352
+		beego.NSRouter("/book/record", &book.BookController{}, "get:GetRecordList"),
353
+		beego.NSRouter("/book/instock/:bookId", &book.BookController{}, "get:GetInStockChangeByBookId"),
354
+		beego.NSRouter("/book/change", &book.BookController{}, "post:AddChangeRecord"),
355
+		beego.NSRouter("/book/borrow/:bookIds/:customerId", &book.BookController{}, "put:BorrowBook"),
356
+		beego.NSRouter("/book/return/:borrowIds", &book.BookController{}, "post:ReturnBook"),
357
+		beego.NSRouter("/book/return/:customerInfo", &book.BookController{}, "get:GetCustomerReturnList"),
358
+		beego.NSRouter("/book/customer/:customerInfo", &book.BookController{}, "put:GetCustomerByCustomerInfo"),
359
+		beego.NSRouter("/book/excel", &book.BookController{}, "post:ExcelInpuData"),
348 360
 	)
349 361
 }

+ 36
- 1
service/book/book.go View File

@@ -248,7 +248,11 @@ func (s *BookServ) ReturnBook(borrowId string) error {
248 248
 	if record == nil {
249 249
 		return errors.New("不存在的借阅信息")
250 250
 	}
251
-	record.BorrowStatus = models.BORROW_TYPE_RETURN
251
+	if record.BorrowStatus == models.BORROW_TYPE_LATE {
252
+		record.BorrowStatus = models.BORROW_TYPE_LATERETURN
253
+	} else {
254
+		record.BorrowStatus = models.BORROW_TYPE_RETURN
255
+	}
252 256
 	record.ReturnDate = time.Now()
253 257
 	_, err = s.dao.UpdateBookRecord(record)
254 258
 	if err != nil {
@@ -337,3 +341,34 @@ func (s *BookServ) ReserveBook(bookid string) (*model.TaBookBorrowRecord, error)
337 341
 	}
338 342
 	return &info, err
339 343
 }
344
+
345
+func (s *BookServ) GetCustomerByCustomerInfo(customerInfo string) (*model.TaCustomer, error) {
346
+	list, err := s.dao.GetCustomerByCustomerInfo(customerInfo)
347
+	if err != nil {
348
+		utils.LogError("获取用户信息失败:", err.Error())
349
+		return nil, errors.New("获取用户信息失败")
350
+	}
351
+	return list, nil
352
+}
353
+
354
+func (s *BookServ) GetRecordList(borrowStatus, caseid, customerName, customerPhone, bookName, barcode string, page, pageSize int) (map[string]interface{}, error) {
355
+	if pageSize == 0 {
356
+		pageSize = service.PAGENUM
357
+	}
358
+	list, err := s.dao.GetRecordList(borrowStatus, caseid, customerName, customerPhone, bookName, barcode, page, pageSize)
359
+	if err != nil {
360
+		utils.LogError("获取借书记录列表失败: " + err.Error())
361
+		return nil, errors.New("获取借书记录列表失败")
362
+	}
363
+	total, err := s.dao.GetRecordListCount(borrowStatus, caseid, customerName, customerPhone, bookName, barcode)
364
+	if err != nil {
365
+		utils.LogError("获取借书记录列表失败: " + err.Error())
366
+		return nil, errors.New("获取借书记录列表失败")
367
+	}
368
+	return map[string]interface{}{
369
+		"list":     list,
370
+		"pageSize": pageSize,
371
+		"pagenum":  total,
372
+		"page":     page,
373
+	}, nil
374
+}