waybill.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  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.Username
  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. var userObj model.SysUser
  601. userSvc := service.SysUser{}
  602. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  603. if err != nil {
  604. e.Error(500, err, err.Error())
  605. return
  606. }
  607. if userObj.Type != model.SysUserTypeSys {
  608. e.Error(500, err, err.Error())
  609. return
  610. }
  611. req.SetUpdateBy(user.GetUserId(c))
  612. err = s.AdminAudit(&req, p)
  613. if err != nil {
  614. e.Error(500, err, err.Error())
  615. return
  616. }
  617. e.OK(req.GetId(), "审核成功")
  618. }
  619. // Delivery 派单
  620. // @Summary 派单
  621. // @Description 派单
  622. // @Tags 运单
  623. // @Accept application/json
  624. // @Product application/json
  625. // @Param data body dto.WaybillDeliveryReq true "body"
  626. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  627. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  628. // @Router /api/waybill/delivery [put]
  629. // @Security Bearer
  630. func (e WaybillController) Delivery(c *gin.Context) {
  631. s := service.Waybill{}
  632. IceRaftSvc := service.IceRaft{}
  633. req := dto.WaybillDeliveryReq{}
  634. err := e.MakeContext(c).
  635. MakeOrm().
  636. Bind(&req).
  637. MakeService(&s.Service).
  638. MakeService(&IceRaftSvc.Service).
  639. Errors
  640. if err != nil {
  641. e.Logger.Error(err)
  642. e.Error(500, err, err.Error())
  643. return
  644. }
  645. p := actions.GetPermissionFromContext(c)
  646. if len(req.WaybillIds) == 0 {
  647. e.Error(500, err, "配送员不能为空")
  648. return
  649. }
  650. oldIceRaftCode := make([]string, 0)
  651. missingIceRaftCode := make([]string, 0)
  652. if req.CoolerBoxId != 0 {
  653. err = IceRaftSvc.GetPageByCoolerBoxId(req.CoolerBoxId, &oldIceRaftCode, p)
  654. if err != nil {
  655. e.Error(500, err, err.Error())
  656. return
  657. }
  658. }
  659. req.SetUpdateBy(user.GetUserId(c))
  660. req.OldIceRaftCode = oldIceRaftCode
  661. err = s.Delivery(&req, p)
  662. if err != nil {
  663. e.Error(500, err, err.Error())
  664. return
  665. }
  666. missingIceRaftCode = lib.FindStrListMissingData(oldIceRaftCode, req.IceRaftCode)
  667. if len(missingIceRaftCode) > 0 {
  668. c.JSON(200, map[string]interface{}{
  669. "requestId": uuid.NewUUID(),
  670. "code": 2000,
  671. "msg": "派单成功",
  672. "status": "success",
  673. "data": missingIceRaftCode,
  674. })
  675. return
  676. }
  677. e.OK(req.GetId(), "派单成功")
  678. }
  679. // Delete 删除运单
  680. // @Summary 删除运单
  681. // @Description 删除运单
  682. // @Tags 运单
  683. // @Accept application/json
  684. // @Product application/json
  685. // @Param data body dto.WaybillDeleteReq true "body"
  686. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  687. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  688. // @Router /api/waybill [delete]
  689. // @Security Bearer
  690. func (e WaybillController) Delete(c *gin.Context) {
  691. s := service.Waybill{}
  692. req := dto.WaybillDeleteReq{}
  693. userSvc := service.SysUser{}
  694. err := e.MakeContext(c).
  695. MakeOrm().
  696. Bind(&req, binding.JSON, nil).
  697. MakeService(&s.Service).
  698. MakeService(&userSvc.Service).
  699. Errors
  700. if err != nil {
  701. e.Logger.Error(err)
  702. e.Error(500, err, err.Error())
  703. return
  704. }
  705. //数据权限检查
  706. p := actions.GetPermissionFromContext(c)
  707. err = s.Remove(&req, p)
  708. if err != nil {
  709. e.Error(500, err, err.Error())
  710. return
  711. }
  712. e.OK(req.GetId(), "删除成功")
  713. }
  714. // Receipt 签收
  715. // @Summary 签收
  716. // @Description 签收
  717. // @Tags 运单
  718. // @Accept application/json
  719. // @Product application/json
  720. // @Param data body dto.WaybillInOutReq true "body"
  721. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  722. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  723. // @Router /api/waybill/receipt [post]
  724. // @Security Bearer
  725. func (e WaybillController) Receipt(c *gin.Context) {
  726. s := service.Waybill{}
  727. req := dto.WaybillReceiptReq{}
  728. err := e.MakeContext(c).
  729. MakeOrm().
  730. Bind(&req, binding.JSON, nil).
  731. MakeService(&s.Service).
  732. Errors
  733. if err != nil {
  734. e.Logger.Error(err)
  735. e.Error(500, err, err.Error())
  736. return
  737. }
  738. //数据权限检查
  739. p := actions.GetPermissionFromContext(c)
  740. err = s.Receipt(&req, p)
  741. if err != nil {
  742. e.Error(500, err, err.Error())
  743. return
  744. }
  745. e.OK(req.WaybillNo, "提交成功")
  746. }
  747. // CustomerReceipt 客户签收
  748. // @Summary 客户签收
  749. // @Description 客户签收
  750. // @Tags 运单
  751. // @Accept application/json
  752. // @Product application/json
  753. // @Param data body dto.WaybillReceiptReq true "body"
  754. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  755. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  756. // @Router /api/waybill/customer/receipt [post]
  757. // @Security Bearer
  758. func (e WaybillController) CustomerReceipt(c *gin.Context) {
  759. s := service.Waybill{}
  760. req := dto.WaybillReceiptReq{}
  761. err := e.MakeContext(c).
  762. MakeOrm().
  763. Bind(&req, binding.JSON, nil).
  764. MakeService(&s.Service).
  765. Errors
  766. if err != nil {
  767. e.Logger.Error(err)
  768. e.Error(500, err, err.Error())
  769. return
  770. }
  771. //req.WaybillNo = lib.AesDecryptCBC(req.WaybillNo, lib.AesKey)
  772. //数据权限检查
  773. err = s.Receipt(&req, nil)
  774. if err != nil {
  775. e.Error(500, err, err.Error())
  776. return
  777. }
  778. e.OK(req.WaybillNo, "签收成功")
  779. }
  780. // CreateSole 生成唯一订单号
  781. // @Summary 生成唯一订单号
  782. // @Description 生成唯一订单号
  783. // @Tags 运单
  784. // @Accept application/json
  785. // @Product application/json
  786. // @Param data body dto.WaybillReceiptReq true "body"
  787. // @Success 200 {string} string "{"code": 200, "message": "生成成功"}"
  788. // @Success 200 {string} string "{"code": -1, "message": "生成成功"}"
  789. // @Router /api/waybill/createsole [get]
  790. // @Security Bearer
  791. func (e WaybillController) CreateSole(c *gin.Context) {
  792. s := service.Waybill{}
  793. err := e.MakeContext(c).
  794. MakeOrm().
  795. MakeService(&s.Service).
  796. Errors
  797. if err != nil {
  798. e.Logger.Error(err)
  799. e.Error(500, err, err.Error())
  800. return
  801. }
  802. sole, err := s.CreateSole()
  803. if err != nil {
  804. e.Error(500, err, err.Error())
  805. return
  806. }
  807. e.OK(sole, "生成成功")
  808. }
  809. // Turnarounds 订单转门店
  810. // @Summary 订单转门店
  811. // @Description 订单转门店
  812. // @Tags 运单
  813. // @Accept application/json
  814. // @Product application/json
  815. // @Param data body dto.WaybillReceiptReq true "body"
  816. // @Success 200 {string} string "{"code": 200, "message": "生成成功"}"
  817. // @Success 200 {string} string "{"code": -1, "message": "生成成功"}"
  818. // @Router /api/waybill/turnarounds [put]
  819. // @Security Bearer
  820. func (e WaybillController) Turnarounds(c *gin.Context) {
  821. s := service.Waybill{}
  822. req := dto.WaybillTurnarounds{}
  823. err := e.MakeContext(c).
  824. MakeOrm().
  825. Bind(&req, binding.JSON).
  826. MakeService(&s.Service).
  827. Errors
  828. if err != nil {
  829. e.Logger.Error(err)
  830. e.Error(500, err, err.Error())
  831. return
  832. }
  833. //数据权限检查
  834. p := actions.GetPermissionFromContext(c)
  835. err = s.WaybillTurnarounds(&req, p)
  836. if err != nil {
  837. e.Error(500, err, err.Error())
  838. return
  839. }
  840. e.OK(req, "转门店成功")
  841. }
  842. // GetCustomerPage 获取客户运单列表
  843. // @Summary 获取客户运单列表
  844. // @Description 获取客户运单列表
  845. // @Tags 运单
  846. // @Param waybillNo query string false "运单号"
  847. // @Param orderNo query string false "订单号"
  848. // @Param status query int false "状态"
  849. // @Param orderStartTime query string false "下单开始时间"
  850. // @Param orderEndTime query string false "下单结束时间"
  851. // @Param pageSize query int false "页条数"
  852. // @Param page query int false "页码"
  853. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  854. // @Router /api/waybill/customer [get]
  855. // @Security Bearer
  856. func (e WaybillController) GetCustomerPage(c *gin.Context) {
  857. s := service.Waybill{}
  858. req := dto.WaybillGetCustomerPageReq{}
  859. err := e.MakeContext(c).
  860. MakeOrm().
  861. Bind(&req, binding.Query).
  862. MakeService(&s.Service).
  863. Errors
  864. if err != nil {
  865. e.Logger.Error(err)
  866. e.Error(500, err, err.Error())
  867. return
  868. }
  869. //数据权限检查
  870. p := actions.GetPermissionFromContext(c)
  871. list := make([]model.Waybill, 0)
  872. var count int64
  873. req.CustomerId = p.UserId
  874. err = s.GetCustomerPage(&req, &list, &count, p)
  875. if err != nil {
  876. e.Error(500, err, err.Error())
  877. return
  878. }
  879. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  880. }
  881. // CustomerExport 导出客户运单列表
  882. // @Summary 导出客户运单列表
  883. // @Description 导出客户运单列表
  884. // @Tags 运单
  885. // @Param waybillNo query string false "运单号"
  886. // @Param orderNo query string false "订单号"
  887. // @Param status query int false "状态"
  888. // @Param orderStartTime query string false "下单开始时间"
  889. // @Param orderEndTime query string false "下单结束时间"
  890. // @Param pageSize query int false "页条数"
  891. // @Param page query int false "页码"
  892. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  893. // @Router /api/waybill/customer/export [get]
  894. // @Security Bearer
  895. func (e WaybillController) CustomerExport(c *gin.Context) {
  896. s := service.Waybill{}
  897. req := dto.WaybillGetCustomerPageReq{}
  898. err := e.MakeContext(c).
  899. MakeOrm().
  900. Bind(&req, binding.Query).
  901. MakeService(&s.Service).
  902. Errors
  903. if err != nil {
  904. e.Logger.Error(err)
  905. e.Error(500, err, err.Error())
  906. return
  907. }
  908. //数据权限检查
  909. p := actions.GetPermissionFromContext(c)
  910. list := make([]model.Waybill, 0)
  911. var count int64
  912. req.CustomerId = p.UserId
  913. req.PageSize = 9999
  914. err = s.GetCustomerPage(&req, &list, &count, p)
  915. if err != nil {
  916. e.Error(500, err, err.Error())
  917. return
  918. }
  919. f := excelize.NewFile() // 设置单元格的值
  920. // 这里设置表头ÒÒ
  921. f.SetCellValue("Sheet1", "A1", "序号")
  922. f.SetCellValue("Sheet1", "B1", "状态")
  923. f.SetCellValue("Sheet1", "C1", "寄件人名称")
  924. f.SetCellValue("Sheet1", "D1", "寄件人电话")
  925. f.SetCellValue("Sheet1", "E1", "寄件人地址")
  926. f.SetCellValue("Sheet1", "F1", "收件人名称")
  927. f.SetCellValue("Sheet1", "G1", "收件人电话")
  928. f.SetCellValue("Sheet1", "H1", "收件人地址")
  929. f.SetCellValue("Sheet1", "I1", "配送人姓名")
  930. f.SetCellValue("Sheet1", "J1", "配送人电话")
  931. f.SetCellValue("Sheet1", "K1", "运输备注")
  932. // 设置列宽
  933. f.SetColWidth("Sheet1", "A", "A", 6)
  934. f.SetColWidth("Sheet1", "B", "B", 8)
  935. f.SetColWidth("Sheet1", "C", "C", 14)
  936. f.SetColWidth("Sheet1", "D", "D", 14)
  937. f.SetColWidth("Sheet1", "E", "E", 30)
  938. f.SetColWidth("Sheet1", "F", "F", 14)
  939. f.SetColWidth("Sheet1", "G", "G", 14)
  940. f.SetColWidth("Sheet1", "H", "H", 30)
  941. f.SetColWidth("Sheet1", "I", "I", 15)
  942. f.SetColWidth("Sheet1", "J", "J", 15)
  943. f.SetColWidth("Sheet1", "K", "K", 15)
  944. line := 1
  945. // 循环写入数据
  946. for i, v := range list {
  947. line++
  948. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  949. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), model.WaybillStatusMap[v.Status])
  950. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.SenderAddressName)
  951. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.SenderAddressPhone)
  952. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.SenderAddressDetails)
  953. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.ConsigneeAddressName)
  954. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.ConsigneeAddressPhone)
  955. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.ConsigneeAddressDetails)
  956. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.DeliveryName)
  957. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.DeliveryPhone)
  958. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), v.Remark)
  959. }
  960. timeStr := time.Now().Format("20060102150405")
  961. filePath := "ofile/" + "运单" + timeStr + ".xlsx"
  962. defer func() {
  963. os.Remove(filePath)
  964. }()
  965. // 保存文件
  966. if err = f.SaveAs(filePath); err != nil {
  967. logs.Error("保存运单失败:", err)
  968. }
  969. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  970. // PathEscape 函数对中文做处理
  971. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape("运单"+timeStr+".xlsx"))
  972. c.Header("Content-Transfer-Encoding", "binary")
  973. c.File(filePath)
  974. }
  975. // CustomerInsert 客户添加运单
  976. // @Summary 客户添加运单
  977. // @Description 客户添加运单
  978. // @Tags 运单
  979. // @Accept application/json
  980. // @Product application/json
  981. // @Param data body dto.WaybillInsertReq true "data"
  982. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  983. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  984. // @Router /api/waybill/customer [post]
  985. // @Security Bearer
  986. func (e WaybillController) CustomerInsert(c *gin.Context) {
  987. s := service.Waybill{}
  988. userSvc := service.SysUser{}
  989. req := dto.WaybillInsertReq{}
  990. err := e.MakeContext(c).
  991. MakeOrm().
  992. Bind(&req, binding.JSON).
  993. MakeService(&s.Service).
  994. MakeService(&userSvc.Service).
  995. Errors
  996. if err != nil {
  997. e.Logger.Error(err)
  998. e.Error(500, err, err.Error())
  999. return
  1000. }
  1001. p := actions.GetPermissionFromContext(c)
  1002. var userObj model.SysUser
  1003. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  1004. if err != nil {
  1005. e.Error(500, err, "获取用户信息失败")
  1006. return
  1007. }
  1008. if p.DeptId == 0 && req.DeptId == 0 {
  1009. e.Error(500, err, "请先选择运输公司")
  1010. return
  1011. }
  1012. // 设置创建人
  1013. req.SetCreateBy(user.GetUserId(c))
  1014. if p.DeptId > 0 {
  1015. req.SetDeptId(p.DeptId)
  1016. }
  1017. err = s.Insert(&req)
  1018. if err != nil {
  1019. e.Error(500, err, err.Error())
  1020. return
  1021. }
  1022. e.OK(req.GetId(), "添加成功")
  1023. }
  1024. // Import 导入运单
  1025. // @Summary 导入运单
  1026. // @Description 导入运单
  1027. // @Tags 运单
  1028. // @Accept application/json
  1029. // @Product application/json
  1030. // @Param data body dto.WaybillImportReq true "data"
  1031. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1032. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1033. // @Router /api/waybill/import [post]
  1034. // @Security Bearer
  1035. func (e WaybillController) Import(c *gin.Context) {
  1036. s := service.Waybill{}
  1037. userSvc := service.SysUser{}
  1038. req := dto.WaybillImportReq{}
  1039. err := e.MakeContext(c).
  1040. MakeOrm().
  1041. Bind(&req, binding.Form).
  1042. MakeService(&s.Service).
  1043. MakeService(&userSvc.Service).
  1044. Errors
  1045. if err != nil {
  1046. e.Logger.Error(err)
  1047. e.Error(500, err, err.Error())
  1048. return
  1049. }
  1050. //读取第一fileName的文件
  1051. fileHeader, err := c.FormFile("file")
  1052. if err != nil {
  1053. err = errors.New("文件格式错误" + err.Error())
  1054. e.Logger.Error(err)
  1055. e.Error(500, err, err.Error())
  1056. return
  1057. }
  1058. if fileHeader.Size > 1024*1024*2 {
  1059. err = errors.New("文件大小超过2M")
  1060. e.Logger.Error(err)
  1061. e.Error(500, err, err.Error())
  1062. return
  1063. }
  1064. file, err := fileHeader.Open()
  1065. if err != nil {
  1066. err = errors.New("文件格式错误" + err.Error())
  1067. e.Logger.Error(err)
  1068. e.Error(500, err, err.Error())
  1069. return
  1070. }
  1071. defer file.Close()
  1072. xlsx, err := excelize.OpenReader(bufio.NewReader(file))
  1073. if err != nil {
  1074. err = errors.New("文件格式错误" + err.Error())
  1075. e.Logger.Error(err)
  1076. e.Error(500, err, err.Error())
  1077. return
  1078. }
  1079. p := actions.GetPermissionFromContext(c)
  1080. if p.DeptId == 0 {
  1081. e.Error(500, err, "获取用户信息失败")
  1082. return
  1083. }
  1084. var userObj model.SysUser
  1085. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  1086. if err != nil {
  1087. e.Error(500, err, "获取用户信息失败")
  1088. return
  1089. }
  1090. rows, _ := xlsx.GetRows("Sheet1")
  1091. for indexRow, row := range rows {
  1092. if indexRow == 0 {
  1093. continue
  1094. }
  1095. if len(row) < 10 {
  1096. for i := 0; i < 10-len(row); i++ {
  1097. row = append(row, "")
  1098. }
  1099. }
  1100. for i, colCell := range row {
  1101. fmt.Println(i, ":", colCell)
  1102. }
  1103. //quantity, _ := strconv.Atoi(row[8])
  1104. obj := dto.WaybillInsertReq{
  1105. Status: 1,
  1106. ConsigneeAddressName: row[0],
  1107. ConsigneeAddressPhone: row[1],
  1108. ConsigneeAddressDetails: row[2],
  1109. TamperProofLabel: row[3], // 防拆标签
  1110. Remark: row[4],
  1111. ReCheckId: p.UserId,
  1112. DeliveryId: p.UserId,
  1113. }
  1114. obj.SetDeptId(p.DeptId)
  1115. obj.SetCreateBy(user.GetUserId(c))
  1116. err = s.Insert(&obj)
  1117. if err != nil {
  1118. e.Error(500, err, err.Error())
  1119. return
  1120. }
  1121. }
  1122. e.OK(len(rows)-1, "导入成功")
  1123. }
  1124. // ExportTemplate 导出运单模板
  1125. // @Summary 导出运单模板
  1126. // @Description 导出运单模板
  1127. // @Tags 运单
  1128. // @Accept application/json
  1129. // @Product application/json
  1130. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1131. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1132. // @Router /api/waybill/export-template [get]
  1133. // @Security Bearer
  1134. func (e WaybillController) ExportTemplate(c *gin.Context) {
  1135. s := service.Waybill{}
  1136. err := e.MakeContext(c).
  1137. MakeOrm().
  1138. MakeService(&s.Service).
  1139. Errors
  1140. if err != nil {
  1141. e.Logger.Error(err)
  1142. e.Error(500, err, err.Error())
  1143. return
  1144. }
  1145. filePath := "./ofile/运单导入模板.xlsx"
  1146. //打开文件
  1147. fileTmp, errByOpenFile := os.Open(filePath)
  1148. defer fileTmp.Close()
  1149. //获取文件的名称
  1150. fileName := path.Base(filePath)
  1151. if errByOpenFile != nil {
  1152. e.Logger.Error(err)
  1153. e.Error(500, err, err.Error())
  1154. return
  1155. }
  1156. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  1157. // PathEscape 函数对中文做处理
  1158. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  1159. c.Header("Content-Transfer-Encoding", "binary")
  1160. c.File(filePath)
  1161. }
  1162. // TemperaturePDF 导出温度记录
  1163. // @Summary 导出温度记录
  1164. // @Description 导出温度记录
  1165. // @Tags 运单
  1166. // @Accept application/json
  1167. // @Product application/json
  1168. // @Param data body dto.WaybillGetByWaybillPdfReq true "data"
  1169. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  1170. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  1171. // @Router /api/waybill/temperature-pdf [get]
  1172. // @Security Bearer
  1173. func (e WaybillController) TemperaturePDF(c *gin.Context) {
  1174. s := service.Waybill{}
  1175. req := dto.WaybillGetByWaybillPdfReq{}
  1176. err := e.MakeContext(c).
  1177. MakeOrm().
  1178. Bind(&req, binding.Query).
  1179. MakeService(&s.Service).
  1180. Errors
  1181. if err != nil {
  1182. e.Logger.Error(err)
  1183. e.Error(500, err, err.Error())
  1184. return
  1185. }
  1186. var waybill model.Waybill
  1187. //p := actions.GetPermissionFromContext(c)
  1188. //err = s.GetByWaybillNo(&req, &waybill, p)
  1189. err = s.GetByWaybillNo(&req, &waybill, nil)
  1190. if err != nil {
  1191. e.Error(500, err, err.Error())
  1192. return
  1193. }
  1194. DeviceSensor_data, waybillPDF, err := s.GetAllData(&dto.WaybillGetByWaybillNoReq{WaybillNo: req.WaybillNo})
  1195. // 最高温度、最低温度、最高湿度、最低湿度
  1196. var maxTemp, minTemp, maxHumidity, minHumidity float32
  1197. // 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
  1198. var maxTempTime, minTempTime, maxHumidityTime, minHumidityTime string
  1199. // 总温度 总湿度
  1200. var totalTemp, totalHumidity float32
  1201. // 平均温度 平均湿度
  1202. var avgTemp, avgHumidity float32
  1203. // 温度阈值,湿度阈值
  1204. var tempThreshold, humidityThreshold string
  1205. // 记录开始时间,记录结束时间
  1206. var s_time, e_time string
  1207. var lastTime string
  1208. var isFirst, isSecond = true, false
  1209. var first_column, second_column []nats_server.DeviceData_R
  1210. if len(DeviceSensor_data) > 0 {
  1211. tempThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1212. humidityThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_rhl, DeviceSensor_data[0].T_rhu)
  1213. s_time = DeviceSensor_data[0].T_time
  1214. e_time = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1215. // 最高温度及时刻
  1216. maxTemp = DeviceSensor_data[0].T_t
  1217. maxTempTime = DeviceSensor_data[0].T_time
  1218. // 最低温度及时刻
  1219. minTemp = DeviceSensor_data[0].T_t
  1220. minTempTime = DeviceSensor_data[0].T_time
  1221. // 最高湿度及时刻
  1222. maxHumidity = DeviceSensor_data[0].T_rh
  1223. maxHumidityTime = DeviceSensor_data[0].T_time
  1224. // 获取最低湿度及时刻
  1225. minHumidity = DeviceSensor_data[0].T_rh
  1226. minHumidityTime = DeviceSensor_data[0].T_time
  1227. for i := 0; i < len(DeviceSensor_data); i++ {
  1228. data := DeviceSensor_data[i]
  1229. if data.T_t > maxTemp {
  1230. maxTemp = data.T_t
  1231. maxTempTime = data.T_time
  1232. }
  1233. if data.T_t < minTemp {
  1234. minTemp = data.T_t
  1235. minTempTime = data.T_time
  1236. }
  1237. totalTemp += data.T_t
  1238. if data.T_rh > maxHumidity {
  1239. maxHumidity = data.T_rh
  1240. maxHumidityTime = data.T_time
  1241. }
  1242. if data.T_rh < minHumidity {
  1243. minHumidity = data.T_rh
  1244. minHumidityTime = data.T_time
  1245. }
  1246. totalHumidity += data.T_rh
  1247. }
  1248. var sn string
  1249. for _, w := range waybillPDF {
  1250. for _, data := range w.Data {
  1251. if len(lastTime) > 0 {
  1252. if lastTime != data.T_time && isFirst == true {
  1253. isFirst = false
  1254. isSecond = true
  1255. } else if lastTime != data.T_time && isSecond == true {
  1256. isFirst = true
  1257. isSecond = false
  1258. }
  1259. }
  1260. if len(sn) > 0 && sn != data.T_sn && len(first_column)%2 == 1 {
  1261. isFirst, isSecond = isSecond, isFirst
  1262. }
  1263. if isFirst {
  1264. first_column = append(first_column, data)
  1265. lastTime = data.T_time
  1266. sn = data.T_sn
  1267. }
  1268. if isSecond {
  1269. second_column = append(second_column, data)
  1270. lastTime = data.T_time
  1271. sn = data.T_sn
  1272. }
  1273. }
  1274. }
  1275. // 平均温度
  1276. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1277. // 平均湿度
  1278. avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1279. }
  1280. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度结束
  1281. pdf := &gopdf.GoPdf{}
  1282. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1283. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1284. if err != nil {
  1285. return
  1286. }
  1287. err = pdf.SetFont("wts", "", 20)
  1288. if err != nil {
  1289. return
  1290. }
  1291. pdf.SetGrayFill(0.5)
  1292. pdf.SetMargins(0, 20, 0, 20)
  1293. pdf.AddPage()
  1294. title := "运单" + req.WaybillNo + "温度记录"
  1295. if req.HumidityShow {
  1296. title = "运单" + req.WaybillNo + "温湿度记录"
  1297. }
  1298. var y float64 = 40
  1299. textw, _ := pdf.MeasureTextWidth(title)
  1300. pdf.SetX((595 / 2) - (textw / 2))
  1301. pdf.SetY(y)
  1302. pdf.Text(title)
  1303. //y += 30
  1304. //pdf.SetFont("wts", "", 16)
  1305. //pdf.SetXY(10, y)
  1306. //pdf.Text("实施设备信息")
  1307. //// 线
  1308. //y += 10
  1309. //pdf.SetLineWidth(0.5)
  1310. //pdf.SetStrokeColor(169, 169, 169)
  1311. //pdf.Line(10, y, 585, y)
  1312. //pdf.SetFont("wts", "", 10)
  1313. //y += 20
  1314. //pdf.SetXY(10, y)
  1315. //pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1316. //pdf.SetXY(300, y)
  1317. //pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1318. y += 25
  1319. pdf.SetFont("wts", "", 16)
  1320. pdf.SetXY(10, y)
  1321. pdf.Text("记录概要信息")
  1322. // 线
  1323. y += 10
  1324. pdf.SetLineWidth(0.5)
  1325. pdf.SetStrokeColor(169, 169, 169)
  1326. pdf.Line(10, y, 585, y)
  1327. y += 20
  1328. pdf.SetFont("wts", "", 10)
  1329. pdf.SetXY(10, y)
  1330. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1331. pdf.SetXY(240, y)
  1332. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1333. sTime, _ := lib.TimeStrToTime(s_time)
  1334. eTime, _ := lib.TimeStrToTime(e_time)
  1335. pdf.SetXY(470, y)
  1336. minutes := int(eTime.Sub(sTime).Minutes())
  1337. hours := minutes / 60
  1338. remainingMinutes := minutes % 60
  1339. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1340. // -------------最高温/湿度 最低温/湿度 平均温/湿度
  1341. y += 15
  1342. pdf.SetXY(10, y)
  1343. pdf.Text(fmt.Sprintf("最高温度:%.1f℃,%s", lib.RoundToDecimal(float64(maxTemp), 1), maxTempTime))
  1344. pdf.SetXY(240, y)
  1345. pdf.Text(fmt.Sprintf("最低温度:%.1f℃,%s", lib.RoundToDecimal(float64(minTemp), 1), minTempTime))
  1346. pdf.SetXY(470, y)
  1347. pdf.Text(fmt.Sprintf("平均温度:%.1f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1348. if req.HumidityShow {
  1349. y += 15
  1350. pdf.SetXY(10, y)
  1351. pdf.Text(fmt.Sprintf("最高湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(maxHumidity), 1), maxHumidityTime))
  1352. pdf.SetXY(240, y)
  1353. pdf.Text(fmt.Sprintf("最低湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(minHumidity), 1), minHumidityTime))
  1354. pdf.SetXY(470, y)
  1355. pdf.Text(fmt.Sprintf("平均湿度:%.1f%%RH", lib.RoundToDecimal(float64(avgHumidity), 1)))
  1356. }
  1357. // -------------温/湿度阈值
  1358. y += 15
  1359. pdf.SetXY(10, y)
  1360. pdf.Text(fmt.Sprintf("温度阈值:%s℃", tempThreshold))
  1361. if req.HumidityShow {
  1362. pdf.SetXY(240, y)
  1363. pdf.Text(fmt.Sprintf("温度阈值:%s%%", humidityThreshold))
  1364. }
  1365. //-------------发货单位,收货单位,备注
  1366. y += 15
  1367. pdf.SetXY(10, y)
  1368. T_forwarding_unit_temp := []rune(waybill.SenderAddressName)
  1369. if len(T_forwarding_unit_temp) > 17 {
  1370. pdf.Text(fmt.Sprintf("发货人:%s", string(T_forwarding_unit_temp[0:17])))
  1371. pdf.SetXY(60, y+15)
  1372. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1373. } else {
  1374. pdf.Text(fmt.Sprintf("发货人:%s", string(T_forwarding_unit_temp)))
  1375. }
  1376. pdf.SetXY(240, y)
  1377. T_consignee_unit_temp := []rune(waybill.ConsigneeAddressName)
  1378. if len(T_consignee_unit_temp) > 17 {
  1379. pdf.Text(fmt.Sprintf("收货人:%s", string(T_consignee_unit_temp[0:17])))
  1380. pdf.SetXY(290, y+15)
  1381. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1382. } else {
  1383. pdf.Text(fmt.Sprintf("收货人:%s", string(T_consignee_unit_temp)))
  1384. }
  1385. // 承运方
  1386. y += 15
  1387. pdf.SetXY(10, y)
  1388. company_name := []rune(waybill.Dept.Name)
  1389. pdf.Text(fmt.Sprintf("承运方:%s", string(company_name)))
  1390. y += 15
  1391. pdf.SetXY(10, y)
  1392. T_remark_temp := []rune(waybill.Remark)
  1393. if len(waybill.Remark) > 35 {
  1394. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:35])))
  1395. pdf.SetXY(10, y+15)
  1396. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[35:])))
  1397. } else {
  1398. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1399. }
  1400. y += 15
  1401. //pdf.SetFont("wts", "", 16)
  1402. //pdf.SetXY(10, y)
  1403. //pdf.Text("记录曲线信息")
  1404. //// 线
  1405. //y += 10
  1406. //pdf.SetLineWidth(0.5)
  1407. //pdf.SetStrokeColor(169, 169, 169)
  1408. //pdf.Line(10, y, 585, y)
  1409. //y += 1
  1410. //
  1411. //var tempFilepath string
  1412. //tempFilepath, err = DeviceDataTemperatureJPG2(s_time, e_time, waybillPDF)
  1413. //if err == nil {
  1414. // imgH, _ := gopdf.ImageHolderByPath(tempFilepath)
  1415. // pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1416. // y += 315
  1417. //}
  1418. //
  1419. //var humidityFilepath string
  1420. //if req.HumidityShow {
  1421. // humidityFilepath, err = DeviceDataHumidityJPG2(s_time, e_time, waybillPDF)
  1422. // if err == nil {
  1423. // imgH, _ := gopdf.ImageHolderByPath(humidityFilepath)
  1424. // pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1425. // y += 315
  1426. // }
  1427. //}
  1428. if y > 841.89 {
  1429. // 图片结束直接分页
  1430. pdf.AddPage()
  1431. y = 20
  1432. }
  1433. y += 20
  1434. pdf.SetFont("wts", "", 16)
  1435. pdf.SetXY(10, y)
  1436. pdf.Text("记录数据信息")
  1437. // 线
  1438. y += 10
  1439. pdf.SetLineWidth(0.5)
  1440. pdf.SetStrokeColor(169, 169, 169)
  1441. pdf.Line(10, y, 585, y)
  1442. y += 10
  1443. pdf.SetFont("wts", "", 10)
  1444. var x float64 = 10
  1445. var w float64 = 112
  1446. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1447. x = x + w
  1448. w = 101
  1449. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1450. if req.HumidityShow {
  1451. x = x + w
  1452. w = 37
  1453. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1454. x = x + w
  1455. w = 37
  1456. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1457. } else {
  1458. x = x + w
  1459. w = 37 * 2
  1460. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1461. }
  1462. x = x + w
  1463. w = 112
  1464. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1465. x = x + w
  1466. w = 101
  1467. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1468. if req.HumidityShow {
  1469. x = x + w
  1470. w = 37
  1471. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1472. x = x + w
  1473. w = 37
  1474. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1475. } else {
  1476. x = x + w
  1477. w = 37 * 2
  1478. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1479. }
  1480. y += 20
  1481. var textH float64 = 25 // if text height is 25px.
  1482. for i, v := range first_column {
  1483. pdf.SetNewY(y, textH)
  1484. y = pdf.GetY()
  1485. x, w = 10, 112
  1486. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1487. x = x + w
  1488. w = 101
  1489. lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1490. // 显示温湿度
  1491. if req.HumidityShow {
  1492. x = x + w
  1493. w = 37
  1494. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1495. x = x + w
  1496. w = 37
  1497. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1498. } else {
  1499. x = x + w
  1500. w = 37 * 2
  1501. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1502. }
  1503. if i < len(second_column) {
  1504. x = x + w
  1505. w = 112
  1506. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1507. x = x + w
  1508. w = 101
  1509. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1510. if req.HumidityShow {
  1511. x = x + w
  1512. w = 37
  1513. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1514. x = x + w
  1515. w = 37
  1516. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1517. } else {
  1518. x = x + w
  1519. w = 37 * 2
  1520. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1521. }
  1522. }
  1523. y += 20
  1524. }
  1525. if len(second_column) > len(first_column) {
  1526. for i := len(first_column); i < len(second_column); i++ {
  1527. pdf.SetNewY(y, textH)
  1528. y = pdf.GetY()
  1529. x, w = 297, 112
  1530. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1531. x = x + w
  1532. w = 101
  1533. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1534. if req.HumidityShow {
  1535. x = x + w
  1536. w = 37
  1537. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1538. x = x + w
  1539. w = 37
  1540. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1541. } else {
  1542. x = x + w
  1543. w = 37 * 2
  1544. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1545. }
  1546. y += 20
  1547. }
  1548. }
  1549. filename := "运单" + req.WaybillNo + "温湿度记录" + time.Now().Format("20060102150405") + ".pdf"
  1550. filePath := "ofile/" + filename
  1551. err = pdf.WritePdf(filePath)
  1552. if err != nil {
  1553. return
  1554. }
  1555. defer func() {
  1556. //os.Remove(tempFilepath)
  1557. //os.Remove(humidityFilepath)
  1558. os.Remove(filePath)
  1559. }()
  1560. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(filename))
  1561. c.Header("Content-Transfer-Encoding", "binary")
  1562. c.File(filePath)
  1563. }
  1564. // 获取温度图片
  1565. func DeviceDataTemperatureJPG(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1566. if len(waybillPDF) == 0 {
  1567. return "", errors.New("暂无数据可生成图片")
  1568. }
  1569. // 创建一个新的绘图
  1570. p := plot.New()
  1571. // 设置绘图标题和标签
  1572. p.Title.Text = "temperature"
  1573. //p.Legend.ThumbnailWidth = 20
  1574. deviceSensorList := []nats_server.DeviceSensor_R{}
  1575. dataList := []nats_server.DeviceData_R{}
  1576. for _, w := range waybillPDF {
  1577. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1578. dataList = append(dataList, w.Data...)
  1579. }
  1580. TemperatureMin := deviceSensorList[0].T_DeviceSensorParameter.T_Tlower
  1581. TemperatureMax := deviceSensorList[0].T_DeviceSensorParameter.T_Tupper
  1582. var ymin, ymax float32
  1583. for i, r := range dataList {
  1584. if i == 0 {
  1585. ymin = r.T_t
  1586. ymax = r.T_t
  1587. }
  1588. if ymin > r.T_t {
  1589. ymin = r.T_t
  1590. }
  1591. if ymax < r.T_t {
  1592. ymax = r.T_t
  1593. }
  1594. }
  1595. var chData = make(chan int, 10)
  1596. var jobGroup sync.WaitGroup
  1597. // 创建温度线
  1598. for i := 0; i < len(deviceSensorList); i++ {
  1599. chData <- 1
  1600. jobGroup.Add(1)
  1601. go func(index int) {
  1602. defer func() {
  1603. <-chData // 完成时chan取出1个
  1604. jobGroup.Done() // 完成时将等待组值减1
  1605. }()
  1606. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1607. r_maps := []nats_server.DeviceData_R{}
  1608. for _, data := range dataList {
  1609. if data.T_sn == sn && data.T_id == id {
  1610. r_maps = append(r_maps, data)
  1611. }
  1612. }
  1613. fmt.Println(r_maps)
  1614. if len(r_maps) == 0 {
  1615. return
  1616. }
  1617. sort.Slice(r_maps, func(i, j int) bool {
  1618. return r_maps[i].T_time < r_maps[j].T_time
  1619. })
  1620. pts := make(plotter.XYs, len(r_maps))
  1621. for j, d := range r_maps {
  1622. t, _ := lib.TimeStrToTime(d.T_time)
  1623. pts[j].X = float64(t.Unix())
  1624. pts[j].Y = float64(d.T_t)
  1625. }
  1626. line, err := plotter.NewLine(pts)
  1627. if err != nil {
  1628. return
  1629. }
  1630. line.Color = randomColor(index)
  1631. p.Add(line)
  1632. }(i)
  1633. }
  1634. jobGroup.Wait()
  1635. st, _ := lib.TimeStrToTime(startTime)
  1636. et, _ := lib.TimeStrToTime(endTime)
  1637. xmin, xmax := float64(st.Unix()), float64(et.Unix())
  1638. // 添加最高,最低标准线 用红色虚线标识
  1639. p.Add(horizontalLine(xmin, xmax, float64(TemperatureMin)))
  1640. p.Add(horizontalLine(xmin, xmax, float64(TemperatureMax)))
  1641. if ymax < TemperatureMax {
  1642. ymax = TemperatureMax
  1643. }
  1644. if ymin > 0 {
  1645. ymin = 0
  1646. }
  1647. p.Y.Min, p.Y.Max = float64(ymin), float64(ymax)
  1648. p.X.Min, p.X.Max = xmin, xmax
  1649. p.Y.Tick.Marker = commaTicks{}
  1650. p.X.Tick.Marker = timeTicks{}
  1651. p.X.Tick.Label.Rotation = math.Pi / 5
  1652. p.X.Tick.Label.YAlign = draw.YCenter
  1653. p.X.Tick.Label.XAlign = draw.XRight
  1654. filepath := "ofile/" + "temperature" + deviceSensorList[0].T_sn + ".jpg"
  1655. // 保存文件
  1656. if err := p.Save(10*vg.Inch, 4*vg.Inch, filepath); err != nil {
  1657. logs.Error(lib.FuncName(), "生成图片失败", err)
  1658. return "", err
  1659. }
  1660. return filepath, nil
  1661. }
  1662. func DeviceDataTemperatureJPG2(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1663. if len(waybillPDF) == 0 {
  1664. return "", errors.New("暂无数据可生成图片")
  1665. }
  1666. deviceSensorList := []nats_server.DeviceSensor_R{}
  1667. dataList := []nats_server.DeviceData_R{}
  1668. for _, w := range waybillPDF {
  1669. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1670. dataList = append(dataList, w.Data...)
  1671. }
  1672. TemperatureMin := deviceSensorList[0].T_DeviceSensorParameter.T_Tlower
  1673. TemperatureMax := deviceSensorList[0].T_DeviceSensorParameter.T_Tupper
  1674. var ymin, ymax float32
  1675. for i, r := range dataList {
  1676. if i == 0 {
  1677. ymin = r.T_t
  1678. ymax = r.T_t
  1679. }
  1680. if ymin > r.T_t {
  1681. ymin = r.T_t
  1682. }
  1683. if ymax < r.T_t {
  1684. ymax = r.T_t
  1685. }
  1686. }
  1687. series := make([]chart.Series, 0)
  1688. var chData = make(chan int, 10)
  1689. var jobGroup sync.WaitGroup
  1690. // 创建温度线
  1691. for i := 0; i < len(deviceSensorList); i++ {
  1692. chData <- 1
  1693. jobGroup.Add(1)
  1694. go func(index int) {
  1695. defer func() {
  1696. <-chData // 完成时chan取出1个
  1697. jobGroup.Done() // 完成时将等待组值减1
  1698. }()
  1699. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1700. r_maps := []nats_server.DeviceData_R{}
  1701. for _, data := range dataList {
  1702. if data.T_sn == sn && data.T_id == id {
  1703. r_maps = append(r_maps, data)
  1704. }
  1705. }
  1706. fmt.Println(r_maps)
  1707. if len(r_maps) == 0 {
  1708. return
  1709. }
  1710. sort.Slice(r_maps, func(i, j int) bool {
  1711. return r_maps[i].T_time < r_maps[j].T_time
  1712. })
  1713. xValues := make([]time.Time, len(r_maps))
  1714. yValues := make([]float64, len(r_maps))
  1715. for j := 0; j < len(r_maps); j++ {
  1716. t, _ := lib.TimeStrToTime(r_maps[j].T_time)
  1717. xValues[j] = t
  1718. yValues[j] = float64(r_maps[j].T_t)
  1719. }
  1720. series = append(series, chart.TimeSeries{
  1721. Name: fmt.Sprintf("%s-%d", sn, id),
  1722. XValues: xValues,
  1723. YValues: yValues,
  1724. })
  1725. }(i)
  1726. }
  1727. jobGroup.Wait()
  1728. if ymax < TemperatureMax {
  1729. ymax = TemperatureMax
  1730. }
  1731. if ymin > 0 {
  1732. ymin = 0
  1733. }
  1734. if ymin > TemperatureMin {
  1735. ymin = TemperatureMin
  1736. }
  1737. st, _ := lib.TimeStrToTime(startTime)
  1738. et, _ := lib.TimeStrToTime(endTime)
  1739. series = append(series, chart.TimeSeries{
  1740. Style: chart.Style{
  1741. StrokeColor: drawing.ColorRed,
  1742. StrokeDashArray: []float64{5.0, 5.0},
  1743. },
  1744. XValues: []time.Time{st, et},
  1745. YValues: []float64{float64(TemperatureMin), float64(TemperatureMin)},
  1746. })
  1747. series = append(series, chart.TimeSeries{
  1748. Style: chart.Style{
  1749. StrokeColor: drawing.ColorRed,
  1750. StrokeDashArray: []float64{5.0, 5.0},
  1751. },
  1752. XValues: []time.Time{st, et},
  1753. YValues: []float64{float64(TemperatureMax), float64(TemperatureMax)},
  1754. })
  1755. font := getZWFont()
  1756. graph := chart.Chart{
  1757. Title: "温度记录",
  1758. TitleStyle: chart.Style{
  1759. FontSize: 15,
  1760. },
  1761. Background: chart.Style{
  1762. Padding: chart.Box{
  1763. Top: 20,
  1764. },
  1765. },
  1766. Font: font,
  1767. XAxis: chart.XAxis{
  1768. Name: "时间",
  1769. ValueFormatter: chart.TimeValueFormatterWithFormat("2006-01-02 15:04"),
  1770. },
  1771. YAxis: chart.YAxis{
  1772. Name: "温度",
  1773. Range: &chart.ContinuousRange{
  1774. Min: float64(ymin),
  1775. Max: float64(ymax + 2),
  1776. },
  1777. },
  1778. Series: series,
  1779. }
  1780. //graph.Elements = []chart.Renderable{
  1781. // chart.Legend(&graph),
  1782. //}
  1783. filepath := "ofile/" + "temperature" + deviceSensorList[0].T_sn + ".jpg"
  1784. f, _ := os.Create(filepath)
  1785. defer f.Close()
  1786. graph.Render(chart.PNG, f)
  1787. return filepath, nil
  1788. }
  1789. // 获取湿度图片
  1790. func DeviceDataHumidityJPG(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1791. if len(waybillPDF) == 0 {
  1792. return "", errors.New("暂无数据可生成图片")
  1793. }
  1794. // 创建一个新的绘图
  1795. p := plot.New()
  1796. // 设置绘图标题和标签
  1797. p.Title.Text = "humidity"
  1798. deviceSensorList := []nats_server.DeviceSensor_R{}
  1799. dataList := []nats_server.DeviceData_R{}
  1800. for _, w := range waybillPDF {
  1801. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1802. dataList = append(dataList, w.Data...)
  1803. }
  1804. humidityMin := deviceSensorList[0].T_DeviceSensorParameter.T_RHlower
  1805. humidityMax := deviceSensorList[0].T_DeviceSensorParameter.T_RHupper
  1806. var ymin, ymax float32
  1807. for i, r := range dataList {
  1808. if i == 0 {
  1809. ymin = r.T_rh
  1810. ymax = r.T_rh
  1811. }
  1812. if ymin > r.T_rh {
  1813. ymin = r.T_rh
  1814. }
  1815. if ymax < r.T_rh {
  1816. ymax = r.T_rh
  1817. }
  1818. }
  1819. var chData = make(chan int, 10)
  1820. var jobGroup sync.WaitGroup
  1821. // 创建温度线
  1822. for i := 0; i < len(deviceSensorList); i++ {
  1823. chData <- 1
  1824. jobGroup.Add(1)
  1825. go func(index int) {
  1826. defer func() {
  1827. <-chData // 完成时chan取出1个
  1828. jobGroup.Done() // 完成时将等待组值减1
  1829. }()
  1830. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1831. r_maps := []nats_server.DeviceData_R{}
  1832. for _, data := range dataList {
  1833. if data.T_sn == sn && data.T_id == id {
  1834. r_maps = append(r_maps, data)
  1835. }
  1836. }
  1837. if len(r_maps) == 0 {
  1838. return
  1839. }
  1840. sort.Slice(r_maps, func(i, j int) bool {
  1841. return r_maps[i].T_time < r_maps[j].T_time
  1842. })
  1843. pts := make(plotter.XYs, len(r_maps))
  1844. for j, d := range r_maps {
  1845. t, _ := lib.TimeStrToTime(d.T_time)
  1846. pts[j].X = float64(t.Unix())
  1847. pts[j].Y = float64(d.T_rh)
  1848. }
  1849. line, err := plotter.NewLine(pts)
  1850. if err != nil {
  1851. return
  1852. }
  1853. line.Color = randomColor(index)
  1854. p.Add(line)
  1855. }(i)
  1856. }
  1857. jobGroup.Wait()
  1858. st, _ := lib.TimeStrToTime(startTime)
  1859. et, _ := lib.TimeStrToTime(endTime)
  1860. xmin, xmax := float64(st.Unix()), float64(et.Unix())
  1861. // 添加最高,最低标准线 用红色虚线标识
  1862. p.Add(horizontalLine(xmin, xmax, float64(humidityMin)))
  1863. p.Add(horizontalLine(xmin, xmax, float64(humidityMax)))
  1864. if ymax < humidityMax {
  1865. ymax = humidityMax
  1866. }
  1867. if ymin > 0 {
  1868. ymin = 0
  1869. }
  1870. p.Y.Min, p.Y.Max = float64(ymin), float64(ymax)
  1871. p.X.Min, p.X.Max = xmin, xmax
  1872. p.Y.Tick.Marker = commaTicks{}
  1873. //p.X.Tick.Marker = plot.TimeTicks{Format: "2006-01-02 15:04:05"}
  1874. p.X.Tick.Marker = timeTicks{}
  1875. p.X.Tick.Label.Rotation = math.Pi / 5
  1876. p.X.Tick.Label.YAlign = draw.YCenter
  1877. p.X.Tick.Label.XAlign = draw.XRight
  1878. filepath := "ofile/" + "humidity" + deviceSensorList[0].T_sn + ".jpg"
  1879. // 保存文件
  1880. if err := p.Save(10*vg.Inch, 4*vg.Inch, filepath); err != nil {
  1881. logs.Error(lib.FuncName(), "生成图片失败", err)
  1882. return "", err
  1883. }
  1884. return filepath, nil
  1885. }
  1886. func DeviceDataHumidityJPG2(startTime, endTime string, waybillPDF []service.WaybillPDF) (string, error) {
  1887. if len(waybillPDF) == 0 {
  1888. return "", errors.New("暂无数据可生成图片")
  1889. }
  1890. deviceSensorList := []nats_server.DeviceSensor_R{}
  1891. dataList := []nats_server.DeviceData_R{}
  1892. for _, w := range waybillPDF {
  1893. deviceSensorList = append(deviceSensorList, w.DeviceSensorList...)
  1894. dataList = append(dataList, w.Data...)
  1895. }
  1896. humidityMin := deviceSensorList[0].T_DeviceSensorParameter.T_RHlower
  1897. humidityMax := deviceSensorList[0].T_DeviceSensorParameter.T_RHupper
  1898. var ymin, ymax float32
  1899. for i, r := range dataList {
  1900. if i == 0 {
  1901. ymin = r.T_t
  1902. ymax = r.T_t
  1903. }
  1904. if ymin > r.T_t {
  1905. ymin = r.T_t
  1906. }
  1907. if ymax < r.T_t {
  1908. ymax = r.T_t
  1909. }
  1910. }
  1911. series := make([]chart.Series, 0)
  1912. var chData = make(chan int, 10)
  1913. var jobGroup sync.WaitGroup
  1914. // 创建温度线
  1915. for i := 0; i < len(deviceSensorList); i++ {
  1916. chData <- 1
  1917. jobGroup.Add(1)
  1918. go func(index int) {
  1919. defer func() {
  1920. <-chData // 完成时chan取出1个
  1921. jobGroup.Done() // 完成时将等待组值减1
  1922. }()
  1923. sn, id := deviceSensorList[index].T_sn, deviceSensorList[index].T_id
  1924. r_maps := []nats_server.DeviceData_R{}
  1925. for _, data := range dataList {
  1926. if data.T_sn == sn && data.T_id == id {
  1927. r_maps = append(r_maps, data)
  1928. }
  1929. }
  1930. if len(r_maps) == 0 {
  1931. return
  1932. }
  1933. sort.Slice(r_maps, func(i, j int) bool {
  1934. return r_maps[i].T_time < r_maps[j].T_time
  1935. })
  1936. xValues := make([]time.Time, len(r_maps))
  1937. yValues := make([]float64, len(r_maps))
  1938. for j := 0; j < len(r_maps); j++ {
  1939. t, _ := lib.TimeStrToTime(r_maps[j].T_time)
  1940. xValues[j] = t
  1941. yValues[j] = float64(r_maps[j].T_rh)
  1942. }
  1943. series = append(series, chart.TimeSeries{
  1944. Name: fmt.Sprintf("%s-%d", sn, id),
  1945. XValues: xValues,
  1946. YValues: yValues,
  1947. })
  1948. }(i)
  1949. }
  1950. jobGroup.Wait()
  1951. if ymax < humidityMax {
  1952. ymax = humidityMax
  1953. }
  1954. if ymin > 0 {
  1955. ymin = 0
  1956. }
  1957. if ymin > humidityMin {
  1958. ymin = humidityMin
  1959. }
  1960. st, _ := lib.TimeStrToTime(startTime)
  1961. et, _ := lib.TimeStrToTime(endTime)
  1962. series = append(series, chart.TimeSeries{
  1963. Style: chart.Style{
  1964. StrokeColor: drawing.ColorRed,
  1965. StrokeDashArray: []float64{5.0, 5.0},
  1966. },
  1967. XValues: []time.Time{st, et},
  1968. YValues: []float64{float64(humidityMin), float64(humidityMin)},
  1969. })
  1970. series = append(series, chart.TimeSeries{
  1971. Style: chart.Style{
  1972. StrokeColor: drawing.ColorRed,
  1973. StrokeDashArray: []float64{5.0, 5.0},
  1974. },
  1975. XValues: []time.Time{st, et},
  1976. YValues: []float64{float64(humidityMax), float64(humidityMax)},
  1977. })
  1978. font := getZWFont()
  1979. graph := chart.Chart{
  1980. Title: "湿度记录",
  1981. TitleStyle: chart.Style{
  1982. FontSize: 15,
  1983. },
  1984. Background: chart.Style{
  1985. Padding: chart.Box{
  1986. Top: 20,
  1987. },
  1988. },
  1989. Font: font,
  1990. XAxis: chart.XAxis{
  1991. Name: "时间",
  1992. ValueFormatter: chart.TimeValueFormatterWithFormat("2006-01-02 15:04"),
  1993. },
  1994. YAxis: chart.YAxis{
  1995. Name: "湿度",
  1996. Range: &chart.ContinuousRange{
  1997. Min: float64(ymin),
  1998. Max: float64(ymax + 2),
  1999. },
  2000. },
  2001. Series: series,
  2002. }
  2003. //graph.Elements = []chart.Renderable{
  2004. // chart.Legend(&graph),
  2005. //}
  2006. filepath := "ofile/" + "humidity" + deviceSensorList[0].T_sn + ".jpg"
  2007. f, _ := os.Create(filepath)
  2008. defer f.Close()
  2009. graph.Render(chart.PNG, f)
  2010. return filepath, nil
  2011. }
  2012. func horizontalLine(xmin, xmax, y float64) *plotter.Line {
  2013. pts := make(plotter.XYs, 2)
  2014. pts[0].X = xmin
  2015. pts[0].Y = y
  2016. pts[1].X = xmax
  2017. pts[1].Y = y
  2018. line, err := plotter.NewLine(pts)
  2019. if err != nil {
  2020. panic(err)
  2021. }
  2022. line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
  2023. line.Color = color.RGBA{R: 255, A: 255}
  2024. return line
  2025. }
  2026. type timeTicks struct{}
  2027. func (timeTicks) Ticks(min, max float64) []plot.Tick {
  2028. tks := plot.TimeTicks{}.Ticks(min, max)
  2029. for i, t := range tks {
  2030. //if t.Label == "" { // Skip minor ticks, they are fine.
  2031. // continue
  2032. //}
  2033. tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
  2034. }
  2035. return tks
  2036. }
  2037. type commaTicks struct{}
  2038. // Ticks computes the default tick marks, but inserts commas
  2039. // into the labels for the major tick marks.
  2040. func (commaTicks) Ticks(min, max float64) []plot.Tick {
  2041. tks := plot.DefaultTicks{}.Ticks(min, max)
  2042. for i, t := range tks {
  2043. //if t.Label == "" { // Skip minor ticks, they are fine.
  2044. // continue
  2045. //}
  2046. tks[i].Label = fmt.Sprintf("%.0f", t.Value)
  2047. }
  2048. return tks
  2049. }
  2050. // 生成随机颜色的辅助函数
  2051. func randomColor(i int) color.RGBA {
  2052. var colors []color.RGBA
  2053. colors = append(colors,
  2054. color.RGBA{R: 52, G: 152, B: 219, A: 255},
  2055. color.RGBA{R: 230, G: 126, B: 34, A: 255},
  2056. color.RGBA{R: 142, G: 68, B: 173, A: 255},
  2057. color.RGBA{R: 211, G: 84, B: 0, A: 255},
  2058. color.RGBA{R: 231, G: 76, B: 60, A: 255},
  2059. color.RGBA{R: 26, G: 188, B: 156, A: 255},
  2060. color.RGBA{R: 243, G: 156, B: 18, A: 255},
  2061. color.RGBA{R: 22, G: 160, B: 133, A: 255},
  2062. color.RGBA{R: 46, G: 204, B: 113, A: 255},
  2063. color.RGBA{R: 39, G: 174, B: 96, A: 255},
  2064. color.RGBA{R: 41, G: 128, B: 185, A: 255},
  2065. color.RGBA{R: 155, G: 89, B: 182, A: 255},
  2066. color.RGBA{R: 192, G: 57, B: 43, A: 255},
  2067. color.RGBA{R: 241, G: 196, B: 15, A: 255},
  2068. )
  2069. return colors[i%len(colors)]
  2070. }
  2071. func randomColor2(i int) drawing.Color {
  2072. var colors []drawing.Color
  2073. colors = append(colors,
  2074. drawing.Color{R: 52, G: 152, B: 219, A: 255},
  2075. drawing.Color{R: 230, G: 126, B: 34, A: 255},
  2076. drawing.Color{R: 142, G: 68, B: 173, A: 255},
  2077. drawing.Color{R: 211, G: 84, B: 0, A: 255},
  2078. drawing.Color{R: 231, G: 76, B: 60, A: 255},
  2079. drawing.Color{R: 26, G: 188, B: 156, A: 255},
  2080. drawing.Color{R: 243, G: 156, B: 18, A: 255},
  2081. drawing.Color{R: 22, G: 160, B: 133, A: 255},
  2082. drawing.Color{R: 46, G: 204, B: 113, A: 255},
  2083. drawing.Color{R: 39, G: 174, B: 96, A: 255},
  2084. drawing.Color{R: 41, G: 128, B: 185, A: 255},
  2085. drawing.Color{R: 155, G: 89, B: 182, A: 255},
  2086. drawing.Color{R: 192, G: 57, B: 43, A: 255},
  2087. drawing.Color{R: 241, G: 196, B: 15, A: 255},
  2088. )
  2089. return colors[i%len(colors)]
  2090. }
  2091. // getZWFont 加载字体
  2092. func getZWFont() *truetype.Font {
  2093. fontFile := "./static/fonts/MiSans-Medium.ttf"
  2094. fontBytes, err := os.ReadFile(fontFile)
  2095. if err != nil {
  2096. log.Println(err)
  2097. return nil
  2098. }
  2099. font, err := truetype.Parse(fontBytes)
  2100. if err != nil {
  2101. log.Println(err)
  2102. return nil
  2103. }
  2104. return font
  2105. }