56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package services
|
||
|
||
import (
|
||
"fmt"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-print/dao"
|
||
"git.rosy.net.cn/jx-print/globals"
|
||
"git.rosy.net.cn/jx-print/model"
|
||
putils "git.rosy.net.cn/jx-print/utils"
|
||
"github.com/gin-gonic/gin"
|
||
"time"
|
||
)
|
||
|
||
func AddPrinters(c *gin.Context, userID string, appID int, printInfos []*model.PrintInfo) (err error) {
|
||
var (
|
||
db = globals.GetDB()
|
||
errs []error
|
||
now = time.Now()
|
||
)
|
||
if len(printInfos) > 50 {
|
||
return fmt.Errorf("每次最多添加50台!")
|
||
}
|
||
if apps, _ := dao.GetApps(db, appID, userID, ""); len(apps) == 0 {
|
||
return fmt.Errorf("未查询到此应用!app_id:%d", appID)
|
||
}
|
||
for _, v := range printInfos {
|
||
printers, _ := dao.GetPrinters(db, appID, v.PrintNo)
|
||
if len(printers) > 0 {
|
||
errs = append(errs, fmt.Errorf("此打印机已被其他应用绑定!print_no :%s 。", v.PrintNo))
|
||
continue
|
||
}
|
||
printer := &model.Printer{
|
||
CreatedAt: &now,
|
||
UpdatedAt: &now,
|
||
DeletedAt: &utils.DefaultTimeValue,
|
||
AppID: appID,
|
||
PrintNo: v.PrintNo,
|
||
Name: v.Name,
|
||
SIM: v.SIM,
|
||
Status: model.PrinterStatusNormal,
|
||
IsOnline: model.PrinterOffline,
|
||
}
|
||
if err = dao.Insert(db, printer); err != nil {
|
||
errs = append(errs, err)
|
||
}
|
||
}
|
||
if len(errs) > 0 {
|
||
err = putils.BuildErr(errs)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func GetPrinters(c *gin.Context, appID int, printNo, name string, status, isOnline, offset, pageSize int) (page *model.PagedInfo, err error) {
|
||
return dao.GetPrintersPage(globals.GetDB(), appID, printNo, name, status, isOnline, offset, pageSize)
|
||
}
|