package controller import ( "bufio" "cold-logistics/app/admin/model" "cold-logistics/app/admin/service" "cold-logistics/app/admin/service/dto" "cold-logistics/common/actions" "errors" "fmt" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/xuri/excelize/v2" "gogs.baozhida.cn/zoie/OAuth-core/api" "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user" _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response" "net/url" "os" "path" ) type WaybillController struct { api.Api } // GetPage 获取运单列表 // @Summary 获取运单列表 // @Description 获取运单列表 // @Tags 运单 // @Param no query string false "运单号" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}" // @Router /api/waybill [get] // @Security Bearer func (e WaybillController) GetPage(c *gin.Context) { s := service.Waybill{} req := dto.WaybillGetPageReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.Query). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) list := make([]model.Waybill, 0) var count int64 err = s.GetPage(&req, &list, &count, p) if err != nil { e.Error(500, err, err.Error()) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // Home 首页统计 // @Summary 首页统计 // @Description 首页统计 // @Tags 运单 // @Param no query string false "运单号" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}" // @Router /api/waybill [get] // @Security Bearer func (e WaybillController) Home(c *gin.Context) { s := service.Waybill{} req := dto.WaybillStatsReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.Query). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) res := s.GetBasicsStats(&req, p) e.OK(res, "查询成功") } // GetPage 获取运单列表 // @Summary 获取运单列表 // @Description 获取运单列表 // @Tags 运单 // @Param no query string false "运单号" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}" // @Router /api/waybill [get] // @Security Bearer func (e WaybillController) GetAppletPage(c *gin.Context) { s := service.Waybill{} req := dto.WaybillGetAppletPageReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.Query). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) list := make([]model.Waybill, 0) var count int64 err = s.GetAppletPage(&req, &list, &count, p) if err != nil { e.Error(500, err, err.Error()) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // Get 通过id获取运单 // @Summary 通过id获取运单 // @Description 通过id获取运单 // @Tags 运单 // @Param id path string true "运单id" // @Success 200 {object} response.Response{data=model.Waybill} "{"code": 200, "data": [...]}" // @Router /api/waybill/{id} [get] // @Security Bearer func (e WaybillController) Get(c *gin.Context) { s := service.Waybill{} req := dto.WaybillGetReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object model.Waybill p := actions.GetPermissionFromContext(c) //数据权限检查 err = s.Get(&req, &object, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(object, "查询成功") } // Insert 添加运单 // @Summary 添加运单 // @Description 添加运单 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/waybill [post] // @Security Bearer func (e WaybillController) Insert(c *gin.Context) { s := service.Waybill{} req := dto.WaybillInsertReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) // 设置创建人 req.SetCreateBy(user.GetUserId(c)) req.SetDeptId(p.DeptId) err = s.Insert(&req) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "创建成功") } // AppletInsert 添加运单app // @Summary 添加运单app // @Description 添加运单app // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/waybill [post] // @Security Bearer func (e WaybillController) AppletInsert(c *gin.Context) { s := service.Waybill{} req := dto.WaybillInsertReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) err = s.AppletInsert(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "添加成功") } // Update 修改运单 // @Summary 修改运单 // @Description 修改运单 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillUpdateReq true "body" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/waybill [put] // @Security Bearer func (e WaybillController) Update(c *gin.Context) { s := service.Waybill{} req := dto.WaybillUpdateReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) req.SetUpdateBy(user.GetUserId(c)) err = s.Update(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "更新成功") } // Delivery 派单 // @Summary 派单 // @Description 派单 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillDeliveryReq true "body" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/waybill [put] // @Security Bearer func (e WaybillController) Delivery(c *gin.Context) { s := service.Waybill{} req := dto.WaybillDeliveryReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) req.SetUpdateBy(user.GetUserId(c)) err = s.Delivery(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "派单成功") } // Delete 删除运单 // @Summary 删除运单 // @Description 删除运单 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillDeleteReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/waybill [delete] // @Security Bearer func (e WaybillController) Delete(c *gin.Context) { s := service.Waybill{} req := dto.WaybillDeleteReq{} userSvc := service.SysUser{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). MakeService(&userSvc.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.Remove(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "删除成功") } // WarehouseIn 入库 // @Summary 入库 // @Description 入库 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInOutReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/waybill/warehouse-in [post] // @Security Bearer func (e WaybillController) WarehouseIn(c *gin.Context) { s := service.Waybill{} req := dto.WaybillInOutReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.WarehouseIn(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.WaybillNoList, "入库成功") } // WarehouseOut 出库 // @Summary 出库 // @Description 出库 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInOutReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/waybill/warehouse-out [post] // @Security Bearer func (e WaybillController) WarehouseOut(c *gin.Context) { s := service.Waybill{} req := dto.WaybillInOutReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.WarehouseOut(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.WaybillNoList, "出库成功") } // CarIn 上车 // @Summary 上车 // @Description 上车 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInOutReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/waybill/car-in [post] // @Security Bearer func (e WaybillController) CarIn(c *gin.Context) { s := service.Waybill{} req := dto.WaybillInOutReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.CarIn(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.WaybillNoList, "装车成功") } // CarOut 下车 // @Summary 下车 // @Description 下车 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInOutReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/waybill/car-out [post] // @Security Bearer func (e WaybillController) CarOut(c *gin.Context) { s := service.Waybill{} req := dto.WaybillInOutReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.CarOut(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.WaybillNoList, "下车成功") } // Receipt 签收 // @Summary 签收 // @Description 签收 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInOutReq true "body" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": -1, "message": "删除失败"}" // @Router /api/waybill/car-out [post] // @Security Bearer func (e WaybillController) Receipt(c *gin.Context) { s := service.Waybill{} req := dto.WaybillReceiptReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON, nil). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) err = s.Receipt(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.WaybillNo, "签收成功") } // GetCustomerPage 获取客户运单列表 // @Summary 获取客户运单列表 // @Description 获取客户运单列表 // @Tags 运单 // @Param no query string false "运单号" // @Param pageSize query int false "页条数" // @Param page query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}" // @Router /api/waybill/customer [get] // @Security Bearer func (e WaybillController) GetCustomerPage(c *gin.Context) { s := service.Waybill{} req := dto.WaybillGetCustomerPageReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.Query). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //数据权限检查 p := actions.GetPermissionFromContext(c) list := make([]model.Waybill, 0) var count int64 req.CustomerId = p.UserId err = s.GetCustomerPage(&req, &list, &count, p) if err != nil { e.Error(500, err, err.Error()) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // CustomerInsert 客户添加运单 // @Summary 客户添加运单 // @Description 客户添加运单 // @Tags 运单 // @Accept application/json // @Product application/json // @Param data body dto.WaybillInsertReq true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/waybill/customer [post] // @Security Bearer func (e WaybillController) CustomerInsert(c *gin.Context) { s := service.Waybill{} userSvc := service.SysUser{} req := dto.WaybillInsertReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.JSON). MakeService(&s.Service). MakeService(&userSvc.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) var userObj model.SysUser err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj) if err != nil { e.Error(500, err, "获取用户信息失败") return } if p.DeptId == 0 && req.DeptId == 0 { e.Error(500, err, "请先选择运输公司") return } // 设置创建人 req.SetCreateBy(user.GetUserId(c)) req.CustomerId = p.UserId req.CustomerName = userObj.NickName if p.DeptId > 0 { req.SetDeptId(p.DeptId) } err = s.Insert(&req) if err != nil { e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "创建成功") } func (e WaybillController) Import(c *gin.Context) { s := service.Waybill{} userSvc := service.SysUser{} req := dto.WaybillImportReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req, binding.Form). MakeService(&s.Service). MakeService(&userSvc.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } //读取第一fileName的文件 fileHeader, err := c.FormFile("file") if err != nil { err = errors.New("文件格式错误" + err.Error()) e.Logger.Error(err) e.Error(500, err, err.Error()) return } if fileHeader.Size > 1024*1024*2 { err = errors.New("文件大小超过2M") e.Logger.Error(err) e.Error(500, err, err.Error()) return } file, err := fileHeader.Open() if err != nil { err = errors.New("文件格式错误" + err.Error()) e.Logger.Error(err) e.Error(500, err, err.Error()) return } defer file.Close() xlsx, err := excelize.OpenReader(bufio.NewReader(file)) if err != nil { err = errors.New("文件格式错误" + err.Error()) e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) if p.DeptId == 0 { e.Error(500, err, "获取用户信息失败") return } var userObj model.SysUser err = userSvc.Get(&dto.SysUserGetReq{Id: p.UserId}, nil, &userObj) if err != nil { e.Error(500, err, "获取用户信息失败") return } var customerId int customerName := req.CustomerName if userObj.UserType == model.UserTypeCustomer { customerId = userObj.Id customerName = userObj.NickName } rows, _ := xlsx.GetRows("Sheet1") for indexRow, row := range rows { if indexRow == 0 { continue } if len(row) < 10 { for i := 0; i < 10-len(row); i++ { row = append(row, "") } } for i, colCell := range row { fmt.Println(i, ":", colCell) } obj := dto.WaybillInsertReq{ Status: 1, SenderAddressName: row[0], SenderAddressPhone: row[1], SenderAddressDetails: row[2], ConsigneeAddressName: row[3], ConsigneeAddressPhone: row[4], ConsigneeAddressDetails: row[5], TemperatureInterval: row[6], DeliveryCondition: row[7], CargoType: row[8], Remark: row[9], CustomerId: customerId, CustomerName: customerName, } obj.SetDeptId(p.DeptId) obj.SetCreateBy(user.GetUserId(c)) err = s.Insert(&obj) if err != nil { e.Error(500, err, err.Error()) return } } e.OK(len(rows)-1, "导入成功") } func (e WaybillController) ExportTemplate(c *gin.Context) { s := service.Waybill{} err := e.MakeContext(c). MakeOrm(). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } filePath := "./ofile/运单导入模板.xlsx" //打开文件 fileTmp, errByOpenFile := os.Open(filePath) defer fileTmp.Close() //获取文件的名称 fileName := path.Base(filePath) if errByOpenFile != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8") // PathEscape 函数对中文做处理 c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName)) c.Header("Content-Transfer-Encoding", "binary") c.File(filePath) }