package service import ( "context" "fmt" "git.hpds.cc/Component/logging" "hpds-iot-web/internal/proto" "hpds-iot-web/model" "net/http" "time" "xorm.io/xorm" ) type SystemService interface { BrandList(ctx context.Context, req proto.BrandRequest) (rsp *proto.BaseResponse, err error) BrandInfo(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) AddBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) EditBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) DeleteBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) } func NewSystemService(engine *xorm.Engine, logger *logging.Logger) SystemService { return &repo{ engine: engine, logger: logger, } } func (rp *repo) BrandList(ctx context.Context, req proto.BrandRequest) (rsp *proto.BaseResponse, err error) { rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Message = "超时/取消" rsp.Err = ctx.Err() return rsp, ctx.Err() default: data := make([]model.Brand, 0) count, err := rp.engine.Where("(? = '' or brand_name like ?)", req.BrandName, "%"+req.BrandName+"%"). And("status = 1").Limit(int(req.Size), int(((req.Page)-1)*req.Size)). FindAndCount(&data) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp = FillPaging(count, req.Page, req.Size, data, rsp) rsp.Err = err return rsp, err } ReturnPoint: if err != nil { rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Err = err rsp.Message = "失败" } return rsp, err } func (rp *repo) BrandInfo(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) { rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Message = "超时/取消" rsp.Err = ctx.Err() return rsp, ctx.Err() default: var h bool item := new(model.Brand) h, err = rp.engine.ID(req.BrandId).Get(item) if err != nil { goto ReturnPoint } if !h { err = fmt.Errorf("未能找到对应的品牌") goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "获取品牌成功" rsp.Err = ctx.Err() rsp.Data = item return rsp, err } ReturnPoint: if err != nil { rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Err = err rsp.Message = "失败" } return rsp, err } func (rp *repo) AddBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) { rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Message = "超时/取消" rsp.Err = ctx.Err() return rsp, ctx.Err() default: item := &model.Brand{ BrandName: req.BrandName, BrandLogo: req.BrandLogo, BrandWeb: req.BrandWeb, Status: 1, CreateAt: time.Now().Unix(), UpdateAt: time.Now().Unix(), } _, err = rp.engine.Insert(item) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "新增品牌成功" rsp.Err = ctx.Err() rsp.Data = item return rsp, err } ReturnPoint: if err != nil { rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Err = err rsp.Message = "失败" } return rsp, err } func (rp *repo) EditBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) { rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Message = "超时/取消" rsp.Err = ctx.Err() return rsp, ctx.Err() default: var h bool item := new(model.Brand) h, err = rp.engine.ID(req.BrandId).Get(item) if err != nil { goto ReturnPoint } if !h { err = fmt.Errorf("未能找到对应的品牌") goto ReturnPoint } if len(req.BrandName) > 0 { item.BrandName = req.BrandName } if len(req.BrandLogo) > 0 { item.BrandLogo = req.BrandLogo } if len(req.BrandWeb) > 0 { item.BrandWeb = req.BrandWeb } item.UpdateAt = time.Now().Unix() _, err = rp.engine.ID(req.BrandId).AllCols().Update(item) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "修改品牌成功" rsp.Err = ctx.Err() rsp.Data = item return rsp, err } ReturnPoint: if err != nil { rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Err = err rsp.Message = "失败" } return rsp, err } func (rp *repo) DeleteBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) { rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Message = "超时/取消" rsp.Err = ctx.Err() return rsp, ctx.Err() default: var h bool item := new(model.Brand) h, err = rp.engine.ID(req.BrandId).Get(item) if err != nil { goto ReturnPoint } if !h { err = fmt.Errorf("未能找到对应的品牌") goto ReturnPoint } item.Status = 0 item.UpdateAt = time.Now().Unix() _, err = rp.engine.ID(req.BrandId).AllCols().Update(item) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "删除品牌成功" rsp.Err = ctx.Err() return rsp, err } ReturnPoint: if err != nil { rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Err = err rsp.Message = "失败" } return rsp, err }