Order.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. package controllers
  2. import (
  3. "Cold_GoodsOrder/Nats/NatsServer"
  4. "Cold_GoodsOrder/conf"
  5. "Cold_GoodsOrder/lib"
  6. "Cold_GoodsOrder/models/Account"
  7. "Cold_GoodsOrder/models/Device"
  8. "Cold_GoodsOrder/models/Function"
  9. "encoding/base64"
  10. "fmt"
  11. "image"
  12. "log"
  13. "math"
  14. "net/url"
  15. "os"
  16. "strconv"
  17. "strings"
  18. "time"
  19. beego "github.com/beego/beego/v2/server/web"
  20. "github.com/qiniu/go-sdk/v7/sms/bytes"
  21. "github.com/signintech/gopdf"
  22. )
  23. // Handle
  24. type OrderController struct {
  25. beego.Controller
  26. User_r Account.User // 登陆的用户
  27. T_pid int // 公司id
  28. }
  29. func (c *OrderController) Prepare() {
  30. c.User_r = *Account.User_r
  31. c.T_pid = *Account.T_pid
  32. }
  33. func (c *OrderController) Order_List() {
  34. type R_JSONS struct {
  35. //必须的大写开头
  36. Data []Function.OrderR
  37. Num int64
  38. Page int
  39. Page_size int
  40. }
  41. var r_jsons R_JSONS
  42. page, _ := c.GetInt("page")
  43. if page < 1 {
  44. page = 1
  45. }
  46. page_z, _ := c.GetInt("page_z")
  47. if page_z < 1 {
  48. page_z = conf.Page_size
  49. }
  50. t_sn := c.GetString("t_sn")
  51. orderid := c.GetString("t_orderid")
  52. startTime := c.GetString("startTime")
  53. endTime := c.GetString("endTime")
  54. c.Data["Name"] = t_sn
  55. r_jsons.Data, r_jsons.Num = Function.Read_Order_List(c.T_pid, page, page_z, orderid, t_sn, startTime, endTime)
  56. r_jsons.Page = page
  57. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  58. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  59. c.ServeJSON()
  60. return
  61. }
  62. func (c *OrderController) CreateOrderNo() {
  63. format := time.Now().Format("20060102")
  64. unix := time.Now().Unix()
  65. OrderNo := fmt.Sprintf("%v%v", format, unix)
  66. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: OrderNo}
  67. c.ServeJSON()
  68. return
  69. }
  70. func (c *OrderController) Order_Add() {
  71. T_orderid := c.GetString("T_orderid")
  72. WaybillNo := c.GetString("WaybillNo")
  73. T_sn := c.GetString("T_sn")
  74. T_receiving := c.GetString("T_receiving")
  75. ShippingUnit := c.GetString("ShippingUnit")
  76. ShippingName := c.GetString("ShippingName")
  77. EstimateStartTime := c.GetString("EstimateStartTime")
  78. EstimateEndTime := c.GetString("EstimateEndTime")
  79. T_text := c.GetString("T_text")
  80. PeerList := c.GetString("PeerList")
  81. DeviceType := c.GetString("DeviceType")
  82. IsExpatriate, _ := c.GetInt("IsExpatriate")
  83. Phone := c.GetString("Phone")
  84. ChildPid := c.GetString("ChildPid")
  85. CouriesName := c.GetString("CouriesName")
  86. CouriesId := c.GetString("CouriesId")
  87. DeliveryAddress := c.GetString("DeliveryAddress")
  88. ReceivingAddress := c.GetString("ReceivingAddress")
  89. if len(EstimateStartTime) <= 0 || len(EstimateEndTime) <= 0 {
  90. c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"}
  91. c.ServeJSON()
  92. return
  93. }
  94. start, err := time.Parse("2006-01-02", EstimateStartTime)
  95. endtime, err := time.Parse("2006-01-02", EstimateEndTime)
  96. if err != nil {
  97. c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"}
  98. c.ServeJSON()
  99. return
  100. }
  101. if len(CouriesId) != 0 {
  102. atoi, _ := strconv.Atoi(CouriesId)
  103. r := Function.Read_Couriers_ById(atoi)
  104. CouriesName = r.Name
  105. }
  106. var_ := Function.Order{
  107. T_pid: c.User_r.T_pid,
  108. T_orderid: T_orderid,
  109. WaybillNo: WaybillNo,
  110. ShippingUnit: ShippingUnit,
  111. T_sn: T_sn,
  112. T_receiving: T_receiving,
  113. EstimateStartTime: start,
  114. EstimateEndTime: endtime,
  115. T_text: T_text,
  116. T_State: 1,
  117. PeerList: PeerList,
  118. DeviceType: DeviceType,
  119. IsExpatriate: IsExpatriate,
  120. Phone: Phone,
  121. ChildPid: ChildPid,
  122. CouriesName: CouriesName,
  123. CouriesId: CouriesId,
  124. DeliveryAddress: DeliveryAddress,
  125. ReceivingAddress: ReceivingAddress,
  126. ShippingName: ShippingName,
  127. }
  128. if var_.IsExpatriate == 2 {
  129. if len(var_.Phone) < 0 {
  130. c.Data["json"] = lib.JSONS{Code: 203, Msg: "电话号码不能为空!"}
  131. c.ServeJSON()
  132. return
  133. }
  134. var_.ExpatriateReceiving = T_receiving
  135. }
  136. defer func() {
  137. s, _ := beego.AppConfig.String("orderUrl")
  138. addr := s + url.QueryEscape(lib.AesEncryptCBC(var_.T_orderid, lib.AesKey))
  139. sprintf := fmt.Sprintf("【冷链订单配送】您的订单正在配送中点击下方链接查看%v", addr)
  140. err = lib.SendModel(Phone, sprintf)
  141. if err != nil {
  142. c.Data["json"] = lib.JSONS{Code: 203, Msg: "发送短信失败!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. }()
  147. id, err := Function.Add_Order(var_)
  148. if err != nil {
  149. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  150. c.ServeJSON()
  151. return
  152. }
  153. NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "添加", var_)
  154. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  155. c.ServeJSON()
  156. }
  157. func (c *OrderController) Order_Get() {
  158. id, _ := c.GetInt("T_id")
  159. r := Function.Read_Order_ById(id)
  160. // 记录不存在
  161. if r.Id == 0 {
  162. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  163. c.ServeJSON()
  164. return
  165. }
  166. if c.User_r.T_pid != r.T_pid {
  167. c.Data["json"] = lib.JSONS{Code: 205, Msg: "没有权限!"}
  168. c.ServeJSON()
  169. return
  170. }
  171. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Function.OrderToOrderR(r)}
  172. c.ServeJSON()
  173. }
  174. func (c *OrderController) Order_Edit() {
  175. id, _ := c.GetInt("T_id")
  176. r := Function.Read_Order_ById(id)
  177. if r.Id == 0 {
  178. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id !"}
  179. c.ServeJSON()
  180. return
  181. }
  182. if c.T_pid != r.T_pid {
  183. c.Data["json"] = lib.JSONS{Code: 205, Msg: "没有权限!"}
  184. c.ServeJSON()
  185. return
  186. }
  187. WaybillNo := c.GetString("WaybillNo")
  188. T_sn := c.GetString("T_sn")
  189. T_receiving := c.GetString("T_receiving")
  190. ShippingUnit := c.GetString("ShippingUnit")
  191. ShippingName := c.GetString("ShippingName")
  192. EstimateStartTime := c.GetString("EstimateStartTime")
  193. EstimateEndTime := c.GetString("EstimateEndTime")
  194. T_text := c.GetString("T_text")
  195. PeerList := c.GetString("PeerList")
  196. DeviceType := c.GetString("DeviceType")
  197. IsExpatriate, _ := c.GetInt("IsExpatriate")
  198. Phone := c.GetString("Phone")
  199. ChildPid := c.GetString("ChildPid")
  200. T_State, _ := c.GetInt("T_State")
  201. CouriesName := c.GetString("CouriesName")
  202. CouriesId := c.GetString("CouriesId")
  203. DeliveryAddress := c.GetString("DeliveryAddress")
  204. ReceivingAddress := c.GetString("ReceivingAddress")
  205. if len(EstimateStartTime) <= 0 || len(EstimateEndTime) <= 0 {
  206. c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"}
  207. c.ServeJSON()
  208. return
  209. }
  210. start, err := time.Parse("2006-01-02", EstimateStartTime)
  211. endtime, err := time.Parse("2006-01-02", EstimateEndTime)
  212. if err != nil {
  213. c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"}
  214. c.ServeJSON()
  215. return
  216. }
  217. if len(WaybillNo) > 0 {
  218. r.WaybillNo = WaybillNo
  219. }
  220. if len(DeliveryAddress) > 0 {
  221. r.DeliveryAddress = DeliveryAddress
  222. }
  223. if len(ReceivingAddress) > 0 {
  224. r.ReceivingAddress = ReceivingAddress
  225. }
  226. if len(ShippingName) > 0 {
  227. r.ShippingName = ShippingName
  228. }
  229. if len(T_sn) > 0 {
  230. r.T_sn = T_sn
  231. }
  232. if len(T_receiving) > 0 {
  233. r.T_receiving = T_receiving
  234. }
  235. if len(T_text) > 0 {
  236. r.T_text = T_text
  237. }
  238. if len(ShippingUnit) > 0 {
  239. r.ShippingUnit = ShippingUnit
  240. }
  241. if len(EstimateStartTime) > 0 {
  242. r.EstimateStartTime = start
  243. }
  244. if len(EstimateEndTime) > 0 {
  245. r.EstimateEndTime = endtime
  246. }
  247. if len(PeerList) > 0 {
  248. r.PeerList = PeerList
  249. }
  250. if len(DeviceType) > 0 {
  251. r.DeviceType = DeviceType
  252. }
  253. if IsExpatriate != r.IsExpatriate {
  254. r.IsExpatriate = IsExpatriate
  255. }
  256. if len(Phone) > 0 {
  257. r.Phone = Phone
  258. }
  259. if len(ChildPid) > 0 {
  260. r.ChildPid = ChildPid
  261. }
  262. if len(CouriesName) > 0 {
  263. r.CouriesName = CouriesName
  264. }
  265. if len(CouriesId) > 0 {
  266. r.CouriesId = CouriesId
  267. }
  268. if T_State != 0 {
  269. r.T_State = T_State
  270. }
  271. is := Function.Update_Order(r, "waybill_no", "t_sn", "t_receiving", "shipping_unit", "estimate_start_time", "estimate_end_time", "peer_list", "device_type", "is_expatriate", "phone", "child_pid", "t__state", "t_text", "couries_name", "couries_id", "delivery_address", "receiving_address", "shipping_name")
  272. if !is {
  273. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  274. c.ServeJSON()
  275. return
  276. }
  277. NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "修改", r)
  278. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  279. c.ServeJSON()
  280. return
  281. }
  282. func (c *OrderController) GetDeviceData() {
  283. var err error
  284. Id, _ := c.GetInt("T_id")
  285. if Id == 0 {
  286. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  287. c.ServeJSON()
  288. return
  289. }
  290. Order_r := Function.Read_Order_ById(Id)
  291. if Order_r.Id == 0 {
  292. c.Data["json"] = lib.JSONS{Code: 207, Msg: "Id Err!"}
  293. c.ServeJSON()
  294. return
  295. }
  296. _, err = NatsServer.ReadDeviceByT_sn(Order_r.T_sn)
  297. if err != nil {
  298. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_sn Err!"}
  299. c.ServeJSON()
  300. return
  301. }
  302. DeviceSensor_r := NatsServer.ReadDeviceSensorALLByT_sn(Order_r.T_sn)
  303. if len(DeviceSensor_r) == 0 {
  304. c.Data["json"] = lib.JSONS{Code: 202, Msg: Order_r.T_sn + "设备没有数据!"}
  305. c.ServeJSON()
  306. return
  307. }
  308. var DeviceSensor_data []Device.DeviceData_R
  309. if Order_r.T_start_Ut.IsZero() {
  310. c.Data["json"] = lib.JSONS{Code: 202, Msg: Order_r.T_sn + "未开启配送"}
  311. c.ServeJSON()
  312. return
  313. }
  314. if Order_r.T_end_Ut.IsZero() {
  315. Order_r.T_end_Ut = time.Now()
  316. }
  317. DeviceSensor_data, _ = NatsServer.ReadDeviceDataListBy_T_snid(Order_r.T_sn+","+strconv.Itoa(DeviceSensor_r[0].T_id), Order_r.T_start_Ut.Format("2006-01-02 15:04:05"), Order_r.T_end_Ut.Format("2006-01-02 15:04:05"), 0, 9999)
  318. if len(DeviceSensor_data) == 0 {
  319. c.Data["json"] = lib.JSONS{Code: 202, Msg: Order_r.T_sn + "设备没有数据!"}
  320. c.ServeJSON()
  321. return
  322. }
  323. lastValidTsite := ""
  324. for i, sensor := range DeviceSensor_data {
  325. if len(sensor.T_site) <= 0 || sensor.T_site == "0,0" {
  326. DeviceSensor_data[i].T_site = lastValidTsite
  327. } else {
  328. lastValidTsite = sensor.T_site
  329. }
  330. }
  331. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功", Data: DeviceSensor_data}
  332. c.ServeJSON()
  333. }
  334. func (c *OrderController) Order_Del() {
  335. Id, _ := c.GetInt("T_id")
  336. r := Function.Read_Order_ById(Id)
  337. if r.Id == 0 {
  338. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  339. c.ServeJSON()
  340. return
  341. }
  342. if c.User_r.T_pid != r.T_pid {
  343. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  344. c.ServeJSON()
  345. return
  346. }
  347. if is := Function.Delete_Order(r); !is {
  348. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  349. c.ServeJSON()
  350. return
  351. }
  352. NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "删除", r)
  353. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  354. c.ServeJSON()
  355. return
  356. }
  357. // Order_PDF 列表 - 接口
  358. func (c *OrderController) Order_PDF() {
  359. var err error
  360. Id, _ := c.GetInt("T_id")
  361. if Id == 0 {
  362. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  363. c.ServeJSON()
  364. return
  365. }
  366. Order_r := Function.Read_Order_ById(Id)
  367. if Order_r.Id == 0 {
  368. c.Data["json"] = lib.JSONS{Code: 207, Msg: "Id Err!"}
  369. c.ServeJSON()
  370. return
  371. }
  372. Device_r, err := NatsServer.ReadDeviceByT_sn(Order_r.T_sn)
  373. if err != nil {
  374. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_sn Err!"}
  375. c.ServeJSON()
  376. return
  377. }
  378. DeviceSensor_r := NatsServer.ReadDeviceSensorALLByT_sn(Order_r.T_sn)
  379. if len(DeviceSensor_r) == 0 {
  380. c.Data["json"] = lib.JSONS{Code: 202, Msg: Order_r.T_sn + "设备没有数据!"}
  381. c.ServeJSON()
  382. return
  383. }
  384. var DeviceSensor_data []Device.DeviceData_R
  385. DeviceSensor_data, _ = NatsServer.ReadDeviceDataListBy_T_snid(Order_r.T_sn+","+strconv.Itoa(DeviceSensor_r[0].T_id), Order_r.T_start_Ut.Format("2006-01-02 15:04:05"), Order_r.T_end_Ut.Format("2006-01-02 15:04:05"), 0, 9999)
  386. if len(DeviceSensor_data) == 0 {
  387. c.Data["json"] = lib.JSONS{Code: 202, Msg: Order_r.T_sn + "设备没有数据!"}
  388. c.ServeJSON()
  389. return
  390. }
  391. pdf := &gopdf.GoPdf{}
  392. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  393. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  394. if err != nil {
  395. c.Data["json"] = lib.JSONS{Code: 204, Msg: err.Error()}
  396. c.ServeJSON()
  397. return
  398. }
  399. err = pdf.SetFont("simsun", "", 24)
  400. if err != nil {
  401. c.Data["json"] = lib.JSONS{Code: 205, Msg: err.Error()}
  402. c.ServeJSON()
  403. return
  404. }
  405. pdf.SetGrayFill(0.5)
  406. pdf.SetMargins(0, 20, 0, 20)
  407. pdf.AddPage()
  408. company, _ := NatsServer.Read_Company_ById(c.T_pid)
  409. log.Println(c.T_pid, c.User_r.T_pid)
  410. textw, _ := pdf.MeasureTextWidth(company.T_name + "-货单详情")
  411. pdf.SetX((595 / 2) - (textw / 2))
  412. pdf.SetY(40)
  413. pdf.Text(company.T_name + "-货单详情")
  414. // 线
  415. pdf.SetLineWidth(2)
  416. pdf.SetLineType("dashed")
  417. pdf.Line(10, 60, 585, 60)
  418. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  419. if err != nil {
  420. c.Data["json"] = lib.JSONS{Code: 206, Msg: err.Error()}
  421. c.ServeJSON()
  422. return
  423. }
  424. err = pdf.SetFont("wts", "", 10)
  425. if err != nil {
  426. c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()}
  427. c.ServeJSON()
  428. return
  429. }
  430. pages := pdf.GetNumberOfPages()
  431. pagenums := fmt.Sprintf("第 %d 页", pages)
  432. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  433. pdf.SetX((595 / 2) - (pagenum / 2))
  434. pdf.SetY(830) // 设置页码位置
  435. pdf.Cell(nil, pagenums)
  436. // -------------
  437. x := 22.0
  438. w1 := 80.0
  439. w2 := 195.0
  440. lib.RectFillColor(pdf, "订单号:", 12, x, 80, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  441. x += w1
  442. lib.RectFillColor(pdf, Order_r.T_orderid, 12, x, 80, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  443. x += w2
  444. lib.RectFillColor(pdf, "运单号:", 12, x, 80, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  445. x += w1
  446. lib.RectFillColor(pdf, Order_r.T_outorderid, 12, x, 80, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  447. x += w2
  448. // -------------
  449. x = 22.0
  450. w1 = 80.0
  451. w2 = 195.0
  452. lib.RectFillColor(pdf, "设备名称:", 12, x, 100, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  453. x += w1
  454. lib.RectFillColor(pdf, Device_r.T_devName, 12, x, 100, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  455. x += w2
  456. lib.RectFillColor(pdf, "设备编号:", 12, x, 100, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  457. x += w1
  458. lib.RectFillColor(pdf, Device_r.T_sn, 12, x, 100, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  459. x += w2
  460. // -------------
  461. x = 22.0
  462. w1 = 80.0
  463. w2 = 240.0
  464. T_time := fmt.Sprintf("%s", Order_r.T_start_Ut.Format("2006-01-02 15:04:05")+" - "+Order_r.T_end_Ut.Format("2006-01-02 15:04:05"))
  465. lib.RectFillColor(pdf, "送货时间:", 12, x, 120, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  466. x += w1
  467. lib.RectFillColor(pdf, T_time, 12, x, 120, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  468. x += w2
  469. T_time = fmt.Sprintf("%s", Order_r.CreateTime.Format("2006-01-02 15:04:05"))
  470. lib.RectFillColor(pdf, "订单时间:", 12, x, 120, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  471. x += w1
  472. lib.RectFillColor(pdf, T_time, 12, x, 120, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  473. // -------------
  474. x = 22.0
  475. w1 = 80.0
  476. w2 = 195.0*2 + 80
  477. shippingName := Order_r.ShippingName
  478. if len(Order_r.ShippingName) == 0 {
  479. shippingName = Order_r.ShippingUnit
  480. }
  481. ShippingUnit := fmt.Sprintf("%s", shippingName)
  482. lib.RectFillColor(pdf, "发货单位:", 12, x, 140, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  483. x += w1
  484. lib.RectFillColor(pdf, ShippingUnit, 12, x, 140, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  485. x += w2
  486. x = 22.0
  487. T_text := fmt.Sprintf("%s", Order_r.T_receiving)
  488. lib.RectFillColor(pdf, "收货单位:", 12, x, 160, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  489. x += w1
  490. lib.RectFillColor(pdf, T_text, 12, x, 160, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  491. x += w2
  492. // 数据汇总--------------------------------
  493. // 最低温度
  494. var T_t_l float32 = 100.0
  495. // 最高温度
  496. var T_t_u float32 = 0.0
  497. var T_t_v float32 = 0.0
  498. for n := 0; len(DeviceSensor_data) > n; n++ {
  499. // -计算温度
  500. if DeviceSensor_data[n].T_t < T_t_l {
  501. T_t_l = DeviceSensor_data[n].T_t
  502. }
  503. if DeviceSensor_data[n].T_t > T_t_u {
  504. T_t_u = DeviceSensor_data[n].T_t
  505. }
  506. T_t_v += DeviceSensor_data[n].T_t
  507. }
  508. var y float64 = 180
  509. x = 22.0
  510. w1 = 60.0
  511. w2 = 50.0
  512. err = pdf.SetFont("wts", "", 10)
  513. if err != nil {
  514. c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()}
  515. c.ServeJSON()
  516. return
  517. }
  518. T_t := fmt.Sprintf(" %.1f℃ ", DeviceSensor_data[len(DeviceSensor_data)-1].T_t)
  519. lib.RectFillColor(pdf, "起送温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  520. x += w1
  521. lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  522. x += w2
  523. T_t = fmt.Sprintf(" %.1f℃ ", DeviceSensor_data[0].T_t)
  524. lib.RectFillColor(pdf, "送达温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  525. x += w1
  526. lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  527. x += w2
  528. T_t = fmt.Sprintf(" %.1f℃ ", T_t_l)
  529. lib.RectFillColor(pdf, "最低温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  530. x += w1
  531. lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  532. x += w2
  533. T_t = fmt.Sprintf(" %.1f℃ ", T_t_v/float32(len(DeviceSensor_data)))
  534. lib.RectFillColor(pdf, "平均温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  535. x += w1
  536. lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  537. x += w2
  538. T_t = fmt.Sprintf(" %.1f℃ ", T_t_u)
  539. lib.RectFillColor(pdf, "最高温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  540. x += w1
  541. lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  542. x += w2
  543. var height int
  544. getString := c.GetString("images")
  545. split := strings.Split(getString, ",")
  546. if len(split) >= 2 {
  547. imageBytes, err := base64.StdEncoding.DecodeString(split[1])
  548. if err != nil {
  549. c.Data["json"] = lib.JSONS{Code: 207, Msg: "无效的图像数据"}
  550. c.ServeJSON()
  551. return
  552. }
  553. decode, _, err := image.Decode(bytes.NewReader(imageBytes))
  554. // 获取图片的高度
  555. bounds := decode.Bounds()
  556. height = bounds.Dy()
  557. if height >= 500 {
  558. height = 500
  559. }
  560. with := bounds.Dx()
  561. log.Println("图片宽高", with, height)
  562. y = 220
  563. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  564. err = pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: float64(height)})
  565. if err != nil {
  566. c.Data["json"] = lib.JSONS{Code: 207, Msg: "添加图片失败"}
  567. c.ServeJSON()
  568. return
  569. }
  570. } else {
  571. c.Data["json"] = lib.JSONS{Code: 207, Msg: "无效的图像数据"}
  572. c.ServeJSON()
  573. return
  574. }
  575. // -----------
  576. textw, _ = pdf.MeasureTextWidth("历史数据")
  577. pdf.SetX(20)
  578. pdf.SetY(float64(230 + height))
  579. pdf.Text("历史数据:")
  580. // 数据展示--------------------------------
  581. //var y float64 = 220
  582. y = float64(240 + height)
  583. x = 22.0
  584. w1 = 94.0
  585. w2 = 43.0
  586. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  587. x += w1
  588. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  589. x += w2
  590. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  591. x += w1
  592. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  593. x += w2
  594. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  595. x += w1
  596. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  597. x += w2
  598. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  599. x += w1
  600. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  601. x += w2
  602. // 数据展示--------------------------------
  603. y = float64(260 + height)
  604. err = pdf.SetFont("wts", "", 8)
  605. if err != nil {
  606. c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()}
  607. c.ServeJSON()
  608. return
  609. }
  610. nln := 0
  611. x = 22.0
  612. for n := 0; len(DeviceSensor_data) > n; n++ {
  613. var textH float64 = 25 // if text height is 25px.
  614. pdf.SetNewY(y, textH)
  615. y = pdf.GetY()
  616. nln++
  617. if nln > 4 {
  618. nln = 1
  619. x = 22.0
  620. y += 18
  621. }
  622. if y >= 794 {
  623. err = pdf.SetFont("wts", "", 10)
  624. if err != nil {
  625. c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()}
  626. c.ServeJSON()
  627. return
  628. }
  629. pdf.AddPage()
  630. pages := pdf.GetNumberOfPages()
  631. pagenums := fmt.Sprintf("第 %d 页", pages)
  632. pagenum, _ := pdf.MeasureTextWidth(pagenums)
  633. pdf.SetX((595 / 2) - (pagenum / 2))
  634. pdf.SetY(830) // 设置页码位置
  635. pdf.Cell(nil, pagenums)
  636. x = 22.0
  637. y = 20
  638. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  639. x += w1
  640. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  641. x += w2
  642. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  643. x += w1
  644. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  645. x += w2
  646. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  647. x += w1
  648. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  649. x += w2
  650. lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  651. x += w1
  652. lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  653. x += w2
  654. x = 22.0
  655. y += 20
  656. }
  657. err = pdf.SetFont("wts", "", 8)
  658. // ------------------
  659. T_t = fmt.Sprintf(" %.1f ", DeviceSensor_data[n].T_t)
  660. T_time = fmt.Sprintf("%s", DeviceSensor_data[n].T_time)
  661. lib.RectFillColor(pdf, T_time, 10, x, y, w1, 18, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  662. x += w1
  663. lib.RectFillColor(pdf, T_t, 10, x, y, w2, 18, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  664. x += w2
  665. }
  666. err = pdf.SetFont("wts", "", 10)
  667. if err != nil {
  668. c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()}
  669. c.ServeJSON()
  670. return
  671. }
  672. //----------------
  673. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  674. err = pdf.WritePdf(timeStr)
  675. if err != nil {
  676. c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()}
  677. c.ServeJSON()
  678. return
  679. }
  680. //上传 OSS
  681. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  682. url, is := NatsServer.Qiniu_UploadFile(timeStr, timeStr)
  683. if !is {
  684. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  685. c.ServeJSON()
  686. return
  687. }
  688. //删除目录
  689. err = os.Remove(timeStr)
  690. if err != nil {
  691. fmt.Println(err)
  692. }
  693. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  694. c.ServeJSON()
  695. return
  696. }