package v1 import ( "git.getensh.com/common/gopkgs/logger" "git.getensh.com/common/gopkgs/tasker/httptasker" "git.getensh.com/common/gopkgs/util" "github.com/gin-gonic/gin" "go.uber.org/zap" "net/http" "property-system-gateway/errors" param_v1 "property-system-gateway/param/v1" "property-system-gateway/parser" "property-system-gateway/pb" "property-system-gateway/pb/v1" "property-system-gateway/utils" ) // // @Summary 添加车辆 // @Description 添加车辆 // @Tags 车辆 // @Accept json // @Produce json // @Param token header string true "token" // @Param body body v1.VehicleAddBody true "信息" // @Success 200 {object} v1.VehicleAddResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/vehicle [post] func (c *Controller) VehicleAdd(ctx *gin.Context) { // 解析参数 req := ¶m_v1.VehicleAddRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VehicleAddBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.VehicleAddResponse{} rpcReq := &v1.VehicleAddRequest{ GardenId: tokenInfo.GardenId, SpaceId: req.SpaceId, PlateNo: req.PlateNo, VehicleType: req.VehicleType, Vin: req.Vin, EngineNo: req.EngineNo, Brand: req.Brand, Color: req.Color, HouseholdUid: req.HouseholdUid, BindType: req.BindType, DisplacementUnit: req.DisplacementUnit, Displacement: req.Displacement, Seat: req.Seat, } rpcRsp, err := pb.Garden.VehicleAdd(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Garden.VehicleAdd"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module: ModuleVehicle, Action: ActionVehicleAdd, Origin: nil, Target: req.VehicleAddBody, UserName: tokenInfo.UserName, Uid: tokenInfo.Uid, Cid: tokenInfo.Cid, GardenId: tokenInfo.GardenId, } go OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 修改车辆 // @Description 修改车辆 // @Tags 车辆 // @Accept json // @Produce json // @Param token header string true "token" // @Param body body v1.VehicleUpdateBody true "信息" // @Success 200 {object} v1.VehicleUpdateResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/vehicle [put] func (c *Controller) VehicleUpdate(ctx *gin.Context) { // 解析参数 req := ¶m_v1.VehicleUpdateRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VehicleUpdateBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.VehicleUpdateResponse{} rpcReq := &v1.VehicleUpdateRequest{ GardenId: tokenInfo.GardenId, SpaceId: req.SpaceId, PlateNo: req.PlateNo, VehicleType: req.VehicleType, Vin: req.Vin, EngineNo: req.EngineNo, Brand: req.Brand, Color: req.Color, HouseholdUid: req.HouseholdUid, BindType: req.BindType, DisplacementUnit: req.DisplacementUnit, Displacement: req.Displacement, Seat: req.Seat, Id: req.Id, } rpcRsp, err := pb.Garden.VehicleUpdate(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Garden.VehicleUpdate"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module: ModuleVehicle, Action: ActionVehicleUpdate, Origin: rpcRsp.Origin, Target: req.VehicleUpdateBody, UserName: tokenInfo.UserName, Uid: tokenInfo.Uid, Cid: tokenInfo.Cid, GardenId: tokenInfo.GardenId, } go OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 删除车辆 // @Description 删除车辆 // @Tags 车辆 // @Accept json // @Produce json // @Param token header string true "token" // @Param id query int true " " // @Success 200 {object} v1.VehicleDelResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/vehicle [delete] func (c *Controller) VehicleDel(ctx *gin.Context) { // 解析参数 req := ¶m_v1.VehicleDelRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.VehicleDelQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.VehicleDelResponse{} rpcReq := &v1.VehicleDelRequest{ GardenId: tokenInfo.GardenId, Id: req.Id, } rpcRsp, err := pb.Garden.VehicleDel(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Garden.VehicleDel"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module: ModuleVehicle, Action: ActionVehicleDel, Origin: rpcRsp.Origin, Target: nil, UserName: tokenInfo.UserName, Uid: tokenInfo.Uid, Cid: tokenInfo.Cid, GardenId: tokenInfo.GardenId, } go OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 车辆列表 // @Description 车辆列表 // @Tags 车辆 // @Accept json // @Produce json // @Param token header string true "token" // @Param page query int false " " // @Param page_size query int false " " // @Param household_id query int false "住户uid" // @Param plate_no query string false "车牌号" // @Success 200 {object} v1.VehicleListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/vehicle [get] func (c *Controller) VehicleList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.VehicleListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.VehicleListQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.VehicleListResponse{} rpcReq := &v1.VehicleListRequest{ GardenId: tokenInfo.GardenId, PageSize: req.PageSize, Page: req.Page, HouseholdUid: req.HouseholdUid, PlateNo: req.PlateNo, } rpcRsp, err := pb.Garden.VehicleList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Garden.VehicleList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if rpcRsp.List == nil { rpcRsp.List = make([]*v1.VehicleItem, 0) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 车辆品牌 // @Description 车辆品牌 // @Tags 车辆 // @Accept json // @Produce json // @Param token header string true "token" // @Success 200 {object} v1.VehicleBrandListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/vehicle/brand [get] func (c *Controller) VehicleBrandList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.VehicleBrandListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { resp := param_v1.VehicleBrandListResponse{} resp.Data.List = make([]param_v1.VehicleBrandItem, len(utils.BrandArray)) for i, v := range utils.BrandArray { resp.Data.List[i] = param_v1.VehicleBrandItem{ Brand: v, BrandPic: parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.BrandBucket + "/" + v + ".png", } } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }