Files

147 lines
3.8 KiB
Go
Raw Permalink Normal View History

2022-11-28 16:38:30 +08:00
package service
import (
2022-12-27 19:18:58 +08:00
"crm/common"
2023-01-25 16:16:36 +08:00
"crm/dao"
2022-11-28 16:38:30 +08:00
"crm/models"
"crm/response"
2022-12-27 19:18:58 +08:00
"strconv"
2022-11-28 16:38:30 +08:00
"time"
)
2023-01-24 20:03:35 +08:00
const (
Open = 1
Close = 2
)
2022-11-28 16:38:30 +08:00
type CustomerService struct {
2023-01-25 16:16:36 +08:00
customerDao *dao.CustomerDao
mailConfigDao *dao.MailConfigDao
}
func NewCustomerService() *CustomerService {
return &CustomerService{
customerDao: dao.NewCustomerDao(),
mailConfigDao: dao.NewMailConfigDao(),
}
2022-11-28 16:38:30 +08:00
}
// 创建客户
func (c *CustomerService) Create(param *models.CustomerCreateParam) int {
2023-01-25 16:16:36 +08:00
if c.customerDao.IsExists(param.Name, param.Creator) {
return response.ErrCodeCustomerHasExist
2022-11-28 16:38:30 +08:00
}
2023-01-25 16:16:36 +08:00
if err := c.customerDao.Create(param); err != nil {
2022-11-28 16:38:30 +08:00
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
// 更新客户
func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
2023-01-25 16:16:36 +08:00
if err := c.customerDao.Update(param); err != nil {
2022-11-28 16:38:30 +08:00
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
2023-01-24 20:03:35 +08:00
// 发送邮件给客户
func (c *CustomerService) SendMail(param *models.CustomerSendMailParam) int {
2023-01-25 16:16:36 +08:00
mc, err := c.mailConfigDao.GetInfo(param.Uid)
2023-01-24 20:03:35 +08:00
if err != nil {
return response.ErrCodeMailSendFailed
}
if mc.Status == Close {
return response.ErrCodeMailSendUnEnable
}
2023-01-30 15:50:10 +08:00
mail := models.MailParam{
Smtp: mc.Stmp,
Port: mc.Port,
AuthCode: mc.AuthCode,
Sender: mc.Email,
Subject: param.Subject,
Content: param.Content,
Attachment: param.Attachment,
Receiver: param.Receiver,
2023-01-24 20:03:35 +08:00
}
2023-01-30 15:50:10 +08:00
if err := common.SendMailToCustomer(mail); err != nil {
2023-01-24 20:03:35 +08:00
return response.ErrCodeMailSendFailed
}
return response.ErrCodeSuccess
}
2022-11-28 16:38:30 +08:00
// 删除客户
func (c *CustomerService) Delete(param *models.CustomerDeleteParam) int {
2023-01-25 16:16:36 +08:00
if err := c.customerDao.Delete(param); err != nil {
2022-11-28 16:38:30 +08:00
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
}
// 查询客户列表
2023-01-25 16:16:36 +08:00
func (c *CustomerService) GetList(param *models.CustomerQueryParam) ([]*models.CustomerList, int64, int) {
customerList, rows, err := c.customerDao.GetList(param)
2022-11-28 16:38:30 +08:00
if err != nil {
2023-01-25 16:16:36 +08:00
return nil, NumberNull, response.ErrCodeFailed
2022-11-28 16:38:30 +08:00
}
return customerList, rows, response.ErrCodeSuccess
}
// 查询客户信息
2023-01-25 16:16:36 +08:00
func (c *CustomerService) GetInfo(param *models.CustomerQueryParam) (*models.CustomerInfo, int) {
customerInfo, err := c.customerDao.GetInfo(param)
2022-11-28 16:38:30 +08:00
if err != nil {
return nil, response.ErrCodeFailed
}
2023-01-25 16:16:36 +08:00
return customerInfo, response.ErrCodeSuccess
2022-11-28 16:38:30 +08:00
}
// 查询客户选项
2023-01-25 16:16:36 +08:00
func (c *CustomerService) GetOption(uid int64) ([]*models.CustomerOption, int) {
option, err := c.customerDao.GetOption(uid)
2022-11-28 16:38:30 +08:00
if err != nil {
return nil, response.ErrCodeFailed
}
return option, response.ErrCodeSuccess
}
2022-12-27 19:18:58 +08:00
// 导出Excel文件
func (c *CustomerService) Export(uid int64) (string, int) {
2023-01-25 16:16:36 +08:00
customers, err := c.customerDao.GetListByUid(uid)
2022-12-27 19:18:58 +08:00
if err != nil {
return StringNull, response.ErrCodeFailed
}
excelRows := make([]models.CustomerExcelRow, 0)
var row models.CustomerExcelRow
for _, c := range customers {
row.Name = c.Name
row.Source = c.Source
row.Phone = c.Phone
row.Email = c.Email
row.Industry = c.Industry
row.Level = c.Level
row.Remarks = c.Remarks
row.Region = c.Region
row.Address = c.Address
if c.Status == 1 {
row.Status = "已签约"
}
if c.Status == 2 {
row.Status = "未签约"
}
row.Created = time.Unix(c.Created, 0).Format("2006-01-02")
if c.Updated != 0 {
row.Updated = time.Unix(c.Updated, 0).Format("2006-01-02")
}
excelRows = append(excelRows, row)
}
sheet := "客户信息"
columns := []string{"名称", "客户来源", "手机号", "邮箱", "客户行业", "客户级别", "备注", "地区", "详细地址", "成交状态", "创建时间", "更新时间"}
fileName := "customer_" + strconv.FormatInt(uid, 10)
file, err := common.GenExcelFile(sheet, columns, excelRows, fileName)
if err != nil {
return StringNull, response.ErrCodeFailed
}
return file, response.ErrCodeSuccess
}