From e3e201d7f4c3f29f6234faee9c187196dcc59ed6 Mon Sep 17 00:00:00 2001 From: howell <2827207845@qq.com> Date: Tue, 18 Jan 2022 16:05:34 +0800 Subject: [PATCH] feat: company --- .../v2/controller/manage/company/company.go | 85 +++++++ internal/v2/logic/company/company.go | 213 ++++++++++++++++++ internal/v2/model/company/company.go | 112 +++++++++ internal/v2/router/manage.go | 15 ++ 4 files changed, 425 insertions(+) create mode 100644 internal/v2/controller/manage/company/company.go create mode 100644 internal/v2/logic/company/company.go create mode 100644 internal/v2/model/company/company.go diff --git a/internal/v2/controller/manage/company/company.go b/internal/v2/controller/manage/company/company.go new file mode 100644 index 0000000..5ddc942 --- /dev/null +++ b/internal/v2/controller/manage/company/company.go @@ -0,0 +1,85 @@ +package company + +import ( + "recook/internal/back" + "recook/internal/libs/bean" + "recook/internal/v2/lib/common" + "recook/internal/v2/logic/company" + "recook/tools" + + "github.com/gin-gonic/gin" +) + +type Proxy struct { +} + +func (o Proxy) List(c *gin.Context) { + var args company.ArgsCompanyList + if err := tools.Params(&args, c); err != nil { + back.Fail(c, err.Error()) + return + } + + if data, total, err := company.Logic.List(args); err != nil { + back.Fail(c, err.Error()) + } else { + back.Suc(c, "ok", bean.ResultLists{ + List: data, + Total: int(total), + }) + } +} + +func (o Proxy) All(c *gin.Context) { + back.Suc(c, "ok", company.Logic.All()) +} + +func (o Proxy) Sure(c *gin.Context) { + var args company.CompanySureApply + if err := tools.Params(&args, c); err != nil { + back.Fail(c, err.Error()) + return + } + + mu, _ := common.GetManageUser(c) + args.UserID = mu.Id + + if err := company.Logic.Sure(args); err != nil { + back.Fail(c, err.Error()) + } else { + back.Suc(c, "ok", "") + } +} + +func (o Proxy) Reject(c *gin.Context) { + var args company.CompanyRejectApply + if err := tools.Params(&args, c); err != nil { + back.Fail(c, err.Error()) + return + } + + mu, _ := common.GetManageUser(c) + args.UserID = mu.Id + + if err := company.Logic.Reject(args); err != nil { + back.Fail(c, err.Error()) + } else { + back.Suc(c, "ok", "") + } +} + +func (o Proxy) Apply(c *gin.Context) { + var args company.CompanyInfo + if err := tools.Params(&args, c); err != nil { + back.Fail(c, err.Error()) + return + } + mu, _ := common.GetManageUser(c) + args.UserID = mu.Id + + if err := company.Logic.Apply(args); err != nil { + back.Fail(c, err.Error()) + } else { + back.Suc(c, "ok", "") + } +} diff --git a/internal/v2/logic/company/company.go b/internal/v2/logic/company/company.go new file mode 100644 index 0000000..cb01f9b --- /dev/null +++ b/internal/v2/logic/company/company.go @@ -0,0 +1,213 @@ +package company + +import ( + "fmt" + "recook/internal/define" + "recook/internal/libs/bean" + "recook/internal/service/baseCode" + "recook/internal/v2/model/company" + "recook/internal/v2/model/recook/manage" + "recook/internal/v2/model/recook/user" + "time" + + "git.oa00.com/go/mysql" + "github.com/golangkit/formatime" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +type logic struct { +} + +var Logic = &logic{} + +type ArgsCompanyList struct { + bean.Page + Name string `json:"name"` + Mobile string `json:"mobile"` + Contract string `json:"contract"` + ApplyUserName string `json:"apply_user_name"` + State int `json:"state"` + Status int `json:"status"` + Start string `json:"start"` + End string `json:"end"` +} + +func (o logic) List(args ArgsCompanyList) (res []company.Info, total int64, err error) { + query := mysql.Db.Table((&company.Info{}).TableName()) + { + if args.Name != "" { + query = query.Where("company_name like ?", fmt.Sprintf("%%%s%%", args.Name)) + } + if args.Mobile != "" { + query = query.Where("mobile=?", args.Mobile) + } + if args.Contract != "" { + query = query.Where("contract_name like ?", fmt.Sprintf("%%%s%%", args.Contract)) + } + + if args.Start != "" { + query = query.Where("created_at > ?", args.Start) + } + if args.End != "" { + query = query.Where("created_at < ?", args.End) + } + if args.ApplyUserName != "" { + sub := mysql.Db.Table((&manage.RecookManageUserInfoModel{}).TableName()).Select("id").Where("name like ?", fmt.Sprintf("%%%s%%", args.Name)) + query = query.Where("apply_user_id in (?)", sub) + } + } + query.Count(&total) + query.Preload(clause.Associations).Preload("Logs.ManageUserInfo").Offset(args.GetStart()).Limit(args.GetLimit()).Find(&res) + return +} + +func (o logic) All() (res []string) { + mysql.Db.Table((&company.Info{}).TableName()).Pluck("company_name", &res) + return +} + +type CompanySureApply struct { + ID uint `json:""` + UserID uint `json:"-"` +} + +func (o logic) Sure(args CompanySureApply) error { + if err := mysql.Db.Transaction(func(tx *gorm.DB) error { + var obj company.Info + if err := tx.First(&obj, "id = ?", args.ID).Error; err != nil { + return err + } + if err := tx.Model(&obj).Updates(company.Info{ + State: company.Pass, + ProcessUserID: args.UserID, + ProcessTime: formatime.NewSecondNow(), + }).Error; err != nil { + return err + } + if err := tx.Create(&company.Log{ + Text: fmt.Sprintf("审核通过,并创建账号%s", obj.Mobile), + UserID: args.UserID, + CreatedAt: formatime.NewSecondNow(), + CompanyID: args.ID, + }).Error; err != nil { + return err + } + var u1 user.RecookUserInfoModel + if err := tx.First(&u1, "moblie = ?", obj.Mobile).Error; err != nil { + return err + } + if u1.Id > 0 { + return fmt.Errorf("该账号已存在") + } else { + mobile := obj.Mobile + sql := `select max(id)+floor(1+rand()*5) from recook_user_info` + var lastId int64 + if err := tx.Raw(sql).Count(&lastId).Error; err != nil { + return err + } + info := user.RecookUserInfoModel{ + Id: uint(lastId), + Nickname: "左家右厨" + mobile[7:], + Mobile: mobile, + Birthday: formatime.NewSecondFrom(define.DefaultBirthday), + HeadImgUrl: "", + Phone: mobile, + Identifier: time.Now().Format("060102") + baseCode.Encode(uint64(lastId)), + Level: 10, + ParentID: 0, + RootID: 0, + } + + if err := tx.Create(&info).Error; err != nil { + tx.Rollback() + err = fmt.Errorf("创建用户错误101:" + err.Error()) + return err + } + + // 生成邀请码和推荐码 + info.InvitationNo = baseCode.Encode(uint64(info.Id)) + if err := tx.Save(&info).Error; err != nil { + err = fmt.Errorf("创建用户错误102:" + err.Error()) + return err + } + // 建立钱包 + wallet := user.RecookUserWalletModel{ + UserId: info.Id, + } + if err := tx.Create(&wallet).Error; err != nil { + tx.Rollback() + err = fmt.Errorf("创建用户错误103:" + err.Error()) + return err + } + } + + return nil + }); err != nil { + return err + } + return nil +} + +type CompanyRejectApply struct { + CompanySureApply + Reason string `json:"reason" validate:"required,maxLen=20"` +} + +func (o logic) Reject(args CompanyRejectApply) error { + if err := mysql.Db.Transaction(func(tx *gorm.DB) error { + var obj company.Info + if err := tx.First(&obj, "id = ?", args.ID).Error; err != nil { + return err + } + if err := tx.Model(&obj).Updates(company.Info{ + State: company.Reject, + ProcessUserID: args.UserID, + ProcessTime: formatime.NewSecondNow(), + }).Error; err != nil { + return err + } + if err := tx.Create(&company.Log{ + Text: args.Reason, + UserID: args.UserID, + CreatedAt: formatime.NewSecondNow(), + CompanyID: args.ID, + }).Error; err != nil { + return err + } + return nil + }); err != nil { + return err + } + return nil +} + +type CompanyInfo struct { + company.Info + UserID uint `json:"user_id"` +} + +func (o logic) Apply(args CompanyInfo) error { + args.State = company.Wait + args.CreatedAt = formatime.NewSecondNow() + args.Logs = nil + args.ID = 0 + if err := mysql.Db.Transaction(func(tx *gorm.DB) error { + if err := tx.Create(&args).Error; err != nil { + return err + } + if err := tx.Create(&company.Log{ + Text: "发起了申请", + UserID: args.UserID, + CreatedAt: formatime.NewSecondNow(), + CompanyID: args.ID, + }).Error; err != nil { + return err + } + + return nil + }); err != nil { + return err + } + return nil +} diff --git a/internal/v2/model/company/company.go b/internal/v2/model/company/company.go new file mode 100644 index 0000000..c4d9240 --- /dev/null +++ b/internal/v2/model/company/company.go @@ -0,0 +1,112 @@ +package company + +import ( + "recook/internal/v2/model/recook/manage" + + "github.com/golangkit/formatime" + "github.com/jinzhu/gorm" +) + +type State uint + +const ( + Wait State = iota + 1 + Pass + Reject +) + +var stateMap = map[State]string{ + Wait: "待审核", + Pass: "已通过", + Reject: "已驳回", +} + +func (o State) Str() string { + return stateMap[o] +} + +type Status uint + +const ( + Normal Status = iota + 1 + Disable +) + +var statusMap = map[Status]string{ + Normal: "正常", + Disable: "禁用", +} + +func (o Status) Str() string { + return statusMap[o] +} + +type Info struct { + ID uint `json:"id"` + ShopName string `json:"shop_name"` + Mobile string `json:"mobile"` + ContractName string `json:"contract_name"` + ContractMobile string `json:"contract_mobile"` + ContractEmail string `json:"contract_email"` + ProcessUserID uint `json:"-"` + ProcessUserInfo manage.RecookManageUserInfoModel `json:"-" gorm:"foreignKey:process_user_id"` + ProcessUserName string `json:"process_user_name" gorm:"-"` + ProcessTime formatime.Second `json:"process_time"` + ContractNo string `json:"contract_no"` + ContractStart formatime.Second `json:"contract_start"` + ContractEnd formatime.Second `json:"contract_end"` + ContractAttach string `json:"contract_attach"` + CompanyName string `json:"company_name"` + RegisterAddress string `json:"register_address"` + BusinessAddress string `json:"business_address"` + BusinessStart formatime.Second `json:"business_start"` + BusinessEnd formatime.Second `json:"business_end"` + BusinessPhoto string `json:"business_photo"` + BusinessPerson string `json:"business_person"` + TaxType string `json:"tax_type"` + TaxBank string `json:"tax_bank"` + TaxNumber string `json:"tax_number"` + TaxAccount string `json:"tax_account"` + TaxPhoto string `json:"tax_photo"` + TaxProve string `json:"tax_prove"` + CreatedAt formatime.Second `json:"created_at"` + ApplyUserID uint `json:"-"` + ApplyUserInfo manage.RecookManageUserInfoModel `json:"-" gorm:"foreignKey:apply_user_id"` + ApplyUserName string `json:"apply_user_name" gorm:"-"` + State State `json:"state"` + StateStr string `json:"state_str" gorm:"-"` + Status Status `json:"status"` + StatusStr string `json:"status_str" gorm:"-"` + Logs []Log `json:"log" gorm:"foreignKey:company_id"` +} + +func (o *Info) TableName() string { + return "jyy_company_info" +} + +func (o *Info) AfterFind(db *gorm.DB) error { + o.ProcessUserName = o.ProcessUserInfo.Name + o.ApplyUserName = o.ApplyUserInfo.Name + o.StateStr = o.State.Str() + o.StatusStr = o.Status.Str() + return nil +} + +type Log struct { + ID uint `json:"id"` + Text string `json:"text"` + UserID uint `json:"-"` + ManageUserInfo manage.RecookManageUserInfoModel `json:"-" gorm:"foreignKey:user_id"` + ManageUserName string `json:"manage_user_name" gorm:"-"` + CreatedAt formatime.Second `json:"created_at"` + CompanyID uint `json:"company_i"` +} + +func (o Log) TableName() string { + return "jyy_company_log" +} + +func (o *Log) AfterFind(db *gorm.DB) error { + o.ManageUserName = o.ManageUserInfo.Name + return nil +} diff --git a/internal/v2/router/manage.go b/internal/v2/router/manage.go index aacbb69..073cca9 100644 --- a/internal/v2/router/manage.go +++ b/internal/v2/router/manage.go @@ -7,6 +7,7 @@ import ( "recook/internal/v2/controller/manage/after" "recook/internal/v2/controller/manage/akuschool" "recook/internal/v2/controller/manage/brand" + "recook/internal/v2/controller/manage/company" "recook/internal/v2/controller/manage/data" "recook/internal/v2/controller/manage/evaluation" "recook/internal/v2/controller/manage/finance/source" @@ -768,4 +769,18 @@ func routerManage(manageRouter *gin.RouterGroup) { } } + + { + companyRouter := manageRouter.Group("company", authorize) + { + proxy := company.Proxy{} + { + companyRouter.POST("list", proxy.List) + companyRouter.POST("all", proxy.All) + companyRouter.POST("sure", proxy.Sure) + companyRouter.POST("reject", proxy.Reject) + companyRouter.POST("apply", proxy.Apply) + } + } + } }