附近小店
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_printer_list.go 14 KiB

4 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "strings"
  13. "time"
  14. "xorm.io/xorm"
  15. )
  16. func PrinterCateList(c *gin.Context) {
  17. var list = []map[string]string{
  18. {
  19. "name": "普通打印机",
  20. "key": "ordinary",
  21. },
  22. }
  23. e.OutSuc(c, list, nil)
  24. return
  25. }
  26. func PrinterLocationList(c *gin.Context) {
  27. var list = []map[string]string{
  28. {
  29. "name": "前台",
  30. "key": "reception",
  31. },
  32. {
  33. "name": "后厨",
  34. "key": "kitchen",
  35. },
  36. }
  37. e.OutSuc(c, list, nil)
  38. return
  39. }
  40. // 打印机列表
  41. func PrinterIndexList(c *gin.Context) {
  42. var search md.IndexRequest
  43. if err := c.ShouldBindJSON(&search); err != nil {
  44. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  45. return
  46. }
  47. engine := MasterDb(c)
  48. if engine == nil {
  49. e.OutErr(c, e.ERR_MASTER_ID, nil)
  50. return
  51. }
  52. list, err := db.GetPrinterIndexList(engine, &search)
  53. fmt.Println(err)
  54. fmt.Println(list)
  55. if err != nil {
  56. e.OutErr(c, e.ERR_BAD_REQUEST, err)
  57. return
  58. }
  59. if list == nil {
  60. list = make([]md.IndexResList, 0)
  61. }
  62. printerModule, _ := db.GetAllPrinterModuleList(engine)
  63. //处理模板数据
  64. var moduleArr = make(map[int]string, 0)
  65. var moduleContentArr = make(map[int]string, 0)
  66. if printerModule != nil {
  67. for _, v := range printerModule {
  68. moduleArr[v.Id] = v.Name
  69. moduleContentArr[v.Id] = v.Content
  70. }
  71. }
  72. var useArr = []string{"离线", "在线"}
  73. for k, v := range list {
  74. isUse := utils.StrToInt(v.IsUse)
  75. list[k].UseStr = useArr[isUse]
  76. ModuleId := utils.StrToInt(v.ModuleId)
  77. list[k].ModuleStr = moduleArr[ModuleId]
  78. list[k].Content = moduleContentArr[ModuleId]
  79. list[k].Img = "http://ossn.izhim.net/o2o_printer.png"
  80. }
  81. e.OutSuc(c, &list, nil)
  82. return
  83. }
  84. // 模板列表
  85. func PrinterModuleList(c *gin.Context) {
  86. var search md.IndexRequest
  87. if err := c.ShouldBindJSON(&search); err != nil {
  88. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  89. return
  90. }
  91. engine := MasterDb(c)
  92. if engine == nil {
  93. e.OutErr(c, e.ERR_MASTER_ID, nil)
  94. return
  95. }
  96. list, err := db.GetPrinterModuleList(engine, &search)
  97. if err != nil {
  98. e.OutErr(c, e.ERR_BAD_REQUEST, err)
  99. return
  100. }
  101. if list == nil {
  102. list = make([]md.ModuleResList, 0)
  103. }
  104. e.OutSuc(c, &list, nil)
  105. return
  106. }
  107. func PrinterIndexSave(c *gin.Context) {
  108. var search md.IndexSaveRequest
  109. if err := c.ShouldBindJSON(&search); err != nil {
  110. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  111. return
  112. }
  113. if search.Type == "" {
  114. search.Type = "ordinary"
  115. }
  116. eg := MasterDb(c)
  117. //判断是不是有人绑过了
  118. if search.Type != "bluetooth" {
  119. count := db.IndexExists(eg, &search)
  120. check := 1
  121. if utils.StrToInt(search.Id) > 0 {
  122. //判断下是不是同一条记录保存
  123. data, _ := db.IndexById(eg, &search)
  124. if data != nil && data.SnNum == search.SnNum {
  125. check = 0
  126. }
  127. }
  128. if count > 0 && check == 1 {
  129. e.OutErr(c, 400, e.NewErr(400, "该打印机已被绑定过"))
  130. return
  131. }
  132. }
  133. user := GetUser(c)
  134. var module = model.CommunityTeamStorePrinter{
  135. Id: utils.StrToInt(search.Id),
  136. StoreId: user.Info.Uid,
  137. SnNum: search.SnNum,
  138. IdentificationCode: search.IdentificationCode,
  139. Name: search.Name,
  140. IsUse: utils.StrToInt(search.IsUse),
  141. UpdateTime: time.Now(),
  142. Type: search.Type,
  143. LocationType: search.LocationType,
  144. }
  145. if search.Type != "bluetooth" {
  146. args := md.PrinterReq{
  147. Sn: search.SnNum,
  148. Key: search.IdentificationCode,
  149. Remark: search.Name,
  150. PrinterType: search.Type,
  151. }
  152. //调用公共代码 调用打印机添加与编辑
  153. if utils.StrToInt(search.Id) > 0 { //编辑
  154. err := EditPrinter(c, args)
  155. if err != nil {
  156. e.OutErr(c, 400, err)
  157. return
  158. }
  159. } else {
  160. err := AddPrinter(c, args)
  161. if err != nil {
  162. e.OutErr(c, 400, err)
  163. return
  164. }
  165. }
  166. }
  167. id, err := db.IndexInsert(eg, &module)
  168. if id == 0 || err != nil {
  169. e.OutErr(c, 400, e.NewErr(400, "保存失败"))
  170. return
  171. }
  172. return
  173. }
  174. // 模板id修改
  175. func PrinterIndexModuleSave(c *gin.Context) {
  176. var search md.IndexSaveRequest
  177. if err := c.ShouldBindJSON(&search); err != nil {
  178. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  179. return
  180. }
  181. var module = model.CommunityTeamStorePrinter{
  182. Id: utils.StrToInt(search.Id),
  183. ModuleId: utils.StrToInt(search.ModuleId),
  184. UpdateTime: time.Now(),
  185. }
  186. id, err := db.IndexUpdate(MasterDb(c), &module, "module_id,update_time")
  187. if id == 0 || err != nil {
  188. e.OutErr(c, 400, e.NewErr(400, "保存失败"))
  189. return
  190. }
  191. e.OutSuc(c, "success", nil)
  192. return
  193. }
  194. // 使用状态修改
  195. func PrinterIndexStateSave(c *gin.Context) {
  196. var search md.IndexSaveRequest
  197. if err := c.ShouldBindJSON(&search); err != nil {
  198. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  199. return
  200. }
  201. var module = model.CommunityTeamStorePrinter{
  202. Id: utils.StrToInt(search.Id),
  203. IsUse: utils.StrToInt(search.IsUse),
  204. UpdateTime: time.Now(),
  205. }
  206. id, err := db.IndexUpdate(MasterDb(c), &module, "is_use,update_time")
  207. if id == 0 || err != nil {
  208. e.OutErr(c, 400, e.NewErr(400, "保存失败"))
  209. return
  210. }
  211. e.OutSuc(c, "success", nil)
  212. return
  213. }
  214. // 删除打印机
  215. func PrinterDelIndex(c *gin.Context, ids string) {
  216. engine := MasterDb(c)
  217. //查出这条记录拿sn码
  218. user := GetUser(c)
  219. storeId := utils.IntToStr(user.Info.Uid)
  220. printerOne, _ := db.GetPrinterIndexById(engine, ids, storeId)
  221. if printerOne.Id == 0 {
  222. e.OutErr(c, 400, e.NewErr(400, "删除失败"))
  223. return
  224. }
  225. if printerOne.Type != "bluetooth" {
  226. //调用打印机删除
  227. args := md.PrinterReq{
  228. Sn: printerOne.SnNum,
  229. PrinterType: printerOne.Type,
  230. }
  231. err := DelPrinter(c, args)
  232. if err != nil {
  233. e.OutErr(c, 400, err)
  234. return
  235. }
  236. }
  237. has, err := engine.Where("id=? and store_id=?", ids, storeId).Delete(new(model.CommunityTeamStorePrinter))
  238. if err != nil {
  239. e.OutErr(c, e.ERR_DB_ORM, err)
  240. return
  241. }
  242. if has == 0 {
  243. e.OutErr(c, 400, e.NewErr(400, "删除失败"))
  244. return
  245. }
  246. e.OutSuc(c, []int{}, nil)
  247. return
  248. }
  249. // 明细筛选条件
  250. func PrinterIndexRecordScreen(c *gin.Context) {
  251. engine := MasterDb(c)
  252. if engine == nil {
  253. e.OutErr(c, e.ERR_MASTER_ID, nil)
  254. return
  255. }
  256. user := GetUser(c)
  257. printer, _ := db.GetPrinterIndexAll(engine, utils.IntToStr(user.Info.Uid))
  258. var r md.RecordPrinterScreen
  259. r.TimeSort = []md.NewSelectList{
  260. {
  261. Key: "",
  262. Name: "默认排序",
  263. },
  264. {
  265. Key: "printer_time_desc",
  266. Name: "时间由近到远",
  267. },
  268. {
  269. Key: "printer_time_asc",
  270. Name: "时间由远到近",
  271. },
  272. }
  273. r.PrinterList = make([]md.NewSelectList, 0)
  274. for _, v := range printer {
  275. var tmp = md.NewSelectList{
  276. Key: utils.IntToStr(v.Id),
  277. Name: v.Name,
  278. }
  279. r.PrinterList = append(r.PrinterList, tmp)
  280. }
  281. e.OutSuc(c, r, nil)
  282. return
  283. }
  284. // 明细
  285. func PrinterIndexRecordList(c *gin.Context) {
  286. var search md.RecordRequest
  287. if err := c.ShouldBindJSON(&search); err != nil {
  288. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  289. return
  290. }
  291. engine := MasterDb(c)
  292. if engine == nil {
  293. e.OutErr(c, e.ERR_MASTER_ID, nil)
  294. return
  295. }
  296. list, err := db.GetPrinterRecordList(engine, &search)
  297. if err != nil {
  298. e.OutErr(c, e.ERR_BAD_REQUEST, err)
  299. return
  300. }
  301. if list == nil {
  302. list = make([]md.RecordResList, 0)
  303. }
  304. var stateArr = []string{"正在打印", "打印成功", "打印失败"}
  305. var ordTypeArr = []string{"聚合收款订单", "外卖订单", "到店订单", "聚合收款订单"}
  306. for k, v := range list {
  307. State := utils.StrToInt(v.State)
  308. list[k].StateStr = stateArr[State]
  309. OrdType := utils.StrToInt(v.OrdType)
  310. list[k].OrdTypeStr = ordTypeArr[OrdType]
  311. list[k].Title = "#" + v.Id + " " + ordTypeArr[OrdType]
  312. if v.OrdId == "XXX" {
  313. list[k].Title = "#" + v.Id + " 测试"
  314. }
  315. list[k].PrintContent = strings.ReplaceAll(v.PrintContent, "<BR>", "\n")
  316. list[k].PrintContent = strings.ReplaceAll(list[k].PrintContent, "<QR>", "<二维码标签左>")
  317. list[k].PrintContent = strings.ReplaceAll(list[k].PrintContent, "</QR>", "<二维码标签右>")
  318. }
  319. e.OutSuc(c, &list, nil)
  320. return
  321. }
  322. func PrinterIndexToSend(c *gin.Context) {
  323. var search md.IndexTestRequest
  324. if err := c.ShouldBindJSON(&search); err != nil {
  325. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  326. return
  327. }
  328. // 加锁 防止并发
  329. mutexKey := fmt.Sprintf("%s:printer_send:p%ss%s", c.GetString("mid"), search.PrinterId, search.StoreId)
  330. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  331. if err != nil {
  332. e.OutErr(c, e.ERR, err)
  333. return
  334. }
  335. if withdrawAvailable != "OK" {
  336. e.OutErr(c, e.ERR, e.NewErr(400000, "操作过于频繁,请稍后再试"))
  337. return
  338. }
  339. eg := MasterDb(c)
  340. printerOne, _ := db.GetPrinterIndexById(eg, search.PrinterId, search.StoreId)
  341. if printerOne.Id == 0 {
  342. e.OutErr(c, 400, e.NewErr(400, "打印机不存在"))
  343. return
  344. }
  345. if printerOne.ModuleId == 0 {
  346. e.OutErr(c, 400, e.NewErr(400, "请选择打印模板"))
  347. return
  348. }
  349. ord := make(map[string]string, 0)
  350. ord["name"] = "XXX"
  351. ord["payment"] = "XXX"
  352. ord["phone"] = "XXX"
  353. ord["address"] = "XXX"
  354. ord["main_ord_id"] = "XXX"
  355. ord["num"] = "XXX"
  356. ord["table_num"] = "XXX"
  357. ord["store_name"] = "XXX"
  358. ord["store_qrcode_url"] = getStoreUrl(c, search.StoreId, "")
  359. ord["qrcode_url"] = getDownUrl(c, "")
  360. goodsInfo := make([]map[string]string, 0)
  361. one := map[string]string{
  362. "name": "测试",
  363. "price": "10",
  364. "num": "1",
  365. "prices": "10",
  366. }
  367. goodsInfo = append(goodsInfo, one)
  368. var args = &md.PrinterRequest{
  369. StoreId: search.StoreId,
  370. PrinterId: search.PrinterId,
  371. MasterId: c.GetString("mid"),
  372. Ord: ord,
  373. GoodsInfo: goodsInfo,
  374. }
  375. ReplaceOne(c, args)
  376. e.OutSuc(c, "success", nil)
  377. return
  378. }
  379. // 处理订单打印内容
  380. func ReplacePrinterContent(c *gin.Context) {
  381. var maps map[string]string
  382. if err := c.ShouldBindJSON(&maps); err != nil {
  383. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  384. return
  385. }
  386. mainOrdId := maps["main_ord_id"]
  387. // 加锁 防止并发
  388. mutexKey := fmt.Sprintf("%s:printer_content:%s", c.GetString("mid"), mainOrdId)
  389. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  390. if err != nil {
  391. e.OutErr(c, e.ERR, err)
  392. return
  393. }
  394. if withdrawAvailable != "OK" {
  395. e.OutErr(c, e.ERR, e.NewErr(400000, "操作过于频繁,请稍后再试"))
  396. return
  397. }
  398. engine := MasterDb(c)
  399. storeId, ord, goodsInfo, err := CommGetPrinterContent(c, mainOrdId)
  400. if err != nil {
  401. e.OutErr(c, 400, err)
  402. return
  403. }
  404. //打印操作
  405. go ReplaceDoing(c, engine, storeId, ord, goodsInfo)
  406. e.OutSuc(c, "success", nil)
  407. return
  408. }
  409. func CommGetPrinterContent(c *gin.Context, mainOrdId string) (int, map[string]string, []map[string]string, error) {
  410. var storeId int
  411. ord := make(map[string]string, 0)
  412. var goodsInfo = make([]map[string]string, 0)
  413. engine := MasterDb(c)
  414. mainOrd := db.GetOrderByOid(engine, mainOrdId)
  415. if mainOrd == nil {
  416. return storeId, ord, goodsInfo, e.NewErr(400, "未查询到订单!")
  417. }
  418. ordItem := db.GetOrderInfoAllEg(engine, mainOrdId)
  419. if ordItem == nil {
  420. return storeId, ord, goodsInfo, e.NewErr(400, "未查询到订单!")
  421. }
  422. //查出订单id
  423. storeId = mainOrd.StoreUid
  424. ord["name"] = mainOrd.BuyName
  425. ord["phone"] = mainOrd.BuyPhone
  426. ord["address"] = mainOrd.Address
  427. ord["main_ord_id"] = utils.Int64ToStr(mainOrd.Oid)
  428. ord["order_type"] = "2"
  429. if mainOrd.Code != "" {
  430. ord["num"] = mainOrd.Code
  431. }
  432. if mainOrd.TableNum != "" {
  433. ord["table_num"] = mainOrd.TableNum
  434. }
  435. var storeInfo model.CommunityTeamStore
  436. engine.Where("uid = ?", storeId).Get(&storeInfo)
  437. ord["store_name"] = storeInfo.Name
  438. userProfile, _ := db.UserProfileFindByID(engine, storeId)
  439. inviteCode := ""
  440. if userProfile != nil && userProfile.Uid > 0 {
  441. inviteCode = userProfile.InviteCode
  442. }
  443. ord["qrcode_url"] = getDownUrl(c, inviteCode)
  444. ord["store_qrcode_url"] = getStoreUrl(c, utils.IntToStr(storeId), inviteCode)
  445. //查出对应商品的信息
  446. var payment float64 = 0
  447. for _, v := range *ordItem {
  448. one := map[string]string{
  449. "name": v.Title,
  450. "price": utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(v.Price)/float64(v.Num), 2)),
  451. "num": utils.IntToStr(v.Num),
  452. "prices": v.Price,
  453. }
  454. payment += utils.StrToFloat64(v.Price)
  455. one["sku_text"] = ""
  456. skuText := make([]map[string]string, 0)
  457. json.Unmarshal([]byte(v.SkuInfo), &skuText)
  458. if len(skuText) > 0 {
  459. skuText1 := ""
  460. for _, v1 := range skuText {
  461. skuText1 += v1["name"] + ":" + v1["value"] + " "
  462. }
  463. one["sku_text"] = skuText1
  464. }
  465. goodsInfo = append(goodsInfo, one)
  466. }
  467. payment = utils.FloatFormat(payment, 2)
  468. ord["payment"] = utils.Float64ToStr(payment)
  469. return storeId, ord, goodsInfo, nil
  470. }
  471. // 替换打印
  472. func ReplaceDoing(c *gin.Context, eg *xorm.Engine, storeId int, ord map[string]string, goodsInfo []map[string]string) {
  473. var args = &md.PrinterRequest{
  474. StoreId: utils.IntToStr(storeId),
  475. PrinterId: "",
  476. MasterId: c.GetString("mid"),
  477. Ord: ord,
  478. GoodsInfo: goodsInfo,
  479. }
  480. ReplaceMore(c, args)
  481. }
  482. func getDownUrl(c *gin.Context, inviteCode string) string {
  483. downLoadRoute := "/#/zy-landing-page/pages/lading-page-download/lading-page-download?invited_code=" + inviteCode
  484. h5Domain := GetWebSiteDomainInfo(c, "wap")
  485. shareUrl := h5Domain + downLoadRoute
  486. //判断生成短链
  487. shareUrl = CommShareShorUrl(c, shareUrl)
  488. return shareUrl
  489. }
  490. func getStoreUrl(c *gin.Context, storeId, invitedCode string) string {
  491. downLoadRoute := "%s/#/zy-o2o-base/pages/store-page/store-page?id=%s&invited_code=%s"
  492. h5Domain := GetWebSiteDomainInfo(c, "wap")
  493. shareUrl := fmt.Sprintf(downLoadRoute, h5Domain, storeId, invitedCode)
  494. //判断生成短链
  495. shareUrl = CommShareShorUrl(c, shareUrl)
  496. return shareUrl
  497. }
  498. func CommShareShorUrl(c *gin.Context, shareUrl string) string {
  499. val := SysCfgFind(c, "share_link_type")
  500. if val["share_link_type"] == "1" { //百度短链
  501. shareUrl, _ = ShortenUrl(shareUrl)
  502. } else if val["share_link_type"] == "2" { //快站短链
  503. shareUrl = KuaiZhanShortURL(c, shareUrl)
  504. }
  505. return shareUrl
  506. }
  507. func PrinterIndexCheck(c *gin.Context) {
  508. var maps map[string]string
  509. if err := c.ShouldBindJSON(&maps); err != nil {
  510. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  511. return
  512. }
  513. mainOrdId := maps["main_ord_id"]
  514. engine := MasterDb(c)
  515. storeId, ord, goodsInfo, err := CommGetPrinterContent(c, mainOrdId)
  516. if err != nil {
  517. e.OutErr(c, 400, err)
  518. return
  519. }
  520. printerOne, _ := db.GetPrinterIndexByBluetooth(engine, utils.IntToStr(storeId))
  521. var res = map[string]interface{}{
  522. "is_has_bluetooth": "0",
  523. "content": "",
  524. }
  525. if printerOne != nil {
  526. res["is_has_bluetooth"] = "1"
  527. }
  528. var args = &md.PrinterRequest{
  529. StoreId: utils.IntToStr(storeId),
  530. PrinterId: "",
  531. MasterId: c.GetString("mid"),
  532. Ord: ord,
  533. GoodsInfo: goodsInfo,
  534. }
  535. res["content"] = GetReplaceContent(c, args)
  536. e.OutSuc(c, res, nil)
  537. return
  538. }