vehicle.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package v1
  2. import (
  3. "git.getensh.com/common/gopkgs/logger"
  4. "git.getensh.com/common/gopkgs/tasker/httptasker"
  5. "git.getensh.com/common/gopkgs/util"
  6. "github.com/gin-gonic/gin"
  7. "go.uber.org/zap"
  8. "net/http"
  9. "property-system-gateway/errors"
  10. param_v1 "property-system-gateway/param/v1"
  11. "property-system-gateway/parser"
  12. "property-system-gateway/pb"
  13. "property-system-gateway/pb/v1"
  14. "property-system-gateway/utils"
  15. )
  16. //
  17. // @Summary 添加车辆
  18. // @Description 添加车辆
  19. // @Tags 车辆
  20. // @Accept json
  21. // @Produce json
  22. // @Param token header string true "token"
  23. // @Param body body v1.VehicleAddBody true "信息"
  24. // @Success 200 {object} v1.VehicleAddResponse
  25. // @Failure 500 {object} base.HTTPError
  26. // @Router /api/v1/vehicle [post]
  27. func (c *Controller) VehicleAdd(ctx *gin.Context) {
  28. // 解析参数
  29. req := &param_v1.VehicleAddRequest{}
  30. parseParamTask := func() error {
  31. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VehicleAddBody)
  32. if err != nil {
  33. logger.Error("func",
  34. zap.String("call", "util.ShouldBind"),
  35. zap.String("error", err.Error()))
  36. return errors.ParamsError
  37. }
  38. return nil
  39. }
  40. // 业务处理
  41. handleServiceTask := func() error {
  42. tokenInfo, err := utils.GetSubjectValue(ctx)
  43. if err != nil {
  44. return err
  45. }
  46. // 响应数据
  47. resp := param_v1.VehicleAddResponse{}
  48. rpcReq := &v1.VehicleAddRequest{
  49. GardenId: tokenInfo.GardenId,
  50. SpaceId: req.SpaceId,
  51. PlateNo: req.PlateNo,
  52. VehicleType: req.VehicleType,
  53. Vin: req.Vin,
  54. EngineNo: req.EngineNo,
  55. Brand: req.Brand,
  56. Color: req.Color,
  57. HouseholdUid: req.HouseholdUid,
  58. BindType: req.BindType,
  59. DisplacementUnit: req.DisplacementUnit,
  60. Displacement: req.Displacement,
  61. Seat: req.Seat,
  62. }
  63. rpcRsp, err := pb.Garden.VehicleAdd(ctx, rpcReq)
  64. if err != nil {
  65. s, _ := json.MarshalToString(req)
  66. logger.Error("func",
  67. zap.String("call", "pb.Garden.VehicleAdd"),
  68. zap.String("params", s),
  69. zap.String("error", err.Error()))
  70. return errors.ErrorTransForm(err)
  71. }
  72. resp.Data = *rpcRsp
  73. ctx.JSON(http.StatusOK, resp)
  74. logReq := OperationLogRequest{
  75. Module: ModuleVehicle,
  76. Action: ActionVehicleAdd,
  77. Origin: nil,
  78. Target: req.VehicleAddBody,
  79. UserName: tokenInfo.UserName,
  80. Uid: tokenInfo.Uid,
  81. Cid: tokenInfo.Cid,
  82. GardenId: tokenInfo.GardenId,
  83. }
  84. go OperationLogAdd(&logReq)
  85. return nil
  86. }
  87. // 执行任务
  88. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  89. }
  90. //
  91. // @Summary 修改车辆
  92. // @Description 修改车辆
  93. // @Tags 车辆
  94. // @Accept json
  95. // @Produce json
  96. // @Param token header string true "token"
  97. // @Param body body v1.VehicleUpdateBody true "信息"
  98. // @Success 200 {object} v1.VehicleUpdateResponse
  99. // @Failure 500 {object} base.HTTPError
  100. // @Router /api/v1/vehicle [put]
  101. func (c *Controller) VehicleUpdate(ctx *gin.Context) {
  102. // 解析参数
  103. req := &param_v1.VehicleUpdateRequest{}
  104. parseParamTask := func() error {
  105. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VehicleUpdateBody)
  106. if err != nil {
  107. logger.Error("func",
  108. zap.String("call", "util.ShouldBind"),
  109. zap.String("error", err.Error()))
  110. return errors.ParamsError
  111. }
  112. return nil
  113. }
  114. // 业务处理
  115. handleServiceTask := func() error {
  116. tokenInfo, err := utils.GetSubjectValue(ctx)
  117. if err != nil {
  118. return err
  119. }
  120. // 响应数据
  121. resp := param_v1.VehicleUpdateResponse{}
  122. rpcReq := &v1.VehicleUpdateRequest{
  123. GardenId: tokenInfo.GardenId,
  124. SpaceId: req.SpaceId,
  125. PlateNo: req.PlateNo,
  126. VehicleType: req.VehicleType,
  127. Vin: req.Vin,
  128. EngineNo: req.EngineNo,
  129. Brand: req.Brand,
  130. Color: req.Color,
  131. HouseholdUid: req.HouseholdUid,
  132. BindType: req.BindType,
  133. DisplacementUnit: req.DisplacementUnit,
  134. Displacement: req.Displacement,
  135. Seat: req.Seat,
  136. Id: req.Id,
  137. }
  138. rpcRsp, err := pb.Garden.VehicleUpdate(ctx, rpcReq)
  139. if err != nil {
  140. s, _ := json.MarshalToString(req)
  141. logger.Error("func",
  142. zap.String("call", "pb.Garden.VehicleUpdate"),
  143. zap.String("params", s),
  144. zap.String("error", err.Error()))
  145. return errors.ErrorTransForm(err)
  146. }
  147. ctx.JSON(http.StatusOK, resp)
  148. logReq := OperationLogRequest{
  149. Module: ModuleVehicle,
  150. Action: ActionVehicleUpdate,
  151. Origin: rpcRsp.Origin,
  152. Target: req.VehicleUpdateBody,
  153. UserName: tokenInfo.UserName,
  154. Uid: tokenInfo.Uid,
  155. Cid: tokenInfo.Cid,
  156. GardenId: tokenInfo.GardenId,
  157. }
  158. go OperationLogAdd(&logReq)
  159. return nil
  160. }
  161. // 执行任务
  162. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  163. }
  164. //
  165. // @Summary 删除车辆
  166. // @Description 删除车辆
  167. // @Tags 车辆
  168. // @Accept json
  169. // @Produce json
  170. // @Param token header string true "token"
  171. // @Param id query int true " "
  172. // @Success 200 {object} v1.VehicleDelResponse
  173. // @Failure 500 {object} base.HTTPError
  174. // @Router /api/v1/vehicle [delete]
  175. func (c *Controller) VehicleDel(ctx *gin.Context) {
  176. // 解析参数
  177. req := &param_v1.VehicleDelRequest{}
  178. parseParamTask := func() error {
  179. err := util.ShouldBind(ctx, &req.Header, nil, &req.VehicleDelQuery, nil)
  180. if err != nil {
  181. logger.Error("func",
  182. zap.String("call", "util.ShouldBind"),
  183. zap.String("error", err.Error()))
  184. return errors.ParamsError
  185. }
  186. return nil
  187. }
  188. // 业务处理
  189. handleServiceTask := func() error {
  190. tokenInfo, err := utils.GetSubjectValue(ctx)
  191. if err != nil {
  192. return err
  193. }
  194. // 响应数据
  195. resp := param_v1.VehicleDelResponse{}
  196. rpcReq := &v1.VehicleDelRequest{
  197. GardenId: tokenInfo.GardenId,
  198. Id: req.Id,
  199. }
  200. rpcRsp, err := pb.Garden.VehicleDel(ctx, rpcReq)
  201. if err != nil {
  202. s, _ := json.MarshalToString(req)
  203. logger.Error("func",
  204. zap.String("call", "pb.Garden.VehicleDel"),
  205. zap.String("params", s),
  206. zap.String("error", err.Error()))
  207. return errors.ErrorTransForm(err)
  208. }
  209. ctx.JSON(http.StatusOK, resp)
  210. logReq := OperationLogRequest{
  211. Module: ModuleVehicle,
  212. Action: ActionVehicleDel,
  213. Origin: rpcRsp.Origin,
  214. Target: nil,
  215. UserName: tokenInfo.UserName,
  216. Uid: tokenInfo.Uid,
  217. Cid: tokenInfo.Cid,
  218. GardenId: tokenInfo.GardenId,
  219. }
  220. go OperationLogAdd(&logReq)
  221. return nil
  222. }
  223. // 执行任务
  224. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  225. }
  226. //
  227. // @Summary 车辆列表
  228. // @Description 车辆列表
  229. // @Tags 车辆
  230. // @Accept json
  231. // @Produce json
  232. // @Param token header string true "token"
  233. // @Param page query int false " "
  234. // @Param page_size query int false " "
  235. // @Param household_id query int false "住户uid"
  236. // @Param plate_no query string false "车牌号"
  237. // @Success 200 {object} v1.VehicleListResponse
  238. // @Failure 500 {object} base.HTTPError
  239. // @Router /api/v1/vehicle [get]
  240. func (c *Controller) VehicleList(ctx *gin.Context) {
  241. // 解析参数
  242. req := &param_v1.VehicleListRequest{}
  243. parseParamTask := func() error {
  244. err := util.ShouldBind(ctx, &req.Header, nil, &req.VehicleListQuery, nil)
  245. if err != nil {
  246. logger.Error("func",
  247. zap.String("call", "util.ShouldBind"),
  248. zap.String("error", err.Error()))
  249. return errors.ParamsError
  250. }
  251. return nil
  252. }
  253. // 业务处理
  254. handleServiceTask := func() error {
  255. tokenInfo, err := utils.GetSubjectValue(ctx)
  256. if err != nil {
  257. return err
  258. }
  259. // 响应数据
  260. resp := param_v1.VehicleListResponse{}
  261. rpcReq := &v1.VehicleListRequest{
  262. GardenId: tokenInfo.GardenId,
  263. PageSize: req.PageSize,
  264. Page: req.Page,
  265. HouseholdUid: req.HouseholdUid,
  266. PlateNo: req.PlateNo,
  267. }
  268. rpcRsp, err := pb.Garden.VehicleList(ctx, rpcReq)
  269. if err != nil {
  270. s, _ := json.MarshalToString(req)
  271. logger.Error("func",
  272. zap.String("call", "pb.Garden.VehicleList"),
  273. zap.String("params", s),
  274. zap.String("error", err.Error()))
  275. return errors.ErrorTransForm(err)
  276. }
  277. if rpcRsp.List == nil {
  278. rpcRsp.List = make([]*v1.VehicleItem, 0)
  279. }
  280. resp.Data = *rpcRsp
  281. ctx.JSON(http.StatusOK, resp)
  282. return nil
  283. }
  284. // 执行任务
  285. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  286. }
  287. //
  288. // @Summary 车辆品牌
  289. // @Description 车辆品牌
  290. // @Tags 车辆
  291. // @Accept json
  292. // @Produce json
  293. // @Param token header string true "token"
  294. // @Success 200 {object} v1.VehicleBrandListResponse
  295. // @Failure 500 {object} base.HTTPError
  296. // @Router /api/v1/vehicle/brand [get]
  297. func (c *Controller) VehicleBrandList(ctx *gin.Context) {
  298. // 解析参数
  299. req := &param_v1.VehicleBrandListRequest{}
  300. parseParamTask := func() error {
  301. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  302. if err != nil {
  303. logger.Error("func",
  304. zap.String("call", "util.ShouldBind"),
  305. zap.String("error", err.Error()))
  306. return errors.ParamsError
  307. }
  308. return nil
  309. }
  310. // 业务处理
  311. handleServiceTask := func() error {
  312. resp := param_v1.VehicleBrandListResponse{}
  313. resp.Data.List = make([]param_v1.VehicleBrandItem, len(utils.BrandArray))
  314. for i, v := range utils.BrandArray {
  315. resp.Data.List[i] = param_v1.VehicleBrandItem{
  316. Brand: v,
  317. BrandPic: parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.BrandBucket + "/" + v + ".png",
  318. }
  319. }
  320. ctx.JSON(http.StatusOK, resp)
  321. return nil
  322. }
  323. // 执行任务
  324. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  325. }