Files
jx-print/services/print_server/app_server/print_bind_store.go
邹宗楠 03069ce0fe 1
2022-10-18 10:30:37 +08:00

105 lines
2.6 KiB
Go

package app_server
import (
"fmt"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/dao"
"git.rosy.net.cn/jx-print/globals"
storeModel "git.rosy.net.cn/jx-print/model/app_model"
"io/ioutil"
"net/http"
"time"
)
type PrintBindStore struct {
}
var PrintBindStoreServer = new(PrintBindStore)
// AddStoreBind 添加打印机门店绑定
func (p *PrintBindStore) AddStoreBind(userId string, store *storeModel.AddBindStore) error {
// 查询打印机绑定门店数量
count, err := dao.QueryBindNumber(userId, store.PrintNo)
if err != nil {
return err
}
if count >= 5 {
return fmt.Errorf("同一打印机最多可绑定五个门店")
}
// 查询打印机是否属于用户
have, _, err := dao.GetPrintById(userId, store.PrintNo)
if err != nil {
return err
}
if !have {
return fmt.Errorf("打印机未绑定在用户下")
}
param := &storeModel.PrintBindStore{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
StoreID: store.StoreID,
StoreName: store.StoreName,
StoreVendor: store.StoreVendor,
PrintNo: store.PrintNo,
StoreStatus: store.StoreStatus,
UserId: userId,
BindStatus: storeModel.StoreBindPrintSuccess,
}
return dao.CreatePrintStoreBind(param)
}
// QueryStoreAndUser 查询门店/打印机/用户之间的关联关系
func (p *PrintBindStore) QueryStoreAndUser(param *storeModel.RelieveStore) (bool, error) {
return dao.QueryStoreAndUserManager(param.UserId, param.PrintNo, param.StoreID)
}
// RelievePrintBindStore 解除门店和打印机的绑定关系
func (p *PrintBindStore) RelievePrintBindStore(userId, printNo string) error {
// 1.修改打印机表
db := globals.GetTxDb()
defer db.Commit()
if err := dao.UpdatePrintUser(db, userId, printNo); err != nil {
db.Rollback()
return err
}
// 修改账号说用户
if err := dao.UpdatePrintToSystem(db, printNo); err != nil {
db.Rollback()
return err
}
// 绑定的门店将用户信息修改掉(暂时没做)
return nil
}
// LoseAuthorize 失去授权回调
func (p *PrintBindStore) LoseAuthorize(storeId int64) error {
return dao.UpdateStoreAuthorize(storeId)
}
// AnalysisStore 解析门店授权
func (p *PrintBindStore) AnalysisStore(request *http.Request) (string, error) {
data, err := ioutil.ReadAll(request.Body)
if err != nil {
return "", err
}
fmt.Println(string(data))
values, err := utils.HTTPBody2Values(data, false)
if err != nil {
return "", err
}
mapData := utils.URLValues2Map(values)
callParam := &call{}
if err := utils.Map2StructByJson(mapData, &callParam, false); err != nil {
return "", err
}
return callParam.StoreId, err
}
type call struct {
StoreId string `json:"storeId" form:"storeId"`
}