// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package company import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "gorm.io/gorm" "property-company/errors" dbmodel "property-company/model" pb_v1 "property-company/pb/v1" "strings" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkCompanyUpdateParam(req *pb_v1.CompanyUpdateRequest) error { switch { case req.SocialCode == "": return status.Error(10003, "社会信用码不能为空") case req.CompanyName == "": return status.Error(10003, "公司名称不能为空") case req.LicenseType != 1 && req.LicenseType != 2: return status.Error(10003, "不支持的营业执照类型") } return nil } // func CompanyUpdate(ctx context.Context, req *pb_v1.CompanyUpdateRequest) (reply *pb_v1.CompanyUpdateReply, err error) { reply = &pb_v1.CompanyUpdateReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() err = checkCompanyUpdateParam(req) if err != nil { return nil, err } company := &dbmodel.TCompany{} where := map[string]interface{}{ "id":req.Cid, } err = company.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return nil, errors.DataBaseError } if company.ID == 0 { return nil, errors.ErrRecordNotFound } reply.Origin = &pb_v1.CompanyUpdateRequest{ Cid:company.ID, CompanyAddr:company.CompanyAddr, CompanyPhone:company.CompanyPhone, CompanyName:company.CompanyName, SocialCode:company.SocialCode, BusinessLicense:company.BusinessLicense, LicenseType:int32(company.LicenseType), Logo:company.Logo, Desc:company.Desc, Site:company.Site, } values := map[string]interface{}{ "company_name":req.CompanyName, "company_phone":req.CompanyPhone, "company_addr":req.CompanyAddr, "logo":req.Logo, "desc":req.Desc, "site":req.Site, "social_code":req.SocialCode, "license_type":int64(req.LicenseType), "business_license":req.BusinessLicense, } err = company.Update(database.DB(), where, values) if err != nil { msg := strings.ToLower(err.Error()) if strings.Contains(msg, "duplicate") { return nil, status.Error(10003, "社会信用码已存在") } return nil, errors.DataBaseError } return reply, nil }