waybill.go 61 KB

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