waybill.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. package service
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service/dto"
  5. "cold-logistics/common/actions"
  6. cDto "cold-logistics/common/dto"
  7. "cold-logistics/common/global"
  8. model2 "cold-logistics/common/model"
  9. "cold-logistics/common/nats/nats_server"
  10. "errors"
  11. "fmt"
  12. "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
  13. "gogs.baozhida.cn/zoie/OAuth-core/service"
  14. "gorm.io/gorm"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. type Waybill struct {
  21. service.Service
  22. }
  23. func WaybillCustomerStatusScopes(status int) func(db *gorm.DB) *gorm.DB {
  24. return func(db *gorm.DB) *gorm.DB {
  25. // 未发货
  26. if status == 1 {
  27. statusList := []int{
  28. model.WaybillStatusTruck,
  29. }
  30. return db.Where("status in (?)", statusList)
  31. }
  32. // 已发货
  33. if status == 2 {
  34. statusList := []int{
  35. model.WaybillStatusTruck,
  36. model.WaybillStatusStorage,
  37. model.WaybillStatusTruckOut,
  38. model.WaybillStatusStorageOut,
  39. model.WaybillStatusVanning,
  40. model.WaybillStatusVanningOut,
  41. }
  42. return db.Where("status in (?)", statusList)
  43. }
  44. // 已签收
  45. if status == 3 {
  46. statusList := []int{
  47. model.WaybillStatusReceipt,
  48. }
  49. return db.Where("status in (?)", statusList)
  50. }
  51. // 已处理
  52. if status == 4 {
  53. statusList := []int{
  54. model.WaybillStatusWaitDelivery,
  55. model.WaybillStatusWaitStorage,
  56. model.WaybillStatusWaitVanning,
  57. }
  58. return db.Where("status in (?)", statusList)
  59. }
  60. return db
  61. }
  62. }
  63. // GetPage 获取Waybill列表
  64. func (e *Waybill) GetPage(c *dto.WaybillGetPageReq, list *[]model.Waybill, count *int64, p *actions.DataPermission) error {
  65. var err error
  66. var data model.Waybill
  67. if c.PageSize == 9999 {
  68. err = e.Orm.Model(&data).
  69. Scopes(
  70. cDto.MakeCondition(c.GetNeedSearch()),
  71. actions.Permission(data.TableName(), p),
  72. ).
  73. Find(list).Limit(-1).Offset(-1).
  74. Count(count).Error
  75. if err != nil {
  76. e.Log.Errorf("db error: %s", err)
  77. return global.GetFailedErr
  78. }
  79. return nil
  80. }
  81. err = e.Orm.Model(&data).
  82. Scopes(
  83. cDto.MakeCondition(c.GetNeedSearch()),
  84. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  85. actions.Permission(data.TableName(), p),
  86. ).
  87. Preload("PrintUser").
  88. Preload("CoolerBox").
  89. Find(list).Limit(-1).Offset(-1).
  90. Count(count).Error
  91. if err != nil {
  92. e.Log.Errorf("db error: %s", err)
  93. return global.GetFailedErr
  94. }
  95. return nil
  96. }
  97. func (e *Waybill) GetAppletPage(c *dto.WaybillGetAppletPageReq, list *[]model.Waybill, count *int64, p *actions.DataPermission) error {
  98. var err error
  99. //var data model.Waybill
  100. var logistics model.WaybillLogistics
  101. err = e.Orm.Table("waybill").
  102. Select("waybill.*,waybill_logistics.status as status").
  103. Scopes(
  104. cDto.MakeCondition(c.GetNeedSearch()),
  105. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  106. actions.Permission(logistics.TableName(), p)).
  107. Where("waybill_logistics.id in (SELECT MAX(id) FROM waybill_logistics where user_id = ? group by waybill_no )", p.UserId).
  108. Joins("left join waybill_logistics on waybill.waybill_no = waybill_logistics.waybill_no").
  109. Find(&list).Limit(-1).Offset(-1).Count(count).Error
  110. if err != nil {
  111. e.Log.Errorf("db error: %s", err)
  112. return global.GetFailedErr
  113. }
  114. for i := 0; i < len(*list); i++ {
  115. if (*list)[i].CoolerBoxId == 0 {
  116. continue
  117. }
  118. var coolerBox model.CoolerBoxOmit
  119. err = e.Orm.First(&coolerBox, (*list)[i].CoolerBoxId).Error
  120. if err != nil {
  121. e.Log.Errorf("查询保温箱信息失败: %s", err)
  122. }
  123. (*list)[i].CoolerBox = coolerBox
  124. }
  125. return nil
  126. }
  127. func (e *Waybill) GetAppletCount(list *[]model.Waybill, count *int64, p *actions.DataPermission) error {
  128. var err error
  129. //var data model.Waybill
  130. var logistics model.WaybillLogistics
  131. err = e.Orm.Table("waybill").
  132. Select("waybill.*,waybill_logistics.status as status").
  133. Scopes(
  134. actions.Permission(logistics.TableName(), p)).
  135. Where("waybill_logistics.id in (SELECT MAX(id) FROM waybill_logistics where user_id = ? group by waybill_no )", p.UserId).
  136. Joins("left join waybill_logistics on waybill.waybill_no = waybill_logistics.waybill_no").
  137. Find(list).Count(count).Error
  138. if err != nil {
  139. e.Log.Errorf("db error: %s", err)
  140. return global.GetFailedErr
  141. }
  142. return nil
  143. }
  144. func (e *Waybill) GetCustomerPage(c *dto.WaybillGetCustomerPageReq, list *[]model.Waybill, count *int64, p *actions.DataPermission) error {
  145. var err error
  146. var data model.Waybill
  147. if c.PageSize == 9999 {
  148. err = e.Orm.Model(&data).
  149. Scopes(
  150. WaybillCustomerStatusScopes(c.Status),
  151. cDto.MakeCondition(c.GetNeedSearch()),
  152. actions.Permission(data.TableName(), p),
  153. ).
  154. Find(list).Limit(-1).Offset(-1).
  155. Count(count).Error
  156. if err != nil {
  157. e.Log.Errorf("db error: %s", err)
  158. return global.GetFailedErr
  159. }
  160. return nil
  161. }
  162. err = e.Orm.Model(&data).
  163. Scopes(
  164. WaybillCustomerStatusScopes(c.Status),
  165. cDto.MakeCondition(c.GetNeedSearch()),
  166. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  167. actions.Permission(data.TableName(), p),
  168. ).
  169. Preload("CoolerBox").
  170. Find(list).Limit(-1).Offset(-1).
  171. Count(count).Error
  172. if err != nil {
  173. e.Log.Errorf("db error: %s", err)
  174. return global.GetFailedErr
  175. }
  176. return nil
  177. }
  178. // Get 获取Waybill对象
  179. func (e *Waybill) Get(d *dto.WaybillGetReq, waybillModel *model.Waybill, p *actions.DataPermission) error {
  180. err := e.Orm.
  181. Scopes(actions.Permission(waybillModel.TableName(), p)).
  182. Where("waybill_no = ?", d.WaybillNo).
  183. Preload("PrintUser").
  184. Preload("CoolerBox").
  185. Preload("Dept").
  186. First(waybillModel).Error
  187. if err != nil {
  188. e.Log.Errorf("db error: %s", err)
  189. if errors.Is(err, gorm.ErrRecordNotFound) {
  190. return global.GetNotFoundOrNoPermissionErr
  191. }
  192. return global.GetFailedErr
  193. }
  194. return nil
  195. }
  196. func (e *Waybill) GetByWaybillNo(d *dto.WaybillGetByWaybillPdfReq, waybillModel *model.Waybill, p *actions.DataPermission) error {
  197. err := e.Orm.
  198. Scopes(actions.Permission(waybillModel.TableName(), p)).
  199. Where("waybill_no = ?", d.WaybillNo).
  200. Preload("Dept").
  201. First(waybillModel).Error
  202. if err != nil {
  203. e.Log.Errorf("db error: %s", err)
  204. if errors.Is(err, gorm.ErrRecordNotFound) {
  205. return global.GetNotFoundOrNoPermissionErr
  206. }
  207. return global.GetFailedErr
  208. }
  209. return nil
  210. }
  211. // Insert 创建Waybill对象
  212. func (e *Waybill) Insert(c *dto.WaybillInsertReq) error {
  213. var err error
  214. var data model.Waybill
  215. tx := e.Orm.Begin()
  216. defer func() {
  217. if err != nil {
  218. tx.Rollback()
  219. } else {
  220. tx.Commit()
  221. }
  222. }()
  223. var no string
  224. for {
  225. no = time.Now().Format("200601021504") + utils.GetRandString(6, "0123456789", 0)
  226. var j int64
  227. err = e.Orm.Model(&data).Where("waybill_no = ?", no).Count(&j).Error
  228. if err != nil {
  229. continue
  230. }
  231. if j == 0 {
  232. break
  233. }
  234. }
  235. // 添加运单
  236. data.WaybillNo = no
  237. c.Generate(&data)
  238. err = tx.Create(&data).Error
  239. if err != nil {
  240. e.Log.Errorf("db error: %s", err)
  241. return global.CreateFailedErr
  242. }
  243. c.Id = data.Id
  244. return nil
  245. }
  246. // AppletInsert 员工添加运单
  247. func (e *Waybill) AppletInsert(c *dto.WaybillInsertReq, p *actions.DataPermission) error {
  248. var err error
  249. var data model.Waybill
  250. tx := e.Orm.Begin()
  251. defer func() {
  252. if err != nil {
  253. tx.Rollback()
  254. } else {
  255. tx.Commit()
  256. }
  257. }()
  258. var userModel = model.SysUser{}
  259. // 查询运单是否存在
  260. err = tx.Scopes(actions.Permission(userModel.TableName(), p)).
  261. First(&userModel, p.UserId).Error
  262. if err != nil {
  263. e.Log.Errorf("db error: %s", err)
  264. if errors.Is(err, gorm.ErrRecordNotFound) {
  265. return global.GetNotFoundErr
  266. }
  267. return global.CreateFailedErr
  268. }
  269. if c.CoolerBoxId == 0 && (userModel.Type != model.SysUserTypeDriver && userModel.Type != model.SysUserTypeWarehouse) && userModel.UserType != "customer" {
  270. err = errors.New("无权添加!")
  271. return err
  272. }
  273. var status = model.WaybillStatusWaitDelivery
  274. var car = model.Car{}
  275. if userModel.Type == model.SysUserTypeDriver {
  276. status = model.WaybillStatusWaitTruck
  277. // 查询车辆库信息
  278. err = tx.Scopes(actions.Permission(car.TableName(), p)).
  279. Where("user_id = ?", p.UserId).
  280. First(&car).Error
  281. if err != nil {
  282. e.Log.Errorf("db error: %s", err)
  283. return errors.New("获取车辆信息失败")
  284. }
  285. }
  286. var warehouse = model.Warehouse{}
  287. if userModel.Type == model.SysUserTypeWarehouse {
  288. status = model.WaybillStatusWaitStorage
  289. // 查询仓库信息
  290. err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
  291. Where("user_id = ?", p.UserId).
  292. First(&warehouse).Error
  293. if err != nil {
  294. e.Log.Errorf("db error: %s", err)
  295. return errors.New("获取仓库信息失败")
  296. }
  297. }
  298. var coolerBox = model.CoolerBox{}
  299. if c.DeliveryCondition == "保温箱" {
  300. status = model.WaybillStatusWaitVanning
  301. // 查询保温箱信息
  302. err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
  303. Where("id = ?", c.CoolerBoxId).
  304. First(&coolerBox).Error
  305. if err != nil {
  306. e.Log.Errorf("db error: %s", err)
  307. return errors.New("获取保温箱信息失败")
  308. }
  309. }
  310. data.PrintUserId = p.UserId
  311. var no string
  312. for {
  313. no = time.Now().Format("200601021504") + utils.GetRandString(6, "0123456789", 0)
  314. var j int64
  315. err = e.Orm.Model(&data).Where("waybill_no = ?", no).Count(&j).Error
  316. if err != nil {
  317. continue
  318. }
  319. if j == 0 {
  320. break
  321. }
  322. }
  323. // 添加运单
  324. data.DeptId = p.DeptId
  325. data.CreateBy = p.UserId
  326. data.WaybillNo = no
  327. c.Generate(&data)
  328. data.Status = status
  329. err = tx.Create(&data).Error
  330. if err != nil {
  331. e.Log.Errorf("db error: %s", err)
  332. return global.CreateFailedErr
  333. }
  334. c.Id = data.Id
  335. // 添加物流
  336. Logistics := model.WaybillLogistics{
  337. WaybillNo: data.WaybillNo,
  338. Status: data.Status,
  339. CarId: car.Id,
  340. WarehouseId: warehouse.Id,
  341. CoolerBoxId: coolerBox.Id,
  342. UserId: p.UserId,
  343. ControlBy: model2.ControlBy{
  344. CreateBy: p.UserId,
  345. },
  346. DeptBy: model2.DeptBy{
  347. DeptId: p.DeptId,
  348. },
  349. }
  350. err = tx.Create(&Logistics).Error
  351. if err != nil {
  352. e.Log.Errorf("db error: %s", err)
  353. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  354. }
  355. return nil
  356. }
  357. // Update 修改Waybill对象
  358. func (e *Waybill) Update(c *dto.WaybillUpdateReq, p *actions.DataPermission) error {
  359. var err error
  360. tx := e.Orm.Begin()
  361. defer func() {
  362. if err != nil {
  363. tx.Rollback()
  364. } else {
  365. tx.Commit()
  366. }
  367. }()
  368. var waybillModel = model.Waybill{}
  369. // 查询运单是否存在
  370. err = e.Orm.Scopes(actions.Permission(waybillModel.TableName(), p)).
  371. First(&waybillModel, c.GetId()).Error
  372. if err != nil {
  373. e.Log.Errorf("db error: %s", err)
  374. if errors.Is(err, gorm.ErrRecordNotFound) {
  375. return global.UpdateNotFoundOrNoPermissionErr
  376. }
  377. return global.UpdateFailedErr
  378. }
  379. c.Generate(&waybillModel)
  380. err = tx.Save(&waybillModel).Error
  381. if err != nil {
  382. e.Log.Errorf("db error: %s", err)
  383. return global.UpdateFailedErr
  384. }
  385. c.Id = waybillModel.Id
  386. return nil
  387. }
  388. func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission) error {
  389. var err error
  390. tx := e.Orm.Begin()
  391. defer func() {
  392. if err != nil {
  393. tx.Rollback()
  394. } else {
  395. tx.Commit()
  396. }
  397. }()
  398. var car = model.Car{}
  399. if c.Type == model.SysUserTypeDriver {
  400. // 查询车辆库信息
  401. err = tx.Scopes(actions.Permission(car.TableName(), p)).
  402. Where("user_id = ?", c.PrintUserId).
  403. First(&car).Error
  404. if err != nil {
  405. e.Log.Errorf("db error: %s", err)
  406. return errors.New("获取车辆信息失败")
  407. }
  408. }
  409. var warehouse = model.Warehouse{}
  410. if c.Type == model.SysUserTypeWarehouse {
  411. // 查询仓库信息
  412. err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
  413. Where("user_id = ?", c.PrintUserId).
  414. First(&warehouse).Error
  415. if err != nil {
  416. e.Log.Errorf("db error: %s", err)
  417. return errors.New("获取仓库信息失败")
  418. }
  419. }
  420. var coolerBox = model.CoolerBox{}
  421. if c.Type == model.TypeCoolerBox {
  422. // 查询保温箱信息
  423. err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
  424. Where("id = ?", c.CoolerBoxId).
  425. First(&coolerBox).Error
  426. if err != nil {
  427. e.Log.Errorf("db error: %s", err)
  428. return errors.New("获取保温箱信息失败")
  429. }
  430. var user = model.SysUser{}
  431. // 查询制单人信息
  432. err = tx.Scopes(actions.Permission(user.TableName(), p)).
  433. Where("id = ?", c.PrintUserId).
  434. First(&user).Error
  435. if err != nil {
  436. e.Log.Errorf("db error: %s", err)
  437. return errors.New("获取保温箱配送人失败")
  438. }
  439. }
  440. for _, id := range c.WaybillIds {
  441. var waybillModel = model.Waybill{}
  442. // 查询运单是否存在
  443. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  444. First(&waybillModel, id).Error
  445. if err != nil {
  446. e.Log.Errorf("db error: %s", err)
  447. if errors.Is(err, gorm.ErrRecordNotFound) {
  448. return global.UpdateNotFoundOrNoPermissionErr
  449. }
  450. return global.UpdateFailedErr
  451. }
  452. // 待派单 待装车 待入库 待装箱
  453. if waybillModel.Status != model.WaybillStatusWaitDelivery &&
  454. waybillModel.Status != model.WaybillStatusWaitTruck &&
  455. waybillModel.Status != model.WaybillStatusWaitStorage &&
  456. waybillModel.Status != model.WaybillStatusWaitVanning {
  457. //err = errors.New(fmt.Sprintf("运单状态为%s,禁止操作!", model.WaybillStatusMap[waybillModel.Status]))
  458. //return err
  459. continue
  460. }
  461. if c.Type == model.SysUserTypeDriver {
  462. waybillModel.Status = model.WaybillStatusWaitTruck
  463. }
  464. if c.Type == model.SysUserTypeWarehouse {
  465. waybillModel.Status = model.WaybillStatusWaitStorage
  466. }
  467. if c.Type == model.TypeCoolerBox {
  468. waybillModel.Status = model.WaybillStatusWaitVanning
  469. waybillModel.CoolerBoxId = coolerBox.Id
  470. }
  471. waybillModel.PrintUserId = c.PrintUserId
  472. err = tx.Save(&waybillModel).Error
  473. if err != nil {
  474. e.Log.Errorf("db error: %s", err)
  475. return global.UpdateFailedErr
  476. }
  477. // 添加物流
  478. Logistics := model.WaybillLogistics{
  479. WaybillNo: waybillModel.WaybillNo,
  480. Status: waybillModel.Status,
  481. CarId: car.Id,
  482. WarehouseId: warehouse.Id,
  483. CoolerBoxId: coolerBox.Id,
  484. UserId: c.PrintUserId,
  485. ControlBy: model2.ControlBy{
  486. CreateBy: p.UserId,
  487. },
  488. DeptBy: model2.DeptBy{
  489. DeptId: p.DeptId,
  490. },
  491. }
  492. err = tx.Create(&Logistics).Error
  493. if err != nil {
  494. e.Log.Errorf("db error: %s", err)
  495. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  496. }
  497. }
  498. return nil
  499. }
  500. // Remove 删除Waybill
  501. func (e *Waybill) Remove(c *dto.WaybillDeleteReq, p *actions.DataPermission) error {
  502. var err error
  503. tx := e.Orm.Begin()
  504. defer func() {
  505. if err != nil {
  506. tx.Rollback()
  507. } else {
  508. tx.Commit()
  509. }
  510. }()
  511. var waybillModel model.Waybill
  512. // 查询运单是否存在
  513. err = e.Orm.Scopes(actions.Permission(waybillModel.TableName(), p)).
  514. First(&waybillModel, c.GetId()).Error
  515. if err != nil {
  516. e.Log.Errorf("db error: %s", err)
  517. if errors.Is(err, gorm.ErrRecordNotFound) {
  518. return global.DeleteNotFoundOrNoPermissionErr
  519. }
  520. return global.DeleteFailedErr
  521. }
  522. if waybillModel.Status != model.WaybillStatusWaitDelivery && waybillModel.Status != model.WaybillStatusWaitTruck && waybillModel.Status != model.WaybillStatusWaitStorage {
  523. return errors.New(fmt.Sprintf("运单状态为%s,禁止删除", model.WaybillStatusMap[waybillModel.Status]))
  524. }
  525. db := tx.Delete(&waybillModel)
  526. if err = db.Error; err != nil {
  527. e.Log.Errorf("db error: %s", err)
  528. return global.DeleteFailedErr
  529. }
  530. if db.RowsAffected == 0 {
  531. return global.DeleteNotFoundOrNoPermissionErr
  532. }
  533. return nil
  534. }
  535. func (e *Waybill) WarehouseIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error {
  536. var err error
  537. tx := e.Orm.Begin()
  538. defer func() {
  539. if err != nil {
  540. tx.Rollback()
  541. } else {
  542. tx.Commit()
  543. }
  544. }()
  545. // 查询仓库信息
  546. var warehouse = model.Warehouse{}
  547. // 查询运单是否存在
  548. err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
  549. Where("user_id = ?", p.UserId).
  550. First(&warehouse).Error
  551. if err != nil {
  552. e.Log.Errorf("db error: %s", err)
  553. return errors.New("获取仓库信息失败")
  554. }
  555. for _, waybillNo := range c.WaybillNoList {
  556. var waybillModel = model.Waybill{}
  557. // 查询运单是否存在
  558. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  559. Where("waybill_no = ?", waybillNo).
  560. First(&waybillModel).Error
  561. if err != nil {
  562. e.Log.Errorf("db error: %s", err)
  563. if errors.Is(err, gorm.ErrRecordNotFound) {
  564. return errors.New(fmt.Sprintf("运单号%s不存在", waybillNo))
  565. }
  566. return errors.New(fmt.Sprintf("运单号%s查询失败", waybillNo))
  567. }
  568. if waybillModel.WarehouseId == warehouse.Id && waybillModel.Status == model.WaybillStatusStorage {
  569. continue
  570. }
  571. if waybillModel.WarehouseId != warehouse.Id && waybillModel.Status == model.WaybillStatusStorage {
  572. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法入库!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  573. return err
  574. }
  575. if waybillModel.Status == model.WaybillStatusWaitTruck || waybillModel.Status == model.WaybillStatusWaitVanning {
  576. // 待装车
  577. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法入库!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  578. return err
  579. }
  580. // 验证时间
  581. var logistics model.WaybillLogistics
  582. err = e.Orm.Where("waybill_no = ?", waybillNo).Last(&logistics).Error
  583. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  584. e.Log.Errorf("获取运单日志失败: %s", err)
  585. return err
  586. }
  587. if time.Time(logistics.CreatedAt).After(time.Time(c.StartTime)) {
  588. err = errors.New(fmt.Sprintf("运单号%s入库时间[%s]与上一阶段时间[%s]冲突,无法入库!", waybillNo, c.StartTime.String(), logistics.CreatedAt.String()))
  589. return err
  590. }
  591. var lng, lat string
  592. lng, lat, err = e.GetSite(p.DeptId, warehouse.Sn, c.StartTime.String())
  593. if err != nil {
  594. e.Log.Errorf("获取定位信息失败: %s", err)
  595. return err
  596. }
  597. // 如果上一个状态为装车,则自动下车
  598. if waybillModel.Status == model.WaybillStatusTruck {
  599. // 已装车
  600. var car model.Car
  601. err = e.Orm.First(&car, waybillModel.CarId).Error
  602. if err != nil {
  603. e.Log.Errorf("db error: %s", err)
  604. return errors.New(fmt.Sprintf("查询仓库信息失败:%s", err))
  605. }
  606. // 添加物流
  607. Logistics := model.WaybillLogistics{
  608. WaybillNo: waybillNo,
  609. Status: model.WaybillStatusTruckOut,
  610. CarId: waybillModel.CarId,
  611. UserId: car.UserId,
  612. Lng: lng,
  613. Lat: lat,
  614. ControlBy: model2.ControlBy{
  615. CreateBy: p.UserId,
  616. },
  617. DeptBy: model2.DeptBy{
  618. DeptId: car.DeptId,
  619. },
  620. ModelTime: model2.ModelTime{
  621. CreatedAt: c.StartTime,
  622. },
  623. }
  624. err = tx.Create(&Logistics).Error
  625. if err != nil {
  626. e.Log.Errorf("db error: %s", err)
  627. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  628. }
  629. // 查询任务
  630. var task model.WaybillTask
  631. err = tx.Model(&task).Where("waybill_no = ? and car_id = ?", waybillNo, waybillModel.CarId).
  632. Last(&task).Error
  633. if err != nil {
  634. e.Log.Errorf("db error: %s", err)
  635. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  636. }
  637. task.EndTime = c.StartTime
  638. task.UpdateBy = p.UserId
  639. err = tx.Save(&task).Error
  640. if err != nil {
  641. e.Log.Errorf("db error: %s", err)
  642. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  643. }
  644. }
  645. // 如果上一个状态为装箱,则自动出箱
  646. if waybillModel.Status == model.WaybillStatusVanning {
  647. // 添加出箱物流记录
  648. Logistics := model.WaybillLogistics{
  649. WaybillNo: waybillNo,
  650. Status: model.WaybillStatusVanningOut,
  651. CoolerBoxId: waybillModel.CoolerBoxId,
  652. UserId: waybillModel.PrintUserId,
  653. Lng: lng,
  654. Lat: lat,
  655. ControlBy: model2.ControlBy{
  656. CreateBy: p.UserId,
  657. },
  658. DeptBy: model2.DeptBy{
  659. DeptId: p.DeptId,
  660. },
  661. ModelTime: model2.ModelTime{
  662. CreatedAt: c.StartTime,
  663. },
  664. }
  665. err = tx.Create(&Logistics).Error
  666. if err != nil {
  667. e.Log.Errorf("db error: %s", err)
  668. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  669. }
  670. // 查询任务
  671. var task model.WaybillTask
  672. err = tx.Model(&task).Where("waybill_no = ? and cooler_box_id = ?", waybillNo, waybillModel.CoolerBoxId).
  673. Last(&task).Error
  674. if err != nil {
  675. e.Log.Errorf("db error: %s", err)
  676. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  677. }
  678. task.EndTime = c.StartTime
  679. task.UpdateBy = p.UserId
  680. err = tx.Save(&task).Error
  681. if err != nil {
  682. e.Log.Errorf("db error: %s", err)
  683. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  684. }
  685. }
  686. if waybillModel.Status == model.WaybillStatusWaitStorage {
  687. waybillModel.DeliveryTime = c.StartTime
  688. }
  689. waybillModel.Status = model.WaybillStatusStorage
  690. waybillModel.WarehouseId = warehouse.Id
  691. err = tx.Save(&waybillModel).Error
  692. if err != nil {
  693. e.Log.Errorf("db error: %s", err)
  694. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  695. }
  696. // 添加物流
  697. Logistics := model.WaybillLogistics{
  698. WaybillNo: waybillNo,
  699. Status: model.WaybillStatusStorage,
  700. WarehouseId: warehouse.Id,
  701. UserId: p.UserId,
  702. Lng: lng,
  703. Lat: lat,
  704. ControlBy: model2.ControlBy{
  705. CreateBy: p.UserId,
  706. },
  707. DeptBy: model2.DeptBy{
  708. DeptId: p.DeptId,
  709. },
  710. ModelTime: model2.ModelTime{
  711. CreatedAt: c.StartTime,
  712. },
  713. }
  714. err = tx.Create(&Logistics).Error
  715. if err != nil {
  716. e.Log.Errorf("db error: %s", err)
  717. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  718. }
  719. // 添加任务
  720. task := model.WaybillTask{
  721. WaybillNo: waybillNo,
  722. WarehouseId: warehouse.Id,
  723. UserId: p.UserId,
  724. Sn: warehouse.Sn,
  725. StartTime: c.StartTime,
  726. ControlBy: model2.ControlBy{
  727. CreateBy: p.UserId,
  728. },
  729. DeptBy: model2.DeptBy{
  730. DeptId: p.DeptId,
  731. },
  732. }
  733. err = tx.Create(&task).Error
  734. if err != nil {
  735. e.Log.Errorf("db error: %s", err)
  736. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  737. }
  738. }
  739. return nil
  740. }
  741. func (e *Waybill) WarehouseOut(c *dto.WaybillInOutReq, p *actions.DataPermission) error {
  742. var err error
  743. tx := e.Orm.Begin()
  744. defer func() {
  745. if err != nil {
  746. tx.Rollback()
  747. } else {
  748. tx.Commit()
  749. }
  750. }()
  751. // 查询仓库信息
  752. var warehouse = model.Warehouse{}
  753. // 查询运单是否存在
  754. err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
  755. Where("user_id = ?", p.UserId).
  756. First(&warehouse).Error
  757. if err != nil {
  758. e.Log.Errorf("db error: %s", err)
  759. return errors.New("获取仓库信息失败")
  760. }
  761. for _, waybillNo := range c.WaybillNoList {
  762. var waybillModel = model.Waybill{}
  763. // 查询运单是否存在
  764. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  765. Where("waybill_no = ?", waybillNo).
  766. First(&waybillModel).Error
  767. if err != nil {
  768. e.Log.Errorf("db error: %s", err)
  769. if errors.Is(err, gorm.ErrRecordNotFound) {
  770. return errors.New(fmt.Sprintf("运单号%s不存在", waybillNo))
  771. }
  772. return errors.New(fmt.Sprintf("运单号%s查询失败", waybillNo))
  773. }
  774. if waybillModel.WarehouseId != warehouse.Id {
  775. return errors.New("请选择正确仓库出库!")
  776. }
  777. if waybillModel.WarehouseId == warehouse.Id && waybillModel.Status == model.WaybillStatusStorageOut {
  778. continue
  779. }
  780. if waybillModel.Status != model.WaybillStatusStorage {
  781. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法出库!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  782. return err
  783. }
  784. waybillModel.Status = model.WaybillStatusStorageOut
  785. waybillModel.WarehouseId = warehouse.Id
  786. err = tx.Save(&waybillModel).Error
  787. if err != nil {
  788. e.Log.Errorf("db error: %s", err)
  789. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  790. }
  791. var lng, lat string
  792. lng, lat, err = e.GetSite(p.DeptId, warehouse.Sn, c.StartTime.String())
  793. if err != nil {
  794. e.Log.Errorf("获取定位信息失败: %s", err)
  795. return err
  796. }
  797. // 添加物流
  798. Logistics := model.WaybillLogistics{
  799. WaybillNo: waybillNo,
  800. Status: model.WaybillStatusStorageOut,
  801. WarehouseId: warehouse.Id,
  802. UserId: p.UserId,
  803. Lng: lng,
  804. Lat: lat,
  805. ControlBy: model2.ControlBy{
  806. CreateBy: p.UserId,
  807. },
  808. DeptBy: model2.DeptBy{
  809. DeptId: p.DeptId,
  810. },
  811. ModelTime: model2.ModelTime{
  812. CreatedAt: c.StartTime,
  813. },
  814. }
  815. err = tx.Create(&Logistics).Error
  816. if err != nil {
  817. e.Log.Errorf("db error: %s", err)
  818. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  819. }
  820. // 查询任务
  821. var task model.WaybillTask
  822. err = tx.Model(&task).Where("waybill_no = ? and warehouse_id = ?", waybillNo, warehouse.Id).
  823. First(&task).Error
  824. if err != nil {
  825. e.Log.Errorf("db error: %s", err)
  826. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  827. }
  828. task.EndTime = c.StartTime
  829. task.UpdateBy = p.UserId
  830. err = tx.Save(&task).Error
  831. if err != nil {
  832. e.Log.Errorf("db error: %s", err)
  833. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  834. }
  835. }
  836. return nil
  837. }
  838. func (e *Waybill) CarIn(c *dto.WaybillInOutReq, p *actions.DataPermission) error {
  839. var err error
  840. tx := e.Orm.Begin()
  841. defer func() {
  842. if err != nil {
  843. tx.Rollback()
  844. } else {
  845. tx.Commit()
  846. }
  847. }()
  848. // 查询车辆信息
  849. var car = model.Car{}
  850. // 查询运单是否存在
  851. err = tx.Scopes(actions.Permission(car.TableName(), p)).
  852. Where("user_id = ?", p.UserId).
  853. First(&car).Error
  854. if err != nil {
  855. e.Log.Errorf("db error: %s", err)
  856. return errors.New("获取车辆绑定信息失败")
  857. }
  858. for _, waybillNo := range c.WaybillNoList {
  859. var waybillModel = model.Waybill{}
  860. // 查询运单是否存在
  861. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  862. Where("waybill_no = ?", waybillNo).
  863. First(&waybillModel).Error
  864. if err != nil {
  865. e.Log.Errorf("db error: %s", err)
  866. if errors.Is(err, gorm.ErrRecordNotFound) {
  867. return errors.New(fmt.Sprintf("运单号%s不存在", waybillNo))
  868. }
  869. return errors.New(fmt.Sprintf("运单号%s查询失败", waybillNo))
  870. }
  871. if waybillModel.CarId == car.Id && waybillModel.Status == model.WaybillStatusTruck {
  872. continue
  873. }
  874. if waybillModel.CarId != car.Id && waybillModel.Status == model.WaybillStatusTruck {
  875. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法装车!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  876. return err
  877. }
  878. if waybillModel.Status == model.WaybillStatusWaitStorage || waybillModel.Status == model.WaybillStatusWaitVanning {
  879. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法装车!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  880. return err
  881. }
  882. // 验证时间
  883. var logistics model.WaybillLogistics
  884. err = e.Orm.Where("waybill_no = ?", waybillNo).Last(&logistics).Error
  885. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  886. e.Log.Errorf("获取运单日志失败: %s", err)
  887. return err
  888. }
  889. if time.Time(logistics.CreatedAt).After(time.Time(c.StartTime)) {
  890. err = errors.New(fmt.Sprintf("运单号%s装车时间[%s]与上一阶段时间[%s]冲突,无法装车!", waybillNo, c.StartTime.String(), logistics.CreatedAt.String()))
  891. return err
  892. }
  893. if waybillModel.Status == model.WaybillStatusWaitTruck {
  894. waybillModel.DeliveryTime = c.StartTime
  895. }
  896. var lng, lat string
  897. lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
  898. if err != nil {
  899. e.Log.Errorf("获取定位信息失败: %s", err)
  900. return err
  901. }
  902. // 如果上一个状态为入库,则自动出库
  903. if waybillModel.Status == model.WaybillStatusStorage {
  904. var warehouse model.Warehouse
  905. err = e.Orm.First(&warehouse, waybillModel.WarehouseId).Error
  906. if err != nil {
  907. e.Log.Errorf("db error: %s", err)
  908. return errors.New(fmt.Sprintf("查询仓库信息失败:%s", err))
  909. }
  910. // 添加物流
  911. Logistics := model.WaybillLogistics{
  912. WaybillNo: waybillNo,
  913. Status: model.WaybillStatusStorageOut,
  914. WarehouseId: waybillModel.WarehouseId,
  915. UserId: warehouse.UserId,
  916. Lng: lng,
  917. Lat: lat,
  918. ControlBy: model2.ControlBy{
  919. CreateBy: p.UserId,
  920. },
  921. DeptBy: model2.DeptBy{
  922. DeptId: warehouse.DeptId,
  923. },
  924. ModelTime: model2.ModelTime{
  925. CreatedAt: c.StartTime,
  926. },
  927. }
  928. err = tx.Create(&Logistics).Error
  929. if err != nil {
  930. e.Log.Errorf("db error: %s", err)
  931. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  932. }
  933. // 查询任务
  934. var task model.WaybillTask
  935. err = tx.Model(&task).Where("waybill_no = ? and warehouse_id = ?", waybillNo, waybillModel.WarehouseId).
  936. Last(&task).Error
  937. if err != nil {
  938. e.Log.Errorf("db error: %s", err)
  939. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  940. }
  941. task.EndTime = c.StartTime
  942. task.UpdateBy = p.UserId
  943. err = tx.Save(&task).Error
  944. if err != nil {
  945. e.Log.Errorf("db error: %s", err)
  946. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  947. }
  948. }
  949. // 如果上一个状态为装箱,则自动出箱
  950. if waybillModel.Status == model.WaybillStatusVanning {
  951. // 添加出箱物流记录
  952. Logistics := model.WaybillLogistics{
  953. WaybillNo: waybillNo,
  954. Status: model.WaybillStatusVanningOut,
  955. CoolerBoxId: waybillModel.CoolerBoxId,
  956. UserId: waybillModel.PrintUserId,
  957. Lng: lng,
  958. Lat: lat,
  959. ControlBy: model2.ControlBy{
  960. CreateBy: p.UserId,
  961. },
  962. DeptBy: model2.DeptBy{
  963. DeptId: p.DeptId,
  964. },
  965. ModelTime: model2.ModelTime{
  966. CreatedAt: c.StartTime,
  967. },
  968. }
  969. err = tx.Create(&Logistics).Error
  970. if err != nil {
  971. e.Log.Errorf("db error: %s", err)
  972. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  973. }
  974. // 查询任务
  975. var task model.WaybillTask
  976. err = tx.Model(&task).Where("waybill_no = ? and cooler_box_id = ?", waybillNo, waybillModel.CoolerBoxId).
  977. Last(&task).Error
  978. if err != nil {
  979. e.Log.Errorf("db error: %s", err)
  980. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  981. }
  982. task.EndTime = c.StartTime
  983. task.UpdateBy = p.UserId
  984. err = tx.Save(&task).Error
  985. if err != nil {
  986. e.Log.Errorf("db error: %s", err)
  987. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  988. }
  989. }
  990. waybillModel.Status = model.WaybillStatusTruck
  991. waybillModel.CarId = car.Id
  992. err = tx.Save(&waybillModel).Error
  993. if err != nil {
  994. e.Log.Errorf("db error: %s", err)
  995. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  996. }
  997. // 添加物流
  998. Logistics := model.WaybillLogistics{
  999. WaybillNo: waybillNo,
  1000. Status: model.WaybillStatusTruck,
  1001. CarId: car.Id,
  1002. UserId: p.UserId,
  1003. Lng: lng,
  1004. Lat: lat,
  1005. ControlBy: model2.ControlBy{
  1006. CreateBy: p.UserId,
  1007. },
  1008. DeptBy: model2.DeptBy{
  1009. DeptId: p.DeptId,
  1010. },
  1011. ModelTime: model2.ModelTime{
  1012. CreatedAt: c.StartTime,
  1013. },
  1014. }
  1015. err = tx.Create(&Logistics).Error
  1016. if err != nil {
  1017. e.Log.Errorf("db error: %s", err)
  1018. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1019. }
  1020. // 添加任务
  1021. task := model.WaybillTask{
  1022. WaybillNo: waybillNo,
  1023. CarId: car.Id,
  1024. UserId: p.UserId,
  1025. Sn: car.Sn,
  1026. StartTime: c.StartTime,
  1027. ControlBy: model2.ControlBy{
  1028. CreateBy: p.UserId,
  1029. },
  1030. DeptBy: model2.DeptBy{
  1031. DeptId: p.DeptId,
  1032. },
  1033. }
  1034. err = tx.Create(&task).Error
  1035. if err != nil {
  1036. e.Log.Errorf("db error: %s", err)
  1037. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1038. }
  1039. }
  1040. return nil
  1041. }
  1042. func (e *Waybill) CarOut(c *dto.WaybillInOutReq, p *actions.DataPermission) error {
  1043. var err error
  1044. tx := e.Orm.Begin()
  1045. defer func() {
  1046. if err != nil {
  1047. tx.Rollback()
  1048. } else {
  1049. tx.Commit()
  1050. }
  1051. }()
  1052. // 查询仓库信息
  1053. var car = model.Car{}
  1054. // 查询运单是否存在
  1055. err = tx.Scopes(actions.Permission(car.TableName(), p)).
  1056. Where("user_id = ?", p.UserId).
  1057. First(&car).Error
  1058. if err != nil {
  1059. e.Log.Errorf("db error: %s", err)
  1060. return errors.New("获取车辆绑定信息失败")
  1061. }
  1062. for _, waybillNo := range c.WaybillNoList {
  1063. var waybillModel = model.Waybill{}
  1064. // 查询运单是否存在
  1065. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  1066. Where("waybill_no = ?", waybillNo).
  1067. First(&waybillModel).Error
  1068. if err != nil {
  1069. e.Log.Errorf("db error: %s", err)
  1070. if errors.Is(err, gorm.ErrRecordNotFound) {
  1071. return errors.New(fmt.Sprintf("运单号%s不存在", waybillNo))
  1072. }
  1073. return errors.New(fmt.Sprintf("运单号%s查询失败", waybillNo))
  1074. }
  1075. if waybillModel.CarId != car.Id {
  1076. return errors.New("请选择正确车辆下车!")
  1077. }
  1078. if waybillModel.CarId == car.Id && waybillModel.Status == model.WaybillStatusTruckOut {
  1079. continue
  1080. }
  1081. if waybillModel.Status != model.WaybillStatusTruck {
  1082. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法下车!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  1083. return err
  1084. }
  1085. waybillModel.Status = model.WaybillStatusTruckOut
  1086. waybillModel.CarId = car.Id
  1087. err = tx.Save(&waybillModel).Error
  1088. if err != nil {
  1089. e.Log.Errorf("db error: %s", err)
  1090. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1091. }
  1092. var lng, lat string
  1093. lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
  1094. if err != nil {
  1095. e.Log.Errorf("获取定位信息失败: %s", err)
  1096. return err
  1097. }
  1098. // 添加物流
  1099. Logistics := model.WaybillLogistics{
  1100. WaybillNo: waybillNo,
  1101. Status: model.WaybillStatusTruckOut,
  1102. CarId: car.Id,
  1103. UserId: p.UserId,
  1104. Lng: lng,
  1105. Lat: lat,
  1106. ControlBy: model2.ControlBy{
  1107. CreateBy: p.UserId,
  1108. },
  1109. DeptBy: model2.DeptBy{
  1110. DeptId: p.DeptId,
  1111. },
  1112. ModelTime: model2.ModelTime{
  1113. CreatedAt: c.StartTime,
  1114. },
  1115. }
  1116. err = tx.Create(&Logistics).Error
  1117. if err != nil {
  1118. e.Log.Errorf("db error: %s", err)
  1119. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1120. }
  1121. // 查询任务
  1122. var task model.WaybillTask
  1123. err = tx.Model(&task).Where("waybill_no = ? and car_id = ?", waybillNo, car.Id).
  1124. First(&task).Error
  1125. if err != nil {
  1126. e.Log.Errorf("db error: %s", err)
  1127. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1128. }
  1129. task.EndTime = c.StartTime
  1130. task.UpdateBy = p.UserId
  1131. err = tx.Save(&task).Error
  1132. if err != nil {
  1133. e.Log.Errorf("db error: %s", err)
  1134. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1135. }
  1136. }
  1137. return nil
  1138. }
  1139. func (e *Waybill) CoolerBoxIn(c *dto.WaybillCoolerBoxInReq, p *actions.DataPermission) error {
  1140. var err error
  1141. tx := e.Orm.Begin()
  1142. defer func() {
  1143. if err != nil {
  1144. tx.Rollback()
  1145. } else {
  1146. tx.Commit()
  1147. }
  1148. }()
  1149. for _, waybillNo := range c.WaybillNoList {
  1150. var waybillModel = model.Waybill{}
  1151. // 查询运单是否存在
  1152. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  1153. Where("waybill_no = ?", waybillNo).
  1154. First(&waybillModel).Error
  1155. if err != nil {
  1156. e.Log.Errorf("db error: %s", err)
  1157. if errors.Is(err, gorm.ErrRecordNotFound) {
  1158. return errors.New(fmt.Sprintf("运单号%s不存在", waybillNo))
  1159. }
  1160. return errors.New(fmt.Sprintf("运单号%s查询失败", waybillNo))
  1161. }
  1162. if waybillModel.Status != model.WaybillStatusWaitVanning &&
  1163. waybillModel.Status != model.WaybillStatusStorage &&
  1164. waybillModel.Status != model.WaybillStatusTruck {
  1165. if !(waybillModel.Status == model.WaybillStatusVanning && waybillModel.PrintUserId != p.UserId) {
  1166. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法装箱!", waybillNo, model.WaybillStatusMap[waybillModel.Status]))
  1167. return err
  1168. }
  1169. }
  1170. // 查询保温箱信息 有可能先装车再装箱 所以需要选保温箱
  1171. var coolerBox = model.CoolerBox{}
  1172. err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
  1173. Where("id = ?", c.CoolerBoxId).
  1174. First(&coolerBox).Error
  1175. if err != nil {
  1176. e.Log.Errorf("db error: %s", err)
  1177. return errors.New("获取保温箱信息失败")
  1178. }
  1179. if waybillModel.CoolerBoxId > 0 && waybillModel.CoolerBoxId != c.CoolerBoxId {
  1180. // 扫码装箱 派单的保温箱与当前保温箱不一致
  1181. err = tx.Model(&waybillModel).Where("waybill_no = ?", waybillNo).
  1182. Updates(map[string]interface{}{
  1183. "cooler_box_id": c.CoolerBoxId,
  1184. "print_user_id": p.UserId,
  1185. }).Error
  1186. if err != nil {
  1187. e.Log.Errorf("db error: %s", err)
  1188. return errors.New("修改运单关联保温箱失败")
  1189. }
  1190. }
  1191. // 验证时间
  1192. var logistics model.WaybillLogistics
  1193. err = e.Orm.Where("waybill_no = ?", waybillNo).Last(&logistics).Error
  1194. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  1195. e.Log.Errorf("获取运单日志失败: %s", err)
  1196. return err
  1197. }
  1198. var lng, lat string
  1199. lng, lat, err = e.GetSite(p.DeptId, coolerBox.Sn, c.StartTime.String())
  1200. if err != nil {
  1201. e.Log.Errorf("获取定位信息失败: %s", err)
  1202. return err
  1203. }
  1204. // 如果上一个状态为已装车,则自动下车
  1205. if waybillModel.Status == model.WaybillStatusTruck {
  1206. // 已装车
  1207. var car model.Car
  1208. err = e.Orm.First(&car, waybillModel.CarId).Error
  1209. if err != nil {
  1210. e.Log.Errorf("db error: %s", err)
  1211. return errors.New(fmt.Sprintf("查询仓库信息失败:%s", err))
  1212. }
  1213. // 添加物流
  1214. Logistics := model.WaybillLogistics{
  1215. WaybillNo: waybillNo,
  1216. Status: model.WaybillStatusTruckOut,
  1217. CarId: waybillModel.CarId,
  1218. UserId: car.UserId,
  1219. Lng: lng,
  1220. Lat: lat,
  1221. ControlBy: model2.ControlBy{
  1222. CreateBy: p.UserId,
  1223. },
  1224. DeptBy: model2.DeptBy{
  1225. DeptId: car.DeptId,
  1226. },
  1227. ModelTime: model2.ModelTime{
  1228. CreatedAt: c.StartTime,
  1229. },
  1230. }
  1231. err = tx.Create(&Logistics).Error
  1232. if err != nil {
  1233. e.Log.Errorf("db error: %s", err)
  1234. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1235. }
  1236. // 查询任务
  1237. var task model.WaybillTask
  1238. err = tx.Model(&task).Where("waybill_no = ? and car_id = ?", waybillNo, waybillModel.CarId).
  1239. Last(&task).Error
  1240. if err != nil {
  1241. e.Log.Errorf("db error: %s", err)
  1242. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1243. }
  1244. task.EndTime = c.StartTime
  1245. task.UpdateBy = p.UserId
  1246. err = tx.Save(&task).Error
  1247. if err != nil {
  1248. e.Log.Errorf("db error: %s", err)
  1249. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1250. }
  1251. }
  1252. // 如果上一个状态为入库,则自动出库
  1253. if waybillModel.Status == model.WaybillStatusStorage {
  1254. var warehouse model.Warehouse
  1255. err = e.Orm.First(&warehouse, waybillModel.WarehouseId).Error
  1256. if err != nil {
  1257. e.Log.Errorf("db error: %s", err)
  1258. return errors.New(fmt.Sprintf("查询仓库信息失败:%s", err))
  1259. }
  1260. // 添加物流
  1261. Logistics := model.WaybillLogistics{
  1262. WaybillNo: waybillNo,
  1263. Status: model.WaybillStatusStorageOut,
  1264. WarehouseId: waybillModel.WarehouseId,
  1265. UserId: warehouse.UserId,
  1266. Lng: lng,
  1267. Lat: lat,
  1268. ControlBy: model2.ControlBy{
  1269. CreateBy: p.UserId,
  1270. },
  1271. DeptBy: model2.DeptBy{
  1272. DeptId: warehouse.DeptId,
  1273. },
  1274. ModelTime: model2.ModelTime{
  1275. CreatedAt: c.StartTime,
  1276. },
  1277. }
  1278. err = tx.Create(&Logistics).Error
  1279. if err != nil {
  1280. e.Log.Errorf("db error: %s", err)
  1281. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1282. }
  1283. // 查询任务
  1284. var task model.WaybillTask
  1285. err = tx.Model(&task).Where("waybill_no = ? and warehouse_id = ?", waybillNo, waybillModel.WarehouseId).
  1286. Last(&task).Error
  1287. if err != nil {
  1288. e.Log.Errorf("db error: %s", err)
  1289. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1290. }
  1291. task.EndTime = c.StartTime
  1292. task.UpdateBy = p.UserId
  1293. err = tx.Save(&task).Error
  1294. if err != nil {
  1295. e.Log.Errorf("db error: %s", err)
  1296. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1297. }
  1298. }
  1299. // 如果上一个状态为装箱,则自动出箱
  1300. if waybillModel.Status == model.WaybillStatusVanning {
  1301. // 添加出箱物流记录
  1302. Logistics := model.WaybillLogistics{
  1303. WaybillNo: waybillNo,
  1304. Status: model.WaybillStatusVanningOut,
  1305. CoolerBoxId: waybillModel.CoolerBoxId,
  1306. UserId: waybillModel.PrintUserId,
  1307. Lng: lng,
  1308. Lat: lat,
  1309. ControlBy: model2.ControlBy{
  1310. CreateBy: p.UserId,
  1311. },
  1312. DeptBy: model2.DeptBy{
  1313. DeptId: p.DeptId,
  1314. },
  1315. ModelTime: model2.ModelTime{
  1316. CreatedAt: c.StartTime,
  1317. },
  1318. }
  1319. err = tx.Create(&Logistics).Error
  1320. if err != nil {
  1321. e.Log.Errorf("db error: %s", err)
  1322. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1323. }
  1324. // 查询任务
  1325. var task model.WaybillTask
  1326. err = tx.Model(&task).Where("waybill_no = ? and cooler_box_id = ?", waybillNo, waybillModel.CoolerBoxId).
  1327. Last(&task).Error
  1328. if err != nil {
  1329. e.Log.Errorf("db error: %s", err)
  1330. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1331. }
  1332. task.EndTime = c.StartTime
  1333. task.UpdateBy = p.UserId
  1334. err = tx.Save(&task).Error
  1335. if err != nil {
  1336. e.Log.Errorf("db error: %s", err)
  1337. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1338. }
  1339. }
  1340. waybillModel.Status = model.WaybillStatusVanning
  1341. waybillModel.CoolerBoxId = coolerBox.Id
  1342. waybillModel.DeliveryTime = c.StartTime
  1343. err = tx.Save(&waybillModel).Error
  1344. if err != nil {
  1345. e.Log.Errorf("db error: %s", err)
  1346. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1347. }
  1348. // 添加物流
  1349. Logistics := model.WaybillLogistics{
  1350. WaybillNo: waybillNo,
  1351. Status: model.WaybillStatusVanning,
  1352. CoolerBoxId: coolerBox.Id,
  1353. UserId: p.UserId,
  1354. Lng: lng,
  1355. Lat: lat,
  1356. ControlBy: model2.ControlBy{
  1357. CreateBy: p.UserId,
  1358. },
  1359. DeptBy: model2.DeptBy{
  1360. DeptId: p.DeptId,
  1361. },
  1362. ModelTime: model2.ModelTime{
  1363. CreatedAt: c.StartTime,
  1364. },
  1365. }
  1366. err = tx.Create(&Logistics).Error
  1367. if err != nil {
  1368. e.Log.Errorf("db error: %s", err)
  1369. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1370. }
  1371. // 添加任务
  1372. task := model.WaybillTask{
  1373. WaybillNo: waybillNo,
  1374. CoolerBoxId: coolerBox.Id,
  1375. UserId: p.UserId,
  1376. Sn: coolerBox.Sn,
  1377. StartTime: c.StartTime,
  1378. ControlBy: model2.ControlBy{
  1379. CreateBy: p.UserId,
  1380. },
  1381. DeptBy: model2.DeptBy{
  1382. DeptId: p.DeptId,
  1383. },
  1384. }
  1385. err = tx.Create(&task).Error
  1386. if err != nil {
  1387. e.Log.Errorf("db error: %s", err)
  1388. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1389. }
  1390. }
  1391. return nil
  1392. }
  1393. func (e *Waybill) Receipt(c *dto.WaybillReceiptReq, p *actions.DataPermission) error {
  1394. var err error
  1395. tx := e.Orm.Begin()
  1396. defer func() {
  1397. if err != nil {
  1398. tx.Rollback()
  1399. } else {
  1400. tx.Commit()
  1401. }
  1402. }()
  1403. var waybillModel = model.Waybill{}
  1404. // 查询运单是否存在
  1405. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  1406. Where("waybill_no = ?", c.WaybillNo).
  1407. First(&waybillModel).Error
  1408. if err != nil {
  1409. e.Log.Errorf("db error: %s", err)
  1410. if errors.Is(err, gorm.ErrRecordNotFound) {
  1411. return errors.New(fmt.Sprintf("运单号%s不存在", c.WaybillNo))
  1412. }
  1413. return errors.New(fmt.Sprintf("运单号%s查询失败", c.WaybillNo))
  1414. }
  1415. if waybillModel.Status == model.WaybillStatusReceipt {
  1416. return nil
  1417. }
  1418. if waybillModel.Status == model.WaybillStatusWaitDelivery ||
  1419. waybillModel.Status == model.WaybillStatusWaitTruck ||
  1420. waybillModel.Status == model.WaybillStatusWaitStorage ||
  1421. waybillModel.Status == model.WaybillStatusWaitVanning {
  1422. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法签收!", c.WaybillNo, model.WaybillStatusMap[waybillModel.Status]))
  1423. return err
  1424. }
  1425. // 保温箱配送 不需要中转
  1426. if waybillModel.Status == model.WaybillStatusVanning || waybillModel.Status == model.WaybillStatusVanningOut {
  1427. // 查询保温箱信息
  1428. var coolerBox = model.CoolerBox{}
  1429. // 查询保温箱是否存在
  1430. err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
  1431. Where("id = ?", waybillModel.CoolerBoxId).
  1432. First(&coolerBox).Error
  1433. if err != nil {
  1434. e.Log.Errorf("db error: %s", err)
  1435. return errors.New("获取保温箱信息失败")
  1436. }
  1437. waybillModel.Status = model.WaybillStatusReceipt
  1438. waybillModel.CoolerBoxId = coolerBox.Id
  1439. waybillModel.ReceiptTime = c.StartTime
  1440. waybillModel.ReceiptImg = c.ReceiptImg
  1441. err = tx.Save(&waybillModel).Error
  1442. if err != nil {
  1443. e.Log.Errorf("db error: %s", err)
  1444. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1445. }
  1446. var lng, lat string
  1447. lng, lat, err = e.GetSite(p.DeptId, coolerBox.Sn, c.StartTime.String())
  1448. if err != nil {
  1449. e.Log.Errorf("获取定位信息失败: %s", err)
  1450. return err
  1451. }
  1452. // 查询任务
  1453. var task model.WaybillTask
  1454. err = tx.Model(&task).Where("waybill_no = ? and cooler_box_id = ?", c.WaybillNo, coolerBox.Id).
  1455. Last(&task).Error
  1456. if err != nil {
  1457. e.Log.Errorf("db error: %s", err)
  1458. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1459. }
  1460. // 未出箱 直接点签收
  1461. if time.Time(task.EndTime).IsZero() {
  1462. task.EndTime = c.StartTime
  1463. task.UpdateBy = p.UserId
  1464. err = tx.Save(&task).Error
  1465. if err != nil {
  1466. e.Log.Errorf("db error: %s", err)
  1467. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1468. }
  1469. // 添加下车物流记录
  1470. Logistics := model.WaybillLogistics{
  1471. WaybillNo: c.WaybillNo,
  1472. Status: model.WaybillStatusVanningOut,
  1473. CoolerBoxId: coolerBox.Id,
  1474. UserId: p.UserId,
  1475. Lng: lng,
  1476. Lat: lat,
  1477. ControlBy: model2.ControlBy{
  1478. CreateBy: p.UserId,
  1479. },
  1480. DeptBy: model2.DeptBy{
  1481. DeptId: p.DeptId,
  1482. },
  1483. ModelTime: model2.ModelTime{
  1484. CreatedAt: c.StartTime,
  1485. },
  1486. }
  1487. err = tx.Create(&Logistics).Error
  1488. if err != nil {
  1489. e.Log.Errorf("db error: %s", err)
  1490. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1491. }
  1492. }
  1493. // 添加签收记录
  1494. Logistics := model.WaybillLogistics{
  1495. WaybillNo: c.WaybillNo,
  1496. Status: model.WaybillStatusReceipt,
  1497. CoolerBoxId: coolerBox.Id,
  1498. UserId: waybillModel.PrintUserId,
  1499. Lng: lng,
  1500. Lat: lat,
  1501. ControlBy: model2.ControlBy{
  1502. CreateBy: p.UserId,
  1503. },
  1504. DeptBy: model2.DeptBy{
  1505. DeptId: p.DeptId,
  1506. },
  1507. ModelTime: model2.ModelTime{
  1508. CreatedAt: c.StartTime,
  1509. },
  1510. }
  1511. err = tx.Create(&Logistics).Error
  1512. if err != nil {
  1513. e.Log.Errorf("db error: %s", err)
  1514. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1515. }
  1516. return nil
  1517. }
  1518. if waybillModel.Status == model.WaybillStatusTruck || waybillModel.Status == model.WaybillStatusTruckOut {
  1519. // 查询车辆信息
  1520. var car = model.Car{}
  1521. // 查询运单是否存在
  1522. err = tx.Scopes(actions.Permission(car.TableName(), p)).
  1523. Where("user_id = ?", p.UserId).
  1524. First(&car).Error
  1525. if err != nil {
  1526. e.Log.Errorf("db error: %s", err)
  1527. return errors.New("获取车辆信息失败")
  1528. }
  1529. waybillModel.Status = model.WaybillStatusReceipt
  1530. waybillModel.CarId = car.Id
  1531. waybillModel.ReceiptTime = c.StartTime
  1532. waybillModel.ReceiptImg = c.ReceiptImg
  1533. err = tx.Save(&waybillModel).Error
  1534. if err != nil {
  1535. e.Log.Errorf("db error: %s", err)
  1536. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1537. }
  1538. var lng, lat string
  1539. lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
  1540. if err != nil {
  1541. e.Log.Errorf("获取定位信息失败: %s", err)
  1542. return err
  1543. }
  1544. // 查询任务
  1545. var task model.WaybillTask
  1546. err = tx.Model(&task).Where("waybill_no = ? and car_id = ?", c.WaybillNo, car.Id).
  1547. Last(&task).Error
  1548. if err != nil {
  1549. e.Log.Errorf("db error: %s", err)
  1550. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1551. }
  1552. // 未下车 直接点签收
  1553. if time.Time(task.EndTime).IsZero() {
  1554. task.EndTime = c.StartTime
  1555. task.UpdateBy = p.UserId
  1556. err = tx.Save(&task).Error
  1557. if err != nil {
  1558. e.Log.Errorf("db error: %s", err)
  1559. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1560. }
  1561. // 添加下车物流记录
  1562. Logistics := model.WaybillLogistics{
  1563. WaybillNo: c.WaybillNo,
  1564. Status: model.WaybillStatusTruckOut,
  1565. CarId: car.Id,
  1566. UserId: p.UserId,
  1567. Lng: lng,
  1568. Lat: lat,
  1569. ControlBy: model2.ControlBy{
  1570. CreateBy: p.UserId,
  1571. },
  1572. DeptBy: model2.DeptBy{
  1573. DeptId: p.DeptId,
  1574. },
  1575. ModelTime: model2.ModelTime{
  1576. CreatedAt: c.StartTime,
  1577. },
  1578. }
  1579. err = tx.Create(&Logistics).Error
  1580. if err != nil {
  1581. e.Log.Errorf("db error: %s", err)
  1582. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1583. }
  1584. }
  1585. // 添加签收记录
  1586. Logistics := model.WaybillLogistics{
  1587. WaybillNo: c.WaybillNo,
  1588. Status: model.WaybillStatusReceipt,
  1589. CarId: car.Id,
  1590. UserId: p.UserId,
  1591. Lng: lng,
  1592. Lat: lat,
  1593. ControlBy: model2.ControlBy{
  1594. CreateBy: p.UserId,
  1595. },
  1596. DeptBy: model2.DeptBy{
  1597. DeptId: p.DeptId,
  1598. },
  1599. ModelTime: model2.ModelTime{
  1600. CreatedAt: c.StartTime,
  1601. },
  1602. }
  1603. err = tx.Create(&Logistics).Error
  1604. if err != nil {
  1605. e.Log.Errorf("db error: %s", err)
  1606. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1607. }
  1608. }
  1609. if waybillModel.Status == model.WaybillStatusStorage || waybillModel.Status == model.WaybillStatusStorageOut {
  1610. // 查询车辆信息
  1611. var warehouse model.Warehouse
  1612. // 查询运单是否存在
  1613. err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
  1614. Where("user_id = ?", p.UserId).
  1615. First(&warehouse).Error
  1616. if err != nil {
  1617. e.Log.Errorf("db error: %s", err)
  1618. return errors.New("获取仓库信息失败")
  1619. }
  1620. waybillModel.Status = model.WaybillStatusReceipt
  1621. waybillModel.WarehouseId = warehouse.Id
  1622. waybillModel.ReceiptTime = c.StartTime
  1623. waybillModel.ReceiptImg = c.ReceiptImg
  1624. err = tx.Save(&waybillModel).Error
  1625. if err != nil {
  1626. e.Log.Errorf("db error: %s", err)
  1627. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1628. }
  1629. var lng, lat string
  1630. lng, lat, err = e.GetSite(p.DeptId, warehouse.Sn, c.StartTime.String())
  1631. if err != nil {
  1632. e.Log.Errorf("获取定位信息失败: %s", err)
  1633. return err
  1634. }
  1635. // 查询任务
  1636. var task model.WaybillTask
  1637. err = tx.Model(&task).Where("waybill_no = ? and warehouse_id = ?", c.WaybillNo, warehouse.Id).
  1638. Last(&task).Error
  1639. if err != nil {
  1640. e.Log.Errorf("db error: %s", err)
  1641. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1642. }
  1643. // 未出库 直接点签收
  1644. if time.Time(task.EndTime).IsZero() {
  1645. task.EndTime = c.StartTime
  1646. task.UpdateBy = p.UserId
  1647. err = tx.Save(&task).Error
  1648. if err != nil {
  1649. e.Log.Errorf("db error: %s", err)
  1650. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1651. }
  1652. // 添加下车物流记录
  1653. Logistics := model.WaybillLogistics{
  1654. WaybillNo: c.WaybillNo,
  1655. Status: model.WaybillStatusTruckOut,
  1656. WarehouseId: warehouse.Id,
  1657. UserId: p.UserId,
  1658. Lng: lng,
  1659. Lat: lat,
  1660. ControlBy: model2.ControlBy{
  1661. CreateBy: p.UserId,
  1662. },
  1663. DeptBy: model2.DeptBy{
  1664. DeptId: p.DeptId,
  1665. },
  1666. ModelTime: model2.ModelTime{
  1667. CreatedAt: c.StartTime,
  1668. },
  1669. }
  1670. err = tx.Create(&Logistics).Error
  1671. if err != nil {
  1672. e.Log.Errorf("db error: %s", err)
  1673. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1674. }
  1675. }
  1676. // 添加签收记录
  1677. Logistics := model.WaybillLogistics{
  1678. WaybillNo: c.WaybillNo,
  1679. Status: model.WaybillStatusReceipt,
  1680. WarehouseId: warehouse.Id,
  1681. UserId: p.UserId,
  1682. Lng: lng,
  1683. Lat: lat,
  1684. ControlBy: model2.ControlBy{
  1685. CreateBy: p.UserId,
  1686. },
  1687. DeptBy: model2.DeptBy{
  1688. DeptId: p.DeptId,
  1689. },
  1690. ModelTime: model2.ModelTime{
  1691. CreatedAt: c.StartTime,
  1692. },
  1693. }
  1694. err = tx.Create(&Logistics).Error
  1695. if err != nil {
  1696. e.Log.Errorf("db error: %s", err)
  1697. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1698. }
  1699. }
  1700. return nil
  1701. }
  1702. // 停止记录
  1703. func (e *Waybill) StopRecord(c *dto.WaybillStopRecordReq, p *actions.DataPermission) error {
  1704. var err error
  1705. tx := e.Orm.Begin()
  1706. defer func() {
  1707. if err != nil {
  1708. tx.Rollback()
  1709. } else {
  1710. tx.Commit()
  1711. }
  1712. }()
  1713. var waybillModel = model.Waybill{}
  1714. // 查询运单是否存在
  1715. err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
  1716. Where("waybill_no = ?", c.WaybillNo).
  1717. First(&waybillModel).Error
  1718. if err != nil {
  1719. e.Log.Errorf("db error: %s", err)
  1720. if errors.Is(err, gorm.ErrRecordNotFound) {
  1721. return errors.New(fmt.Sprintf("运单号%s不存在", c.WaybillNo))
  1722. }
  1723. return errors.New(fmt.Sprintf("运单号%s查询失败", c.WaybillNo))
  1724. }
  1725. if waybillModel.Status == model.WaybillStatusTruckOut ||
  1726. waybillModel.Status == model.WaybillStatusStorageOut ||
  1727. waybillModel.Status == model.WaybillStatusVanningOut {
  1728. return nil
  1729. }
  1730. if waybillModel.Status == model.WaybillStatusWaitDelivery ||
  1731. waybillModel.Status == model.WaybillStatusWaitTruck ||
  1732. waybillModel.Status == model.WaybillStatusWaitStorage ||
  1733. waybillModel.Status == model.WaybillStatusWaitVanning {
  1734. err = errors.New(fmt.Sprintf("运单号%s状态为%s,无法停止记录!", c.WaybillNo, model.WaybillStatusMap[waybillModel.Status]))
  1735. return err
  1736. }
  1737. if waybillModel.Status == model.WaybillStatusVanning {
  1738. // 查询保温箱信息
  1739. var coolerBox = model.CoolerBox{}
  1740. // 查询保温箱是否存在
  1741. err = tx.Scopes(actions.Permission(coolerBox.TableName(), p)).
  1742. Where("id = ?", waybillModel.CoolerBoxId).
  1743. First(&coolerBox).Error
  1744. if err != nil {
  1745. e.Log.Errorf("db error: %s", err)
  1746. return errors.New("获取保温箱信息失败")
  1747. }
  1748. waybillModel.Status = model.WaybillStatusVanningOut
  1749. err = tx.Save(&waybillModel).Error
  1750. if err != nil {
  1751. e.Log.Errorf("db error: %s", err)
  1752. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1753. }
  1754. var lng, lat string
  1755. lng, lat, err = e.GetSite(p.DeptId, coolerBox.Sn, c.StartTime.String())
  1756. if err != nil {
  1757. e.Log.Errorf("获取定位信息失败: %s", err)
  1758. return err
  1759. }
  1760. // 查询任务
  1761. var task model.WaybillTask
  1762. err = tx.Model(&task).Where("waybill_no = ? and cooler_box_id = ?", c.WaybillNo, coolerBox.Id).
  1763. Last(&task).Error
  1764. if err != nil {
  1765. e.Log.Errorf("db error: %s", err)
  1766. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1767. }
  1768. if task.StartTime.Local().After(c.StartTime.Local()) {
  1769. return errors.New(fmt.Sprintf("停止时间不能早于任务开始时间%s", task.StartTime.String()))
  1770. }
  1771. // 未出箱 停止记录
  1772. if time.Time(task.EndTime).IsZero() {
  1773. task.EndTime = c.StartTime
  1774. task.UpdateBy = p.UserId
  1775. err = tx.Save(&task).Error
  1776. if err != nil {
  1777. e.Log.Errorf("db error: %s", err)
  1778. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1779. }
  1780. // 添加下车物流记录
  1781. Logistics := model.WaybillLogistics{
  1782. WaybillNo: c.WaybillNo,
  1783. Status: model.WaybillStatusVanningOut,
  1784. CoolerBoxId: coolerBox.Id,
  1785. UserId: p.UserId,
  1786. Lng: lng,
  1787. Lat: lat,
  1788. ControlBy: model2.ControlBy{
  1789. CreateBy: p.UserId,
  1790. },
  1791. DeptBy: model2.DeptBy{
  1792. DeptId: p.DeptId,
  1793. },
  1794. ModelTime: model2.ModelTime{
  1795. CreatedAt: c.StartTime,
  1796. },
  1797. }
  1798. err = tx.Create(&Logistics).Error
  1799. if err != nil {
  1800. e.Log.Errorf("db error: %s", err)
  1801. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1802. }
  1803. }
  1804. return nil
  1805. }
  1806. if waybillModel.Status == model.WaybillStatusTruck {
  1807. // 查询车辆信息
  1808. var car = model.Car{}
  1809. // 查询运单是否存在
  1810. err = tx.Scopes(actions.Permission(car.TableName(), p)).
  1811. Where("user_id = ?", p.UserId).
  1812. First(&car).Error
  1813. if err != nil {
  1814. e.Log.Errorf("db error: %s", err)
  1815. return errors.New("获取车辆信息失败")
  1816. }
  1817. waybillModel.Status = model.WaybillStatusTruckOut
  1818. err = tx.Save(&waybillModel).Error
  1819. if err != nil {
  1820. e.Log.Errorf("db error: %s", err)
  1821. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1822. }
  1823. var lng, lat string
  1824. lng, lat, err = e.GetSite(p.DeptId, car.Sn, c.StartTime.String())
  1825. if err != nil {
  1826. e.Log.Errorf("获取定位信息失败: %s", err)
  1827. return err
  1828. }
  1829. // 查询任务
  1830. var task model.WaybillTask
  1831. err = tx.Model(&task).Where("waybill_no = ? and car_id = ?", c.WaybillNo, car.Id).
  1832. Last(&task).Error
  1833. if err != nil {
  1834. e.Log.Errorf("db error: %s", err)
  1835. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1836. }
  1837. if task.StartTime.Local().After(c.StartTime.Local()) {
  1838. return errors.New(fmt.Sprintf("停止时间不能早于任务开始时间%s", task.StartTime.String()))
  1839. }
  1840. // 未下车 停止记录
  1841. if time.Time(task.EndTime).IsZero() {
  1842. task.EndTime = c.StartTime
  1843. task.UpdateBy = p.UserId
  1844. err = tx.Save(&task).Error
  1845. if err != nil {
  1846. e.Log.Errorf("db error: %s", err)
  1847. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1848. }
  1849. // 添加下车物流记录
  1850. Logistics := model.WaybillLogistics{
  1851. WaybillNo: c.WaybillNo,
  1852. Status: model.WaybillStatusTruckOut,
  1853. CarId: car.Id,
  1854. UserId: p.UserId,
  1855. Lng: lng,
  1856. Lat: lat,
  1857. ControlBy: model2.ControlBy{
  1858. CreateBy: p.UserId,
  1859. },
  1860. DeptBy: model2.DeptBy{
  1861. DeptId: p.DeptId,
  1862. },
  1863. ModelTime: model2.ModelTime{
  1864. CreatedAt: c.StartTime,
  1865. },
  1866. }
  1867. err = tx.Create(&Logistics).Error
  1868. if err != nil {
  1869. e.Log.Errorf("db error: %s", err)
  1870. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1871. }
  1872. }
  1873. }
  1874. if waybillModel.Status == model.WaybillStatusStorage {
  1875. // 查询车辆信息
  1876. var warehouse model.Warehouse
  1877. // 查询运单是否存在
  1878. err = tx.Scopes(actions.Permission(warehouse.TableName(), p)).
  1879. Where("user_id = ?", p.UserId).
  1880. First(&warehouse).Error
  1881. if err != nil {
  1882. e.Log.Errorf("db error: %s", err)
  1883. return errors.New("获取仓库信息失败")
  1884. }
  1885. waybillModel.Status = model.WaybillStatusStorageOut
  1886. err = tx.Save(&waybillModel).Error
  1887. if err != nil {
  1888. e.Log.Errorf("db error: %s", err)
  1889. return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
  1890. }
  1891. var lng, lat string
  1892. lng, lat, err = e.GetSite(p.DeptId, warehouse.Sn, c.StartTime.String())
  1893. if err != nil {
  1894. e.Log.Errorf("获取定位信息失败: %s", err)
  1895. return err
  1896. }
  1897. // 查询任务
  1898. var task model.WaybillTask
  1899. err = tx.Model(&task).Where("waybill_no = ? and warehouse_id = ?", c.WaybillNo, warehouse.Id).
  1900. Last(&task).Error
  1901. if err != nil {
  1902. e.Log.Errorf("db error: %s", err)
  1903. return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
  1904. }
  1905. if task.StartTime.Local().After(c.StartTime.Local()) {
  1906. return errors.New(fmt.Sprintf("停止时间不能早于任务开始时间%s", task.StartTime.String()))
  1907. }
  1908. // 未出库 停止记录
  1909. if time.Time(task.EndTime).IsZero() {
  1910. task.EndTime = c.StartTime
  1911. task.UpdateBy = p.UserId
  1912. err = tx.Save(&task).Error
  1913. if err != nil {
  1914. e.Log.Errorf("db error: %s", err)
  1915. return errors.New(fmt.Sprintf("保存运单任务信息失败:%s", err))
  1916. }
  1917. // 添加下车物流记录
  1918. Logistics := model.WaybillLogistics{
  1919. WaybillNo: c.WaybillNo,
  1920. Status: model.WaybillStatusTruckOut,
  1921. WarehouseId: warehouse.Id,
  1922. UserId: p.UserId,
  1923. Lng: lng,
  1924. Lat: lat,
  1925. ControlBy: model2.ControlBy{
  1926. CreateBy: p.UserId,
  1927. },
  1928. DeptBy: model2.DeptBy{
  1929. DeptId: p.DeptId,
  1930. },
  1931. ModelTime: model2.ModelTime{
  1932. CreatedAt: c.StartTime,
  1933. },
  1934. }
  1935. err = tx.Create(&Logistics).Error
  1936. if err != nil {
  1937. e.Log.Errorf("db error: %s", err)
  1938. return errors.New(fmt.Sprintf("保存运单物流信息失败:%s", err))
  1939. }
  1940. }
  1941. }
  1942. return nil
  1943. }
  1944. // 获取入库 出库 上车 下车 定位信息
  1945. func (e *Waybill) GetSite(companyId int, sn string, time string) (lng, lat string, err error) {
  1946. // 获取公司秘钥
  1947. var company model.SysDept
  1948. company, err = model.GetCompanyById(companyId)
  1949. if err != nil {
  1950. e.Log.Errorf("db error: %s", err)
  1951. return lng, lat, model.GetCompanyKeyErr
  1952. }
  1953. // 获取传感器信息
  1954. var deviceSensorList = []nats_server.DeviceSensor_R{}
  1955. var count int64
  1956. deviceSensorList, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(sn, company.ColdKey)
  1957. if err != nil || count == 0 {
  1958. err = errors.New("查询设备定位信息失败")
  1959. return lng, lat, err
  1960. }
  1961. var deviceData nats_server.DeviceData_
  1962. deviceData, err = nats_server.Cold_ReadDeviceDataBy_T_snid_T_time(deviceSensorList[0].T_sn, deviceSensorList[0].T_id, time)
  1963. if err != nil {
  1964. err = errors.New("查询设备定位信息失败")
  1965. return lng, lat, err
  1966. }
  1967. if len(deviceData.T_site) > 0 {
  1968. site := strings.Split(deviceSensorList[0].T_DeviceSensorData.T_site, ",")
  1969. if len(site) == 2 {
  1970. lng = site[0]
  1971. lat = site[1]
  1972. }
  1973. }
  1974. return lng, lat, nil
  1975. }
  1976. // 获取今日运单数 未派单 未装车 未入库 运送中
  1977. // 本月运单总数 上月运单总数 本年运单总数 上年运单总数
  1978. func (e *Waybill) GetBasicsStats(c *dto.WaybillStatsReq, p *actions.DataPermission) dto.WaybillStatsRes {
  1979. var res dto.WaybillStatsRes
  1980. var data model.Waybill
  1981. type DateCount struct {
  1982. Date string
  1983. Count int64
  1984. }
  1985. yearCount := make([]DateCount, 0)
  1986. monthCount := make([]DateCount, 0)
  1987. now := time.Now()
  1988. todayStartTime := now.Format("2006-01-02") + " 00:00:00"
  1989. todayEndTime := now.Format("2006-01-02") + " 23:59:59"
  1990. // 获取上个月第一天
  1991. firstDayOfLastMonth := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
  1992. // 获取去年的第一天
  1993. firstDayOfLastYear := time.Date(now.Year()-1, time.January, 1, 0, 0, 0, 0, now.Location())
  1994. monthStartTime := firstDayOfLastMonth.Format("2006-01-02") + " 00:00:00"
  1995. yearStartTime := firstDayOfLastYear.Format("2006-01-02") + " 00:00:00"
  1996. // 今日总运单数
  1997. e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("order_time between ? and ?", todayStartTime, todayEndTime).Count(&res.TodayNum)
  1998. // 待派单
  1999. e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitDelivery).Count(&res.WaitDeliveryNum)
  2000. // 未装车
  2001. e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitTruck).Count(&res.WaitTruckNum)
  2002. // 未入库
  2003. e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitStorage).Count(&res.WaitStorageNum)
  2004. // 未装箱
  2005. e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status = ?", model.WaybillStatusWaitVanning).Count(&res.WaitVanningNum)
  2006. // 配送中
  2007. e.Orm.Model(&data).Scopes(actions.Permission(data.TableName(), p)).Where("status in (?)",
  2008. []int{model.WaybillStatusTruck, model.WaybillStatusStorage,
  2009. model.WaybillStatusTruckOut, model.WaybillStatusStorageOut,
  2010. model.WaybillStatusVanning, model.WaybillStatusVanningOut,
  2011. }).Count(&res.InDeliveryNum)
  2012. // 获取本月,上月数据
  2013. e.Orm.Model(&data).Select("date_format(order_time,'%Y%m') date,count(1) as count ").Scopes(actions.Permission(data.TableName(), p)).
  2014. Where("order_time between ? and ?", monthStartTime, now).Group("date").Find(&monthCount)
  2015. for _, month := range monthCount {
  2016. if month.Date == now.Format("200601") {
  2017. res.ThisMonthNum = month.Count
  2018. }
  2019. if month.Date == firstDayOfLastMonth.Format("200601") {
  2020. res.LastMonthNum = month.Count
  2021. }
  2022. }
  2023. // 获取本年,上年数据
  2024. e.Orm.Model(&data).Select("date_format(order_time,'%Y') date,count(1) as count ").Scopes(actions.Permission(data.TableName(), p)).
  2025. Where("order_time between ? and ?", yearStartTime, now).Group("date").Find(&yearCount)
  2026. for _, month := range yearCount {
  2027. if month.Date == now.Format("2006") {
  2028. res.ThisYearNum = month.Count
  2029. }
  2030. if month.Date == firstDayOfLastYear.Format("2006") {
  2031. res.LastYearNum = month.Count
  2032. }
  2033. }
  2034. if c.Type == "year" {
  2035. // 获取上个月第一天
  2036. year, _ := strconv.Atoi(c.Date)
  2037. firstDayOfyear := time.Date(year, 1, 1, 0, 0, 0, 0, now.Location())
  2038. lastDayOfyear := time.Date(year, 12, 31, 23, 59, 59, 0, now.Location())
  2039. // 年度数据统计
  2040. e.Orm.Model(&data).Select("date_format(order_time,'%Y-%m') date,count(1) as num ").Scopes(actions.Permission(data.TableName(), p)).
  2041. Where("order_time between ? and ?", firstDayOfyear, lastDayOfyear).Group("date").Find(&res.Stats)
  2042. }
  2043. if c.Type == "month" {
  2044. // 获取上个月第一天
  2045. month, _ := time.ParseInLocation("2006-01", c.Date, time.Local)
  2046. firstDayOfMonth := time.Date(month.Year(), month.Month(), 1, 0, 0, 0, 0, now.Location())
  2047. lastDayOfMonth := time.Date(month.Year(), month.Month()+1, 1, 23, 59, 59, 0, now.Location()).Add(-time.Hour * 24)
  2048. e.Orm.Model(&data).Select("date_format(order_time,'%Y-%m-%d') date,count(1) as num ").Scopes(actions.Permission(data.TableName(), p)).
  2049. Where("order_time between ? and ?", firstDayOfMonth, lastDayOfMonth).Group("date").Find(&res.Stats)
  2050. }
  2051. return res
  2052. }
  2053. // 获取运单所有温湿度数据
  2054. func (e *Waybill) GetAllData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.DeviceData_R, []WaybillPDF, error) {
  2055. var err error
  2056. var data model.WaybillTask
  2057. var waybill model.Waybill
  2058. var taskList []model.WaybillTask
  2059. var waybillPDF []WaybillPDF
  2060. dataList := make([]nats_server.DeviceData_R, 0)
  2061. err = e.Orm.Model(&waybill).Where("waybill_no = ?", c.WaybillNo).First(&waybill).Error
  2062. if err != nil {
  2063. e.Log.Errorf("db error: %s", err)
  2064. return dataList, waybillPDF, errors.New("获取运单信息失败")
  2065. }
  2066. // 未签收,不返回数据
  2067. if waybill.Status != model.WaybillStatusReceipt {
  2068. return dataList, waybillPDF, nil
  2069. }
  2070. // 获取公司秘钥
  2071. var company model.SysDept
  2072. company, err = model.GetCompanyById(waybill.DeptId)
  2073. if err != nil {
  2074. e.Log.Errorf("db error: %s", err)
  2075. return dataList, waybillPDF, model.GetCompanyKeyErr
  2076. }
  2077. err = e.Orm.Model(&data).
  2078. Where("waybill_no = ?", c.WaybillNo).
  2079. Order("id desc").
  2080. Find(&taskList).Error
  2081. if err != nil {
  2082. e.Log.Errorf("db error: %s", err)
  2083. return dataList, waybillPDF, global.GetFailedErr
  2084. }
  2085. // 获取最后一个任务id
  2086. var lastWaybillTask model.WaybillTask
  2087. err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", c.WaybillNo).Last(&lastWaybillTask).Error
  2088. if err != nil {
  2089. e.Log.Errorf("db error: %s", err)
  2090. err = errors.New("获取运单信息错误!")
  2091. return dataList, waybillPDF, err
  2092. }
  2093. // 创建名称到权重的映射
  2094. orderMap := make(map[string]int)
  2095. for i, v := range taskList {
  2096. orderMap[v.Sn] = i
  2097. }
  2098. for i := 0; i < len(taskList); i++ {
  2099. // 获取传感器信息
  2100. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(taskList[i].Sn, company.ColdKey)
  2101. var T_snid string
  2102. var list []nats_server.DeviceData_R
  2103. for _, r := range deviceSensorList {
  2104. T_snid += fmt.Sprintf("%s,%d|", r.T_sn, r.T_id)
  2105. }
  2106. var count int64
  2107. list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, taskList[i].StartTime.String(), taskList[i].EndTime.String(), 0, 9999)
  2108. if err != nil {
  2109. e.Log.Errorf("nats 获取温湿度信息失败: %s", err)
  2110. return dataList, waybillPDF, global.GetFailedErr
  2111. }
  2112. firstMap := map[int]nats_server.DeviceData_R{}
  2113. lastMap := map[int]nats_server.DeviceData_R{}
  2114. if count > 0 {
  2115. for _, v := range deviceSensorList {
  2116. for j := 0; j < len(list); j++ {
  2117. if v.T_id == list[j].T_id {
  2118. if list[j].T_time != taskList[i].StartTime.String() {
  2119. firstData := list[len(list)-1-j]
  2120. firstData.T_time = taskList[i].StartTime.String()
  2121. firstMap[v.T_id] = firstData
  2122. count += 1
  2123. }
  2124. break
  2125. }
  2126. }
  2127. if waybill.Status == model.WaybillStatusReceipt {
  2128. for k := len(list) - 1; k >= 0; k-- {
  2129. if v.T_id == list[k].T_id {
  2130. if taskList[i].Id == lastWaybillTask.Id && list[k].T_time != taskList[i].EndTime.String() && !time.Time(taskList[i].EndTime).IsZero() {
  2131. lastData := list[len(list)-1-k]
  2132. lastData.T_time = taskList[i].EndTime.String()
  2133. lastMap[v.T_id] = lastData
  2134. count += 1
  2135. }
  2136. break
  2137. }
  2138. }
  2139. }
  2140. }
  2141. }
  2142. for _, v := range firstMap {
  2143. list = append(list, v)
  2144. }
  2145. for _, v := range lastMap {
  2146. list = append(list, v)
  2147. }
  2148. sort.Slice(list, func(x, y int) bool {
  2149. if list[x].T_time == list[y].T_time {
  2150. // 如果时间相同,则按预设顺序排序
  2151. return orderMap[list[x].T_sn] < orderMap[list[y].T_sn]
  2152. }
  2153. return list[x].T_time > list[y].T_time
  2154. })
  2155. dataList = append(dataList, list...)
  2156. waybillPDF = append(waybillPDF, WaybillPDF{
  2157. Data: list,
  2158. DeviceSensorList: deviceSensorList,
  2159. Task: taskList[i],
  2160. })
  2161. }
  2162. sort.Slice(dataList, func(i, j int) bool {
  2163. if dataList[i].T_time == dataList[j].T_time {
  2164. // 如果时间相同,则按预设顺序排序
  2165. return orderMap[dataList[i].T_sn] < orderMap[dataList[j].T_sn]
  2166. }
  2167. return dataList[i].T_time < dataList[j].T_time
  2168. })
  2169. return dataList, waybillPDF, nil
  2170. }
  2171. // 获取前两个探头数据
  2172. func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillPdfReq) ([]nats_server.DeviceData_R, []DeviceDataPdf, []WaybillPDF, error) {
  2173. var err error
  2174. var data model.WaybillTask
  2175. var waybill model.Waybill
  2176. var taskList []model.WaybillTask
  2177. var deviceDataPdf []DeviceDataPdf
  2178. var waybillPDF []WaybillPDF
  2179. dataList := make([]nats_server.DeviceData_R, 0)
  2180. err = e.Orm.Model(&waybill).Where("waybill_no = ?", c.WaybillNo).First(&waybill).Error
  2181. if err != nil {
  2182. e.Log.Errorf("db error: %s", err)
  2183. return dataList, deviceDataPdf, waybillPDF, errors.New("获取运单信息失败")
  2184. }
  2185. // 未签收,不返回数据
  2186. if waybill.Status != model.WaybillStatusReceipt &&
  2187. waybill.Status != model.WaybillStatusTruckOut &&
  2188. waybill.Status != model.WaybillStatusStorageOut &&
  2189. waybill.Status != model.WaybillStatusVanningOut {
  2190. return dataList, deviceDataPdf, waybillPDF, nil
  2191. }
  2192. // 获取公司秘钥
  2193. var company model.SysDept
  2194. company, err = model.GetCompanyById(waybill.DeptId)
  2195. if err != nil {
  2196. e.Log.Errorf("db error: %s", err)
  2197. return dataList, deviceDataPdf, waybillPDF, model.GetCompanyKeyErr
  2198. }
  2199. err = e.Orm.Model(&data).Scopes(WaybillTaskIdsScopes(c.WaybillTaskIds)).
  2200. Where("waybill_no = ?", c.WaybillNo).
  2201. Preload("Warehouse").Preload("Car").Preload("CoolerBox").
  2202. Find(&taskList).Error
  2203. if err != nil {
  2204. e.Log.Errorf("db error: %s", err)
  2205. return dataList, deviceDataPdf, waybillPDF, global.GetFailedErr
  2206. }
  2207. // 创建名称到权重的映射
  2208. orderMap := make(map[string]int)
  2209. for i, v := range taskList {
  2210. orderMap[v.Sn] = i
  2211. }
  2212. for i := 0; i < len(taskList); i++ {
  2213. // 获取传感器信息
  2214. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(taskList[i].Sn, company.ColdKey)
  2215. var T_snid string
  2216. var list []nats_server.DeviceData_R
  2217. t_id_list := [2]int{-1, -2}
  2218. for j, r := range deviceSensorList {
  2219. if j >= 2 {
  2220. break
  2221. }
  2222. T_snid += fmt.Sprintf("%s,%d|", r.T_sn, r.T_id)
  2223. t_id_list[j] = r.T_id
  2224. }
  2225. var count int64
  2226. list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, taskList[i].StartTime.String(), taskList[i].EndTime.String(), 0, 9999)
  2227. if err != nil {
  2228. e.Log.Errorf("nats 获取温湿度信息失败: %s", err)
  2229. return dataList, deviceDataPdf, waybillPDF, global.GetFailedErr
  2230. }
  2231. firstMap := map[int]nats_server.DeviceData_R{}
  2232. lastMap := map[int]nats_server.DeviceData_R{}
  2233. if count > 0 {
  2234. for _, v := range deviceSensorList {
  2235. for j := 0; j < len(list); j++ {
  2236. if v.T_id == list[j].T_id {
  2237. if list[j].T_time != taskList[i].StartTime.String() {
  2238. firstData := list[len(list)-1-j]
  2239. firstData.T_time = taskList[i].StartTime.String()
  2240. firstMap[v.T_id] = firstData
  2241. count += 1
  2242. }
  2243. break
  2244. }
  2245. }
  2246. for k := len(list) - 1; k >= 0; k-- {
  2247. if v.T_id == list[k].T_id {
  2248. if list[k].T_time != taskList[i].EndTime.String() && !time.Time(taskList[i].EndTime).IsZero() {
  2249. lastData := list[len(list)-1-k]
  2250. lastData.T_time = taskList[i].EndTime.String()
  2251. lastMap[v.T_id] = lastData
  2252. count += 1
  2253. }
  2254. break
  2255. }
  2256. }
  2257. }
  2258. }
  2259. for _, v := range firstMap {
  2260. list = append(list, v)
  2261. }
  2262. for _, v := range lastMap {
  2263. list = append(list, v)
  2264. }
  2265. name := ""
  2266. sn := ""
  2267. if taskList[i].WarehouseId > 0 {
  2268. name = taskList[i].Warehouse.Name
  2269. sn = taskList[i].Warehouse.Sn
  2270. }
  2271. if taskList[i].CarId > 0 {
  2272. name = taskList[i].Car.CarNo
  2273. sn = taskList[i].Car.Sn
  2274. }
  2275. if taskList[i].CoolerBoxId > 0 {
  2276. name = taskList[i].CoolerBox.Name
  2277. sn = taskList[i].CoolerBox.Sn
  2278. }
  2279. deviceDataPdfList := DeviceSensorDataListToDeviceDataPdfList(name, sn, list, t_id_list[0], t_id_list[1])
  2280. deviceDataPdf = append(deviceDataPdf, deviceDataPdfList...)
  2281. dataList = append(dataList, list...)
  2282. waybillPDF = append(waybillPDF, WaybillPDF{
  2283. Data: list,
  2284. DeviceSensorList: deviceSensorList,
  2285. Task: taskList[i],
  2286. DeviceDataPdf: deviceDataPdfList,
  2287. })
  2288. }
  2289. sort.Slice(dataList, func(i, j int) bool {
  2290. if dataList[i].T_time == dataList[j].T_time {
  2291. // 如果时间相同,则按预设顺序排序
  2292. return orderMap[dataList[i].T_sn] < orderMap[dataList[j].T_sn]
  2293. }
  2294. return dataList[i].T_time < dataList[j].T_time
  2295. })
  2296. sort.Slice(deviceDataPdf, func(i, j int) bool {
  2297. if deviceDataPdf[i].T_time == deviceDataPdf[j].T_time {
  2298. // 如果时间相同,则按预设顺序排序
  2299. return orderMap[dataList[i].T_sn] < orderMap[dataList[j].T_sn]
  2300. }
  2301. return deviceDataPdf[i].T_time < deviceDataPdf[j].T_time
  2302. })
  2303. return dataList, deviceDataPdf, waybillPDF, nil
  2304. }
  2305. type WaybillPDF struct {
  2306. Data []nats_server.DeviceData_R
  2307. DeviceSensorList []nats_server.DeviceSensor_R
  2308. Task model.WaybillTask
  2309. DeviceDataPdf []DeviceDataPdf
  2310. }
  2311. type DeviceDataPdf struct {
  2312. T_sn string // 设备名称
  2313. T_name string // 设备名称
  2314. T_time string // 时间
  2315. T_id1 *float32 // 传感器id1温度
  2316. T_id2 *float32 // 传感器id2温度
  2317. }
  2318. func DeviceSensorDataListToDeviceDataPdfList(T_name, T_sn string, list []nats_server.DeviceData_R, T_id1 int, T_id2 int) []DeviceDataPdf {
  2319. // 创建一个 map 用于存储 DeviceData_Pdf 结果
  2320. resultMap := make(map[string]DeviceDataPdf)
  2321. // 遍历第一个列表,将数据存入 resultMap
  2322. for _, item := range list {
  2323. var pdf DeviceDataPdf
  2324. key := item.T_time
  2325. if _, ok := resultMap[key]; !ok {
  2326. pdf.T_time = item.T_time
  2327. T_name_s := []rune(T_name)
  2328. if len(T_name_s) > 8 {
  2329. pdf.T_name = string(T_name_s[:8]) + "..."
  2330. } else {
  2331. pdf.T_name = T_name
  2332. }
  2333. pdf.T_sn = T_sn
  2334. resultMap[key] = pdf
  2335. }
  2336. data := resultMap[key]
  2337. if item.T_id == T_id1 {
  2338. t1 := item.T_t
  2339. data.T_id1 = &t1
  2340. } else if item.T_id == T_id2 {
  2341. t2 := item.T_t
  2342. data.T_id2 = &t2
  2343. }
  2344. resultMap[key] = data
  2345. }
  2346. // 将 resultMap 转换为 DeviceData_Pdf 列表
  2347. var finalList []DeviceDataPdf
  2348. for _, v := range resultMap {
  2349. finalList = append(finalList, v)
  2350. }
  2351. return finalList
  2352. }
  2353. // 获取运单所有温湿度数据
  2354. func (e *Waybill) VerifyDataQualified(waybillNo string) (qualified bool, err error) {
  2355. var data model.WaybillTask
  2356. var waybill model.Waybill
  2357. var taskList []model.WaybillTask
  2358. err = e.Orm.Model(&waybill).Where("waybill_no = ?", waybillNo).First(&waybill).Error
  2359. if err != nil {
  2360. e.Log.Errorf("db error: %s", err)
  2361. return qualified, errors.New("获取运单信息失败")
  2362. }
  2363. // 未签收,不返回数据
  2364. if waybill.Status != model.WaybillStatusReceipt {
  2365. return qualified, nil
  2366. }
  2367. // 获取公司秘钥
  2368. var company model.SysDept
  2369. company, err = model.GetCompanyById(waybill.DeptId)
  2370. if err != nil {
  2371. e.Log.Errorf("db error: %s", err)
  2372. return qualified, model.GetCompanyKeyErr
  2373. }
  2374. err = e.Orm.Model(&data).
  2375. Where("waybill_no = ?", waybillNo).
  2376. Order("id desc").
  2377. Find(&taskList).Error
  2378. if err != nil {
  2379. e.Log.Errorf("db error: %s", err)
  2380. return qualified, global.GetFailedErr
  2381. }
  2382. // 获取最后一个任务id
  2383. //var lastWaybillTask model.WaybillTask
  2384. //err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", waybillNo).Last(&lastWaybillTask).Error
  2385. //if err != nil {
  2386. // e.Log.Errorf("db error: %s", err)
  2387. // err = errors.New("获取运单信息错误!")
  2388. // return qualified, err
  2389. //}
  2390. // 创建名称到权重的映射
  2391. orderMap := make(map[string]int)
  2392. for i, v := range taskList {
  2393. orderMap[v.Sn] = i
  2394. }
  2395. for i := 0; i < len(taskList); i++ {
  2396. // 获取传感器信息
  2397. deviceSensorList, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey(taskList[i].Sn, company.ColdKey)
  2398. var T_snid string
  2399. deviceSensorParameterMap := map[string]nats_server.DeviceSensorParameter_R{}
  2400. for _, r := range deviceSensorList {
  2401. T_snid += fmt.Sprintf("%s,%d|", r.T_sn, r.T_id)
  2402. deviceSensorParameterMap[fmt.Sprintf("%s-%d", r.T_sn, r.T_id)] = r.T_DeviceSensorParameter
  2403. }
  2404. var list []nats_server.DeviceData_R
  2405. list, _, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, taskList[i].StartTime.String(), taskList[i].EndTime.String(), 0, 9999)
  2406. if err != nil {
  2407. e.Log.Errorf("nats 获取温湿度信息失败: %s", err)
  2408. return qualified, global.GetFailedErr
  2409. }
  2410. // 判断数据是否合格
  2411. qualified = true
  2412. for _, v := range list {
  2413. dsp := deviceSensorParameterMap[fmt.Sprintf("%s-%d", v.T_sn, v.T_id)]
  2414. if v.T_t < dsp.T_Tlower || v.T_t > dsp.T_Tupper || v.T_rh < dsp.T_RHlower || v.T_t > dsp.T_RHupper {
  2415. qualified = false
  2416. break
  2417. }
  2418. }
  2419. }
  2420. return qualified, nil
  2421. }