附近小店
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.
 
 
 

566 lines
15 KiB

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