waybill.go 63 KB

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