refactor: service module and dao module

This commit is contained in:
zchengo
2023-01-25 16:16:36 +08:00
parent 42d264f8ad
commit c1a3f31cad
17 changed files with 775 additions and 346 deletions
+23
View File
@@ -1,5 +1,11 @@
package dao
import (
"context"
"crm/global"
"crm/models"
)
const (
// 数据库表名
@@ -15,3 +21,20 @@ const (
NumberNull = 0
StringNull = ""
)
var ctx = context.Background()
// RestPage 分页查询
// page 设置起始页、每页条数,
// name 查询目标表的名称
// query 查询条件,
// dest 查询结果绑定的结构体,
// bind 绑定表结构对应的结构体
func restPage(page models.Page, name string, query interface{}, dest interface{}, bind interface{}) (int64, error) {
if page.PageNum > 0 && page.PageSize > 0 {
offset := (page.PageNum - 1) * page.PageSize
global.Db.Offset(offset).Limit(page.PageSize).Table(name).Where(query).Find(dest)
}
res := global.Db.Table(name).Where(query).Find(bind)
return res.RowsAffected, res.Error
}
+105
View File
@@ -0,0 +1,105 @@
package dao
import (
"crm/global"
"crm/models"
"time"
)
type ContractDao struct {
}
func NewContractDao() *ContractDao {
return &ContractDao{}
}
func (c *ContractDao) Create(param *models.ContractCreateParam) error {
contract := models.Contract{
Name: param.Name,
Amount: param.Amount,
BeginTime: param.BeginTime,
OverTime: param.OverTime,
Remarks: param.Remarks,
Cid: param.Cid,
Productlist: param.Productlist,
Status: param.Status,
Creator: param.Creator,
Created: time.Now().Unix(),
}
return global.Db.Create(&contract).Error
}
func (c *ContractDao) Update(param *models.ContractUpdateParam) error {
contract := models.Contract{
Id: param.Id,
Name: param.Name,
Amount: param.Amount,
BeginTime: param.BeginTime,
OverTime: param.OverTime,
Remarks: param.Remarks,
Cid: param.Cid,
Productlist: param.Productlist,
Status: param.Status,
Updated: time.Now().Unix(),
}
db := global.Db.Model(&contract).Select("*").Omit("id", "creator", "created")
return db.Updates(&contract).Error
}
func (c *ContractDao) Delete(param *models.ContractDeleteParam) error {
return global.Db.Delete(&models.Contract{}, param.Ids).Error
}
func (c *ContractDao) GetList(param *models.ContractQueryParam) ([]*models.ContractList, int64, error) {
contractList := make([]*models.ContractList, 0)
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
j := "inner join customer on contract.cid = customer.id and contract.creator = ?"
// 分页查询
offset := (param.Page.PageNum - 1) * param.Page.PageSize
mdb := global.Db.Offset(offset).Limit(param.Page.PageSize).Table(CONTRACT).Select(s)
var err error
if param.Id != 0 {
err = mdb.Joins(j+" and contract.id = ?", param.Creator, param.Id).Scan(&contractList).Error
} else {
err = mdb.Joins(j, param.Creator).Scan(&contractList).Error
}
if err != nil {
return nil, 0, err
}
var rows int64
global.Db.Raw("select count(*) from contract where creator = ?", param.Creator).Scan(&rows)
return contractList, rows, nil
}
func (c *ContractDao) GetListByUid(uid int64) ([]*models.ContractList, error) {
contracts := make([]*models.ContractList, 0)
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
j := "left join customer on contract.cid = customer.id and contract.creator = ?"
err := global.Db.Table(CONTRACT).Select(s).Joins(j, uid).Scan(&contracts).Error
if err != nil {
return nil, err
}
return contracts, nil
}
func (c *ContractDao) GetInfo(param *models.ContractQueryParam) (*models.ContractInfo, error) {
contract := models.Contract{
Id: param.Id,
}
contractInfo := models.ContractInfo{}
err := global.Db.Table(CONTRACT).Where(&contract).First(&contractInfo).Error
if err != nil {
return nil, err
}
return &contractInfo, nil
}
func (c *ContractDao) GetAddedPList(id int64) (*models.Contract, error) {
var contract models.Contract
err := global.Db.Table(CONTRACT).Select("productlist").First(&contract, id).Error
if err != nil {
return nil, err
}
return &contract, nil
}
+107
View File
@@ -0,0 +1,107 @@
package dao
import (
"crm/global"
"crm/models"
"time"
)
type CustomerDao struct {
}
func NewCustomerDao() *CustomerDao {
return &CustomerDao{}
}
func (c *CustomerDao) Create(param *models.CustomerCreateParam) error {
customer := models.Customer{
Name: param.Name,
Source: param.Source,
Phone: param.Phone,
Email: param.Email,
Industry: param.Industry,
Level: param.Level,
Remarks: param.Remarks,
Region: param.Region,
Address: param.Address,
Status: 1,
Creator: param.Creator,
Created: time.Now().Unix(),
}
return global.Db.Create(&customer).Error
}
func (c *CustomerDao) Update(param *models.CustomerUpdateParam) error {
customer := models.Customer{
Id: param.Id,
Name: param.Name,
Source: param.Source,
Phone: param.Phone,
Email: param.Email,
Industry: param.Industry,
Level: param.Level,
Remarks: param.Remarks,
Region: param.Region,
Address: param.Address,
Status: param.Status,
Updated: time.Now().Unix(),
}
db := global.Db.Model(&customer).Select("*").Omit("id", "creator", "created")
return db.Updates(&customer).Error
}
func (c *CustomerDao) Delete(param *models.CustomerDeleteParam) error {
return global.Db.Delete(&models.Customer{}, param.Ids).Error
}
func (c *CustomerDao) IsExists(name string, uid int64) bool {
var customer models.Customer
db := global.Db.Table(CUSTOMER).Where("name = ? and creator = ?", name, uid).First(&customer)
return db.RowsAffected != NumberNull
}
func (c *CustomerDao) GetList(param *models.CustomerQueryParam) ([]*models.CustomerList, int64, error) {
customer := models.Customer{
Name: param.Name,
Creator: param.Creator,
}
customerList := make([]*models.CustomerList, 0)
rows, err := restPage(param.Page, CUSTOMER, customer, &customerList, &[]*models.CustomerList{})
if err != nil {
return nil, 0, err
}
return customerList, rows, nil
}
func (c *CustomerDao) GetInfo(param *models.CustomerQueryParam) (*models.CustomerInfo, error) {
customer := models.Customer{
Id: param.Id,
}
customerInfo := models.CustomerInfo{}
err := global.Db.Table(CUSTOMER).Where(&customer).First(&customerInfo).Error
if err != nil {
return nil, err
}
return &customerInfo, nil
}
func (c *CustomerDao) GetOption(uid int64) ([]*models.CustomerOption, error) {
customer := models.Customer{
Creator: uid,
}
option := make([]*models.CustomerOption, 0)
err := global.Db.Table(CUSTOMER).Where(&customer).Find(&option).Error
if err != nil {
return nil, err
}
return option, nil
}
func (c *CustomerDao) GetListByUid(uid int64) ([]*models.Customer, error) {
customers := make([]*models.Customer, 0)
err := global.Db.Where("creator = ?", uid).Find(&customers).Error
if err != nil {
return nil, err
}
return customers, nil
}
+32
View File
@@ -0,0 +1,32 @@
package dao
import (
"crm/global"
"crm/models"
)
type DashboardDao struct {
}
func NewDashboardDao() *DashboardDao {
return &DashboardDao{}
}
func (d *DashboardDao) Count(uid int64, days int) models.DashboardSum {
var ds models.DashboardSum
global.Db.Raw("select count(*) from customer where creator = ?", uid).Scan(&ds.Customers)
global.Db.Raw("select count(*) from contract where creator = ?", uid).Scan(&ds.Contracts)
global.Db.Raw("select sum(amount) from contract where creator = ?", uid).Scan(&ds.ContractAmount)
global.Db.Raw("select count(*) from product where creator = ?", uid).Scan(&ds.Products)
s := "industry as name, count(*) as value"
global.Db.Model(&models.Customer{}).Select(s).Where("creator = ?", uid).Group("industry").Find(&ds.CustomerIndustry)
return ds
}
func (d *DashboardDao) AmountSum(start, end, uid int64) float64 {
var as float64
con := "created > ? and created < ? and status = 1 and creator = ?"
global.Db.Table(CONTRACT).Where(con, start, end, uid).Pluck("amount", &as)
return as
}
+60
View File
@@ -0,0 +1,60 @@
package dao
import (
"crm/global"
"crm/models"
"time"
)
const (
Read = 1 // 已读
UnRead = 2 // 未读
)
type NoticeDao struct {
}
func NewNoticeDao() *NoticeDao {
return &NoticeDao{}
}
func (n *NoticeDao) Create(param *models.NoticeCreateParam) error {
notice := models.Notice{
Content: param.Content,
Status: UnRead,
Creator: param.Creator,
Created: time.Now().Unix(),
}
return global.Db.Create(&notice).Error
}
func (n *NoticeDao) Update(param *models.NoticeUpdateParam) error {
notice := models.Notice{
Id: param.Id,
Status: Read,
Updated: time.Now().Unix(),
}
return global.Db.Model(&notice).Updates(&notice).Error
}
func (n *NoticeDao) GetUnReadCountByUid(uid int64) (models.UnReadNotice, error) {
var unRead models.UnReadNotice
raw := "select count(*) from notice where status = 2 and creator = ?"
if err := global.Db.Raw(raw, uid).Scan(&unRead.Count).Error; err != nil {
return unRead, err
}
return unRead, nil
}
func (n *NoticeDao) Delete(param *models.NoticeDeleteParam) error {
return global.Db.Delete(&models.Notice{}, param.Ids).Error
}
func (n *NoticeDao) GetList(uid int64) ([]*models.NoticeList, error) {
noticeList := make([]*models.NoticeList, 0)
db := global.Db.Table(NOTICE).Where("creator = ?", uid)
if err := db.Order("status desc").Order("created desc").Find(&noticeList).Error; err != nil {
return nil, err
}
return noticeList, nil
}
+98
View File
@@ -0,0 +1,98 @@
package dao
import (
"crm/global"
"crm/models"
"time"
)
type ProductDao struct {
}
func NewProductDao() *ProductDao {
return &ProductDao{}
}
func (p *ProductDao) Create(param *models.ProductCreateParam) error {
product := models.Product{
Name: param.Name,
Type: param.Type,
Unit: param.Unit,
Code: param.Code,
Price: param.Price,
Description: param.Description,
Status: param.Status,
Creator: param.Creator,
Created: time.Now().Unix(),
}
return global.Db.Create(&product).Error
}
func (p *ProductDao) Update(param *models.ProductUpdateParam) error {
product := models.Product{
Id: param.Id,
Name: param.Name,
Type: param.Type,
Unit: param.Unit,
Code: param.Code,
Price: param.Price,
Description: param.Description,
Status: param.Status,
Updated: time.Now().Unix(),
}
db := global.Db.Model(&product).Select("*").Omit("id", "creator", "created")
return db.Updates(&product).Error
}
func (p *ProductDao) Delete(param *models.ProductDeleteParam) error {
return global.Db.Delete(&models.Product{}, param.Ids).Error
}
func (p *ProductDao) IsExists(name string, uid int64) bool {
var product models.Product
db := global.Db.Table(PRODUCT).Where("name = ? and creator = ?", name, uid).First(&product)
return db.RowsAffected != NumberNull
}
func (p *ProductDao) GetList(param *models.ProductQueryParam) ([]*models.ProductList, int64, error) {
product := models.Product{
Name: param.Name,
Status: param.Status,
Creator: param.Creator,
}
productList := make([]*models.ProductList, 0)
rows, err := restPage(param.Page, PRODUCT, product, &productList, &[]*models.ProductList{})
if err != nil {
return nil, 0, err
}
return productList, rows, err
}
func (p *ProductDao) GetListByIds(ids []int64) ([]*models.Products, error) {
products := make([]*models.Products, 0)
if err := global.Db.Table(PRODUCT).Find(&products, ids).Error; err != nil {
return nil, err
}
return products, nil
}
func (p *ProductDao) GetInfo(param *models.ProductQueryParam) (*models.ProductInfo, error) {
product := models.Product{
Id: param.Id,
}
productInfo := models.ProductInfo{}
err := global.Db.Table(PRODUCT).Where(&product).First(&productInfo).Error
if err != nil {
return nil, err
}
return &productInfo, err
}
func (p *ProductDao) GetListByUid(uid int64) ([]*models.Product, error) {
products := make([]*models.Product, 0)
err := global.Db.Where("creator = ?", uid).Find(&products).Error
if err != nil {
return nil, err
}
return products, nil
}
+65
View File
@@ -0,0 +1,65 @@
package dao
import (
"crm/global"
"crm/models"
"encoding/json"
"time"
)
type SubscribeDao struct {
}
func NewSubscribeDao() *SubscribeDao {
return &SubscribeDao{}
}
func (s *SubscribeDao) Create(param *models.SubscribeCreateParam) error {
subscribe := models.Subscribe{
Uid: param.Uid,
Version: param.Version,
Expired: param.Expired,
Created: time.Now().Unix(),
}
return global.Db.Table(SUBSCRIBE).Create(&subscribe).Error
}
func (s *SubscribeDao) Update(param *models.SubscribeUpdateParam) error {
subscribe := models.Subscribe{
Version: param.Version,
Expired: param.Expired,
Updated: time.Now().Unix(),
}
return global.Db.Model(&models.Subscribe{}).Where("uid = ?", param.Uid).Updates(&subscribe).Error
}
func (s *SubscribeDao) UpdateVersion(uid int64, v int) error {
return global.Db.Model(&models.Subscribe{}).Where("uid = ?", uid).Update("version", v).Error
}
func (u *SubscribeDao) IsExists(uid int64) bool {
var subscribe models.Subscribe
db := global.Db.Table(SUBSCRIBE).Where("uid = ?", uid).First(&subscribe)
return db.RowsAffected != NumberNull
}
func (s *SubscribeDao) GetInfo(uid int64) (*models.SubscribeInfo, error) {
var si models.SubscribeInfo
if err := global.Db.Table(SUBSCRIBE).First(&si, "uid = ?", uid).Error; err != nil {
return nil, err
}
return &si, nil
}
func (s *SubscribeDao) SetOrder(tradeNo string, param *models.SubscribePayOrder) error {
order, _ := json.Marshal(&param)
return global.Rdb.Set(ctx, tradeNo, string(order), time.Minute*30).Err()
}
func (s *SubscribeDao) GetOrder(orderNo string) (string, error) {
orderJson, err := global.Rdb.Get(ctx, orderNo).Result()
if err != nil {
return StringNull, err
}
return orderJson, nil
}
+103
View File
@@ -0,0 +1,103 @@
package dao
import (
"crm/global"
"crm/models"
"fmt"
"strconv"
"time"
"gorm.io/gorm"
)
type UserDao struct {
}
func NewUserDao() *UserDao {
return &UserDao{}
}
func (u *UserDao) Create(param *models.UserCreateParam) error {
user := models.User{
Email: param.Email,
Password: param.Password,
Status: 1,
Created: time.Now().Unix(),
}
return global.Db.Create(&user).Error
}
func (u *UserDao) UpdatePass(email, password string) error {
userPass := models.User{
Password: password,
Updated: time.Now().Unix(),
}
return global.Db.Model(&models.User{}).Where("email = ?", email).Updates(&userPass).Error
}
func (u *UserDao) IsExists(email string) bool {
rows := global.Db.Where("email = ?", email).First(&models.User{}).RowsAffected
return rows != NumberNull
}
func (u *UserDao) GetUid(email string) (int64, error) {
user, err := u.GetUser(email)
if err != nil {
return NumberNull, err
}
return user.Id, nil
}
func (u *UserDao) GetUser(email string) (*models.User, error) {
var user models.User
err := global.Db.Table(USER).Where("email = ?", email).First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func (u *UserDao) DeleteData(param models.UserDeleteParam) error {
return global.Db.Transaction(func(tx *gorm.DB) error {
mw := map[interface{}]string{
&models.Product{}: "creator = ?",
&models.Customer{}: "creator = ?",
&models.Contract{}: "creator = ?",
&models.MailConfig{}: "creator = ?",
&models.Subscribe{}: "uid = ?",
&models.User{}: "id = ?",
}
for k, v := range mw {
if err := tx.Where(v, param.Id).Delete(k).Error; err != nil {
return err
}
}
return nil
})
}
func (u *UserDao) GetInfo(uid int64) (*models.UserPersonInfo, error) {
var user models.UserPersonInfo
err := global.Db.Transaction(func(tx *gorm.DB) error {
if err := tx.Table(USER).Where("id = ?", uid).First(&user).Error; err != nil {
return err
}
var subscribe models.Subscribe
if err := tx.Table(SUBSCRIBE).Select("version").Where("uid = ?", uid).First(&subscribe).Error; err != nil {
return err
}
user.Version = subscribe.Version
return nil
})
return &user, err
}
func (u *UserDao) SetCode(code int, email string) error {
key := fmt.Sprintf("user:%s:code", email)
return global.Rdb.SetEx(ctx, key, strconv.Itoa(code), 10*time.Minute).Err()
}
func (u *UserDao) GetCode(email string) string {
key := fmt.Sprintf("user:%s:code", email)
return global.Rdb.Get(ctx, key).Val()
}
+5
View File
@@ -20,10 +20,14 @@ const (
ErrCodeEmailFormatInvalid = 10008 // 邮箱格式无效
ErrCodeUserPassResetFailed = 10009 // 用户密码重置失败
ErrCodeCustomerHasExist = 20001 // 客户名称已经存在
ErrCodePayFailed = 20001 // 支付宝支付失败
ErrCodeFileExportFailed = 30001 // 文件导出失败
ErrCodeProductHasExist = 40001 // 产品名称已经存在
ErrCodeMailConfigInvalid = 50001 // 邮件服务配置无效
ErrCodeMailSendFailed = 50002 // 邮件发送失败
ErrCodeMailSendUnEnable = 50003 // 邮件服务未开启
@@ -46,6 +50,7 @@ var msg = map[int]string{
ErrCodeEmailFormatInvalid: "email format invalid",
ErrCodeUserPassResetFailed: "user password reset failed",
ErrCodeFileExportFailed: "file export failed",
ErrCodeProductHasExist: "product has exist",
ErrCodeMailConfigInvalid: "mail config invalid",
ErrCodeMailSendFailed: "mail send failed",
ErrCodeMailSendUnEnable: "mail send server unEnable",
-19
View File
@@ -1,9 +1,7 @@
package service
import (
"context"
"crm/global"
"crm/models"
"crm/response"
"os"
@@ -39,23 +37,6 @@ const (
SUBSCRIBE_NOTICE_TEMPLATE2 = "你订阅了高级版"
)
var ctx = context.Background()
// RestPage 分页查询
// page 设置起始页、每页条数,
// name 查询目标表的名称
// query 查询条件,
// dest 查询结果绑定的结构体,
// bind 绑定表结构对应的结构体
func restPage(page models.Page, name string, query interface{}, dest interface{}, bind interface{}) (int64, error) {
if page.PageNum > 0 && page.PageSize > 0 {
offset := (page.PageNum - 1) * page.PageSize
global.Db.Offset(offset).Limit(page.PageSize).Table(name).Where(query).Find(dest)
}
res := global.Db.Table(name).Where(query).Find(bind)
return res.RowsAffected, res.Error
}
// 初始化数据库数据
func InitData() int {
env := global.Config.Server.Runenv
+26 -64
View File
@@ -2,7 +2,7 @@ package service
import (
"crm/common"
"crm/global"
"crm/dao"
"crm/models"
"crm/response"
"strconv"
@@ -11,23 +11,20 @@ import (
)
type ContractService struct {
contractDao *dao.ContractDao
productDao *dao.ProductDao
}
func NewContractService() *ContractService {
return &ContractService{
contractDao: dao.NewContractDao(),
productDao: dao.NewProductDao(),
}
}
// 创建合同
func (c *ContractService) Create(param *models.ContractCreateParam) int {
contract := models.Contract{
Name: param.Name,
Amount: param.Amount,
BeginTime: param.BeginTime,
OverTime: param.OverTime,
Remarks: param.Remarks,
Cid: param.Cid,
Productlist: param.Productlist,
Status: param.Status,
Creator: param.Creator,
Created: time.Now().Unix(),
}
if err := global.Db.Create(&contract).Error; err != nil {
if err := c.contractDao.Create(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -35,20 +32,7 @@ func (c *ContractService) Create(param *models.ContractCreateParam) int {
// 更新合同
func (c *ContractService) Update(param *models.ContractUpdateParam) int {
contract := models.Contract{
Id: param.Id,
Name: param.Name,
Amount: param.Amount,
BeginTime: param.BeginTime,
OverTime: param.OverTime,
Remarks: param.Remarks,
Cid: param.Cid,
Productlist: param.Productlist,
Status: param.Status,
Updated: time.Now().Unix(),
}
db := global.Db.Model(&contract).Select("*").Omit("id", "creator", "created")
if err := db.Updates(&contract).Error; err != nil {
if err := c.contractDao.Update(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -56,61 +40,42 @@ func (c *ContractService) Update(param *models.ContractUpdateParam) int {
// 删除合同
func (c *ContractService) Delete(param *models.ContractDeleteParam) int {
if err := global.Db.Delete(&models.Contract{}, param.Ids).Error; err != nil {
if err := c.contractDao.Delete(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
// 查询合同列表
func (c *ContractService) QueryList(param *models.ContractQueryParam) ([]*models.ContractList, int64, int) {
contractList := make([]*models.ContractList, 0)
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
j := "inner join customer on contract.cid = customer.id and contract.creator = ?"
// 分页查询
offset := (param.Page.PageNum - 1) * param.Page.PageSize
mdb := global.Db.Offset(offset).Limit(param.Page.PageSize).Table(CONTRACT).Select(s)
var err error
if param.Id != 0 {
err = mdb.Joins(j+" and contract.id = ?", param.Creator, param.Id).Scan(&contractList).Error
} else {
err = mdb.Joins(j, param.Creator).Scan(&contractList).Error
}
func (c *ContractService) GetList(param *models.ContractQueryParam) ([]*models.ContractList, int64, int) {
contractList, rows, err := c.contractDao.GetList(param)
if err != nil {
return nil, 0, response.ErrCodeFailed
return nil, NumberNull, response.ErrCodeFailed
}
var rows int64
global.Db.Raw("select count(*) from contract where creator = ?", param.Creator).Scan(&rows)
return contractList, rows, response.ErrCodeSuccess
}
// 查询合同信息
func (c *ContractService) QueryInfo(param *models.ContractQueryParam) (*models.ContractInfo, int) {
contract := models.Contract{
Id: param.Id,
}
contractInfo := models.ContractInfo{}
err := global.Db.Table(CONTRACT).Where(&contract).First(&contractInfo).Error
func (c *ContractService) GetInfo(param *models.ContractQueryParam) (*models.ContractInfo, int) {
contractInfo, err := c.contractDao.GetInfo(param)
if err != nil {
return nil, response.ErrCodeFailed
}
return &contractInfo, response.ErrCodeSuccess
return contractInfo, response.ErrCodeSuccess
}
// 在编辑合同中,添加产品后,返回已添加的产品列表
func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*models.Products, int) {
func (c *ContractService) GetProductList(param *models.ContractQueryParam) ([]*models.Products, int) {
if param.Id == 0 {
products := make([]*models.Products, 0)
if err := global.Db.Table(PRODUCT).Find(&products, param.Pids).Error; err != nil {
products, err := c.productDao.GetListByIds(param.Pids)
if err != nil {
return nil, response.ErrCodeFailed
}
return products, response.ErrCodeSuccess
}
// 默认已添加的产品列表
var contract models.Contract
err := global.Db.Table(CONTRACT).Select("productlist").First(&contract, param.Id).Error
contract, err := c.contractDao.GetAddedPList(param.Id)
if err != nil {
return nil, response.ErrCodeFailed
}
@@ -136,8 +101,8 @@ func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*model
addedPids = append(addedPids, pid)
}
}
products := make([]*models.Products, 0)
if err := global.Db.Table(PRODUCT).Find(&products, addedPids).Error; err != nil {
products, err := c.productDao.GetListByIds(addedPids)
if err != nil {
return nil, response.ErrCodeFailed
}
addedProductList = append(addedProductList, products...)
@@ -146,10 +111,7 @@ func (c *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*model
// 导出Excel文件
func (c *ContractService) Export(uid int64) (string, int) {
contracts := make([]models.ContractList, 0)
s := "contract.id, contract.name, contract.amount, contract.begin_time, contract.over_time, customer.name as cname, contract.remarks, contract.status, contract.created, contract.updated"
j := "left join customer on contract.cid = customer.id and contract.creator = ?"
err := global.Db.Table(CONTRACT).Select(s).Joins(j, uid).Scan(&contracts).Error
contracts, err := c.contractDao.GetListByUid(uid)
if err != nil {
return StringNull, response.ErrCodeFailed
}
+25 -57
View File
@@ -2,7 +2,7 @@ package service
import (
"crm/common"
"crm/global"
"crm/dao"
"crm/models"
"crm/response"
"strconv"
@@ -15,25 +15,23 @@ const (
)
type CustomerService struct {
customerDao *dao.CustomerDao
mailConfigDao *dao.MailConfigDao
}
func NewCustomerService() *CustomerService {
return &CustomerService{
customerDao: dao.NewCustomerDao(),
mailConfigDao: dao.NewMailConfigDao(),
}
}
// 创建客户
func (c *CustomerService) Create(param *models.CustomerCreateParam) int {
customer := models.Customer{
Name: param.Name,
Source: param.Source,
Phone: param.Phone,
Email: param.Email,
Industry: param.Industry,
Level: param.Level,
Remarks: param.Remarks,
Region: param.Region,
Address: param.Address,
Status: 1,
Creator: param.Creator,
Created: time.Now().Unix(),
if c.customerDao.IsExists(param.Name, param.Creator) {
return response.ErrCodeCustomerHasExist
}
if err := global.Db.Create(&customer).Error; err != nil {
if err := c.customerDao.Create(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -41,22 +39,7 @@ func (c *CustomerService) Create(param *models.CustomerCreateParam) int {
// 更新客户
func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
customer := models.Customer{
Id: param.Id,
Name: param.Name,
Source: param.Source,
Phone: param.Phone,
Email: param.Email,
Industry: param.Industry,
Level: param.Level,
Remarks: param.Remarks,
Region: param.Region,
Address: param.Address,
Status: param.Status,
Updated: time.Now().Unix(),
}
db := global.Db.Model(&customer).Select("*").Omit("id", "creator", "created")
if err := db.Updates(&customer).Error; err != nil {
if err := c.customerDao.Update(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -64,8 +47,7 @@ func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
// 发送邮件给客户
func (c *CustomerService) SendMail(param *models.CustomerSendMailParam) int {
var mc models.MailConfig
err := global.Db.Model(&models.MailConfig{}).Where("creator = ?", param.Uid).First(&mc).Error
mc, err := c.mailConfigDao.GetInfo(param.Uid)
if err != nil {
return response.ErrCodeMailSendFailed
}
@@ -89,46 +71,33 @@ func (c *CustomerService) SendMail(param *models.CustomerSendMailParam) int {
// 删除客户
func (c *CustomerService) Delete(param *models.CustomerDeleteParam) int {
if err := global.Db.Delete(&models.Customer{}, param.Ids).Error; err != nil {
if err := c.customerDao.Delete(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
// 查询客户列表
func (c *CustomerService) QueryList(param *models.CustomerQueryParam) ([]*models.CustomerList, int64, int) {
customer := models.Customer{
Name: param.Name,
Creator: param.Creator,
}
customerList := make([]*models.CustomerList, 0)
rows, err := restPage(param.Page, CUSTOMER, customer, &customerList, &[]*models.CustomerList{})
func (c *CustomerService) GetList(param *models.CustomerQueryParam) ([]*models.CustomerList, int64, int) {
customerList, rows, err := c.customerDao.GetList(param)
if err != nil {
return nil, 0, response.ErrCodeFailed
return nil, NumberNull, response.ErrCodeFailed
}
return customerList, rows, response.ErrCodeSuccess
}
// 查询客户信息
func (c *CustomerService) QueryInfo(param *models.CustomerQueryParam) (*models.CustomerInfo, int) {
customer := models.Customer{
Id: param.Id,
}
customerInfo := models.CustomerInfo{}
err := global.Db.Table(CUSTOMER).Where(&customer).First(&customerInfo).Error
func (c *CustomerService) GetInfo(param *models.CustomerQueryParam) (*models.CustomerInfo, int) {
customerInfo, err := c.customerDao.GetInfo(param)
if err != nil {
return nil, response.ErrCodeFailed
}
return &customerInfo, response.ErrCodeSuccess
return customerInfo, response.ErrCodeSuccess
}
// 查询客户选项
func (c *CustomerService) QueryOption(uid int64) ([]*models.CustomerOption, int) {
customer := models.Customer{
Creator: uid,
}
option := make([]*models.CustomerOption, 0)
err := global.Db.Table(CUSTOMER).Where(&customer).Find(&option).Error
func (c *CustomerService) GetOption(uid int64) ([]*models.CustomerOption, int) {
option, err := c.customerDao.GetOption(uid)
if err != nil {
return nil, response.ErrCodeFailed
}
@@ -137,8 +106,7 @@ func (c *CustomerService) QueryOption(uid int64) ([]*models.CustomerOption, int)
// 导出Excel文件
func (c *CustomerService) Export(uid int64) (string, int) {
customers := make([]models.Customer, 0)
err := global.Db.Where("creator = ?", uid).Find(&customers).Error
customers, err := c.customerDao.GetListByUid(uid)
if err != nil {
return StringNull, response.ErrCodeFailed
}
+13 -14
View File
@@ -1,35 +1,34 @@
package service
import (
"crm/global"
"crm/dao"
"crm/models"
"time"
)
type DashboardService struct {
dashboardDao *dao.DashboardDao
}
func NewDashboardService() *DashboardService {
return &DashboardService{
dashboardDao: dao.NewDashboardDao(),
}
}
// 数据汇总
func (d *DashboardService) Summary(uid int64, days int) models.DashboardSum {
var ds models.DashboardSum
global.Db.Raw("select count(*) from customer where creator = ?", uid).Scan(&ds.Customers)
global.Db.Raw("select count(*) from contract where creator = ?", uid).Scan(&ds.Contracts)
global.Db.Raw("select sum(amount) from contract where creator = ?", uid).Scan(&ds.ContractAmount)
global.Db.Raw("select count(*) from product where creator = ?", uid).Scan(&ds.Products)
ds := d.dashboardDao.Count(uid, days)
now := time.Now().Unix()
con := "created > ? and created < ? and status = 1 and creator = ?"
ds.Amount = make([]float64, days)
ds.Date = make([]string, days)
for i, d := 0, days; i < days; i++ {
day := now - (int64(d) * 24 * 60 * 60)
for i, dd := 0, days; i < days; i++ {
day := now - (int64(dd) * 24 * 60 * 60)
start, end := dayRange(day)
global.Db.Table(CONTRACT).Where(con, start, end, uid).Pluck("amount", &ds.Amount[i])
ds.Amount[i] = d.dashboardDao.AmountSum(start, end, uid)
ds.Date[i] = time.Unix(day, 0).Format("01-02")
d--
dd--
}
s := "industry as name, count(*) as value"
global.Db.Model(&models.Customer{}).Select(s).Where("creator = ?", uid).Group("industry").Find(&ds.CustomerIndustry)
return ds
}
+19 -31
View File
@@ -1,29 +1,24 @@
package service
import (
"crm/global"
"crm/dao"
"crm/models"
"crm/response"
"time"
)
const (
Read = 1 // 已读
UnRead = 2 // 未读
)
type NoticeService struct {
noticeDao *dao.NoticeDao
}
func NewNoticeService() *NoticeService {
return &NoticeService{
noticeDao: dao.NewNoticeDao(),
}
}
// 新建通知
func (n *NoticeService) Create(param *models.NoticeParam) int {
notice := models.Notice{
Content: param.Content,
Status: UnRead,
Creator: param.Creator,
Created: time.Now().Unix(),
}
if err := global.Db.Create(&notice).Error; err != nil {
func (n *NoticeService) Create(param *models.NoticeCreateParam) int {
if err := n.noticeDao.Create(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -31,31 +26,24 @@ func (n *NoticeService) Create(param *models.NoticeParam) int {
// 更新通知
func (n *NoticeService) Update(param *models.NoticeUpdateParam) int {
notice := models.Notice{
Id: param.Id,
Status: Read,
Updated: time.Now().Unix(),
}
if err := global.Db.Model(&notice).Updates(&notice).Error; err != nil {
if err := n.noticeDao.Update(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
// 获取未读通知数量
func (n *NoticeService) UnReadCount(uid int64) (models.UnReadNotice, int) {
var unRead models.UnReadNotice
raw := "select count(*) from notice where status = 2 and creator = ?"
if err := global.Db.Raw(raw, uid).Scan(&unRead.Count).Error; err != nil {
return unRead, response.ErrCodeFailed
func (n *NoticeService) GetUnReadCount(uid int64) (models.UnReadNotice, int) {
unReadCount, err := n.noticeDao.GetUnReadCountByUid(uid)
if err != nil {
return unReadCount, response.ErrCodeFailed
}
return unRead, response.ErrCodeSuccess
return unReadCount, response.ErrCodeSuccess
}
// 删除通知
func (n *NoticeService) Delete(param *models.NoticeDeleteParam) int {
err := global.Db.Delete(&models.Notice{}, param.Ids).Error
if err != nil {
if err := n.noticeDao.Delete(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -63,8 +51,8 @@ func (n *NoticeService) Delete(param *models.NoticeDeleteParam) int {
// 获取通知列表
func (n *NoticeService) GetList(uid int64) ([]*models.NoticeList, int) {
noticeList := make([]*models.NoticeList, 0)
if err := global.Db.Table(NOTICE).Where("creator = ?", uid).Order("status desc").Order("created desc").Find(&noticeList).Error; err != nil {
noticeList, err := n.noticeDao.GetList(uid)
if err != nil {
return nil, response.ErrCodeFailed
}
return noticeList, response.ErrCodeSuccess
+20 -45
View File
@@ -2,7 +2,7 @@ package service
import (
"crm/common"
"crm/global"
"crm/dao"
"crm/models"
"crm/response"
"strconv"
@@ -10,22 +10,21 @@ import (
)
type ProductService struct {
productDao *dao.ProductDao
}
func NewProductService() *ProductService {
return &ProductService{
productDao: dao.NewProductDao(),
}
}
// 创建产品
func (p *ProductService) Create(param *models.ProductCreateParam) int {
product := models.Product{
Name: param.Name,
Type: param.Type,
Unit: param.Unit,
Code: param.Code,
Price: param.Price,
Description: param.Description,
Status: param.Status,
Creator: param.Creator,
Created: time.Now().Unix(),
if p.productDao.IsExists(param.Name, param.Creator) {
return response.ErrCodeProductHasExist
}
if err := global.Db.Create(&product).Error; err != nil {
if err := p.productDao.Create(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -33,19 +32,7 @@ func (p *ProductService) Create(param *models.ProductCreateParam) int {
// 更新产品
func (p *ProductService) Update(param *models.ProductUpdateParam) int {
product := models.Product{
Id: param.Id,
Name: param.Name,
Type: param.Type,
Unit: param.Unit,
Code: param.Code,
Price: param.Price,
Description: param.Description,
Status: param.Status,
Updated: time.Now().Unix(),
}
db := global.Db.Model(&product).Select("*").Omit("id", "creator", "created")
if err := db.Updates(&product).Error; err != nil {
if err := p.productDao.Update(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -53,45 +40,33 @@ func (p *ProductService) Update(param *models.ProductUpdateParam) int {
// 删除产品
func (p *ProductService) Delete(param *models.ProductDeleteParam) int {
err := global.Db.Delete(&models.Product{}, param.Ids).Error
if err != nil {
if err := p.productDao.Delete(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
// 查询产品列表
func (p *ProductService) QueryList(param *models.ProductQueryParam) ([]*models.ProductList, int64, int) {
product := models.Product{
Name: param.Name,
Status: param.Status,
Creator: param.Creator,
}
productList := make([]*models.ProductList, 0)
rows, err := restPage(param.Page, PRODUCT, product, &productList, &[]*models.ProductList{})
func (p *ProductService) GetList(param *models.ProductQueryParam) ([]*models.ProductList, int64, int) {
productList, rows, err := p.productDao.GetList(param)
if err != nil {
return nil, 0, response.ErrCodeFailed
return nil, NumberNull, response.ErrCodeFailed
}
return productList, rows, response.ErrCodeSuccess
}
// 查询产品信息
func (p *ProductService) QueryInfo(param *models.ProductQueryParam) (*models.ProductInfo, int) {
product := models.Product{
Id: param.Id,
}
productInfo := models.ProductInfo{}
err := global.Db.Table(PRODUCT).Where(&product).First(&productInfo).Error
func (p *ProductService) GetInfo(param *models.ProductQueryParam) (*models.ProductInfo, int) {
productInfo, err := p.productDao.GetInfo(param)
if err != nil {
return nil, response.ErrCodeFailed
}
return &productInfo, response.ErrCodeSuccess
return productInfo, response.ErrCodeSuccess
}
// 导出Excel文件
func (p *ProductService) Export(uid int64) (string, int) {
products := make([]models.Product, 0)
err := global.Db.Where("creator = ?", uid).Find(&products).Error
products, err := p.productDao.GetListByUid(uid)
if err != nil {
return StringNull, response.ErrCodeFileExportFailed
}
+29 -33
View File
@@ -1,6 +1,7 @@
package service
import (
"crm/dao"
"crm/global"
"crm/models"
"crm/response"
@@ -20,12 +21,14 @@ const (
)
type SubscribeService struct {
noticeService *NoticeService
subscribeDao *dao.SubscribeDao
noticeDao *dao.NoticeDao
}
func NewSubscribeService() *SubscribeService {
subscribeService := SubscribeService{
noticeService: &NoticeService{},
subscribeDao: dao.NewSubscribeDao(),
noticeDao: dao.NewNoticeDao(),
}
return &subscribeService
}
@@ -38,7 +41,6 @@ func (s *SubscribeService) Pay(param models.SubscribePayParam) (*models.Subscrib
var p = alipay.TradePagePay{}
p.NotifyURL = global.Config.Alipay.NotifyURL
p.ReturnURL = global.Config.Alipay.ReturnURL
fmt.Println(p.ReturnURL)
p.Subject = "支付测试:" + tradeNo
p.OutTradeNo = tradeNo
p.ProductCode = "FAST_INSTANT_TRADE_PAY"
@@ -50,18 +52,11 @@ func (s *SubscribeService) Pay(param models.SubscribePayParam) (*models.Subscrib
}
// 缓存订单信息
order, _ := json.Marshal(&models.SubscribePayOrder{
Uid: param.Uid,
order := &models.SubscribePayOrder{
Uid: param.Uid,
Version: param.Version,
})
err := global.Rdb.Set(ctx, tradeNo, string(order), time.Minute*30).Err()
if err != nil {
return nil, response.ErrCodeFailed
}
// 设置支付状态
key := fmt.Sprintf("uid:%v:pay:status", param.Uid)
if err := global.Rdb.Set(ctx, key, Paying, time.Minute*10).Err(); err != nil {
if err := s.subscribeDao.SetOrder(tradeNo, order); err != nil {
return nil, response.ErrCodeFailed
}
@@ -94,7 +89,7 @@ func (s *SubscribeService) Callback(outTradeNo string) (string, int) {
// 获取订单信息
var order models.SubscribePayOrder
orderJson, err := global.Rdb.Get(ctx, outTradeNo).Result()
orderJson, err := s.subscribeDao.GetOrder(outTradeNo)
if err != nil {
return StringNull, response.ErrCodeFailed
}
@@ -113,27 +108,28 @@ func (s *SubscribeService) Callback(outTradeNo string) (string, int) {
expired = time.Now().Unix() + int64(31104000)
content = SUBSCRIBE_NOTICE_TEMPLATE2
}
subscribe := models.Subscribe{
Version: order.Version,
Expired: expired,
}
if global.Db.Table(SUBSCRIBE).Where("uid = ?", order.Uid).First(&models.Subscribe{}).RowsAffected == 0 {
subscribe.Uid = order.Uid
subscribe.Created = time.Now().Unix()
err := global.Db.Table(SUBSCRIBE).Create(&subscribe).Error
if err != nil {
if !s.subscribeDao.IsExists(order.Uid) {
subscribe := models.SubscribeCreateParam{
Uid: order.Uid,
Version: order.Version,
Expired: expired,
}
if err := s.subscribeDao.Create(&subscribe); err != nil {
return StringNull, response.ErrCodeFailed
}
} else {
subscribe.Updated = time.Now().Unix()
err = global.Db.Model(&models.Subscribe{}).Where("uid = ?", order.Uid).Updates(&subscribe).Error
if err != nil {
subscribe := models.SubscribeUpdateParam{
Uid: order.Uid,
Version: order.Version,
Expired: expired,
}
if err := s.subscribeDao.Update(&subscribe); err != nil {
return StringNull, response.ErrCodeFailed
}
}
// 订阅通知
s.noticeService.Create(&models.NoticeParam{
s.noticeDao.Create(&models.NoticeCreateParam{
Content: content,
Creator: order.Uid,
})
@@ -174,19 +170,19 @@ func (s *SubscribeService) Notify(data url.Values, outTradeNo string) int {
// 获取订阅信息
func (s *SubscribeService) GetInfo(uid int64) (*models.SubscribeInfo, int) {
var si models.SubscribeInfo
if err := global.Db.Table(SUBSCRIBE).First(&si, "uid = ?", uid).Error; err != nil {
si, err := s.subscribeDao.GetInfo(uid)
if err != nil {
return nil, response.ErrCodeFailed
}
// 判断用户订阅是否过期
if si.Version == 2 && time.Now().Unix() > int64(si.Expired) {
err := global.Db.Model(&models.Subscribe{}).Where("uid = ?", uid).Update("version", 1).Error
if err != nil {
if err := s.subscribeDao.UpdateVersion(uid, 1); err != nil {
return nil, response.ErrCodeFailed
}
}
if err := global.Db.Table(SUBSCRIBE).First(&si, "uid = ?", uid).Error; err != nil {
subscribeInfo, err := s.subscribeDao.GetInfo(uid)
if err != nil {
return nil, response.ErrCodeFailed
}
return &si, response.ErrCodeSuccess
return subscribeInfo, response.ErrCodeSuccess
}
+45 -83
View File
@@ -2,16 +2,13 @@ package service
import (
"crm/common"
"crm/global"
"crm/dao"
"crm/models"
"crm/response"
"fmt"
"log"
"strconv"
"time"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
const (
@@ -19,12 +16,16 @@ const (
)
type UserService struct {
noticeService *NoticeService
userDao *dao.UserDao
subscribeDao *dao.SubscribeDao
noticeDao *dao.NoticeDao
}
func NewUserService() *UserService {
userService := UserService{
noticeService: &NoticeService{},
userDao: dao.NewUserDao(),
subscribeDao: dao.NewSubscribeDao(),
noticeDao: dao.NewNoticeDao(),
}
return &userService
}
@@ -33,15 +34,12 @@ func NewUserService() *UserService {
func (u *UserService) Register(param *models.UserCreateParam) int {
// 判断用户是否存在
var user = models.User{}
err := global.Db.Where("email = ?", param.Email).First(&user).Error
if err == nil && user.Id > 0 {
if u.userDao.IsExists(param.Email) {
return response.ErrCodeUserHasExist
}
// 校验验证码是否正确
key := fmt.Sprintf("user:%s:code", param.Email)
code := global.Rdb.Get(ctx, key).Val()
code := u.userDao.GetCode(param.Email)
if code != param.Code {
return response.ErrCodeVerityCodeInvalid
}
@@ -51,42 +49,41 @@ func (u *UserService) Register(param *models.UserCreateParam) int {
if err != nil {
return response.ErrCodeFailed
}
param.Password = string(password)
// 创建用户
newUser := models.User{
Email: param.Email,
Password: string(password),
Status: 1,
Created: time.Now().Unix(),
if err := u.userDao.Create(param); err != nil {
return response.ErrCodeFailed
}
if err := global.Db.Create(&newUser).Error; err != nil {
// 获取UID
uid, err := u.userDao.GetUid(param.Email)
if err != nil {
return response.ErrCodeFailed
}
// 新用户默认订阅免费版
subscribe := models.Subscribe{
Version: 1,
Created: time.Now().Unix(),
}
if global.Db.Table(SUBSCRIBE).Where("uid = ?", newUser.Id).First(&models.Subscribe{}).RowsAffected == 0 {
subscribe.Uid = newUser.Id
subscribe.Created = time.Now().Unix()
err := global.Db.Table(SUBSCRIBE).Create(&subscribe).Error
if err != nil {
if !u.subscribeDao.IsExists(uid) {
subscribe := models.SubscribeCreateParam{
Uid: uid,
Version: 1,
}
if err := u.subscribeDao.Create(&subscribe); err != nil {
return response.ErrCodeFailed
}
} else {
subscribe.Updated = time.Now().Unix()
err = global.Db.Model(&models.Subscribe{}).Where("uid = ?", newUser.Id).Updates(&subscribe).Error
if err != nil {
subscribe := models.SubscribeUpdateParam{
Uid: uid,
Version: 1,
}
if err := u.subscribeDao.Update(&subscribe); err != nil {
return response.ErrCodeFailed
}
}
// 注册通知
u.noticeService.Create(&models.NoticeParam{
u.noticeDao.Create(&models.NoticeCreateParam{
Content: REGISTER_NOTICE_TEMPLATE,
Creator: newUser.Id,
Creator: uid,
})
return response.ErrCodeSuccess
@@ -96,12 +93,16 @@ func (u *UserService) Register(param *models.UserCreateParam) int {
func (u *UserService) Login(param *models.UserLoginParam) (*models.UserInfo, int) {
// 判断用户是否存在
var user = models.User{}
err := global.Db.Where("email = ?", param.Email).First(&user).Error
if err != nil {
if !u.userDao.IsExists(param.Email) {
return nil, response.ErrCodeUserNotExist
}
// 获取用户信息
user, err := u.userDao.GetUser(param.Email)
if err != nil {
return nil, response.ErrCodeFailed
}
// 校验账号密码
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(param.Password))
if err != nil {
@@ -121,7 +122,7 @@ func (u *UserService) Login(param *models.UserLoginParam) (*models.UserInfo, int
}
// 登录通知
u.noticeService.Create(&models.NoticeParam{
u.noticeDao.Create(&models.NoticeCreateParam{
Content: LOGIN_NOTICE_TEMPLATE,
Creator: userInfo.Uid,
})
@@ -135,8 +136,7 @@ func (u *UserService) GetVerifyCode(email string) int {
code := common.RandInt(100000, 999998)
// 保存验证码
key := fmt.Sprintf("user:%s:code", email)
if err := global.Rdb.SetEx(ctx, key, strconv.Itoa(code), 10*time.Minute).Err(); err != nil {
if err := u.userDao.SetCode(code, email); err != nil {
return response.ErrCodeFailed
}
@@ -152,14 +152,12 @@ func (u *UserService) GetVerifyCode(email string) int {
// 忘记密码
func (u *UserService) ForgotPass(param *models.UserPassParam) int {
// 判断用户是否存在
var user = models.User{}
if err := global.Db.Where("email = ?", param.Email).First(&user).Error; err != nil {
if !u.userDao.IsExists(param.Email) {
return response.ErrCodeUserNotExist
}
// 校验验证码是否正确
key := fmt.Sprintf("user:%s:code", param.Email)
code := global.Rdb.Get(ctx, key).Val()
code := u.userDao.GetCode(param.Email)
if code != param.Code {
return response.ErrCodeVerityCodeInvalid
}
@@ -169,12 +167,7 @@ func (u *UserService) ForgotPass(param *models.UserPassParam) int {
if err != nil {
return response.ErrCodeFailed
}
upass := models.User{
Password: string(password),
Updated: time.Now().Unix(),
}
err = global.Db.Model(&models.User{}).Where("email = ?", param.Email).Updates(&upass).Error
if err != nil {
if err := u.userDao.UpdatePass(param.Email, string(password)); err != nil {
return response.ErrCodeUserPassResetFailed
}
return response.ErrCodeSuccess
@@ -183,27 +176,12 @@ func (u *UserService) ForgotPass(param *models.UserPassParam) int {
// 注销账号
func (u *UserService) Delete(param models.UserDeleteParam) int {
// 校验验证码是否正确
key := fmt.Sprintf("user:%s:code", param.Email)
code := global.Rdb.Get(ctx, key).Val()
code := u.userDao.GetCode(param.Email)
if code != param.Code {
return response.ErrCodeVerityCodeInvalid
}
err := global.Db.Transaction(func(tx *gorm.DB) error {
mw := map[interface{}]string{
&models.Product{}: "creator = ?",
&models.Customer{}: "creator = ?",
&models.Contract{}: "creator = ?",
&models.Subscribe{}: "uid = ?",
&models.User{}: "id = ?",
}
for k, v := range mw {
if err := tx.Where(v, param.Id).Delete(k).Error; err != nil {
return err
}
}
return nil
})
if err != nil {
// 删除用户所有数据
if err := u.userDao.DeleteData(param); err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -211,25 +189,9 @@ func (u *UserService) Delete(param models.UserDeleteParam) int {
// 获取用户信息
func (u *UserService) GetInfo(uid int64) (*models.UserPersonInfo, int) {
var user models.UserPersonInfo
err := global.Db.Transaction(func(tx *gorm.DB) error {
if err := tx.Table(USER).Where("id = ?", uid).First(&user).Error; err != nil {
return err
}
var subscribe models.Subscribe
if err := tx.Table(SUBSCRIBE).Select("version").Where("uid = ?", uid).First(&subscribe).Error; err != nil {
return err
}
user.Version = subscribe.Version
return nil
})
userInfo, err := u.userDao.GetInfo(uid)
if err != nil {
return nil, response.ErrCodeFailed
}
return &user, response.ErrCodeSuccess
}
// 校验用户Token
func (u *UserService) VerifyToken(token string) error {
return global.Rdb.Exists(ctx, token).Err()
return userInfo, response.ErrCodeSuccess
}