作者 yangfu

增加:增加 公司属性接口

package company
import (
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/company"
"strings"
)
func Property(header *protocol.RequestHeader, request *protocolx.PropertyRequest) (rsp *protocolx.PropertyResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.PropertyResponse{}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
//用户查询区域
if (request.Action & 0x01) > 0 {
rsp.Districts, _ = getDistricts(header.UserId, transactionContext)
}
//查询合伙人类型
if (request.Action & 0x02) > 0 {
rsp.JoinWays, _ = getJoinWays(transactionContext)
}
err = transactionContext.CommitTransaction()
return
}
func Partners(header *protocol.RequestHeader, request *protocolx.PartnersRequest) (rsp *protocolx.PartnersResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.PartnersResponse{}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
err = transactionContext.CommitTransaction()
return
}
func Statistics(header *protocol.RequestHeader, request *protocolx.StatisticsRequest) (rsp *protocolx.StatisticsResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.StatisticsResponse{}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
err = transactionContext.CommitTransaction()
return
}
// 获取区域列表
func getDistricts(userId int64, transactionContext *transaction.TransactionContext) (districts []protocolx.Districts, err error) {
var (
user *domain.Users
partnerInfos []*domain.PartnerInfo
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
PartnerInfoRepository, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
)
if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil {
log.Error(err)
return
}
if user == nil || len(user.AccessPartners) == 0 {
return
}
if _, partnerInfos, err = PartnerInfoRepository.Find(map[string]interface{}{
"inPartnerIds": user.AccessPartnerIds(),
"sortByCreateTime": domain.ASC},
); err != nil {
return
}
for i := range partnerInfos {
partner := partnerInfos[i]
include := false
if partner.RegionInfo == nil {
continue
}
for j := range districts {
if strings.TrimSpace(districts[j].Name) == strings.TrimSpace(partner.RegionInfo.RegionName) {
include = true
break
}
}
if !include {
districts = append(districts, protocolx.Districts{
Id: partner.RegionInfo.RegionId,
Name: partner.RegionInfo.RegionName,
})
}
}
return
}
// 获取合伙人类型列表
func getJoinWays(transactionContext *transaction.TransactionContext) (joinWays []protocolx.JoinWays, err error) {
var (
CategoryInfoRepository, _ = factory.CreatePartnerCategoryInfoRepository(transactionContext)
categories []*domain.PartnerCategoryInfo
)
if _, categories, err = CategoryInfoRepository.Find(nil); err != nil && len(categories) == 0 {
return
}
for i := range categories {
c := categories[i]
joinWays = append(joinWays, protocolx.JoinWays{Type: c.Id, Name: c.Name})
}
return
}
... ...
... ... @@ -8,9 +8,14 @@ var LOG_LEVEL = "debug"
var LOG_File = "app.log"
var MMM_SMS_SERVICE_HOST = "https://sms.fjmaimaimai.com:9897"
var UCENTER_SERVICE_HOST = "https://suplus-ucenter-dev.fjmaimaimai.com"
var UCENTER_APP_KEY = "0c2c2a23dfc64ae230f5c54ab243ab52"
func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
}
if os.Getenv("UCENTER_SERVICE_HOST") != "" {
UCENTER_SERVICE_HOST = os.Getenv("UCENTER_SERVICE_HOST")
}
}
... ...
... ... @@ -46,6 +46,17 @@ type Users struct {
AccessPartners []*PartnerInfo
}
func (Users *Users) AccessPartnerIds() []int64 {
var partnerIds []int64
if Users == nil {
return partnerIds
}
for i := range Users.AccessPartners {
partnerIds = append(partnerIds, Users.AccessPartners[i].Id)
}
return partnerIds
}
type UsersRepository interface {
Save(dm *Users) (*Users, error)
Remove(dm *Users) (*Users, error)
... ...
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type UsersDao struct {
transactionContext *transaction.TransactionContext
}
func (dao *UsersDao) UserAccessPartners(queryOptions map[string]interface{}) ([]*domain.Users, error) {
tx := dao.transactionContext.PgTx
m := new(models.Users)
var users *domain.Users
query := NewQuery(tx.Model(m), queryOptions)
if inParterIds, ok := queryOptions["inParterIds"]; ok {
query.Relation("PartnerInfo", func(q *orm.Query) (*orm.Query, error) {
return q.Where("id in (?)", pg.In(inParterIds.([]int64))), nil
})
}
err := query.Select(&users)
return nil, err
}
func NewUsersDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UsersDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -6,6 +6,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/repository"
http_gateway "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
... ... @@ -53,6 +54,8 @@ func (svr *PgLoginService) ManagerLogin(phone string, password string) (err erro
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
ucenerSvr := http_gateway.NewHttplibUCenterApiServiceGateway()
_, err = ucenerSvr.ServerLogin(phone, password, 1)
return
}
... ...
package repository
import (
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
... ... @@ -55,6 +56,11 @@ func (repository *PartnerInfoRepository) FindOne(queryOptions map[string]interfa
query.SetWhere("partner_info.account = ?", "account")
query.SetWhere("partner_info.status = ?", "status")
query.SetWhere("partner_info.company_id = ?", "companyId")
//if inParterIds,ok :=queryOptions["inParterIds"];ok{
// query.Relation("PartnerInfo", func(q *orm.Query) (*orm.Query, error) {
// return q.Where("id in (?)",pg.In(inParterIds.([]int64))),nil
// })
//}
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此合伙人")
}
... ... @@ -76,6 +82,10 @@ func (repository *PartnerInfoRepository) Find(queryOptions map[string]interface{
SetLimit().
SetOrder("partner_info.create_at", "sortByCreateTime").
SetOrder("partner_info.update_at", "sortByUpdateTime")
if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
}
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, PartnerInfos, err
... ...
... ... @@ -66,6 +66,7 @@ func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (in
var UsersModels []*models.Users
Userss := make([]*domain.Users, 0)
query := NewQuery(tx.Model(&UsersModels), queryOptions).
SetWhere("id = ?", "id").
SetWhere("phone = ?", "phone").
SetWhere("company_id = ?", "companyId").
SetWhere(`status = ?`, "status").
... ...
package svr
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"strconv"
"strings"
"time"
)
type HttplibUCenterApiServiceGateway struct {
httplibBaseServiceGateway
}
// 服务登录
func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, password string, loginType int) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "auth", "serverLogin"}, "/")
request := serviceGateway.createRequest(url, "post")
request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(phone)
options["password"] = strings.TrimSpace(password)
options["type"] = loginType
request.JSONBody(options)
response := make(map[string]interface{})
//data,_:=request.Bytes()
//fmt.Println(string(data))
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
// 修改密码
func (serviceGateway *HttplibUCenterApiServiceGateway) UpdateUserPassword(uid int64, phone, password string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "users", fmt.Sprintf("%v", uid)}, "/")
request := serviceGateway.createRequest(url, "put")
request.Header("appKey", constant.UCENTER_APP_KEY)
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(phone)
options["password"] = strings.TrimSpace(password)
request.JSONBody(options)
response := make(map[string]interface{})
//data,_:=request.Bytes()
//fmt.Println(string(data))
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) handlerError(in map[string]interface{}) (int, error) {
var rspCode int
var err error
if code, ok := in["code"]; ok {
rspCode, _ = strconv.Atoi(fmt.Sprintf("%v", code))
} else {
err = fmt.Errorf("网关解析错误")
}
if msg, ok := in["msg"]; ok {
msg := msg.(string)
if rspCode != 0 && len(msg) > 0 {
err = fmt.Errorf(msg)
}
}
return rspCode, err
}
func NewHttplibUCenterApiServiceGateway() *HttplibUCenterApiServiceGateway {
return &HttplibUCenterApiServiceGateway{
httplibBaseServiceGateway: httplibBaseServiceGateway{
baseURL: constant.UCENTER_SERVICE_HOST,
connectTimeout: 100 * time.Second,
readWriteTimeout: 30 * time.Second,
},
}
}
... ...
package controllers
import (
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/company"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/company"
)
type CompanyController struct {
BaseController
}
// Property
// 测试
func (this *CompanyController) Property() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocolx.PropertyRequest
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := this.GetRequestHeader(this.Ctx)
data, err := company.Property(header, request)
if err != nil {
log.Error(err)
}
msg = protocol.NewReturnResponse(data, err)
}
// Partners
// 测试
func (this *CompanyController) Partners() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocolx.PartnersRequest
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := this.GetRequestHeader(this.Ctx)
data, err := company.Partners(header, request)
if err != nil {
log.Error(err)
}
msg = protocol.NewReturnResponse(data, err)
}
// Statistics
// 测试
func (this *CompanyController) Statistics() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocolx.StatisticsRequest
if err := this.JsonUnmarshal(&request); err != nil {
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := this.GetRequestHeader(this.Ctx)
data, err := company.Statistics(header, request)
if err != nil {
log.Error(err)
}
msg = protocol.NewReturnResponse(data, err)
}
... ...
... ... @@ -31,6 +31,10 @@ func init() {
nsV1.Router("/dividend/statistics", &controllers.DividendController{}, "Post:DividendStatistics")
nsV1.Router("/dividend/orders", &controllers.DividendController{}, "Post:DividendOrders")
nsV1.Router("/company/property", &controllers.CompanyController{}, "post:Property")
nsV1.Router("/company/partners", &controllers.CompanyController{}, "post:Partners")
nsV1.Router("/company/statistics", &controllers.CompanyController{}, "post:Statistics")
beego.AddNamespace(nsV1)
InitV2()
... ...
package company
type PartnersRequest struct {
// 唯一标识
Id int64 `json:"id"`
}
... ...
package company
type PartnersResponse struct {
// 唯一标识
Id int64 `json:"id"`
}
... ...
package company
type PropertyRequest struct {
// 企业属性请求动作(0x1区域、0x2合伙类型)
Action int `json:"action"`
}
... ...
package company
type PropertyResponse struct {
// 唯一标识
Districts []Districts `json:"districts"`
JoinWays []JoinWays `json:"joinWays"`
}
// 区域
type Districts struct {
Id int `json:"id"`
Name string `json:"name"`
}
// 合作类型
type JoinWays struct {
Type int64 `json:"type"`
Name string `json:"name"`
}
... ...
package company
type StatisticsRequest struct {
// 唯一标识
Id int64 `json:"id"`
}
... ...
package company
type StatisticsResponse struct {
// 唯一标识
Id int64 `json:"id"`
}
... ...