1
This commit is contained in:
@@ -1,152 +1,150 @@
|
||||
package app_server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-print/dao"
|
||||
"git.rosy.net.cn/jx-print/globals"
|
||||
tempModel "git.rosy.net.cn/jx-print/model/app_model"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var SystemTemp map[string]*tempModel.SystemTemp
|
||||
|
||||
func init() {
|
||||
SystemTemp = make(map[string]*tempModel.SystemTemp, 0)
|
||||
var sysTemp = TempServer{}
|
||||
sysTempList, err := sysTemp.QuerySystemTemp()
|
||||
if err != nil {
|
||||
globals.SugarLogger.Debug("query system temp err :", err)
|
||||
return
|
||||
}
|
||||
if len(sysTempList) <= 0 {
|
||||
globals.SugarLogger.Debug("query system temp err :", "system temp don't have")
|
||||
// 不存在系统模板,初始化系统模板
|
||||
//InitSystemTemp()
|
||||
}
|
||||
temp := make(map[string]*tempModel.SystemTemp, 0)
|
||||
for _, v := range sysTempList {
|
||||
temp[v.TempSize] = v
|
||||
SystemTemp[v.TempSize] = v
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
param := &tempModel.SystemTemp{
|
||||
CreatedAt: &now,
|
||||
UpdatedAt: &now,
|
||||
LastOperator: "system",
|
||||
DeletedAt: &utils.DefaultTimeValue,
|
||||
TempName: "",
|
||||
TempRank: SystemTempKey,
|
||||
Temp: "",
|
||||
UserId: "system_user",
|
||||
TempType: tempModel.TempTypeMerchant,
|
||||
TempSize: tempModel.SystemTempSizeBig,
|
||||
PrintSn: "system",
|
||||
IsUse: 1,
|
||||
}
|
||||
|
||||
// 初始化大字体模板
|
||||
if _, v := temp[tempModel.SystemTempSizeBig]; !v {
|
||||
param.TempName = "system" + tempModel.SystemTempSizeBig
|
||||
param.Temp = SystemTempValue
|
||||
if err := dao.AddTemp(param); err != nil {
|
||||
globals.SugarLogger.Debug("init system temp err :", err)
|
||||
}
|
||||
SystemTemp[tempModel.SystemTempSizeBig] = param
|
||||
}
|
||||
|
||||
// 初始化中字体模板
|
||||
if _, v := temp[tempModel.SystemTempSizeMedium]; !v {
|
||||
param.TempName = "system" + tempModel.SystemTempSizeMedium
|
||||
medium := strings.Replace(SystemTempValue, "<b>", "<hb>", -1)
|
||||
medium2 := strings.Replace(medium, "</b>", "</hb>", -1)
|
||||
param.Temp = medium2
|
||||
param.TempSize = tempModel.SystemTempSizeMedium
|
||||
param.IsUse = 2
|
||||
if err := dao.AddTemp(param); err != nil {
|
||||
globals.SugarLogger.Debug("init system temp err :", err)
|
||||
}
|
||||
SystemTemp[tempModel.SystemTempSizeMedium] = param
|
||||
}
|
||||
|
||||
// 初始化小字体模板
|
||||
if _, v := temp[tempModel.SystemTempSizeSmall]; !v {
|
||||
param.TempName = "system" + tempModel.SystemTempSizeSmall
|
||||
medium := strings.Replace(SystemTempValue, "<b>", " ", -1)
|
||||
medium2 := strings.Replace(medium, "</b>", " ", -1)
|
||||
param.Temp = medium2
|
||||
param.TempSize = tempModel.SystemTempSizeSmall
|
||||
param.IsUse = 2
|
||||
if err := dao.AddTemp(param); err != nil {
|
||||
globals.SugarLogger.Debug("init system temp err :", err)
|
||||
}
|
||||
SystemTemp[tempModel.SystemTempSizeSmall] = param
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//var SystemTemp map[string]*tempModel.SystemTemp
|
||||
//
|
||||
//func init() {
|
||||
// SystemTemp = make(map[string]*tempModel.SystemTemp, 0)
|
||||
// var sysTemp = TempServer{}
|
||||
// sysTempList, err := sysTemp.QuerySystemTemp()
|
||||
// if err != nil {
|
||||
// globals.SugarLogger.Debug("query system temp err :", err)
|
||||
// return
|
||||
// }
|
||||
// if len(sysTempList) <= 0 {
|
||||
// globals.SugarLogger.Debug("query system temp err :", "system temp don't have")
|
||||
// // 不存在系统模板,初始化系统模板
|
||||
// //InitSystemTemp()
|
||||
// }
|
||||
// temp := make(map[string]*tempModel.SystemTemp, 0)
|
||||
// for _, v := range sysTempList {
|
||||
// temp[v.TempSize] = v
|
||||
// SystemTemp[v.TempSize] = v
|
||||
// }
|
||||
//
|
||||
// now := time.Now()
|
||||
// param := &tempModel.SystemTemp{
|
||||
// CreatedAt: &now,
|
||||
// UpdatedAt: &now,
|
||||
// LastOperator: "system",
|
||||
// DeletedAt: &utils.DefaultTimeValue,
|
||||
// TempName: "",
|
||||
// TempRank: SystemTempKey,
|
||||
// Temp: "",
|
||||
// UserId: "system_user",
|
||||
// TempType: tempModel.TempTypeMerchant,
|
||||
// TempSize: tempModel.SystemTempSizeBig,
|
||||
// PrintSn: "system",
|
||||
// IsUse: 1,
|
||||
// }
|
||||
//
|
||||
// // 初始化大字体模板
|
||||
// if _, v := temp[tempModel.SystemTempSizeBig]; !v {
|
||||
// param.TempName = "system" + tempModel.SystemTempSizeBig
|
||||
// param.Temp = SystemTempValue
|
||||
// if err := dao.AddTemp(param); err != nil {
|
||||
// globals.SugarLogger.Debug("init system temp err :", err)
|
||||
// }
|
||||
// SystemTemp[tempModel.SystemTempSizeBig] = param
|
||||
// }
|
||||
//
|
||||
// // 初始化中字体模板
|
||||
// if _, v := temp[tempModel.SystemTempSizeMedium]; !v {
|
||||
// param.TempName = "system" + tempModel.SystemTempSizeMedium
|
||||
// medium := strings.Replace(SystemTempValue, "<b>", "<hb>", -1)
|
||||
// medium2 := strings.Replace(medium, "</b>", "</hb>", -1)
|
||||
// param.Temp = medium2
|
||||
// param.TempSize = tempModel.SystemTempSizeMedium
|
||||
// param.IsUse = 2
|
||||
// if err := dao.AddTemp(param); err != nil {
|
||||
// globals.SugarLogger.Debug("init system temp err :", err)
|
||||
// }
|
||||
// SystemTemp[tempModel.SystemTempSizeMedium] = param
|
||||
// }
|
||||
//
|
||||
// // 初始化小字体模板
|
||||
// if _, v := temp[tempModel.SystemTempSizeSmall]; !v {
|
||||
// param.TempName = "system" + tempModel.SystemTempSizeSmall
|
||||
// medium := strings.Replace(SystemTempValue, "<b>", " ", -1)
|
||||
// medium2 := strings.Replace(medium, "</b>", " ", -1)
|
||||
// param.Temp = medium2
|
||||
// param.TempSize = tempModel.SystemTempSizeSmall
|
||||
// param.IsUse = 2
|
||||
// if err := dao.AddTemp(param); err != nil {
|
||||
// globals.SugarLogger.Debug("init system temp err :", err)
|
||||
// }
|
||||
// SystemTemp[tempModel.SystemTempSizeSmall] = param
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
||||
type TempServer struct {
|
||||
}
|
||||
|
||||
// MakePrintMsgOnTemp 将打印数据渲染到模板当中
|
||||
func (t *TempServer) MakePrintMsgOnTemp(param map[string]string, userId string) (string, error) {
|
||||
// 查询用户默认模板,不存在则使用系统默认模板
|
||||
var userTemp *tempModel.SystemTemp
|
||||
userTemp, isHave, err := dao.SelectUserDefaultTemp(userId, tempModel.TempTypeMerchant)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if userTemp == nil || !isHave {
|
||||
userTemp = SystemTemp[tempModel.SystemTempSizeBig]
|
||||
}
|
||||
|
||||
// 需要打印数据
|
||||
printMsg := ""
|
||||
printValue := make([]interface{}, 0, 0)
|
||||
for _, v := range strings.Split(userTemp.TempRank, ",") {
|
||||
switch v {
|
||||
case "skuName", "skuNumber", "skuPrice", "skuAllPrice", "allSkuTypeCount", "allSkuCount":
|
||||
continue
|
||||
case "goodsListDetail":
|
||||
printMsg += TempTag[v]
|
||||
skuList := make([]*SkuListPrintOrder, 0, 0)
|
||||
if err := json.Unmarshal([]byte(param[v]), skuList); err != nil {
|
||||
return "", err
|
||||
}
|
||||
for i := 0; i < len(skuList); i++ {
|
||||
printMsg += TempTag["skuName"]
|
||||
printMsg += TempTag["skuNumber"]
|
||||
printMsg += TempTag["skuPrice"]
|
||||
printMsg += TempTag["skuAllPrice"]
|
||||
printValue = append(printValue, skuList[i].SkuName, skuList[i].SkuName, skuList[i].SalePrice, skuList[i].TotalCountPrice)
|
||||
if skuList[i].Upc != "" {
|
||||
printMsg += TempTag["sku"]
|
||||
printValue = append(printValue, skuList[i].Upc)
|
||||
}
|
||||
|
||||
}
|
||||
case "businessType":
|
||||
if param[v] == "2" { // 是预订单
|
||||
printMsg += TempTag[v]
|
||||
}
|
||||
default:
|
||||
printMsg += TempTag[v]
|
||||
printValue = append(printValue, param[v])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return strings.Replace(fmt.Sprintf(strings.Replace(printMsg, "\n", "", -1), printValue...), "\\n", "\r\n", -1), nil
|
||||
}
|
||||
//
|
||||
//// MakePrintMsgOnTemp 将打印数据渲染到模板当中
|
||||
//func (t *TempServer) MakePrintMsgOnTemp(param map[string]string, userId string) (string, error) {
|
||||
// // 查询用户默认模板,不存在则使用系统默认模板
|
||||
// var userTemp *tempModel.SystemTemp
|
||||
// userTemp, isHave, err := dao.SelectUserDefaultTemp(userId, tempModel.TempTypeMerchant)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// if userTemp == nil || !isHave {
|
||||
// userTemp = SystemTemp[tempModel.SystemTempSizeBig]
|
||||
// }
|
||||
//
|
||||
// // 需要打印数据
|
||||
// printMsg := ""
|
||||
// printValue := make([]interface{}, 0, 0)
|
||||
// for _, v := range strings.Split(userTemp.TempRank, ",") {
|
||||
// switch v {
|
||||
// case "skuName", "skuNumber", "skuPrice", "skuAllPrice", "allSkuTypeCount", "allSkuCount":
|
||||
// continue
|
||||
// case "goodsListDetail":
|
||||
// printMsg += TempTag[v]
|
||||
// skuList := make([]*SkuListPrintOrder, 0, 0)
|
||||
// if err := json.Unmarshal([]byte(param[v]), skuList); err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// for i := 0; i < len(skuList); i++ {
|
||||
// printMsg += TempTag["skuName"]
|
||||
// printMsg += TempTag["skuNumber"]
|
||||
// printMsg += TempTag["skuPrice"]
|
||||
// printMsg += TempTag["skuAllPrice"]
|
||||
// printValue = append(printValue, skuList[i].SkuName, skuList[i].SkuName, skuList[i].SalePrice, skuList[i].TotalCountPrice)
|
||||
// if skuList[i].Upc != "" {
|
||||
// printMsg += TempTag["sku"]
|
||||
// printValue = append(printValue, skuList[i].Upc)
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// case "businessType":
|
||||
// if param[v] == "2" { // 是预订单
|
||||
// printMsg += TempTag[v]
|
||||
// }
|
||||
// default:
|
||||
// printMsg += TempTag[v]
|
||||
// printValue = append(printValue, param[v])
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// return strings.Replace(fmt.Sprintf(strings.Replace(printMsg, "\n", "", -1), printValue...), "\\n", "\r\n", -1), nil
|
||||
//}
|
||||
|
||||
// AddOrUpdateTemp 添加或者修改模板
|
||||
func (t *TempServer) AddOrUpdateTemp(param *tempModel.AddTemp) error {
|
||||
temp, err := dao.GetTempByName(param.UserId, param.TempName)
|
||||
temp, err := dao.GetTempByName(param.UserId, param.PrintSn, param.TempName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,41 +152,13 @@ func (t *TempServer) AddOrUpdateTemp(param *tempModel.AddTemp) error {
|
||||
return errors.New("模板名称重复")
|
||||
}
|
||||
|
||||
// 解析模板
|
||||
var tempTagList []*tempModel.TempContext
|
||||
if err := json.Unmarshal([]byte(param.Temp), tempTagList); err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Slice(tempTagList, func(i, j int) bool {
|
||||
if tempTagList[i].Rank > tempTagList[j].Rank {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
rankKey := ""
|
||||
contextStr := "{"
|
||||
for i := 0; i < len(tempTagList); i++ {
|
||||
if i != len(tempTagList)-1 {
|
||||
key := tempTagList[i].MatchContext + ","
|
||||
rankKey += key
|
||||
} else {
|
||||
rankKey += tempTagList[i].MatchContext
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(tempTagList); i++ {
|
||||
if i != len(tempTagList)-1 {
|
||||
contextStr += tempTagList[i].MatchContext + ":" + tempTagList[i].MatchContext + ","
|
||||
} else {
|
||||
contextStr += tempTagList[i].MatchContext + ":" + tempTagList[i].MatchContext + "}"
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
userTemp := &tempModel.SystemTemp{
|
||||
CreatedAt: param.CreatedAt,
|
||||
UpdatedAt: &now,
|
||||
TempName: param.TempName,
|
||||
TempRank: rankKey,
|
||||
Temp: contextStr,
|
||||
TempRank: param.TempRank,
|
||||
Temp: param.Temp,
|
||||
UserId: param.UserId,
|
||||
TempType: param.TempType,
|
||||
TempSize: param.TempSize,
|
||||
@@ -196,11 +166,12 @@ func (t *TempServer) AddOrUpdateTemp(param *tempModel.AddTemp) error {
|
||||
DeletedAt: &utils.DefaultTimeValue,
|
||||
PrintSn: param.PrintSn,
|
||||
IsUse: param.IsUse,
|
||||
Properties: param.Properties,
|
||||
}
|
||||
|
||||
// 如果默认打印改模板,其他打印模板则改成不使用
|
||||
if param.IsUse == 1 {
|
||||
if err := dao.UpdateOtherTempStatus(param.UserId); err != nil {
|
||||
if err := dao.UpdateOtherTempStatus(param.UserId, param.PrintSn); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -214,17 +185,36 @@ func (t *TempServer) AddOrUpdateTemp(param *tempModel.AddTemp) error {
|
||||
// 修改
|
||||
userTemp.UpdatedAt = &now
|
||||
userTemp.ID = param.ID
|
||||
return dao.UpdateTemp(userTemp, []string{"id"})
|
||||
return dao.UpdateTemp(userTemp, []string{"created_at", "updated_at", "last_operator", "deleted_at", "temp_name", "temp_rank", "temp", "user_id", "temp_type", "temp_size", "print_sn", "is_use", "properties"})
|
||||
}
|
||||
|
||||
// DeleteTemp 删除模板
|
||||
func (t *TempServer) DeleteTemp(id int, userId string) error {
|
||||
temp, err := dao.QueryTempById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if temp.TempType == tempModel.TempTypeMerchant || temp.TempType == tempModel.TempTypeConsumer {
|
||||
return fmt.Errorf("此模板为系统模板无法删除")
|
||||
}
|
||||
|
||||
return dao.DeleteTemp(id, userId)
|
||||
}
|
||||
|
||||
// QueryTempList 查询用户模板
|
||||
func (t *TempServer) QueryTempList(userId string) ([]*tempModel.SystemTemp, error) {
|
||||
return dao.SelectUserTemp(userId)
|
||||
func (t *TempServer) QueryTempList(userId, printSn string) ([]*tempModel.SystemTemp, error) {
|
||||
userList, err := dao.SelectUserTemp(userId, printSn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 查询系统模板
|
||||
systemTemp, err := t.QuerySystemTemp()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userList = append(userList, systemTemp...)
|
||||
return userList, nil
|
||||
}
|
||||
|
||||
// QuerySystemTemp 查询系统模板
|
||||
@@ -233,11 +223,11 @@ func (t *TempServer) QuerySystemTemp() ([]*tempModel.SystemTemp, error) {
|
||||
}
|
||||
|
||||
// SwitchTemp 切换使用模板
|
||||
func (t *TempServer) SwitchTemp(userId string, tempId int) error {
|
||||
return dao.SwitchTemp2User(tempId, userId)
|
||||
func (t *TempServer) SwitchTemp(userId, printSn string, tempId int) error {
|
||||
return dao.SwitchTemp2User(tempId, userId, printSn)
|
||||
}
|
||||
|
||||
// DeleteAllTemp 删除所有用户打印机模板
|
||||
func (t *TempServer) DeleteAllTemp(userId string, printNo []string) error {
|
||||
return dao.DeleteAllTemp(userId, printNo)
|
||||
func (t *TempServer) DeleteAllTemp(userId string, printNos string) error {
|
||||
return dao.DeleteAllTemp(userId, printNos)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user