waybill.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. package controller
  2. import (
  3. "bufio"
  4. "cold-logistics/app/admin/model"
  5. "cold-logistics/app/admin/service"
  6. "cold-logistics/app/admin/service/dto"
  7. "cold-logistics/common/actions"
  8. "errors"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/gin-gonic/gin/binding"
  12. "github.com/xuri/excelize/v2"
  13. "gogs.baozhida.cn/zoie/OAuth-core/api"
  14. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  15. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  16. "net/url"
  17. "os"
  18. "path"
  19. )
  20. type WaybillController struct {
  21. api.Api
  22. }
  23. // GetPage 获取运单列表
  24. // @Summary 获取运单列表
  25. // @Description 获取运单列表
  26. // @Tags 运单
  27. // @Param no query string false "运单号"
  28. // @Param pageSize query int false "页条数"
  29. // @Param page query int false "页码"
  30. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  31. // @Router /api/waybill [get]
  32. // @Security Bearer
  33. func (e WaybillController) GetPage(c *gin.Context) {
  34. s := service.Waybill{}
  35. req := dto.WaybillGetPageReq{}
  36. err := e.MakeContext(c).
  37. MakeOrm().
  38. Bind(&req, binding.Query).
  39. MakeService(&s.Service).
  40. Errors
  41. if err != nil {
  42. e.Logger.Error(err)
  43. e.Error(500, err, err.Error())
  44. return
  45. }
  46. //数据权限检查
  47. p := actions.GetPermissionFromContext(c)
  48. list := make([]model.Waybill, 0)
  49. var count int64
  50. err = s.GetPage(&req, &list, &count, p)
  51. if err != nil {
  52. e.Error(500, err, err.Error())
  53. return
  54. }
  55. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  56. }
  57. // Home 首页统计
  58. // @Summary 首页统计
  59. // @Description 首页统计
  60. // @Tags 运单
  61. // @Param no query string false "运单号"
  62. // @Param pageSize query int false "页条数"
  63. // @Param page query int false "页码"
  64. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  65. // @Router /api/waybill [get]
  66. // @Security Bearer
  67. func (e WaybillController) Home(c *gin.Context) {
  68. s := service.Waybill{}
  69. req := dto.WaybillStatsReq{}
  70. err := e.MakeContext(c).
  71. MakeOrm().
  72. Bind(&req, binding.Query).
  73. MakeService(&s.Service).
  74. Errors
  75. if err != nil {
  76. e.Logger.Error(err)
  77. e.Error(500, err, err.Error())
  78. return
  79. }
  80. //数据权限检查
  81. p := actions.GetPermissionFromContext(c)
  82. res := s.GetBasicsStats(&req, p)
  83. e.OK(res, "查询成功")
  84. }
  85. // GetPage 获取运单列表
  86. // @Summary 获取运单列表
  87. // @Description 获取运单列表
  88. // @Tags 运单
  89. // @Param no query string false "运单号"
  90. // @Param pageSize query int false "页条数"
  91. // @Param page query int false "页码"
  92. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  93. // @Router /api/waybill [get]
  94. // @Security Bearer
  95. func (e WaybillController) GetAppletPage(c *gin.Context) {
  96. s := service.Waybill{}
  97. req := dto.WaybillGetAppletPageReq{}
  98. err := e.MakeContext(c).
  99. MakeOrm().
  100. Bind(&req, binding.Query).
  101. MakeService(&s.Service).
  102. Errors
  103. if err != nil {
  104. e.Logger.Error(err)
  105. e.Error(500, err, err.Error())
  106. return
  107. }
  108. //数据权限检查
  109. p := actions.GetPermissionFromContext(c)
  110. list := make([]model.Waybill, 0)
  111. var count int64
  112. err = s.GetAppletPage(&req, &list, &count, p)
  113. if err != nil {
  114. e.Error(500, err, err.Error())
  115. return
  116. }
  117. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  118. }
  119. // Get 通过id获取运单
  120. // @Summary 通过id获取运单
  121. // @Description 通过id获取运单
  122. // @Tags 运单
  123. // @Param id path string true "运单id"
  124. // @Success 200 {object} response.Response{data=model.Waybill} "{"code": 200, "data": [...]}"
  125. // @Router /api/waybill/{id} [get]
  126. // @Security Bearer
  127. func (e WaybillController) Get(c *gin.Context) {
  128. s := service.Waybill{}
  129. req := dto.WaybillGetReq{}
  130. err := e.MakeContext(c).
  131. MakeOrm().
  132. Bind(&req, nil).
  133. MakeService(&s.Service).
  134. Errors
  135. if err != nil {
  136. e.Logger.Error(err)
  137. e.Error(500, err, err.Error())
  138. return
  139. }
  140. var object model.Waybill
  141. p := actions.GetPermissionFromContext(c)
  142. //数据权限检查
  143. err = s.Get(&req, &object, p)
  144. if err != nil {
  145. e.Error(500, err, err.Error())
  146. return
  147. }
  148. e.OK(object, "查询成功")
  149. }
  150. // Insert 添加运单
  151. // @Summary 添加运单
  152. // @Description 添加运单
  153. // @Tags 运单
  154. // @Accept application/json
  155. // @Product application/json
  156. // @Param data body dto.WaybillInsertReq true "data"
  157. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  158. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  159. // @Router /api/waybill [post]
  160. // @Security Bearer
  161. func (e WaybillController) Insert(c *gin.Context) {
  162. s := service.Waybill{}
  163. req := dto.WaybillInsertReq{}
  164. err := e.MakeContext(c).
  165. MakeOrm().
  166. Bind(&req, binding.JSON).
  167. MakeService(&s.Service).
  168. Errors
  169. if err != nil {
  170. e.Logger.Error(err)
  171. e.Error(500, err, err.Error())
  172. return
  173. }
  174. p := actions.GetPermissionFromContext(c)
  175. // 设置创建人
  176. req.SetCreateBy(user.GetUserId(c))
  177. req.SetDeptId(p.DeptId)
  178. err = s.Insert(&req)
  179. if err != nil {
  180. e.Error(500, err, err.Error())
  181. return
  182. }
  183. e.OK(req.GetId(), "创建成功")
  184. }
  185. // AppletInsert 添加运单app
  186. // @Summary 添加运单app
  187. // @Description 添加运单app
  188. // @Tags 运单
  189. // @Accept application/json
  190. // @Product application/json
  191. // @Param data body dto.WaybillInsertReq true "data"
  192. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  193. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  194. // @Router /api/waybill [post]
  195. // @Security Bearer
  196. func (e WaybillController) AppletInsert(c *gin.Context) {
  197. s := service.Waybill{}
  198. req := dto.WaybillInsertReq{}
  199. err := e.MakeContext(c).
  200. MakeOrm().
  201. Bind(&req, binding.JSON).
  202. MakeService(&s.Service).
  203. Errors
  204. if err != nil {
  205. e.Logger.Error(err)
  206. e.Error(500, err, err.Error())
  207. return
  208. }
  209. p := actions.GetPermissionFromContext(c)
  210. err = s.AppletInsert(&req, p)
  211. if err != nil {
  212. e.Error(500, err, err.Error())
  213. return
  214. }
  215. e.OK(req.GetId(), "添加成功")
  216. }
  217. // Update 修改运单
  218. // @Summary 修改运单
  219. // @Description 修改运单
  220. // @Tags 运单
  221. // @Accept application/json
  222. // @Product application/json
  223. // @Param data body dto.WaybillUpdateReq true "body"
  224. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  225. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  226. // @Router /api/waybill [put]
  227. // @Security Bearer
  228. func (e WaybillController) Update(c *gin.Context) {
  229. s := service.Waybill{}
  230. req := dto.WaybillUpdateReq{}
  231. err := e.MakeContext(c).
  232. MakeOrm().
  233. Bind(&req).
  234. MakeService(&s.Service).
  235. Errors
  236. if err != nil {
  237. e.Logger.Error(err)
  238. e.Error(500, err, err.Error())
  239. return
  240. }
  241. p := actions.GetPermissionFromContext(c)
  242. req.SetUpdateBy(user.GetUserId(c))
  243. err = s.Update(&req, p)
  244. if err != nil {
  245. e.Error(500, err, err.Error())
  246. return
  247. }
  248. e.OK(req.GetId(), "更新成功")
  249. }
  250. // Delivery 派单
  251. // @Summary 派单
  252. // @Description 派单
  253. // @Tags 运单
  254. // @Accept application/json
  255. // @Product application/json
  256. // @Param data body dto.WaybillDeliveryReq true "body"
  257. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  258. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  259. // @Router /api/waybill [put]
  260. // @Security Bearer
  261. func (e WaybillController) Delivery(c *gin.Context) {
  262. s := service.Waybill{}
  263. req := dto.WaybillDeliveryReq{}
  264. err := e.MakeContext(c).
  265. MakeOrm().
  266. Bind(&req).
  267. MakeService(&s.Service).
  268. Errors
  269. if err != nil {
  270. e.Logger.Error(err)
  271. e.Error(500, err, err.Error())
  272. return
  273. }
  274. p := actions.GetPermissionFromContext(c)
  275. req.SetUpdateBy(user.GetUserId(c))
  276. err = s.Delivery(&req, p)
  277. if err != nil {
  278. e.Error(500, err, err.Error())
  279. return
  280. }
  281. e.OK(req.GetId(), "派单成功")
  282. }
  283. // Delete 删除运单
  284. // @Summary 删除运单
  285. // @Description 删除运单
  286. // @Tags 运单
  287. // @Accept application/json
  288. // @Product application/json
  289. // @Param data body dto.WaybillDeleteReq true "body"
  290. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  291. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  292. // @Router /api/waybill [delete]
  293. // @Security Bearer
  294. func (e WaybillController) Delete(c *gin.Context) {
  295. s := service.Waybill{}
  296. req := dto.WaybillDeleteReq{}
  297. userSvc := service.SysUser{}
  298. err := e.MakeContext(c).
  299. MakeOrm().
  300. Bind(&req, binding.JSON, nil).
  301. MakeService(&s.Service).
  302. MakeService(&userSvc.Service).
  303. Errors
  304. if err != nil {
  305. e.Logger.Error(err)
  306. e.Error(500, err, err.Error())
  307. return
  308. }
  309. //数据权限检查
  310. p := actions.GetPermissionFromContext(c)
  311. err = s.Remove(&req, p)
  312. if err != nil {
  313. e.Error(500, err, err.Error())
  314. return
  315. }
  316. e.OK(req.GetId(), "删除成功")
  317. }
  318. // WarehouseIn 入库
  319. // @Summary 入库
  320. // @Description 入库
  321. // @Tags 运单
  322. // @Accept application/json
  323. // @Product application/json
  324. // @Param data body dto.WaybillInOutReq true "body"
  325. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  326. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  327. // @Router /api/waybill/warehouse-in [post]
  328. // @Security Bearer
  329. func (e WaybillController) WarehouseIn(c *gin.Context) {
  330. s := service.Waybill{}
  331. req := dto.WaybillInOutReq{}
  332. err := e.MakeContext(c).
  333. MakeOrm().
  334. Bind(&req, binding.JSON, nil).
  335. MakeService(&s.Service).
  336. Errors
  337. if err != nil {
  338. e.Logger.Error(err)
  339. e.Error(500, err, err.Error())
  340. return
  341. }
  342. //数据权限检查
  343. p := actions.GetPermissionFromContext(c)
  344. err = s.WarehouseIn(&req, p)
  345. if err != nil {
  346. e.Error(500, err, err.Error())
  347. return
  348. }
  349. e.OK(req.WaybillNoList, "入库成功")
  350. }
  351. // WarehouseOut 出库
  352. // @Summary 出库
  353. // @Description 出库
  354. // @Tags 运单
  355. // @Accept application/json
  356. // @Product application/json
  357. // @Param data body dto.WaybillInOutReq true "body"
  358. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  359. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  360. // @Router /api/waybill/warehouse-out [post]
  361. // @Security Bearer
  362. func (e WaybillController) WarehouseOut(c *gin.Context) {
  363. s := service.Waybill{}
  364. req := dto.WaybillInOutReq{}
  365. err := e.MakeContext(c).
  366. MakeOrm().
  367. Bind(&req, binding.JSON, nil).
  368. MakeService(&s.Service).
  369. Errors
  370. if err != nil {
  371. e.Logger.Error(err)
  372. e.Error(500, err, err.Error())
  373. return
  374. }
  375. //数据权限检查
  376. p := actions.GetPermissionFromContext(c)
  377. err = s.WarehouseOut(&req, p)
  378. if err != nil {
  379. e.Error(500, err, err.Error())
  380. return
  381. }
  382. e.OK(req.WaybillNoList, "出库成功")
  383. }
  384. // CarIn 上车
  385. // @Summary 上车
  386. // @Description 上车
  387. // @Tags 运单
  388. // @Accept application/json
  389. // @Product application/json
  390. // @Param data body dto.WaybillInOutReq true "body"
  391. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  392. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  393. // @Router /api/waybill/car-in [post]
  394. // @Security Bearer
  395. func (e WaybillController) CarIn(c *gin.Context) {
  396. s := service.Waybill{}
  397. req := dto.WaybillInOutReq{}
  398. err := e.MakeContext(c).
  399. MakeOrm().
  400. Bind(&req, binding.JSON, nil).
  401. MakeService(&s.Service).
  402. Errors
  403. if err != nil {
  404. e.Logger.Error(err)
  405. e.Error(500, err, err.Error())
  406. return
  407. }
  408. //数据权限检查
  409. p := actions.GetPermissionFromContext(c)
  410. err = s.CarIn(&req, p)
  411. if err != nil {
  412. e.Error(500, err, err.Error())
  413. return
  414. }
  415. e.OK(req.WaybillNoList, "装车成功")
  416. }
  417. // CarOut 下车
  418. // @Summary 下车
  419. // @Description 下车
  420. // @Tags 运单
  421. // @Accept application/json
  422. // @Product application/json
  423. // @Param data body dto.WaybillInOutReq true "body"
  424. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  425. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  426. // @Router /api/waybill/car-out [post]
  427. // @Security Bearer
  428. func (e WaybillController) CarOut(c *gin.Context) {
  429. s := service.Waybill{}
  430. req := dto.WaybillInOutReq{}
  431. err := e.MakeContext(c).
  432. MakeOrm().
  433. Bind(&req, binding.JSON, nil).
  434. MakeService(&s.Service).
  435. Errors
  436. if err != nil {
  437. e.Logger.Error(err)
  438. e.Error(500, err, err.Error())
  439. return
  440. }
  441. //数据权限检查
  442. p := actions.GetPermissionFromContext(c)
  443. err = s.CarOut(&req, p)
  444. if err != nil {
  445. e.Error(500, err, err.Error())
  446. return
  447. }
  448. e.OK(req.WaybillNoList, "下车成功")
  449. }
  450. // Receipt 签收
  451. // @Summary 签收
  452. // @Description 签收
  453. // @Tags 运单
  454. // @Accept application/json
  455. // @Product application/json
  456. // @Param data body dto.WaybillInOutReq true "body"
  457. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  458. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  459. // @Router /api/waybill/car-out [post]
  460. // @Security Bearer
  461. func (e WaybillController) Receipt(c *gin.Context) {
  462. s := service.Waybill{}
  463. req := dto.WaybillReceiptReq{}
  464. err := e.MakeContext(c).
  465. MakeOrm().
  466. Bind(&req, binding.JSON, nil).
  467. MakeService(&s.Service).
  468. Errors
  469. if err != nil {
  470. e.Logger.Error(err)
  471. e.Error(500, err, err.Error())
  472. return
  473. }
  474. //数据权限检查
  475. p := actions.GetPermissionFromContext(c)
  476. err = s.Receipt(&req, p)
  477. if err != nil {
  478. e.Error(500, err, err.Error())
  479. return
  480. }
  481. e.OK(req.WaybillNo, "签收成功")
  482. }
  483. // GetCustomerPage 获取客户运单列表
  484. // @Summary 获取客户运单列表
  485. // @Description 获取客户运单列表
  486. // @Tags 运单
  487. // @Param no query string false "运单号"
  488. // @Param pageSize query int false "页条数"
  489. // @Param page query int false "页码"
  490. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
  491. // @Router /api/waybill/customer [get]
  492. // @Security Bearer
  493. func (e WaybillController) GetCustomerPage(c *gin.Context) {
  494. s := service.Waybill{}
  495. req := dto.WaybillGetCustomerPageReq{}
  496. err := e.MakeContext(c).
  497. MakeOrm().
  498. Bind(&req, binding.Query).
  499. MakeService(&s.Service).
  500. Errors
  501. if err != nil {
  502. e.Logger.Error(err)
  503. e.Error(500, err, err.Error())
  504. return
  505. }
  506. //数据权限检查
  507. p := actions.GetPermissionFromContext(c)
  508. list := make([]model.Waybill, 0)
  509. var count int64
  510. req.CustomerId = p.UserId
  511. err = s.GetCustomerPage(&req, &list, &count, p)
  512. if err != nil {
  513. e.Error(500, err, err.Error())
  514. return
  515. }
  516. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  517. }
  518. // CustomerInsert 客户添加运单
  519. // @Summary 客户添加运单
  520. // @Description 客户添加运单
  521. // @Tags 运单
  522. // @Accept application/json
  523. // @Product application/json
  524. // @Param data body dto.WaybillInsertReq true "data"
  525. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  526. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  527. // @Router /api/waybill/customer [post]
  528. // @Security Bearer
  529. func (e WaybillController) CustomerInsert(c *gin.Context) {
  530. s := service.Waybill{}
  531. userSvc := service.SysUser{}
  532. req := dto.WaybillInsertReq{}
  533. err := e.MakeContext(c).
  534. MakeOrm().
  535. Bind(&req, binding.JSON).
  536. MakeService(&s.Service).
  537. MakeService(&userSvc.Service).
  538. Errors
  539. if err != nil {
  540. e.Logger.Error(err)
  541. e.Error(500, err, err.Error())
  542. return
  543. }
  544. p := actions.GetPermissionFromContext(c)
  545. var userObj model.SysUser
  546. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  547. if err != nil {
  548. e.Error(500, err, "获取用户信息失败")
  549. return
  550. }
  551. if p.DeptId == 0 && req.DeptId == 0 {
  552. e.Error(500, err, "请先选择运输公司")
  553. return
  554. }
  555. // 设置创建人
  556. req.SetCreateBy(user.GetUserId(c))
  557. req.CustomerId = p.UserId
  558. req.CustomerName = userObj.NickName
  559. if p.DeptId > 0 {
  560. req.SetDeptId(p.DeptId)
  561. }
  562. err = s.Insert(&req)
  563. if err != nil {
  564. e.Error(500, err, err.Error())
  565. return
  566. }
  567. e.OK(req.GetId(), "创建成功")
  568. }
  569. func (e WaybillController) Import(c *gin.Context) {
  570. s := service.Waybill{}
  571. userSvc := service.SysUser{}
  572. req := dto.WaybillImportReq{}
  573. err := e.MakeContext(c).
  574. MakeOrm().
  575. Bind(&req, binding.Form).
  576. MakeService(&s.Service).
  577. MakeService(&userSvc.Service).
  578. Errors
  579. if err != nil {
  580. e.Logger.Error(err)
  581. e.Error(500, err, err.Error())
  582. return
  583. }
  584. //读取第一fileName的文件
  585. fileHeader, err := c.FormFile("file")
  586. if err != nil {
  587. err = errors.New("文件格式错误" + err.Error())
  588. e.Logger.Error(err)
  589. e.Error(500, err, err.Error())
  590. return
  591. }
  592. if fileHeader.Size > 1024*1024*2 {
  593. err = errors.New("文件大小超过2M")
  594. e.Logger.Error(err)
  595. e.Error(500, err, err.Error())
  596. return
  597. }
  598. file, err := fileHeader.Open()
  599. if err != nil {
  600. err = errors.New("文件格式错误" + err.Error())
  601. e.Logger.Error(err)
  602. e.Error(500, err, err.Error())
  603. return
  604. }
  605. defer file.Close()
  606. xlsx, err := excelize.OpenReader(bufio.NewReader(file))
  607. if err != nil {
  608. err = errors.New("文件格式错误" + err.Error())
  609. e.Logger.Error(err)
  610. e.Error(500, err, err.Error())
  611. return
  612. }
  613. p := actions.GetPermissionFromContext(c)
  614. if p.DeptId == 0 {
  615. e.Error(500, err, "获取用户信息失败")
  616. return
  617. }
  618. var userObj model.SysUser
  619. err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj)
  620. if err != nil {
  621. e.Error(500, err, "获取用户信息失败")
  622. return
  623. }
  624. var customerId int
  625. customerName := req.CustomerName
  626. if userObj.UserType == model.UserTypeCustomer {
  627. customerId = userObj.Id
  628. customerName = userObj.NickName
  629. }
  630. rows, _ := xlsx.GetRows("Sheet1")
  631. for indexRow, row := range rows {
  632. if indexRow == 0 {
  633. continue
  634. }
  635. if len(row) < 10 {
  636. for i := 0; i < 10-len(row); i++ {
  637. row = append(row, "")
  638. }
  639. }
  640. for i, colCell := range row {
  641. fmt.Println(i, ":", colCell)
  642. }
  643. obj := dto.WaybillInsertReq{
  644. Status: 1,
  645. SenderAddressName: row[0],
  646. SenderAddressPhone: row[1],
  647. SenderAddressDetails: row[2],
  648. ConsigneeAddressName: row[3],
  649. ConsigneeAddressPhone: row[4],
  650. ConsigneeAddressDetails: row[5],
  651. TemperatureInterval: row[6],
  652. DeliveryCondition: row[7],
  653. CargoType: row[8],
  654. Remark: row[9],
  655. CustomerId: customerId,
  656. CustomerName: customerName,
  657. }
  658. obj.SetDeptId(p.DeptId)
  659. obj.SetCreateBy(user.GetUserId(c))
  660. err = s.Insert(&obj)
  661. if err != nil {
  662. e.Error(500, err, err.Error())
  663. return
  664. }
  665. }
  666. e.OK(len(rows)-1, "导入成功")
  667. }
  668. func (e WaybillController) ExportTemplate(c *gin.Context) {
  669. s := service.Waybill{}
  670. err := e.MakeContext(c).
  671. MakeOrm().
  672. MakeService(&s.Service).
  673. Errors
  674. if err != nil {
  675. e.Logger.Error(err)
  676. e.Error(500, err, err.Error())
  677. return
  678. }
  679. filePath := "./ofile/运单导入模板.xlsx"
  680. //打开文件
  681. fileTmp, errByOpenFile := os.Open(filePath)
  682. defer fileTmp.Close()
  683. //获取文件的名称
  684. fileName := path.Base(filePath)
  685. if errByOpenFile != nil {
  686. e.Logger.Error(err)
  687. e.Error(500, err, err.Error())
  688. return
  689. }
  690. c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  691. // PathEscape 函数对中文做处理
  692. c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  693. c.Header("Content-Transfer-Encoding", "binary")
  694. c.File(filePath)
  695. }