附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_replace_printer_content.go 6.7 KiB

4 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "applet/app/utils/logx"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. "time"
  12. "xorm.io/xorm"
  13. )
  14. // 替换
  15. func ReplaceOne(c *gin.Context, args *md.PrinterRequest) {
  16. eg := MasterDb(c)
  17. printerOne, _ := db.GetPrinterIndexById(eg, args.PrinterId, args.StoreId)
  18. if printerOne.Type == "bluetooth" {
  19. return
  20. }
  21. module, _ := db.GetAllPrinterModuleListByModuleIdOne(eg, printerOne.ModuleId)
  22. ReplaceStr(c, eg, args, printerOne, module.Content)
  23. }
  24. func GetReplaceContent(c *gin.Context, args *md.PrinterRequest) string {
  25. eg := MasterDb(c)
  26. printerOne, _ := db.GetPrinterIndexByBluetooth(eg, args.StoreId)
  27. if printerOne == nil {
  28. return ""
  29. }
  30. module, _ := db.GetAllPrinterModuleListByModuleIdOne(eg, printerOne.ModuleId)
  31. content := GetCommContent(args, printerOne, module.Content)
  32. return content
  33. }
  34. // 替换
  35. func ReplaceMore(c *gin.Context, args *md.PrinterRequest) {
  36. eg := MasterDb(c)
  37. fmt.Println(args)
  38. //查出这个店铺下的所有打印机
  39. list, err := db.GetPrinterIndexAll(eg, args.StoreId)
  40. if err != nil {
  41. logx.Info(err)
  42. return
  43. }
  44. //循环拿出模板id
  45. var moduleIds = make([]int, 0)
  46. for _, v := range list {
  47. if v.IsUse == 0 || v.Type == "bluetooth" || (args.Ord["order_type"] == "face_to_pay" && v.LocationType != "reception") {
  48. continue
  49. }
  50. if v.ModuleId > 0 {
  51. moduleIds = append(moduleIds, v.ModuleId)
  52. }
  53. }
  54. if moduleIds == nil || len(moduleIds) == 0 {
  55. return
  56. }
  57. //查出对应模板数据
  58. moduleList, err := db.GetAllPrinterModuleListByModuleId(eg, moduleIds)
  59. if err != nil {
  60. logx.Info(err)
  61. return
  62. }
  63. moduleArr := make(map[int]model.CommunityTeamStorePrinterModule, 0)
  64. for _, v := range moduleList {
  65. moduleArr[v.Id] = v
  66. }
  67. //处理数据内容
  68. for _, v := range list {
  69. if v.IsUse == 0 {
  70. continue
  71. }
  72. moduleId := v.ModuleId
  73. moduleData := moduleArr[moduleId]
  74. if moduleData.Content == "" {
  75. continue
  76. }
  77. content := moduleData.Content
  78. ReplaceStr(c, eg, args, &v, content)
  79. }
  80. }
  81. func GetCommContent(arg *md.PrinterRequest, list *model.CommunityTeamStorePrinter, content string) string {
  82. goodsInfoStr := ReplaceGoodsInfo(arg.GoodsInfo, list.Type, 15, 22, 26, 32)
  83. content = strings.ReplaceAll(content, "{商品信息}", goodsInfoStr)
  84. content = strings.ReplaceAll(content, "{付款金额}", arg.Ord["payment"])
  85. content = strings.ReplaceAll(content, "{取餐号}", arg.Ord["num"])
  86. content = strings.ReplaceAll(content, "{桌号}", arg.Ord["table_num"])
  87. content = strings.ReplaceAll(content, "{订单号}", arg.Ord["main_ord_id"])
  88. content = strings.ReplaceAll(content, "{姓名}", arg.Ord["name"])
  89. content = strings.ReplaceAll(content, "{电话}", arg.Ord["phone"])
  90. content = strings.ReplaceAll(content, "{打印时间}", time.Now().Format("2006-01-02 15:04:05"))
  91. content = strings.ReplaceAll(content, "{收货地址}", arg.Ord["address"])
  92. content = strings.ReplaceAll(content, "{二维码链接}", arg.Ord["qrcode_url"])
  93. content = strings.ReplaceAll(content, "{二维码标签左}", "<QR>")
  94. content = strings.ReplaceAll(content, "{二维码标签右}", "</QR>")
  95. content = strings.ReplaceAll(content, "{商家名称}", arg.Ord["store_name"])
  96. content = strings.ReplaceAll(content, "{商家店铺首页二维码链接}", arg.Ord["store_qrcode_url"])
  97. if list.Type == "cloud" {
  98. content = strings.ReplaceAll(content, "{居中放大左}", "<FS><center>")
  99. content = strings.ReplaceAll(content, "{居中放大右}", "</center></FS>")
  100. } else {
  101. content = strings.ReplaceAll(content, "{居中放大左}", "<CB>")
  102. content = strings.ReplaceAll(content, "{居中放大右}", "</CB>")
  103. content = strings.ReplaceAll(content, "\n", "<BR>")
  104. content = strings.ReplaceAll(content, "\\n", "<BR>")
  105. }
  106. return content
  107. }
  108. // 字符替换
  109. func ReplaceStr(c *gin.Context, eg *xorm.Engine, arg *md.PrinterRequest, list *model.CommunityTeamStorePrinter, content string) {
  110. content = GetCommContent(arg, list, content)
  111. fmt.Println(content)
  112. if arg.Ord["order_type"] == "face_to_pay" {
  113. arg.Ord["order_type"] = "3"
  114. }
  115. //加入打印明细
  116. var detailData = model.CommunityTeamStorePrinterDetail{
  117. OrdId: arg.Ord["main_ord_id"],
  118. OrdType: utils.StrToInt(arg.Ord["order_type"]),
  119. PrinterId: list.Id,
  120. PrinterModuleId: list.ModuleId,
  121. PrintContent: content,
  122. StoreId: utils.StrToInt(arg.StoreId),
  123. CreateTime: time.Now(),
  124. UpdateTime: time.Now(),
  125. }
  126. has, err := db.DetailInsert(eg, &detailData)
  127. if has == 0 || err != nil {
  128. return
  129. }
  130. fmt.Println(detailData.Id)
  131. //调用打印机打印
  132. oids := fmt.Sprintf("%s%d%d", detailData.OrdId, detailData.PrinterId, detailData.CreateTime.Unix())
  133. oid, msg := CommDoing(c, content, list.SnNum, list.Type, oids)
  134. if msg != "" {
  135. return
  136. }
  137. detailData.State = 1
  138. if msg != "" {
  139. logx.Info(msg)
  140. detailData.State = 2
  141. detailData.FailMsg = msg
  142. } else {
  143. detailData.PrinterOid = oid
  144. }
  145. detailData.PrintTime = time.Now()
  146. detailData.UpdateTime = time.Now()
  147. _, err = db.DetailUpdate(eg, detailData.Id, &detailData, "printer_oid,print_time,update_time,state,fail_msg")
  148. logx.Info(err)
  149. }
  150. // 处理商品信息
  151. func ReplaceGoodsInfo(goodsInfo []map[string]string, printerType string, nameLen, priceLen, numLen, pricesLen int) string {
  152. goodsInfoStr := ""
  153. var sub = []int{0, nameLen, priceLen, numLen, pricesLen}
  154. for _, v := range goodsInfo {
  155. name := v["name"]
  156. skuText := v["sku_text"]
  157. price := v["price"]
  158. num := v["num"]
  159. prices := v["prices"]
  160. goodsInfoStr += CheckStr(name, 0, sub)
  161. goodsInfoStr += CheckStr(price, 1, sub)
  162. goodsInfoStr += CheckStr(num, 2, sub)
  163. goodsInfoStr += CheckStr(prices, 3, sub)
  164. if printerType == "cloud" {
  165. goodsInfoStr += "\n"
  166. } else {
  167. goodsInfoStr += "<BR>"
  168. }
  169. if skuText != "" {
  170. goodsInfoStr += CheckStr(skuText, 0, sub)
  171. if printerType == "cloud" {
  172. goodsInfoStr += "\n"
  173. } else {
  174. goodsInfoStr += "<BR>"
  175. }
  176. }
  177. }
  178. return goodsInfoStr
  179. }
  180. func CheckStr(str string, index int, sub []int) string {
  181. kw := ""
  182. lens := len(str)
  183. if lens > 15 && index == 0 {
  184. if lens > 15 && lens <= 32 {
  185. kw += str
  186. for i := 0; i < 32-lens; i++ {
  187. kw += " "
  188. }
  189. kw += "<BR>"
  190. for i := 0; i < 15; i++ {
  191. kw += " "
  192. }
  193. }
  194. if lens > 32 {
  195. if lens > 16*4 {
  196. lens = 16 * 4
  197. }
  198. kw += str[0:lens]
  199. kw += "<BR>"
  200. str2 := str[lens:]
  201. str2Len := len(str2)
  202. kw += str2
  203. key := sub[index+1] - str2Len - sub[index]
  204. for i := 0; i < key; i++ {
  205. kw += " "
  206. }
  207. }
  208. } else {
  209. kw += str
  210. key := sub[index+1] - lens - sub[index]
  211. if index == 0 {
  212. key = 20 - lens
  213. if lens < 15 {
  214. key = 19 - lens
  215. }
  216. if lens < 10 {
  217. key = 18 - lens
  218. }
  219. if lens < 7 {
  220. key = 17 - lens
  221. }
  222. fmt.Println(lens)
  223. fmt.Println(key)
  224. }
  225. for i := 0; i < key; i++ {
  226. kw += " "
  227. }
  228. }
  229. return kw
  230. }