waybill.go 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  1. package controller
  2. import (
  3. "bufio"
  4. "cold-delivery/app/admin/model"
  5. "cold-delivery/app/admin/service"
  6. "cold-delivery/app/admin/service/dto"
  7. "cold-delivery/common/actions"
  8. "cold-delivery/common/lib"
  9. "cold-delivery/common/nats/nats_server"
  10. "errors"
  11. "fmt"
  12. "github.com/beego/beego/v2/core/logs"
  13. "github.com/gin-gonic/gin"
  14. "github.com/gin-gonic/gin/binding"
  15. "github.com/golang/freetype/truetype"
  16. "github.com/pborman/uuid"
  17. "github.com/signintech/gopdf"
  18. "github.com/wcharczuk/go-chart/v2"
  19. "github.com/wcharczuk/go-chart/v2/drawing"
  20. "github.com/xuri/excelize/v2"
  21. "gogs.baozhida.cn/zoie/OAuth-core/api"
  22. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  23. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  24. "gonum.org/v1/plot"
  25. "gonum.org/v1/plot/plotter"
  26. "gonum.org/v1/plot/vg"
  27. "gonum.org/v1/plot/vg/draw"
  28. "image/color"
  29. "log"
  30. "math"
  31. "net/url"
  32. "os"
  33. "path"
  34. "sort"
  35. "sync"
  36. "time"
  37. )
  38. type WaybillController struct {
  39. api.Api
  40. }
  41. // GetPage 获取运单列表
  42. // @Summary 获取运单列表
  43. // @Description 获取运单列表
  44. // @Tags 运单
  45. // @Param waybillNo query string false "运单号"
  46. // @Param orderNo query string false "订单号"
  47. // @Param status query int false "状态"
  48. // @Param orderStartTime query string false "下单开始时间"
  49. // @Param orderEndTime query string false "下单结束时间"
  50. // @Param pageSize query int false "页条数"
  51. // @Param page query int false "页码"
  52. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  53. // @Router /api/waybill [get]
  54. // @Security Bearer
  55. func (e WaybillController) GetPage(c *gin.Context) {
  56. s := service.Waybill{}
  57. userSvc := service.SysUser{}
  58. req := dto.WaybillGetPageReq{}
  59. err := e.MakeContext(c).
  60. MakeOrm().
  61. Bind(&req, binding.Query).
  62. MakeService(&s.Service).
  63. MakeService(&userSvc.Service).
  64. Errors
  65. if err != nil {
  66. e.Logger.Error(err)
  67. e.Error(500, err, err.Error())
  68. return
  69. }
  70. //数据权限检查
  71. p := actions.GetPermissionFromContext(c)
  72. var userObj model.SysUser
  73. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  74. if err != nil {
  75. e.Error(500, err, err.Error())
  76. return
  77. }
  78. list := make([]model.Waybill, 0)
  79. var count int64
  80. if userObj.Type == model.SysUserTypeSys {
  81. err = s.GetPage(&req, &list, &count, p)
  82. } else {
  83. err = s.GetUserPage(&req, &list, &count, p)
  84. }
  85. if err != nil {
  86. e.Error(500, err, err.Error())
  87. return
  88. }
  89. for i, _ := range list {
  90. var ReCheckuser model.SysUser
  91. err = userSvc.GetUser(list[i].ReCheckId, &ReCheckuser)
  92. if err != nil {
  93. e.Error(500, err, err.Error())
  94. return
  95. }
  96. list[i].ReCheck.Id = list[i].ReCheckId
  97. list[i].ReCheck.NickName = ReCheckuser.NickName
  98. list[i].ReCheck.Phone = ReCheckuser.Phone
  99. }
  100. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  101. }
  102. // Export 导出运单列表
  103. // @Summary 导出运单列表
  104. // @Description 导出运单列表
  105. // @Tags 运单
  106. // @Param waybillNo query string false "运单号"
  107. // @Param pageSize query int false "页条数"
  108. // @Param page query int false "页码"
  109. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  110. // @Router /api/waybill/export [get]
  111. // @Security Bearer
  112. func (e WaybillController) Export(c *gin.Context) {
  113. s := service.Waybill{}
  114. req := dto.WaybillGetPageReq{}
  115. err := e.MakeContext(c).
  116. MakeOrm().
  117. Bind(&req, binding.Query).
  118. MakeService(&s.Service).
  119. Errors
  120. if err != nil {
  121. e.Logger.Error(err)
  122. e.Error(500, err, err.Error())
  123. return
  124. }
  125. //数据权限检查
  126. p := actions.GetPermissionFromContext(c)
  127. list := make([]model.Waybill, 0)
  128. var count int64
  129. req.PageSize = 9999
  130. err = s.GetPage(&req, &list, &count, p)
  131. if err != nil {
  132. e.Error(500, err, err.Error())
  133. return
  134. }
  135. f := excelize.NewFile() // 设置单元格的值
  136. // 这里设置表头ÒÒ
  137. f.SetCellValue("Sheet1", "A1", "序号")
  138. f.SetCellValue("Sheet1", "B1", "状态")
  139. f.SetCellValue("Sheet1", "C1", "寄件人名称")
  140. f.SetCellValue("Sheet1", "D1", "寄件人电话")
  141. f.SetCellValue("Sheet1", "E1", "寄件人地址")
  142. f.SetCellValue("Sheet1", "F1", "收件人名称")
  143. f.SetCellValue("Sheet1", "G1", "收件人电话")
  144. f.SetCellValue("Sheet1", "H1", "收件人地址")
  145. f.SetCellValue("Sheet1", "I1", "防拆标签码")
  146. f.SetCellValue("Sheet1", "J1", "运输备注")
  147. // 设置列宽
  148. f.SetColWidth("Sheet1", "A", "A", 6)
  149. f.SetColWidth("Sheet1", "B", "B", 8)
  150. f.SetColWidth("Sheet1", "C", "C", 14)
  151. f.SetColWidth("Sheet1", "D", "D", 14)
  152. f.SetColWidth("Sheet1", "E", "E", 30)
  153. f.SetColWidth("Sheet1", "F", "F", 14)
  154. f.SetColWidth("Sheet1", "G", "G", 14)
  155. f.SetColWidth("Sheet1", "H", "H", 30)
  156. f.SetColWidth("Sheet1", "I", "J", 15)
  157. f.SetColWidth("Sheet1", "K", "K", 30)
  158. line := 1
  159. // 循环写入数据
  160. for i, v := range list {
  161. line++
  162. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  163. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), model.WaybillStatusMap[v.Status])
  164. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.SenderAddressName)
  165. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.SenderAddressPhone)
  166. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.SenderAddressDetails)
  167. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.ConsigneeAddressName)
  168. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.ConsigneeAddressPhone)
  169. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.ConsigneeAddressDetails)
  170. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.DeliveryName)
  171. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.DeliveryPhone)
  172. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), v.Remark)
  173. }
  174. timeStr := time.Now().Format("20060102150405")
  175. filePath := "ofile/" + "运单" + timeStr + ".xlsx"
  176. // 保存文件
  177. if err = f.SaveAs(filePath); err != nil {
  178. logs.Error("保存运单失败:", err)
  179. }
  180. defer func() {
  181. os.Remove(filePath)
  182. }()
  183. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  184. // PathEscape 函数对中文做处理
  185. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape("运单"+timeStr+".xlsx"))
  186. c.Header("Content-Transfer-Encoding", "binary")
  187. c.File(filePath)
  188. }
  189. // Home 首页统计
  190. // @Summary 首页统计
  191. // @Description 首页统计
  192. // @Tags 运单
  193. // @Param data body dto.WaybillStatsReq true "data"
  194. // @Param date query string true "日期"
  195. // @Param type query string true "类型 month-月 year-年"
  196. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  197. // @Router /api/waybill/home [get]
  198. // @Security Bearer
  199. func (e WaybillController) Home(c *gin.Context) {
  200. s := service.Waybill{}
  201. userSvc := service.SysUser{}
  202. req := dto.WaybillStatsReq{}
  203. err := e.MakeContext(c).
  204. MakeOrm().
  205. Bind(&req, binding.Query).
  206. MakeService(&s.Service).
  207. MakeService(&userSvc.Service).
  208. Errors
  209. if err != nil {
  210. e.Logger.Error(err)
  211. e.Error(500, err, err.Error())
  212. return
  213. }
  214. //数据权限检查
  215. p := actions.GetPermissionFromContext(c)
  216. var userObj model.SysUser
  217. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  218. if err != nil {
  219. e.Error(500, err, err.Error())
  220. return
  221. }
  222. var res dto.WaybillStatsRes
  223. if userObj.Type == model.SysUserTypeSys {
  224. res = s.GetBasicsStats(&req, p)
  225. } else {
  226. res = s.GetUserBasicsStats(&req, p, service.GetUserBasicsStatsScopes(p.UserId))
  227. }
  228. e.OK(res, "查询成功")
  229. }
  230. // UserStats 用户运单数量统计
  231. // @Summary 首页统计
  232. // @Description 首页统计
  233. // @Tags 运单
  234. // @Param data body dto.WaybillUserStatsReq true "data"
  235. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  236. // @Router /api/waybill/user-stats [post]
  237. // @Security Bearer
  238. func (e WaybillController) UserStats(c *gin.Context) {
  239. s := service.Waybill{}
  240. userSvc := service.SysUser{}
  241. req := dto.WaybillUserStatsReq{}
  242. err := e.MakeContext(c).
  243. MakeOrm().
  244. Bind(&req, binding.JSON).
  245. MakeService(&s.Service).
  246. MakeService(&userSvc.Service).
  247. Errors
  248. if err != nil {
  249. e.Logger.Error(err)
  250. e.Error(500, err, err.Error())
  251. return
  252. }
  253. //数据权限检查
  254. p := actions.GetPermissionFromContext(c)
  255. var userObj model.SysUser
  256. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  257. if err != nil {
  258. e.Error(500, err, err.Error())
  259. return
  260. }
  261. var res []dto.WaybillUserStats
  262. if req.UserType == "delivery" {
  263. if userObj.Type == model.SysUserTypeSys {
  264. res = s.GetDeliveryStats(&req, p)
  265. } else {
  266. req.UserIds = []int{p.UserId}
  267. res = s.GetDeliveryStats(&req, p)
  268. }
  269. }
  270. if req.UserType == "reCheck" {
  271. if userObj.Type == model.SysUserTypeSys {
  272. res = s.GetReCheckStats(&req, p)
  273. } else {
  274. req.UserIds = []int{p.UserId}
  275. res = s.GetReCheckStats(&req, p)
  276. }
  277. }
  278. e.OK(res, "查询成功")
  279. }
  280. // GetAppletPage 获取运单列表
  281. // @Summary 获取运单列表
  282. // @Description 获取运单列表
  283. // @Tags 运单
  284. // @Param waybillNo query string false "运单号"
  285. // @Param status query int false "状态"
  286. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  287. // @Router /api/waybill/applet [get]
  288. // @Security Bearer
  289. func (e WaybillController) GetAppletPage(c *gin.Context) {
  290. s := service.Waybill{}
  291. req := dto.WaybillGetAppletPageReq{}
  292. err := e.MakeContext(c).
  293. MakeOrm().
  294. Bind(&req, binding.Query).
  295. MakeService(&s.Service).
  296. Errors
  297. if err != nil {
  298. e.Logger.Error(err)
  299. e.Error(500, err, err.Error())
  300. return
  301. }
  302. //数据权限检查
  303. p := actions.GetPermissionFromContext(c)
  304. list := make([]model.Waybill, 0)
  305. var count int64
  306. err = s.GetAppletPage(&req, &list, &count, p)
  307. if err != nil {
  308. e.Error(500, err, err.Error())
  309. return
  310. }
  311. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  312. }
  313. // GetAppletCount 获取app运单统计数量
  314. // @Summary 获取app运单统计数量
  315. // @Description 获取app运单统计数量
  316. // @Tags 运单
  317. // @Param waybillNo query string false "运单号"
  318. // @Param status query int false "状态"
  319. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  320. // @Router /api/waybill/applet-count [get]
  321. // @Security Bearer
  322. func (e WaybillController) GetAppletCount(c *gin.Context) {
  323. s := service.Waybill{}
  324. userSvc := service.SysUser{}
  325. req := dto.WaybillGetAppletPageReq{}
  326. err := e.MakeContext(c).
  327. MakeOrm().
  328. Bind(&req, binding.Query).
  329. MakeService(&s.Service).
  330. MakeService(&userSvc.Service).
  331. Errors
  332. if err != nil {
  333. e.Logger.Error(err)
  334. e.Error(500, err, err.Error())
  335. return
  336. }
  337. //数据权限检查
  338. p := actions.GetPermissionFromContext(c)
  339. var userObj model.SysUser
  340. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  341. if err != nil {
  342. e.Error(500, err, err.Error())
  343. return
  344. }
  345. list := make([]model.Waybill, 0)
  346. var count int64
  347. if userObj.UserType == "customer" {
  348. r := dto.WaybillGetCustomerPageReq{}
  349. r.PageSize = 9999
  350. r.CustomerId = p.UserId
  351. err = s.GetCustomerPage(&r, &list, &count, p)
  352. var statusCount = make(map[int]int)
  353. statusCount[0] = int(count)
  354. for _, waybill := range list {
  355. if _, ok := statusCount[waybill.Status]; ok {
  356. statusCount[waybill.Status] += 1
  357. } else {
  358. statusCount[waybill.Status] = 1
  359. }
  360. }
  361. e.OK(statusCount, "查询成功")
  362. return
  363. }
  364. err = s.GetAppletCount(&list, &count, p)
  365. if err != nil {
  366. e.Error(500, err, err.Error())
  367. return
  368. }
  369. var statusCount = make(map[int]int)
  370. statusCount[0] = int(count)
  371. for _, waybill := range list {
  372. if _, ok := statusCount[waybill.Status]; ok {
  373. statusCount[waybill.Status] += 1
  374. } else {
  375. statusCount[waybill.Status] = 1
  376. }
  377. }
  378. e.OK(statusCount, "查询成功")
  379. }
  380. // Get 通过id获取运单
  381. // @Summary 通过id获取运单
  382. // @Description 通过id获取运单
  383. // @Tags 运单
  384. // @Param id path string true "运单id"
  385. // @Success 200 {object} response.Response{data=model.Waybill} "{"code": 200, "data": [...]}"
  386. // @Router /api/waybill/{id} [get]
  387. // @Security Bearer
  388. func (e WaybillController) Get(c *gin.Context) {
  389. s := service.Waybill{}
  390. req := dto.WaybillGetReq{}
  391. err := e.MakeContext(c).
  392. MakeOrm().
  393. Bind(&req, nil).
  394. MakeService(&s.Service).
  395. Errors
  396. if err != nil {
  397. e.Logger.Error(err)
  398. e.Error(500, err, err.Error())
  399. return
  400. }
  401. var object model.Waybill
  402. p := actions.GetPermissionFromContext(c)
  403. //数据权限检查
  404. err = s.Get(&req, &object, p)
  405. if err != nil {
  406. e.Error(500, err, err.Error())
  407. return
  408. }
  409. e.OK(object, "查询成功")
  410. }
  411. // GetByWaybillNo 通过id获取运单
  412. // @Summary 通过id获取运单
  413. // @Description 通过id获取运单
  414. // @Tags 运单
  415. // @Param waybillNo path string true "运单编号-必填"
  416. // @Success 200 {object} response.Response{data=model.Waybill} "{"code": 200, "data": [...]}"
  417. // @Router /api/waybill/no [get]
  418. // @Security Bearer
  419. func (e WaybillController) GetByWaybillNo(c *gin.Context) {
  420. s := service.Waybill{}
  421. req := dto.WaybillGetByWaybillPdfReq{}
  422. err := e.MakeContext(c).
  423. MakeOrm().
  424. Bind(&req, binding.Query).
  425. MakeService(&s.Service).
  426. Errors
  427. if err != nil {
  428. e.Logger.Error(err)
  429. e.Error(500, err, err.Error())
  430. return
  431. }
  432. var object model.Waybill
  433. //数据权限检查
  434. err = s.GetByWaybillNo(&req, &object, nil)
  435. if err != nil {
  436. e.Error(500, err, err.Error())
  437. return
  438. }
  439. e.OK(object, "查询成功")
  440. }
  441. // Insert 添加运单
  442. // @Summary 添加运单
  443. // @Description 添加运单
  444. // @Tags 运单
  445. // @Accept application/json
  446. // @Product application/json
  447. // @Param data body dto.WaybillInsertReq true "data"
  448. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  449. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  450. // @Router /api/waybill [post]
  451. // @Security Bearer
  452. func (e WaybillController) Insert(c *gin.Context) {
  453. s := service.Waybill{}
  454. req := dto.WaybillInsertReq{}
  455. err := e.MakeContext(c).
  456. MakeOrm().
  457. Bind(&req, binding.JSON).
  458. MakeService(&s.Service).
  459. Errors
  460. if err != nil {
  461. e.Logger.Error(err)
  462. e.Error(500, err, err.Error())
  463. return
  464. }
  465. p := actions.GetPermissionFromContext(c)
  466. // 设置创建人
  467. req.SetCreateBy(user.GetUserId(c))
  468. req.SetDeptId(p.DeptId)
  469. err = s.Insert(&req)
  470. if err != nil {
  471. e.Error(500, err, err.Error())
  472. return
  473. }
  474. e.OK(req.GetId(), "添加成功")
  475. }
  476. // UpdateStatus 修改运单状态
  477. // @Summary 修改运单状态
  478. // @Description 修改运单状态
  479. // @Tags 运单
  480. // @Accept application/json
  481. // @Product application/json
  482. // @Param data body dto.WaybillUpdateStatusReq true "body"
  483. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  484. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  485. // @Router /api/waybill/update-status [put]
  486. // @Security Bearer
  487. func (e WaybillController) UpdateStatus(c *gin.Context) {
  488. s := service.Waybill{}
  489. req := dto.UpdateStatusReq{}
  490. err := e.MakeContext(c).
  491. MakeOrm().
  492. Bind(&req).
  493. MakeService(&s.Service).
  494. Errors
  495. if err != nil {
  496. e.Logger.Error(err)
  497. e.Error(500, err, err.Error())
  498. return
  499. }
  500. p := actions.GetPermissionFromContext(c)
  501. req.SetUpdateBy(user.GetUserId(c))
  502. err = s.UpdateStatus(&req, p)
  503. if err != nil {
  504. e.Error(500, err, err.Error())
  505. return
  506. }
  507. e.OK(req.GetId(), "修改成功")
  508. }
  509. // Update 修改运单
  510. // @Summary 修改运单
  511. // @Description 修改运单
  512. // @Tags 运单
  513. // @Accept application/json
  514. // @Product application/json
  515. // @Param data body dto.WaybillUpdateReq true "body"
  516. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  517. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  518. // @Router /api/waybill [put]
  519. // @Security Bearer
  520. func (e WaybillController) Update(c *gin.Context) {
  521. s := service.Waybill{}
  522. req := dto.WaybillUpdateReq{}
  523. err := e.MakeContext(c).
  524. MakeOrm().
  525. Bind(&req).
  526. MakeService(&s.Service).
  527. Errors
  528. if err != nil {
  529. e.Logger.Error(err)
  530. e.Error(500, err, err.Error())
  531. return
  532. }
  533. p := actions.GetPermissionFromContext(c)
  534. req.SetUpdateBy(user.GetUserId(c))
  535. err = s.Update(&req, p)
  536. if err != nil {
  537. e.Error(500, err, err.Error())
  538. return
  539. }
  540. e.OK(req.GetId(), "修改成功")
  541. }
  542. // Audit 审核订单
  543. // @Summary 审核订单
  544. // @Description 审核订单
  545. // @Tags 运单
  546. // @Accept application/json
  547. // @Product application/json
  548. // @Param data body dto.WaybillUpdateReq true "body"
  549. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  550. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  551. // @Router /api/waybill/audit [put]
  552. // @Security Bearer
  553. func (e WaybillController) Audit(c *gin.Context) {
  554. s := service.Waybill{}
  555. req := dto.WaybillUpdateReqaudit{}
  556. err := e.MakeContext(c).
  557. MakeOrm().
  558. Bind(&req).
  559. MakeService(&s.Service).
  560. Errors
  561. if err != nil {
  562. e.Logger.Error(err)
  563. e.Error(500, err, err.Error())
  564. return
  565. }
  566. p := actions.GetPermissionFromContext(c)
  567. req.SetUpdateBy(user.GetUserId(c))
  568. err = s.Audit(&req, p)
  569. if err != nil {
  570. e.Error(500, err, err.Error())
  571. return
  572. }
  573. e.OK(req.GetId(), "审核成功")
  574. }
  575. // AdminAudit 管理员审核订单
  576. // @Summary 管理员审核订单
  577. // @Description 审核订单
  578. // @Tags 运单
  579. // @Accept application/json
  580. // @Product application/json
  581. // @Param data body dto.WaybillAdmin true "body"
  582. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  583. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  584. // @Router /api/waybill/adminaudit [put]
  585. // @Security Bearer
  586. func (e WaybillController) AdminAudit(c *gin.Context) {
  587. s := service.Waybill{}
  588. req := dto.WaybillAdmin{}
  589. err := e.MakeContext(c).
  590. MakeOrm().
  591. Bind(&req).
  592. MakeService(&s.Service).
  593. Errors
  594. if err != nil {
  595. e.Logger.Error(err)
  596. e.Error(500, err, err.Error())
  597. return
  598. }
  599. p := actions.GetPermissionFromContext(c)
  600. err, userObj := service.GetUserProfile(e.Orm, p.UserId)
  601. if err != nil {
  602. e.Error(500, err, "获取用户信息失败")
  603. return
  604. }
  605. if userObj.Type != model.SysUserTypeSys {
  606. e.Error(500, err, err.Error())
  607. return
  608. }
  609. req.SetUpdateBy(user.GetUserId(c))
  610. err = s.AdminAudit(&req, p)
  611. if err != nil {
  612. e.Error(500, err, err.Error())
  613. return
  614. }
  615. e.OK(req.GetId(), "审核成功")
  616. }
  617. // Delivery 派单
  618. // @Summary 派单
  619. // @Description 派单
  620. // @Tags 运单
  621. // @Accept application/json
  622. // @Product application/json
  623. // @Param data body dto.WaybillDeliveryReq true "body"
  624. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  625. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  626. // @Router /api/waybill/delivery [put]
  627. // @Security Bearer
  628. func (e WaybillController) Delivery(c *gin.Context) {
  629. s := service.Waybill{}
  630. IceRaftSvc := service.IceRaft{}
  631. req := dto.WaybillDeliveryReq{}
  632. err := e.MakeContext(c).
  633. MakeOrm().
  634. Bind(&req).
  635. MakeService(&s.Service).
  636. MakeService(&IceRaftSvc.Service).
  637. Errors
  638. if err != nil {
  639. e.Logger.Error(err)
  640. e.Error(500, err, err.Error())
  641. return
  642. }
  643. p := actions.GetPermissionFromContext(c)
  644. if len(req.WaybillIds) == 0 {
  645. e.Error(500, err, "配送员不能为空")
  646. return
  647. }
  648. oldIceRaftCode := make([]string, 0)
  649. missingIceRaftCode := make([]string, 0)
  650. if req.CoolerBoxId != 0 {
  651. err = IceRaftSvc.GetPageByCoolerBoxId(req.CoolerBoxId, &oldIceRaftCode, p)
  652. if err != nil {
  653. e.Error(500, err, err.Error())
  654. return
  655. }
  656. }
  657. req.SetUpdateBy(user.GetUserId(c))
  658. req.OldIceRaftCode = oldIceRaftCode
  659. err = s.Delivery(&req, p)
  660. if err != nil {
  661. e.Error(500, err, err.Error())
  662. return
  663. }
  664. missingIceRaftCode = lib.FindStrListMissingData(oldIceRaftCode, req.IceRaftCode)
  665. if len(missingIceRaftCode) > 0 {
  666. c.JSON(200, map[string]interface{}{
  667. "requestId": uuid.NewUUID(),
  668. "code": 2000,
  669. "msg": "派单成功",
  670. "status": "success",
  671. "data": missingIceRaftCode,
  672. })
  673. return
  674. }
  675. e.OK(req.GetId(), "派单成功")
  676. }
  677. // Delete 删除运单
  678. // @Summary 删除运单
  679. // @Description 删除运单
  680. // @Tags 运单
  681. // @Accept application/json
  682. // @Product application/json
  683. // @Param data body dto.WaybillDeleteReq true "body"
  684. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  685. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  686. // @Router /api/waybill [delete]
  687. // @Security Bearer
  688. func (e WaybillController) Delete(c *gin.Context) {
  689. s := service.Waybill{}
  690. req := dto.WaybillDeleteReq{}
  691. userSvc := service.SysUser{}
  692. err := e.MakeContext(c).
  693. MakeOrm().
  694. Bind(&req, binding.JSON, nil).
  695. MakeService(&s.Service).
  696. MakeService(&userSvc.Service).
  697. Errors
  698. if err != nil {
  699. e.Logger.Error(err)
  700. e.Error(500, err, err.Error())
  701. return
  702. }
  703. //数据权限检查
  704. p := actions.GetPermissionFromContext(c)
  705. err = s.Remove(&req, p)
  706. if err != nil {
  707. e.Error(500, err, err.Error())
  708. return
  709. }
  710. e.OK(req.GetId(), "删除成功")
  711. }
  712. // Receipt 签收
  713. // @Summary 签收
  714. // @Description 签收
  715. // @Tags 运单
  716. // @Accept application/json
  717. // @Product application/json
  718. // @Param data body dto.WaybillInOutReq true "body"
  719. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  720. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  721. // @Router /api/waybill/receipt [post]
  722. // @Security Bearer
  723. func (e WaybillController) Receipt(c *gin.Context) {
  724. s := service.Waybill{}
  725. req := dto.WaybillReceiptReq{}
  726. err := e.MakeContext(c).
  727. MakeOrm().
  728. Bind(&req, binding.JSON, nil).
  729. MakeService(&s.Service).
  730. Errors
  731. if err != nil {
  732. e.Logger.Error(err)
  733. e.Error(500, err, err.Error())
  734. return
  735. }
  736. //数据权限检查
  737. p := actions.GetPermissionFromContext(c)
  738. err = s.Receipt(&req, p)
  739. if err != nil {
  740. e.Error(500, err, err.Error())
  741. return
  742. }
  743. e.OK(req.WaybillNo, "提交成功")
  744. }
  745. // CustomerReceipt 客户签收
  746. // @Summary 客户签收
  747. // @Description 客户签收
  748. // @Tags 运单
  749. // @Accept application/json
  750. // @Product application/json
  751. // @Param data body dto.WaybillReceiptReq true "body"
  752. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  753. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  754. // @Router /api/waybill/customer/receipt [post]
  755. // @Security Bearer
  756. func (e WaybillController) CustomerReceipt(c *gin.Context) {
  757. s := service.Waybill{}
  758. req := dto.WaybillReceiptReq{}
  759. err := e.MakeContext(c).
  760. MakeOrm().
  761. Bind(&req, binding.JSON, nil).
  762. MakeService(&s.Service).
  763. Errors
  764. if err != nil {
  765. e.Logger.Error(err)
  766. e.Error(500, err, err.Error())
  767. return
  768. }
  769. p := actions.GetPermissionFromContext(c)
  770. //req.WaybillNo = lib.AesDecryptCBC(req.WaybillNo, lib.AesKey)
  771. //数据权限检查
  772. err = s.Receipt(&req, p)
  773. if err != nil {
  774. e.Error(500, err, err.Error())
  775. return
  776. }
  777. e.OK(req.WaybillNo, "签收成功")
  778. }
  779. // CreateSole 生成唯一订单号
  780. // @Summary 生成唯一订单号
  781. // @Description 生成唯一订单号
  782. // @Tags 运单
  783. // @Accept application/json
  784. // @Product application/json
  785. // @Param data body dto.WaybillReceiptReq true "body"
  786. // @Success 200 {string} string "{"code": 200, "message": "生成成功"}"
  787. // @Success 200 {string} string "{"code": -1, "message": "生成成功"}"
  788. // @Router /api/waybill/createsole [get]
  789. // @Security Bearer
  790. func (e WaybillController) CreateSole(c *gin.Context) {
  791. s := service.Waybill{}
  792. err := e.MakeContext(c).
  793. MakeOrm().
  794. MakeService(&s.Service).
  795. Errors
  796. if err != nil {
  797. e.Logger.Error(err)
  798. e.Error(500, err, err.Error())
  799. return
  800. }
  801. sole, err := s.CreateSole()
  802. if err != nil {
  803. e.Error(500, err, err.Error())
  804. return
  805. }
  806. e.OK(sole, "生成成功")
  807. }
  808. // Turnarounds 订单转门店
  809. // @Summary 订单转门店
  810. // @Description 订单转门店
  811. // @Tags 运单
  812. // @Accept application/json
  813. // @Product application/json
  814. // @Param data body dto.WaybillReceiptReq true "body"
  815. // @Success 200 {string} string "{"code": 200, "message": "生成成功"}"
  816. // @Success 200 {string} string "{"code": -1, "message": "生成成功"}"
  817. // @Router /api/waybill/turnarounds [put]
  818. // @Security Bearer
  819. func (e WaybillController) Turnarounds(c *gin.Context) {
  820. s := service.Waybill{}
  821. req := dto.WaybillTurnarounds{}
  822. err := e.MakeContext(c).
  823. MakeOrm().
  824. Bind(&req, binding.JSON).
  825. MakeService(&s.Service).
  826. Errors
  827. if err != nil {
  828. e.Logger.Error(err)
  829. e.Error(500, err, err.Error())
  830. return
  831. }
  832. //数据权限检查
  833. p := actions.GetPermissionFromContext(c)
  834. err = s.WaybillTurnarounds(&req, p)
  835. if err != nil {
  836. e.Error(500, err, err.Error())
  837. return
  838. }
  839. e.OK(req, "转门店成功")
  840. }
  841. // SecondaryDistribution 二次分配订单
  842. // @Summary 二次分配订单
  843. // @Description 二次分配订单
  844. // @Tags 运单
  845. // @Accept application/json
  846. // @Product application/json
  847. // @Param data body dto.WaybillReceiptReq true "body"
  848. // @Success 200 {string} string "{"code": 200, "message": "生成成功"}"
  849. // @Success 200 {string} string "{"code": -1, "message": "生成成功"}"
  850. // @Router /api/waybill/SecondaryDistribution [put]
  851. // @Security Bearer
  852. func (e WaybillController) SecondaryDistribution(c *gin.Context) {
  853. s := service.Waybill{}
  854. req := dto.WaybillAssignment{}
  855. err := e.MakeContext(c).
  856. MakeOrm().
  857. Bind(&req, binding.JSON).
  858. MakeService(&s.Service).
  859. Errors
  860. if err != nil {
  861. e.Logger.Error(err)
  862. e.Error(500, err, err.Error())
  863. return
  864. }
  865. //数据权限检查
  866. p := actions.GetPermissionFromContext(c)
  867. err = s.SecondaryDistribution(&req, p)
  868. if err != nil {
  869. e.Error(500, err, err.Error())
  870. return
  871. }
  872. e.OK(req.WaybillNo, "分配成功")
  873. }
  874. // GetCustomerPage 获取客户运单列表
  875. // @Summary 获取客户运单列表
  876. // @Description 获取客户运单列表
  877. // @Tags 运单
  878. // @Param waybillNo query string false "运单号"
  879. // @Param orderNo query string false "订单号"
  880. // @Param status query int false "状态"
  881. // @Param orderStartTime query string false "下单开始时间"
  882. // @Param orderEndTime query string false "下单结束时间"
  883. // @Param pageSize query int false "页条数"
  884. // @Param page query int false "页码"
  885. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  886. // @Router /api/waybill/customer [get]
  887. // @Security Bearer
  888. func (e WaybillController) GetCustomerPage(c *gin.Context) {
  889. s := service.Waybill{}
  890. req := dto.WaybillGetCustomerPageReq{}
  891. err := e.MakeContext(c).
  892. MakeOrm().
  893. Bind(&req, binding.Query).
  894. MakeService(&s.Service).
  895. Errors
  896. if err != nil {
  897. e.Logger.Error(err)
  898. e.Error(500, err, err.Error())
  899. return
  900. }
  901. //数据权限检查
  902. p := actions.GetPermissionFromContext(c)
  903. list := make([]model.Waybill, 0)
  904. var count int64
  905. req.CustomerId = p.UserId
  906. err = s.GetCustomerPage(&req, &list, &count, p)
  907. if err != nil {
  908. e.Error(500, err, err.Error())
  909. return
  910. }
  911. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  912. }
  913. // CustomerExport 导出客户运单列表
  914. // @Summary 导出客户运单列表
  915. // @Description 导出客户运单列表
  916. // @Tags 运单
  917. // @Param waybillNo query string false "运单号"
  918. // @Param orderNo query string false "订单号"
  919. // @Param status query int false "状态"
  920. // @Param orderStartTime query string false "下单开始时间"
  921. // @Param orderEndTime query string false "下单结束时间"
  922. // @Param pageSize query int false "页条数"
  923. // @Param page query int false "页码"
  924. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  925. // @Router /api/waybill/customer/export [get]
  926. // @Security Bearer
  927. func (e WaybillController) CustomerExport(c *gin.Context) {
  928. s := service.Waybill{}
  929. req := dto.WaybillGetCustomerPageReq{}
  930. err := e.MakeContext(c).
  931. MakeOrm().
  932. Bind(&req, binding.Query).
  933. MakeService(&s.Service).
  934. Errors
  935. if err != nil {
  936. e.Logger.Error(err)
  937. e.Error(500, err, err.Error())
  938. return
  939. }
  940. //数据权限检查
  941. p := actions.GetPermissionFromContext(c)
  942. list := make([]model.Waybill, 0)
  943. var count int64
  944. req.CustomerId = p.UserId
  945. req.PageSize = 9999
  946. err = s.GetCustomerPage(&req, &list, &count, p)
  947. if err != nil {
  948. e.Error(500, err, err.Error())
  949. return
  950. }
  951. f := excelize.NewFile() // 设置单元格的值
  952. // 这里设置表头ÒÒ
  953. f.SetCellValue("Sheet1", "A1", "序号")
  954. f.SetCellValue("Sheet1", "B1", "状态")
  955. f.SetCellValue("Sheet1", "C1", "寄件人名称")
  956. f.SetCellValue("Sheet1", "D1", "寄件人电话")
  957. f.SetCellValue("Sheet1", "E1", "寄件人地址")
  958. f.SetCellValue("Sheet1", "F1", "收件人名称")
  959. f.SetCellValue("Sheet1", "G1", "收件人电话")
  960. f.SetCellValue("Sheet1", "H1", "收件人地址")
  961. f.SetCellValue("Sheet1", "I1", "配送人姓名")
  962. f.SetCellValue("Sheet1", "J1", "配送人电话")
  963. f.SetCellValue("Sheet1", "K1", "运输备注")
  964. // 设置列宽
  965. f.SetColWidth("Sheet1", "A", "A", 6)
  966. f.SetColWidth("Sheet1", "B", "B", 8)
  967. f.SetColWidth("Sheet1", "C", "C", 14)
  968. f.SetColWidth("Sheet1", "D", "D", 14)
  969. f.SetColWidth("Sheet1", "E", "E", 30)
  970. f.SetColWidth("Sheet1", "F", "F", 14)
  971. f.SetColWidth("Sheet1", "G", "G", 14)
  972. f.SetColWidth("Sheet1", "H", "H", 30)
  973. f.SetColWidth("Sheet1", "I", "I", 15)
  974. f.SetColWidth("Sheet1", "J", "J", 15)
  975. f.SetColWidth("Sheet1", "K", "K", 15)
  976. line := 1
  977. // 循环写入数据
  978. for i, v := range list {
  979. line++
  980. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  981. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), model.WaybillStatusMap[v.Status])
  982. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.SenderAddressName)
  983. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.SenderAddressPhone)
  984. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.SenderAddressDetails)
  985. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.ConsigneeAddressName)
  986. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.ConsigneeAddressPhone)
  987. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.ConsigneeAddressDetails)
  988. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.DeliveryName)
  989. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.DeliveryPhone)
  990. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), v.Remark)
  991. }
  992. timeStr := time.Now().Format("20060102150405")
  993. filePath := "ofile/" + "运单" + timeStr + ".xlsx"
  994. defer func() {
  995. os.Remove(filePath)
  996. }()
  997. // 保存文件
  998. if err = f.SaveAs(filePath); err != nil {
  999. logs.Error("保存运单失败:", err)
  1000. }
  1001. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  1002. // PathEscape 函数对中文做处理
  1003. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape("运单"+timeStr+".xlsx"))
  1004. c.Header("Content-Transfer-Encoding", "binary")
  1005. c.File(filePath)
  1006. }
  1007. // CustomerInsert 客户添加运单
  1008. // @Summary 客户添加运单
  1009. // @Description 客户添加运单
  1010. // @Tags 运单
  1011. // @Accept application/json
  1012. // @Product application/json
  1013. // @Param data body dto.WaybillInsertReq true "data"
  1014. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1015. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1016. // @Router /api/waybill/customer [post]
  1017. // @Security Bearer
  1018. func (e WaybillController) CustomerInsert(c *gin.Context) {
  1019. s := service.Waybill{}
  1020. userSvc := service.SysUser{}
  1021. req := dto.WaybillInsertReq{}
  1022. err := e.MakeContext(c).
  1023. MakeOrm().
  1024. Bind(&req, binding.JSON).
  1025. MakeService(&s.Service).
  1026. MakeService(&userSvc.Service).
  1027. Errors
  1028. if err != nil {
  1029. e.Logger.Error(err)
  1030. e.Error(500, err, err.Error())
  1031. return
  1032. }
  1033. p := actions.GetPermissionFromContext(c)
  1034. var userObj model.SysUser
  1035. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  1036. if err != nil {
  1037. e.Error(500, err, "获取用户信息失败")
  1038. return
  1039. }
  1040. if p.DeptId == 0 && req.DeptId == 0 {
  1041. e.Error(500, err, "请先选择运输公司")
  1042. return
  1043. }
  1044. // 设置创建人
  1045. req.SetCreateBy(user.GetUserId(c))
  1046. if p.DeptId > 0 {
  1047. req.SetDeptId(p.DeptId)
  1048. }
  1049. err = s.Insert(&req)
  1050. if err != nil {
  1051. e.Error(500, err, err.Error())
  1052. return
  1053. }
  1054. e.OK(req.GetId(), "添加成功")
  1055. }
  1056. // Import 导入运单
  1057. // @Summary 导入运单
  1058. // @Description 导入运单
  1059. // @Tags 运单
  1060. // @Accept application/json
  1061. // @Product application/json
  1062. // @Param data body dto.WaybillImportReq true "data"
  1063. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1064. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1065. // @Router /api/waybill/import [post]
  1066. // @Security Bearer
  1067. func (e WaybillController) Import(c *gin.Context) {
  1068. s := service.Waybill{}
  1069. userSvc := service.SysUser{}
  1070. req := dto.WaybillImportReq{}
  1071. err := e.MakeContext(c).
  1072. MakeOrm().
  1073. Bind(&req, binding.Form).
  1074. MakeService(&s.Service).
  1075. MakeService(&userSvc.Service).
  1076. Errors
  1077. if err != nil {
  1078. e.Logger.Error(err)
  1079. e.Error(500, err, err.Error())
  1080. return
  1081. }
  1082. //读取第一fileName的文件
  1083. fileHeader, err := c.FormFile("file")
  1084. if err != nil {
  1085. err = errors.New("文件格式错误" + err.Error())
  1086. e.Logger.Error(err)
  1087. e.Error(500, err, err.Error())
  1088. return
  1089. }
  1090. if fileHeader.Size > 1024*1024*2 {
  1091. err = errors.New("文件大小超过2M")
  1092. e.Logger.Error(err)
  1093. e.Error(500, err, err.Error())
  1094. return
  1095. }
  1096. file, err := fileHeader.Open()
  1097. if err != nil {
  1098. err = errors.New("文件格式错误" + err.Error())
  1099. e.Logger.Error(err)
  1100. e.Error(500, err, err.Error())
  1101. return
  1102. }
  1103. defer file.Close()
  1104. xlsx, err := excelize.OpenReader(bufio.NewReader(file))
  1105. if err != nil {
  1106. err = errors.New("文件格式错误" + err.Error())
  1107. e.Logger.Error(err)
  1108. e.Error(500, err, err.Error())
  1109. return
  1110. }
  1111. p := actions.GetPermissionFromContext(c)
  1112. if p.DeptId == 0 {
  1113. e.Error(500, err, "获取用户信息失败")
  1114. return
  1115. }
  1116. var userObj model.SysUser
  1117. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  1118. if err != nil {
  1119. e.Error(500, err, "获取用户信息失败")
  1120. return
  1121. }
  1122. rows, _ := xlsx.GetRows("Sheet1")
  1123. for indexRow, row := range rows {
  1124. if indexRow == 0 {
  1125. continue
  1126. }
  1127. if len(row) < 10 {
  1128. for i := 0; i < 10-len(row); i++ {
  1129. row = append(row, "")
  1130. }
  1131. }
  1132. for i, colCell := range row {
  1133. fmt.Println(i, ":", colCell)
  1134. }
  1135. //quantity, _ := strconv.Atoi(row[8])
  1136. obj := dto.WaybillInsertReq{
  1137. Status: 1,
  1138. ConsigneeAddressName: row[0],
  1139. ConsigneeAddressPhone: row[1],
  1140. ConsigneeAddressDetails: row[2],
  1141. TamperProofLabel: row[3], // 防拆标签
  1142. Remark: row[4],
  1143. ReCheckId: p.UserId,
  1144. DeliveryId: p.UserId,
  1145. }
  1146. obj.SetDeptId(p.DeptId)
  1147. obj.SetCreateBy(user.GetUserId(c))
  1148. err = s.Insert(&obj)
  1149. if err != nil {
  1150. e.Error(500, err, err.Error())
  1151. return
  1152. }
  1153. }
  1154. e.OK(len(rows)-1, "导入成功")
  1155. }
  1156. // ExportTemplate 导出运单模板
  1157. // @Summary 导出运单模板
  1158. // @Description 导出运单模板
  1159. // @Tags 运单
  1160. // @Accept application/json
  1161. // @Product application/json
  1162. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1163. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1164. // @Router /api/waybill/export-template [get]
  1165. // @Security Bearer
  1166. func (e WaybillController) ExportTemplate(c *gin.Context) {
  1167. s := service.Waybill{}
  1168. err := e.MakeContext(c).
  1169. MakeOrm().
  1170. MakeService(&s.Service).
  1171. Errors
  1172. if err != nil {
  1173. e.Logger.Error(err)
  1174. e.Error(500, err, err.Error())
  1175. return
  1176. }
  1177. filePath := "./ofile/运单导入模板.xlsx"
  1178. //打开文件
  1179. fileTmp, errByOpenFile := os.Open(filePath)
  1180. defer fileTmp.Close()
  1181. //获取文件的名称
  1182. fileName := path.Base(filePath)
  1183. if errByOpenFile != nil {
  1184. e.Logger.Error(err)
  1185. e.Error(500, err, err.Error())
  1186. return
  1187. }
  1188. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  1189. // PathEscape 函数对中文做处理
  1190. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  1191. c.Header("Content-Transfer-Encoding", "binary")
  1192. c.File(filePath)
  1193. }
  1194. // TemperaturePDF 导出温度记录
  1195. // @Summary 导出温度记录
  1196. // @Description 导出温度记录
  1197. // @Tags 运单
  1198. // @Accept application/json
  1199. // @Product application/json
  1200. // @Param data body dto.WaybillGetByWaybillPdfReq true "data"
  1201. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1202. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1203. // @Router /api/waybill/temperature-pdf [get]
  1204. // @Security Bearer
  1205. func (e WaybillController) TemperaturePDF(c *gin.Context) {
  1206. s := service.Waybill{}
  1207. req := dto.WaybillGetByWaybillPdfReq{}
  1208. err := e.MakeContext(c).
  1209. MakeOrm().
  1210. Bind(&req, binding.Query).
  1211. MakeService(&s.Service).
  1212. Errors
  1213. if err != nil {
  1214. e.Logger.Error(err)
  1215. e.Error(500, err, err.Error())
  1216. return
  1217. }
  1218. var waybill model.Waybill
  1219. //p := actions.GetPermissionFromContext(c)
  1220. //err = s.GetByWaybillNo(&req, &waybill, p)
  1221. err = s.GetByWaybillNo(&req, &waybill, nil)
  1222. if err != nil {
  1223. e.Error(500, err, err.Error())
  1224. return
  1225. }
  1226. DeviceSensor_data, waybillPDF, err := s.GetAllData(&dto.WaybillGetByWaybillNoReq{WaybillNo: req.WaybillNo})
  1227. // 最高温度、最低温度、最高湿度、最低湿度
  1228. var maxTemp, minTemp, maxHumidity, minHumidity float32
  1229. // 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
  1230. var maxTempTime, minTempTime, maxHumidityTime, minHumidityTime string
  1231. // 总温度 总湿度
  1232. var totalTemp, totalHumidity float32
  1233. // 平均温度 平均湿度
  1234. var avgTemp, avgHumidity float32
  1235. // 温度阈值,湿度阈值
  1236. var tempThreshold, humidityThreshold string
  1237. // 记录开始时间,记录结束时间
  1238. var s_time, e_time string
  1239. var lastTime string
  1240. var isFirst, isSecond = true, false
  1241. var first_column, second_column []nats_server.DeviceData_R
  1242. if len(DeviceSensor_data) > 0 {
  1243. tempThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1244. humidityThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_rhl, DeviceSensor_data[0].T_rhu)
  1245. s_time = DeviceSensor_data[0].T_time
  1246. e_time = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1247. // 最高温度及时刻
  1248. maxTemp = DeviceSensor_data[0].T_t
  1249. maxTempTime = DeviceSensor_data[0].T_time
  1250. // 最低温度及时刻
  1251. minTemp = DeviceSensor_data[0].T_t
  1252. minTempTime = DeviceSensor_data[0].T_time
  1253. // 最高湿度及时刻
  1254. maxHumidity = DeviceSensor_data[0].T_rh
  1255. maxHumidityTime = DeviceSensor_data[0].T_time
  1256. // 获取最低湿度及时刻
  1257. minHumidity = DeviceSensor_data[0].T_rh
  1258. minHumidityTime = DeviceSensor_data[0].T_time
  1259. for i := 0; i < len(DeviceSensor_data); i++ {
  1260. data := DeviceSensor_data[i]
  1261. if data.T_t > maxTemp {
  1262. maxTemp = data.T_t
  1263. maxTempTime = data.T_time
  1264. }
  1265. if data.T_t < minTemp {
  1266. minTemp = data.T_t
  1267. minTempTime = data.T_time
  1268. }
  1269. totalTemp += data.T_t
  1270. if data.T_rh > maxHumidity {
  1271. maxHumidity = data.T_rh
  1272. maxHumidityTime = data.T_time
  1273. }
  1274. if data.T_rh < minHumidity {
  1275. minHumidity = data.T_rh
  1276. minHumidityTime = data.T_time
  1277. }
  1278. totalHumidity += data.T_rh
  1279. }
  1280. var sn string
  1281. for _, w := range waybillPDF {
  1282. for _, data := range w.Data {
  1283. if len(lastTime) > 0 {
  1284. if lastTime != data.T_time && isFirst == true {
  1285. isFirst = false
  1286. isSecond = true
  1287. } else if lastTime != data.T_time && isSecond == true {
  1288. isFirst = true
  1289. isSecond = false
  1290. }
  1291. }
  1292. if len(sn) > 0 && sn != data.T_sn && len(first_column)%2 == 1 {
  1293. isFirst, isSecond = isSecond, isFirst
  1294. }
  1295. if isFirst {
  1296. first_column = append(first_column, data)
  1297. lastTime = data.T_time
  1298. sn = data.T_sn
  1299. }
  1300. if isSecond {
  1301. second_column = append(second_column, data)
  1302. lastTime = data.T_time
  1303. sn = data.T_sn
  1304. }
  1305. }
  1306. }
  1307. // 平均温度
  1308. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1309. // 平均湿度
  1310. avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1311. }
  1312. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度结束
  1313. pdf := &gopdf.GoPdf{}
  1314. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1315. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1316. if err != nil {
  1317. return
  1318. }
  1319. err = pdf.SetFont("wts", "", 20)
  1320. if err != nil {
  1321. return
  1322. }
  1323. pdf.SetGrayFill(0.5)
  1324. pdf.SetMargins(0, 20, 0, 20)
  1325. pdf.AddPage()
  1326. title := "运单" + req.WaybillNo + "温度记录"
  1327. if req.HumidityShow {
  1328. title = "运单" + req.WaybillNo + "温湿度记录"
  1329. }
  1330. var y float64 = 40
  1331. textw, _ := pdf.MeasureTextWidth(title)
  1332. pdf.SetX((595 / 2) - (textw / 2))
  1333. pdf.SetY(y)
  1334. pdf.Text(title)
  1335. //y += 30
  1336. //pdf.SetFont("wts", "", 16)
  1337. //pdf.SetXY(10, y)
  1338. //pdf.Text("实施设备信息")
  1339. //// 线
  1340. //y += 10
  1341. //pdf.SetLineWidth(0.5)
  1342. //pdf.SetStrokeColor(169, 169, 169)
  1343. //pdf.Line(10, y, 585, y)
  1344. //pdf.SetFont("wts", "", 10)
  1345. //y += 20
  1346. //pdf.SetXY(10, y)
  1347. //pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1348. //pdf.SetXY(300, y)
  1349. //pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1350. y += 25
  1351. pdf.SetFont("wts", "", 16)
  1352. pdf.SetXY(10, y)
  1353. pdf.Text("记录概要信息")
  1354. // 线
  1355. y += 10
  1356. pdf.SetLineWidth(0.5)
  1357. pdf.SetStrokeColor(169, 169, 169)
  1358. pdf.Line(10, y, 585, y)
  1359. y += 20
  1360. pdf.SetFont("wts", "", 10)
  1361. pdf.SetXY(10, y)
  1362. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1363. pdf.SetXY(240, y)
  1364. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1365. sTime, _ := lib.TimeStrToTime(s_time)
  1366. eTime, _ := lib.TimeStrToTime(e_time)
  1367. pdf.SetXY(470, y)
  1368. minutes := int(eTime.Sub(sTime).Minutes())
  1369. hours := minutes / 60
  1370. remainingMinutes := minutes % 60
  1371. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1372. // -------------最高温/湿度 最低温/湿度 平均温/湿度
  1373. y += 15
  1374. pdf.SetXY(10, y)
  1375. pdf.Text(fmt.Sprintf("最高温度:%.1f℃,%s", lib.RoundToDecimal(float64(maxTemp), 1), maxTempTime))
  1376. pdf.SetXY(240, y)
  1377. pdf.Text(fmt.Sprintf("最低温度:%.1f℃,%s", lib.RoundToDecimal(float64(minTemp), 1), minTempTime))
  1378. pdf.SetXY(470, y)
  1379. pdf.Text(fmt.Sprintf("平均温度:%.1f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1380. if req.HumidityShow {
  1381. y += 15
  1382. pdf.SetXY(10, y)
  1383. pdf.Text(fmt.Sprintf("最高湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(maxHumidity), 1), maxHumidityTime))
  1384. pdf.SetXY(240, y)
  1385. pdf.Text(fmt.Sprintf("最低湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(minHumidity), 1), minHumidityTime))
  1386. pdf.SetXY(470, y)
  1387. pdf.Text(fmt.Sprintf("平均湿度:%.1f%%RH", lib.RoundToDecimal(float64(avgHumidity), 1)))
  1388. }
  1389. // -------------温/湿度阈值
  1390. y += 15
  1391. pdf.SetXY(10, y)
  1392. pdf.Text(fmt.Sprintf("温度阈值:%s℃", tempThreshold))
  1393. if req.HumidityShow {
  1394. pdf.SetXY(240, y)
  1395. pdf.Text(fmt.Sprintf("温度阈值:%s%%", humidityThreshold))
  1396. }
  1397. //-------------发货单位,收货单位,备注
  1398. y += 15
  1399. pdf.SetXY(10, y)
  1400. T_forwarding_unit_temp := []rune(waybill.SenderAddressName)
  1401. if len(T_forwarding_unit_temp) > 17 {
  1402. pdf.Text(fmt.Sprintf("发货人:%s", string(T_forwarding_unit_temp[0:17])))
  1403. pdf.SetXY(60, y+15)
  1404. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1405. } else {
  1406. pdf.Text(fmt.Sprintf("发货人:%s", string(T_forwarding_unit_temp)))
  1407. }
  1408. pdf.SetXY(240, y)
  1409. T_consignee_unit_temp := []rune(waybill.ConsigneeAddressName)
  1410. if len(T_consignee_unit_temp) > 17 {
  1411. pdf.Text(fmt.Sprintf("收货人:%s", string(T_consignee_unit_temp[0:17])))
  1412. pdf.SetXY(290, y+15)
  1413. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1414. } else {
  1415. pdf.Text(fmt.Sprintf("收货人:%s", string(T_consignee_unit_temp)))
  1416. }
  1417. // 承运方
  1418. y += 15
  1419. pdf.SetXY(10, y)
  1420. company_name := []rune(waybill.Dept.Name)
  1421. pdf.Text(fmt.Sprintf("承运方:%s", string(company_name)))
  1422. y += 15
  1423. pdf.SetXY(10, y)
  1424. T_remark_temp := []rune(waybill.Remark)
  1425. if len(waybill.Remark) > 35 {
  1426. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:35])))
  1427. pdf.SetXY(10, y+15)
  1428. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[35:])))
  1429. } else {
  1430. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1431. }
  1432. y += 15
  1433. //pdf.SetFont("wts", "", 16)
  1434. //pdf.SetXY(10, y)
  1435. //pdf.Text("记录曲线信息")
  1436. //// 线
  1437. //y += 10
  1438. //pdf.SetLineWidth(0.5)
  1439. //pdf.SetStrokeColor(169, 169, 169)
  1440. //pdf.Line(10, y, 585, y)
  1441. //y += 1
  1442. //
  1443. //var tempFilepath string
  1444. //tempFilepath, err = DeviceDataTemperatureJPG2(s_time, e_time, waybillPDF)
  1445. //if err == nil {
  1446. // imgH, _ := gopdf.ImageHolderByPath(tempFilepath)
  1447. // pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1448. // y += 315
  1449. //}
  1450. //
  1451. //var humidityFilepath string
  1452. //if req.HumidityShow {
  1453. // humidityFilepath, err = DeviceDataHumidityJPG2(s_time, e_time, waybillPDF)
  1454. // if err == nil {
  1455. // imgH, _ := gopdf.ImageHolderByPath(humidityFilepath)
  1456. // pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1457. // y += 315
  1458. // }
  1459. //}
  1460. if y > 841.89 {
  1461. // 图片结束直接分页
  1462. pdf.AddPage()
  1463. y = 20
  1464. }
  1465. y += 20
  1466. pdf.SetFont("wts", "", 16)
  1467. pdf.SetXY(10, y)
  1468. pdf.Text("记录数据信息")
  1469. // 线
  1470. y += 10
  1471. pdf.SetLineWidth(0.5)
  1472. pdf.SetStrokeColor(169, 169, 169)
  1473. pdf.Line(10, y, 585, y)
  1474. y += 10
  1475. pdf.SetFont("wts", "", 10)
  1476. var x float64 = 10
  1477. var w float64 = 112
  1478. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1479. x = x + w
  1480. w = 101
  1481. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1482. if req.HumidityShow {
  1483. x = x + w
  1484. w = 37
  1485. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1486. x = x + w
  1487. w = 37
  1488. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1489. } else {
  1490. x = x + w
  1491. w = 37 * 2
  1492. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1493. }
  1494. x = x + w
  1495. w = 112
  1496. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1497. x = x + w
  1498. w = 101
  1499. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1500. if req.HumidityShow {
  1501. x = x + w
  1502. w = 37
  1503. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1504. x = x + w
  1505. w = 37
  1506. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1507. } else {
  1508. x = x + w
  1509. w = 37 * 2
  1510. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1511. }
  1512. y += 20
  1513. var textH float64 = 25 // if text height is 25px.
  1514. for i, v := range first_column {
  1515. pdf.SetNewY(y, textH)
  1516. y = pdf.GetY()
  1517. x, w = 10, 112
  1518. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1519. x = x + w
  1520. w = 101
  1521. lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1522. // 显示温湿度
  1523. if req.HumidityShow {
  1524. x = x + w
  1525. w = 37
  1526. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1527. x = x + w
  1528. w = 37
  1529. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1530. } else {
  1531. x = x + w
  1532. w = 37 * 2
  1533. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1534. }
  1535. if i < len(second_column) {
  1536. x = x + w
  1537. w = 112
  1538. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1539. x = x + w
  1540. w = 101
  1541. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1542. if req.HumidityShow {
  1543. x = x + w
  1544. w = 37
  1545. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1546. x = x + w
  1547. w = 37
  1548. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1549. } else {
  1550. x = x + w
  1551. w = 37 * 2
  1552. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1553. }
  1554. }
  1555. y += 20
  1556. }
  1557. if len(second_column) > len(first_column) {
  1558. for i := len(first_column); i < len(second_column); i++ {
  1559. pdf.SetNewY(y, textH)
  1560. y = pdf.GetY()
  1561. x, w = 297, 112
  1562. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1563. x = x + w
  1564. w = 101
  1565. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1566. if req.HumidityShow {
  1567. x = x + w
  1568. w = 37
  1569. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1570. x = x + w
  1571. w = 37
  1572. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1573. } else {
  1574. x = x + w
  1575. w = 37 * 2
  1576. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1577. }
  1578. y += 20
  1579. }
  1580. }
  1581. filename := "运单" + req.WaybillNo + "温湿度记录" + time.Now().Format("20060102150405") + ".pdf"
  1582. filePath := "ofile/" + filename
  1583. err = pdf.WritePdf(filePath)
  1584. if err != nil {
  1585. return
  1586. }
  1587. defer func() {
  1588. //os.Remove(tempFilepath)
  1589. //os.Remove(humidityFilepath)
  1590. os.Remove(filePath)
  1591. }()
  1592. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(filename))
  1593. c.Header("Content-Transfer-Encoding", "binary")
  1594. c.File(filePath)
  1595. }
  1596. // 获取温度图片
  1597. func DeviceDataTemperatureJPG(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1598. if len(waybillPDF) == 0 {
  1599. return "", errors.New("暂无数据可生成图片")
  1600. }
  1601. // 创建一个新的绘图
  1602. p := plot.New()
  1603. // 设置绘图标题和标签
  1604. p.Title.Text = "temperature"
  1605. //p.Legend.ThumbnailWidth = 20
  1606. deviceSensorList := []nats_server.DeviceSensor_R{}
  1607. dataList := []nats_server.DeviceData_R{}
  1608. for _, w := range waybillPDF {
  1609. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1610. dataList = append(dataList, w.Data...)
  1611. }
  1612. TemperatureMin := deviceSensorList[0].T_DeviceSensorParameter.T_Tlower
  1613. TemperatureMax := deviceSensorList[0].T_DeviceSensorParameter.T_Tupper
  1614. var ymin, ymax float32
  1615. for i, r := range dataList {
  1616. if i == 0 {
  1617. ymin = r.T_t
  1618. ymax = r.T_t
  1619. }
  1620. if ymin > r.T_t {
  1621. ymin = r.T_t
  1622. }
  1623. if ymax < r.T_t {
  1624. ymax = r.T_t
  1625. }
  1626. }
  1627. var chData = make(chan int, 10)
  1628. var jobGroup sync.WaitGroup
  1629. // 创建温度线
  1630. for i := 0; i < len(deviceSensorList); i++ {
  1631. chData <- 1
  1632. jobGroup.Add(1)
  1633. go func(index int) {
  1634. defer func() {
  1635. <-chData // 完成时chan取出1个
  1636. jobGroup.Done() // 完成时将等待组值减1
  1637. }()
  1638. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1639. r_maps := []nats_server.DeviceData_R{}
  1640. for _, data := range dataList {
  1641. if data.T_sn == sn && data.T_id == id {
  1642. r_maps = append(r_maps, data)
  1643. }
  1644. }
  1645. fmt.Println(r_maps)
  1646. if len(r_maps) == 0 {
  1647. return
  1648. }
  1649. sort.Slice(r_maps, func(i, j int) bool {
  1650. return r_maps[i].T_time < r_maps[j].T_time
  1651. })
  1652. pts := make(plotter.XYs, len(r_maps))
  1653. for j, d := range r_maps {
  1654. t, _ := lib.TimeStrToTime(d.T_time)
  1655. pts[j].X = float64(t.Unix())
  1656. pts[j].Y = float64(d.T_t)
  1657. }
  1658. line, err := plotter.NewLine(pts)
  1659. if err != nil {
  1660. return
  1661. }
  1662. line.Color = randomColor(index)
  1663. p.Add(line)
  1664. }(i)
  1665. }
  1666. jobGroup.Wait()
  1667. st, _ := lib.TimeStrToTime(startTime)
  1668. et, _ := lib.TimeStrToTime(endTime)
  1669. xmin, xmax := float64(st.Unix()), float64(et.Unix())
  1670. // 添加最高,最低标准线 用红色虚线标识
  1671. p.Add(horizontalLine(xmin, xmax, float64(TemperatureMin)))
  1672. p.Add(horizontalLine(xmin, xmax, float64(TemperatureMax)))
  1673. if ymax < TemperatureMax {
  1674. ymax = TemperatureMax
  1675. }
  1676. if ymin > 0 {
  1677. ymin = 0
  1678. }
  1679. p.Y.Min, p.Y.Max = float64(ymin), float64(ymax)
  1680. p.X.Min, p.X.Max = xmin, xmax
  1681. p.Y.Tick.Marker = commaTicks{}
  1682. p.X.Tick.Marker = timeTicks{}
  1683. p.X.Tick.Label.Rotation = math.Pi / 5
  1684. p.X.Tick.Label.YAlign = draw.YCenter
  1685. p.X.Tick.Label.XAlign = draw.XRight
  1686. filepath := "ofile/" + "temperature" + deviceSensorList[0].T_sn + ".jpg"
  1687. // 保存文件
  1688. if err := p.Save(10*vg.Inch, 4*vg.Inch, filepath); err != nil {
  1689. logs.Error(lib.FuncName(), "生成图片失败", err)
  1690. return "", err
  1691. }
  1692. return filepath, nil
  1693. }
  1694. func DeviceDataTemperatureJPG2(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1695. if len(waybillPDF) == 0 {
  1696. return "", errors.New("暂无数据可生成图片")
  1697. }
  1698. deviceSensorList := []nats_server.DeviceSensor_R{}
  1699. dataList := []nats_server.DeviceData_R{}
  1700. for _, w := range waybillPDF {
  1701. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1702. dataList = append(dataList, w.Data...)
  1703. }
  1704. TemperatureMin := deviceSensorList[0].T_DeviceSensorParameter.T_Tlower
  1705. TemperatureMax := deviceSensorList[0].T_DeviceSensorParameter.T_Tupper
  1706. var ymin, ymax float32
  1707. for i, r := range dataList {
  1708. if i == 0 {
  1709. ymin = r.T_t
  1710. ymax = r.T_t
  1711. }
  1712. if ymin > r.T_t {
  1713. ymin = r.T_t
  1714. }
  1715. if ymax < r.T_t {
  1716. ymax = r.T_t
  1717. }
  1718. }
  1719. series := make([]chart.Series, 0)
  1720. var chData = make(chan int, 10)
  1721. var jobGroup sync.WaitGroup
  1722. // 创建温度线
  1723. for i := 0; i < len(deviceSensorList); i++ {
  1724. chData <- 1
  1725. jobGroup.Add(1)
  1726. go func(index int) {
  1727. defer func() {
  1728. <-chData // 完成时chan取出1个
  1729. jobGroup.Done() // 完成时将等待组值减1
  1730. }()
  1731. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1732. r_maps := []nats_server.DeviceData_R{}
  1733. for _, data := range dataList {
  1734. if data.T_sn == sn && data.T_id == id {
  1735. r_maps = append(r_maps, data)
  1736. }
  1737. }
  1738. fmt.Println(r_maps)
  1739. if len(r_maps) == 0 {
  1740. return
  1741. }
  1742. sort.Slice(r_maps, func(i, j int) bool {
  1743. return r_maps[i].T_time < r_maps[j].T_time
  1744. })
  1745. xValues := make([]time.Time, len(r_maps))
  1746. yValues := make([]float64, len(r_maps))
  1747. for j := 0; j < len(r_maps); j++ {
  1748. t, _ := lib.TimeStrToTime(r_maps[j].T_time)
  1749. xValues[j] = t
  1750. yValues[j] = float64(r_maps[j].T_t)
  1751. }
  1752. series = append(series, chart.TimeSeries{
  1753. Name: fmt.Sprintf("%s-%d", sn, id),
  1754. XValues: xValues,
  1755. YValues: yValues,
  1756. })
  1757. }(i)
  1758. }
  1759. jobGroup.Wait()
  1760. if ymax < TemperatureMax {
  1761. ymax = TemperatureMax
  1762. }
  1763. if ymin > 0 {
  1764. ymin = 0
  1765. }
  1766. if ymin > TemperatureMin {
  1767. ymin = TemperatureMin
  1768. }
  1769. st, _ := lib.TimeStrToTime(startTime)
  1770. et, _ := lib.TimeStrToTime(endTime)
  1771. series = append(series, chart.TimeSeries{
  1772. Style: chart.Style{
  1773. StrokeColor: drawing.ColorRed,
  1774. StrokeDashArray: []float64{5.0, 5.0},
  1775. },
  1776. XValues: []time.Time{st, et},
  1777. YValues: []float64{float64(TemperatureMin), float64(TemperatureMin)},
  1778. })
  1779. series = append(series, chart.TimeSeries{
  1780. Style: chart.Style{
  1781. StrokeColor: drawing.ColorRed,
  1782. StrokeDashArray: []float64{5.0, 5.0},
  1783. },
  1784. XValues: []time.Time{st, et},
  1785. YValues: []float64{float64(TemperatureMax), float64(TemperatureMax)},
  1786. })
  1787. font := getZWFont()
  1788. graph := chart.Chart{
  1789. Title: "温度记录",
  1790. TitleStyle: chart.Style{
  1791. FontSize: 15,
  1792. },
  1793. Background: chart.Style{
  1794. Padding: chart.Box{
  1795. Top: 20,
  1796. },
  1797. },
  1798. Font: font,
  1799. XAxis: chart.XAxis{
  1800. Name: "时间",
  1801. ValueFormatter: chart.TimeValueFormatterWithFormat("2006-01-02 15:04"),
  1802. },
  1803. YAxis: chart.YAxis{
  1804. Name: "温度",
  1805. Range: &chart.ContinuousRange{
  1806. Min: float64(ymin),
  1807. Max: float64(ymax + 2),
  1808. },
  1809. },
  1810. Series: series,
  1811. }
  1812. //graph.Elements = []chart.Renderable{
  1813. // chart.Legend(&graph),
  1814. //}
  1815. filepath := "ofile/" + "temperature" + deviceSensorList[0].T_sn + ".jpg"
  1816. f, _ := os.Create(filepath)
  1817. defer f.Close()
  1818. graph.Render(chart.PNG, f)
  1819. return filepath, nil
  1820. }
  1821. // 获取湿度图片
  1822. func DeviceDataHumidityJPG(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1823. if len(waybillPDF) == 0 {
  1824. return "", errors.New("暂无数据可生成图片")
  1825. }
  1826. // 创建一个新的绘图
  1827. p := plot.New()
  1828. // 设置绘图标题和标签
  1829. p.Title.Text = "humidity"
  1830. deviceSensorList := []nats_server.DeviceSensor_R{}
  1831. dataList := []nats_server.DeviceData_R{}
  1832. for _, w := range waybillPDF {
  1833. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1834. dataList = append(dataList, w.Data...)
  1835. }
  1836. humidityMin := deviceSensorList[0].T_DeviceSensorParameter.T_RHlower
  1837. humidityMax := deviceSensorList[0].T_DeviceSensorParameter.T_RHupper
  1838. var ymin, ymax float32
  1839. for i, r := range dataList {
  1840. if i == 0 {
  1841. ymin = r.T_rh
  1842. ymax = r.T_rh
  1843. }
  1844. if ymin > r.T_rh {
  1845. ymin = r.T_rh
  1846. }
  1847. if ymax < r.T_rh {
  1848. ymax = r.T_rh
  1849. }
  1850. }
  1851. var chData = make(chan int, 10)
  1852. var jobGroup sync.WaitGroup
  1853. // 创建温度线
  1854. for i := 0; i < len(deviceSensorList); i++ {
  1855. chData <- 1
  1856. jobGroup.Add(1)
  1857. go func(index int) {
  1858. defer func() {
  1859. <-chData // 完成时chan取出1个
  1860. jobGroup.Done() // 完成时将等待组值减1
  1861. }()
  1862. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1863. r_maps := []nats_server.DeviceData_R{}
  1864. for _, data := range dataList {
  1865. if data.T_sn == sn && data.T_id == id {
  1866. r_maps = append(r_maps, data)
  1867. }
  1868. }
  1869. if len(r_maps) == 0 {
  1870. return
  1871. }
  1872. sort.Slice(r_maps, func(i, j int) bool {
  1873. return r_maps[i].T_time < r_maps[j].T_time
  1874. })
  1875. pts := make(plotter.XYs, len(r_maps))
  1876. for j, d := range r_maps {
  1877. t, _ := lib.TimeStrToTime(d.T_time)
  1878. pts[j].X = float64(t.Unix())
  1879. pts[j].Y = float64(d.T_rh)
  1880. }
  1881. line, err := plotter.NewLine(pts)
  1882. if err != nil {
  1883. return
  1884. }
  1885. line.Color = randomColor(index)
  1886. p.Add(line)
  1887. }(i)
  1888. }
  1889. jobGroup.Wait()
  1890. st, _ := lib.TimeStrToTime(startTime)
  1891. et, _ := lib.TimeStrToTime(endTime)
  1892. xmin, xmax := float64(st.Unix()), float64(et.Unix())
  1893. // 添加最高,最低标准线 用红色虚线标识
  1894. p.Add(horizontalLine(xmin, xmax, float64(humidityMin)))
  1895. p.Add(horizontalLine(xmin, xmax, float64(humidityMax)))
  1896. if ymax < humidityMax {
  1897. ymax = humidityMax
  1898. }
  1899. if ymin > 0 {
  1900. ymin = 0
  1901. }
  1902. p.Y.Min, p.Y.Max = float64(ymin), float64(ymax)
  1903. p.X.Min, p.X.Max = xmin, xmax
  1904. p.Y.Tick.Marker = commaTicks{}
  1905. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  1906. p.X.Tick.Marker = timeTicks{}
  1907. p.X.Tick.Label.Rotation = math.Pi / 5
  1908. p.X.Tick.Label.YAlign = draw.YCenter
  1909. p.X.Tick.Label.XAlign = draw.XRight
  1910. filepath := "ofile/" + "humidity" + deviceSensorList[0].T_sn + ".jpg"
  1911. // 保存文件
  1912. if err := p.Save(10*vg.Inch, 4*vg.Inch, filepath); err != nil {
  1913. logs.Error(lib.FuncName(), "生成图片失败", err)
  1914. return "", err
  1915. }
  1916. return filepath, nil
  1917. }
  1918. func DeviceDataHumidityJPG2(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1919. if len(waybillPDF) == 0 {
  1920. return "", errors.New("暂无数据可生成图片")
  1921. }
  1922. deviceSensorList := []nats_server.DeviceSensor_R{}
  1923. dataList := []nats_server.DeviceData_R{}
  1924. for _, w := range waybillPDF {
  1925. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1926. dataList = append(dataList, w.Data...)
  1927. }
  1928. humidityMin := deviceSensorList[0].T_DeviceSensorParameter.T_RHlower
  1929. humidityMax := deviceSensorList[0].T_DeviceSensorParameter.T_RHupper
  1930. var ymin, ymax float32
  1931. for i, r := range dataList {
  1932. if i == 0 {
  1933. ymin = r.T_t
  1934. ymax = r.T_t
  1935. }
  1936. if ymin > r.T_t {
  1937. ymin = r.T_t
  1938. }
  1939. if ymax < r.T_t {
  1940. ymax = r.T_t
  1941. }
  1942. }
  1943. series := make([]chart.Series, 0)
  1944. var chData = make(chan int, 10)
  1945. var jobGroup sync.WaitGroup
  1946. // 创建温度线
  1947. for i := 0; i < len(deviceSensorList); i++ {
  1948. chData <- 1
  1949. jobGroup.Add(1)
  1950. go func(index int) {
  1951. defer func() {
  1952. <-chData // 完成时chan取出1个
  1953. jobGroup.Done() // 完成时将等待组值减1
  1954. }()
  1955. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1956. r_maps := []nats_server.DeviceData_R{}
  1957. for _, data := range dataList {
  1958. if data.T_sn == sn && data.T_id == id {
  1959. r_maps = append(r_maps, data)
  1960. }
  1961. }
  1962. if len(r_maps) == 0 {
  1963. return
  1964. }
  1965. sort.Slice(r_maps, func(i, j int) bool {
  1966. return r_maps[i].T_time < r_maps[j].T_time
  1967. })
  1968. xValues := make([]time.Time, len(r_maps))
  1969. yValues := make([]float64, len(r_maps))
  1970. for j := 0; j < len(r_maps); j++ {
  1971. t, _ := lib.TimeStrToTime(r_maps[j].T_time)
  1972. xValues[j] = t
  1973. yValues[j] = float64(r_maps[j].T_rh)
  1974. }
  1975. series = append(series, chart.TimeSeries{
  1976. Name: fmt.Sprintf("%s-%d", sn, id),
  1977. XValues: xValues,
  1978. YValues: yValues,
  1979. })
  1980. }(i)
  1981. }
  1982. jobGroup.Wait()
  1983. if ymax < humidityMax {
  1984. ymax = humidityMax
  1985. }
  1986. if ymin > 0 {
  1987. ymin = 0
  1988. }
  1989. if ymin > humidityMin {
  1990. ymin = humidityMin
  1991. }
  1992. st, _ := lib.TimeStrToTime(startTime)
  1993. et, _ := lib.TimeStrToTime(endTime)
  1994. series = append(series, chart.TimeSeries{
  1995. Style: chart.Style{
  1996. StrokeColor: drawing.ColorRed,
  1997. StrokeDashArray: []float64{5.0, 5.0},
  1998. },
  1999. XValues: []time.Time{st, et},
  2000. YValues: []float64{float64(humidityMin), float64(humidityMin)},
  2001. })
  2002. series = append(series, chart.TimeSeries{
  2003. Style: chart.Style{
  2004. StrokeColor: drawing.ColorRed,
  2005. StrokeDashArray: []float64{5.0, 5.0},
  2006. },
  2007. XValues: []time.Time{st, et},
  2008. YValues: []float64{float64(humidityMax), float64(humidityMax)},
  2009. })
  2010. font := getZWFont()
  2011. graph := chart.Chart{
  2012. Title: "湿度记录",
  2013. TitleStyle: chart.Style{
  2014. FontSize: 15,
  2015. },
  2016. Background: chart.Style{
  2017. Padding: chart.Box{
  2018. Top: 20,
  2019. },
  2020. },
  2021. Font: font,
  2022. XAxis: chart.XAxis{
  2023. Name: "时间",
  2024. ValueFormatter: chart.TimeValueFormatterWithFormat("2006-01-02 15:04"),
  2025. },
  2026. YAxis: chart.YAxis{
  2027. Name: "湿度",
  2028. Range: &chart.ContinuousRange{
  2029. Min: float64(ymin),
  2030. Max: float64(ymax + 2),
  2031. },
  2032. },
  2033. Series: series,
  2034. }
  2035. //graph.Elements = []chart.Renderable{
  2036. // chart.Legend(&graph),
  2037. //}
  2038. filepath := "ofile/" + "humidity" + deviceSensorList[0].T_sn + ".jpg"
  2039. f, _ := os.Create(filepath)
  2040. defer f.Close()
  2041. graph.Render(chart.PNG, f)
  2042. return filepath, nil
  2043. }
  2044. func horizontalLine(xmin, xmax, y float64) *plotter.Line {
  2045. pts := make(plotter.XYs, 2)
  2046. pts[0].X = xmin
  2047. pts[0].Y = y
  2048. pts[1].X = xmax
  2049. pts[1].Y = y
  2050. line, err := plotter.NewLine(pts)
  2051. if err != nil {
  2052. panic(err)
  2053. }
  2054. line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
  2055. line.Color = color.RGBA{R: 255, A: 255}
  2056. return line
  2057. }
  2058. type timeTicks struct{}
  2059. func (timeTicks) Ticks(min, max float64) []plot.Tick {
  2060. tks := plot.TimeTicks{}.Ticks(min, max)
  2061. for i, t := range tks {
  2062. //if t.Label == "" { // Skip minor ticks, they are fine.
  2063. // continue
  2064. //}
  2065. tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
  2066. }
  2067. return tks
  2068. }
  2069. type commaTicks struct{}
  2070. // Ticks computes the default tick marks, but inserts commas
  2071. // into the labels for the major tick marks.
  2072. func (commaTicks) Ticks(min, max float64) []plot.Tick {
  2073. tks := plot.DefaultTicks{}.Ticks(min, max)
  2074. for i, t := range tks {
  2075. //if t.Label == "" { // Skip minor ticks, they are fine.
  2076. // continue
  2077. //}
  2078. tks[i].Label = fmt.Sprintf("%.0f", t.Value)
  2079. }
  2080. return tks
  2081. }
  2082. // 生成随机颜色的辅助函数
  2083. func randomColor(i int) color.RGBA {
  2084. var colors []color.RGBA
  2085. colors = append(colors,
  2086. color.RGBA{R: 52, G: 152, B: 219, A: 255},
  2087. color.RGBA{R: 230, G: 126, B: 34, A: 255},
  2088. color.RGBA{R: 142, G: 68, B: 173, A: 255},
  2089. color.RGBA{R: 211, G: 84, B: 0, A: 255},
  2090. color.RGBA{R: 231, G: 76, B: 60, A: 255},
  2091. color.RGBA{R: 26, G: 188, B: 156, A: 255},
  2092. color.RGBA{R: 243, G: 156, B: 18, A: 255},
  2093. color.RGBA{R: 22, G: 160, B: 133, A: 255},
  2094. color.RGBA{R: 46, G: 204, B: 113, A: 255},
  2095. color.RGBA{R: 39, G: 174, B: 96, A: 255},
  2096. color.RGBA{R: 41, G: 128, B: 185, A: 255},
  2097. color.RGBA{R: 155, G: 89, B: 182, A: 255},
  2098. color.RGBA{R: 192, G: 57, B: 43, A: 255},
  2099. color.RGBA{R: 241, G: 196, B: 15, A: 255},
  2100. )
  2101. return colors[i%len(colors)]
  2102. }
  2103. func randomColor2(i int) drawing.Color {
  2104. var colors []drawing.Color
  2105. colors = append(colors,
  2106. drawing.Color{R: 52, G: 152, B: 219, A: 255},
  2107. drawing.Color{R: 230, G: 126, B: 34, A: 255},
  2108. drawing.Color{R: 142, G: 68, B: 173, A: 255},
  2109. drawing.Color{R: 211, G: 84, B: 0, A: 255},
  2110. drawing.Color{R: 231, G: 76, B: 60, A: 255},
  2111. drawing.Color{R: 26, G: 188, B: 156, A: 255},
  2112. drawing.Color{R: 243, G: 156, B: 18, A: 255},
  2113. drawing.Color{R: 22, G: 160, B: 133, A: 255},
  2114. drawing.Color{R: 46, G: 204, B: 113, A: 255},
  2115. drawing.Color{R: 39, G: 174, B: 96, A: 255},
  2116. drawing.Color{R: 41, G: 128, B: 185, A: 255},
  2117. drawing.Color{R: 155, G: 89, B: 182, A: 255},
  2118. drawing.Color{R: 192, G: 57, B: 43, A: 255},
  2119. drawing.Color{R: 241, G: 196, B: 15, A: 255},
  2120. )
  2121. return colors[i%len(colors)]
  2122. }
  2123. // getZWFont 加载字体
  2124. func getZWFont() *truetype.Font {
  2125. fontFile := "./static/fonts/MiSans-Medium.ttf"
  2126. fontBytes, err := os.ReadFile(fontFile)
  2127. if err != nil {
  2128. log.Println(err)
  2129. return nil
  2130. }
  2131. font, err := truetype.Parse(fontBytes)
  2132. if err != nil {
  2133. log.Println(err)
  2134. return nil
  2135. }
  2136. return font
  2137. }